001 /*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016 package org.opengion.fukurou.taglet;
017
018 import com.sun.javadoc.Tag;
019
020 import org.opengion.fukurou.util.FileUtil;
021 import org.opengion.fukurou.util.StringUtil;
022
023 import java.io.File;
024 import java.io.PrintWriter;
025 import java.io.IOException;
026
027 /**
028 * Tag æƒ??をå?力すã‚?PrintWriter 相当クラスですã?
029 *
030 * @version 4.0
031 * @author Kazuhiko Hasegawa
032 * @since JDK5.0,
033 */
034 public final class DocletTagWriter {
035 private final PrintWriter outFile ;
036 private final boolean rtn2br ; // 改行コードを <br/>に置換するかどã�?�‹
037
038 private static final String ENCODE = "UTF-8" ;
039
040 /** リターンコーãƒ? System.getProperty("line.separator") */
041 public static final String CR = System.getProperty("line.separator");
042 /** HTML上ã?ブレーク <br> + CR */
043 public static final String BR = "<br>" + CR ;
044
045 /**
046 * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
047 *
048 * 初期エンコードで出力しますã?
049 *
050 * @param file 出力ファイルå�?
051 * @throws IOException なんらかã?エラーが発生したå?合ã?
052 */
053 public DocletTagWriter( final String file ) throws IOException {
054 this( file,ENCODE,false );
055 }
056
057 /**
058 * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
059 *
060 * @param file 出力ファイルå�?
061 * @param encode エンコーãƒ?
062 * @throws IOException なんらかã?エラーが発生したå?合ã?
063 */
064 public DocletTagWriter( final String file,final String encode ) throws IOException {
065 this( file,encode,false );
066 }
067
068 /**
069 * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
070 *
071 * @param file 出力ファイルå�?
072 * @param encode エンコーãƒ?
073 * @param r2b 改行コードを BRタグに置換するかどã�?�‹
074 * @throws IOException なんらかã?エラーが発生したå?合ã?
075 */
076 public DocletTagWriter( final String file,final String encode,final boolean r2b ) throws IOException {
077 outFile = FileUtil.getPrintWriter( new File( file ),encode );
078 rtn2br = r2b;
079 }
080
081 /**
082 * 出力ファイルをクロースしますã?
083 *
084 */
085 public void close() {
086 outFile.close();
087 }
088
089 /**
090 * 可変長のæ–?—å?引数を取りã?æ–?—å?をå?力しますã?
091 * æ–?—å?のæœ?¾Œに改行が入りますã?
092 *
093 * @param str String...
094 */
095 public void printTag( final String... str ) {
096 for( int i=0; i<str.length; i++ ) {
097 if( rtn2br ) { outFile.print( str[i].replaceAll( CR,BR ) ); }
098 else { outFile.print( str[i] ); }
099 }
100 outFile.println();
101 }
102
103 /**
104 * タグ配å?を受け取りã?タグ出力しますã?
105 *
106 * 従来は、Tagがã??‘つの場合と配å?の場合で改行å?力をåˆ?�‘てã�?�¾したがã?改行しなã�?�“とにしますã?
107 *
108 * @og.rev 5.5.4.1 (2012/07/06) {@og.value package.class#field} の処ç�?対å¿?
109 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
110 * @og.rev 5.5.4.2 (2012/07/13) タグ出力ã?æœ?¾Œに改行を入れておきますã?
111 * @og.rev 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
112 * @og.rev 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
113 * @og.rev 5.6.3.3 (2013/04/19) @og.tag などに @og.doc03Link が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
114 * @og.rev 5.7.1.1 (2013/12/13) ä¸?—¦æ–?—å?に入れて、rtn2br の判定å?ç�?‚’行いますã?
115 *
116 * @param tag タグ配å?
117 */
118 public void printTag( final Tag[] tag ) {
119 for( int i=0; i<tag.length; i++ ) {
120 String tagName = tag[i].name();
121 String data = "";
122 // {@og.value package.class#field} の処ç�?‚’行いますã?
123 if( tagName.equalsIgnoreCase( "@og.value" ) ) {
124 // outFile.print( DocletUtil.valueTag( tag[i]) );
125 data = DocletUtil.valueTag( tag[i] );
126 }
127 // 5.6.3.3 (2013/04/19) {@og.doc03Link ・・・} の処ç�?‚’行いますã?
128 else if( tagName.equalsIgnoreCase( "@og.doc03Link" ) ) {
129 // outFile.print( DocletUtil.doc03LinkTag( tag[i]) );
130 data = DocletUtil.doc03LinkTag( tag[i] );
131 }
132 // 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
133 else if( ! tagName.equalsIgnoreCase( "Text" ) ) {
134 printTag( tag[i].inlineTags() );
135 }
136 else {
137 // String data = DocletUtil.htmlFilter( tag[i].text() );
138 // String data = StringUtil.htmlFilter( tag[i].text() ).trim(); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
139 data = StringUtil.htmlFilter( tag[i].text() ).trim(); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
140
141 // if( rtn2br ) {
142 // outFile.print( data.replaceAll( CR,BR ) );
143 //// outFile.print( BR );
144 // }
145 // else {
146 //// outFile.println( data );
147 // outFile.print( data );
148 // }
149 }
150 if( rtn2br ) {
151 outFile.print( data.replaceAll( CR,BR ) );
152 }
153 else {
154 outFile.print( data );
155 }
156 }
157 // outFile.println(); // 5.5.4.2 (2012/07/13) タグ出力ã?æœ?¾Œに改è¡?
158 }
159
160 /**
161 * æ–?—å?引数ã‚??’つと、タグ配å?を受け取りã?タグ出力しますã?
162 *
163 * @param str1 第ä¸?–‡字å?
164 * @param tag タグ配å?
165 * @param str3 第三文字å?
166 */
167 public void printTag( final String str1,final Tag[] tag, final String str3 ) {
168 outFile.print( str1 );
169 printTag( tag );
170 outFile.println( str3 );
171 }
172
173 /**
174 * タグ配å?を受け取りã?タグ出力しますã?
175 *
176 * @param tag Tag[]
177 */
178 // public void printTag( final Tag[] tag ) {
179 // if( tag.length == 1 ) {
180 // String data = DocletUtil.htmlFilter( tag[0].text() );
181 // if( rtn2br ) { outFile.print( data.replaceAll( CR,BR ) ); }
182 // else { outFile.print( data ); }
183 // }
184 // else {
185 // for( int i=0; i<tag.length; i++ ) {
186 // String data = DocletUtil.htmlFilter( tag[i].text() );
187 // if( rtn2br ) {
188 // outFile.print( data.replaceAll( CR,BR ) );
189 // outFile.print( BR );
190 // }
191 // else {
192 // outFile.println( data );
193 // }
194 // }
195 // }
196 // }
197
198 /**
199 * タグ配å?を受け取りã?タグ出力しますã?
200 * è¤?•°のタグをå?力するå?合に、カンマ区åˆ?‚Šæ–?—で連結しますã?
201 *
202 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
203 *
204 * @param tag タグ配å?
205 */
206 public void printCSVTag( final Tag[] tag ) {
207 for( int i=0; i<tag.length; i++ ) {
208 // String data = DocletUtil.htmlFilter( tag[i].text() );
209 String data = StringUtil.htmlFilter( tag[i].text() ); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
210 if( i > 0 ) { outFile.print( "," ); }
211 outFile.print( data );
212 }
213 }
214
215 /**
216 * タグ配å?を受け取りã?タグ出力しますã?
217 * ここでは、タグ毎にタグの名称とå†?®¹をå?力し、改行を行いますã?
218 * 特殊å?ç�?¼šここでは、og.rev タグは取り込みませんã€?
219 *
220 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
221 *
222 * @param tag タグ配å?
223 */
224 public void printTagsInfo( final Tag[] tag ) {
225 for( int i=0; i<tag.length; i++ ) {
226 String tagName = tag[i].name();
227 if( tagName.equalsIgnoreCase( "@og.rev" ) ) {
228 continue;
229 }
230 outFile.print( tagName );
231 outFile.print( " " );
232 // outFile.print( DocletUtil.htmlFilter( tag[i].text() ) );
233 outFile.print( StringUtil.htmlFilter( tag[i].text() ) ); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
234 if( rtn2br ) { outFile.print( BR ); }
235 else { outFile.println(); }
236 }
237 }
238
239 /**
240 * æ–?—å?引数ã‚??’つと、タグ配å?を受け取りã?先é?ä¸?–‡字ã?タグ出力しますã?
241 *
242 * @param str1 第ä¸?–‡字å?
243 * @param tag タグ配å?
244 * @param str3 第三文字å?
245 */
246 public void printChar( final String str1,final Tag[] tag, final String str3 ) {
247 outFile.print( str1 );
248 if( tag.length > 0 ) {
249 String str = tag[0].text();
250 if( str != null && str.length() > 0 ) {
251 outFile.print( str.charAt(0) );
252 }
253 }
254 outFile.println( str3 );
255 }
256 }