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上ã?ブレーク  &lt;br&gt; + CR  */
043            public static final String BR = "&lt;br&gt;" + 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) {&#064;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    }