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 */
052 public DocletTagWriter( final String file ) throws IOException {
053 this( file,ENCODE,false );
054 }
055
056 /**
057 * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
058 *
059 * @param file 出力ファイルå�?
060 * @param encode エンコーãƒ?
061 */
062 public DocletTagWriter( final String file,final String encode ) throws IOException {
063 this( file,encode,false );
064 }
065
066 /**
067 * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
068 *
069 * @param file 出力ファイルå�?
070 * @param encode エンコーãƒ?
071 * @param r2b 改行コードを BRタグに置換するかどã�?�‹
072 */
073 public DocletTagWriter( final String file,final String encode,final boolean r2b ) throws IOException {
074 outFile = FileUtil.getPrintWriter( new File( file ),encode );
075 rtn2br = r2b;
076 }
077
078 /**
079 * 出力ファイルをクロースしますã?
080 *
081 */
082 public void close() {
083 outFile.close();
084 }
085
086 /**
087 * 可変長のæ–?—å?引数を取りã?æ–?—å?をå?力しますã?
088 * æ–?—å?のæœ?¾Œに改行が入りますã?
089 *
090 * @param str String...
091 */
092 public void printTag( final String... str ) {
093 for( int i=0; i<str.length; i++ ) {
094 if( rtn2br ) { outFile.print( str[i].replaceAll( CR,BR ) ); }
095 else { outFile.print( str[i] ); }
096 }
097 outFile.println();
098 }
099
100 /**
101 * タグ配å?を受け取りã?タグ出力しますã?
102 *
103 * 従来は、Tagがã??‘つの場合と配å?の場合で改行å?力をåˆ?�‘てã�?�¾したがã?改行しなã�?�“とにしますã?
104 *
105 * @og.rev 5.5.4.1 (2012/07/06) {@og.value package.class#field} の処ç�?対å¿?
106 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
107 * @og.rev 5.5.4.2 (2012/07/13) タグ出力ã?æœ?¾Œに改行を入れておきますã?
108 * @og.rev 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
109 * @og.rev 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
110 * @og.rev 5.6.3.3 (2013/04/19) @og.tag などに @og.doc03Link が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
111 *
112 * @param tag タグ配å?
113 */
114 public void printTag( final Tag[] tag ) {
115 for( int i=0; i<tag.length; i++ ) {
116 String tagName = tag[i].name();
117 // {@og.value package.class#field} の処ç�?‚’行いますã?
118 if( tagName.equalsIgnoreCase( "@og.value" ) ) {
119 outFile.print( DocletUtil.valueTag( tag[i]) );
120 }
121 // 5.6.3.3 (2013/04/19) {@og.doc03Link ・・・} の処ç�?‚’行いますã?
122 else if( tagName.equalsIgnoreCase( "@og.doc03Link" ) ) {
123 outFile.print( DocletUtil.doc03LinkTag( tag[i]) );
124 }
125 // 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれてã�?‚‹場合ã?処ç�?‚’追åŠ?
126 else if( ! tagName.equalsIgnoreCase( "Text" ) ) {
127 printTag( tag[i].inlineTags() );
128 }
129 else {
130 // String data = DocletUtil.htmlFilter( tag[i].text() );
131 String data = StringUtil.htmlFilter( tag[i].text() ).trim(); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
132
133 if( rtn2br ) {
134 outFile.print( data.replaceAll( CR,BR ) );
135 // outFile.print( BR );
136 }
137 else {
138 // outFile.println( data );
139 outFile.print( data );
140 }
141 }
142 }
143 // outFile.println(); // 5.5.4.2 (2012/07/13) タグ出力ã?æœ?¾Œに改è¡?
144 }
145
146 /**
147 * タグ配å?を受け取りã?タグ出力しますã?
148 *
149 * @param tag Tag[]
150 */
151 // public void printTag( final Tag[] tag ) {
152 // if( tag.length == 1 ) {
153 // String data = DocletUtil.htmlFilter( tag[0].text() );
154 // if( rtn2br ) { outFile.print( data.replaceAll( CR,BR ) ); }
155 // else { outFile.print( data ); }
156 // }
157 // else {
158 // for( int i=0; i<tag.length; i++ ) {
159 // String data = DocletUtil.htmlFilter( tag[i].text() );
160 // if( rtn2br ) {
161 // outFile.print( data.replaceAll( CR,BR ) );
162 // outFile.print( BR );
163 // }
164 // else {
165 // outFile.println( data );
166 // }
167 // }
168 // }
169 // }
170
171 /**
172 * タグ配å?を受け取りã?タグ出力しますã?
173 * è¤?•°のタグをå?力するå?合に、カンマ区åˆ?‚Šæ–?—で連結しますã?
174 *
175 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
176 *
177 * @param tag タグ配å?
178 */
179 public void printCSVTag( final Tag[] tag ) {
180 for( int i=0; i<tag.length; i++ ) {
181 // String data = DocletUtil.htmlFilter( tag[i].text() );
182 String data = StringUtil.htmlFilter( tag[i].text() ); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
183 if( i > 0 ) { outFile.print( "," ); }
184 outFile.print( data );
185 }
186 }
187
188 /**
189 * タグ配å?を受け取りã?タグ出力しますã?
190 * ここでは、タグ毎にタグの名称とå†?®¹をå?力し、改行を行いますã?
191 * 特殊å?ç�?¼šここでは、og.rev タグは取り込みませんã€?
192 *
193 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
194 *
195 * @param tag タグ配å?
196 */
197 public void printTagsInfo( final Tag[] tag ) {
198 for( int i=0; i<tag.length; i++ ) {
199 String tagName = tag[i].name();
200 if( tagName.equalsIgnoreCase( "@og.rev" ) ) {
201 continue;
202 }
203 outFile.print( tagName );
204 outFile.print( " " );
205 // outFile.print( DocletUtil.htmlFilter( tag[i].text() ) );
206 outFile.print( StringUtil.htmlFilter( tag[i].text() ) ); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
207 if( rtn2br ) { outFile.print( BR ); }
208 else { outFile.println(); }
209 }
210 }
211
212 /**
213 * æ–?—å?引数ã‚??’つと、タグ配å?を受け取りã?タグ出力しますã?
214 *
215 * @param str1 第ä¸?–‡字å?
216 * @param tag タグ配å?
217 * @param str3 第三文字å?
218 */
219 public void printTag( final String str1,final Tag[] tag, final String str3 ) {
220 outFile.print( str1 );
221 printTag( tag );
222 outFile.println( str3 );
223 }
224
225 /**
226 * æ–?—å?引数ã‚??’つと、タグ配å?を受け取りã?先é?ä¸?–‡字ã?タグ出力しますã?
227 *
228 * @param str1 第ä¸?–‡字å?
229 * @param tag タグ配å?
230 * @param str3 第三文字å?
231 */
232 public void printChar( final String str1,final Tag[] tag, final String str3 ) {
233 outFile.print( str1 );
234 if( tag.length > 0 ) {
235 String str = tag[0].text();
236 if( str != null && str.length() > 0 ) {
237 outFile.print( str.charAt(0) );
238 }
239 }
240 outFile.println( str3 );
241 }
242 }