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             */
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) {&#064;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             * @og.rev 5.7.1.1 (2013/12/13) ä¸?—¦æ–?­—å?ã«å…¥ã‚Œã¦ã€rtn2br ã®åˆ¤å®šå?ç?‚’行ã„ã¾ã™ã?
112             *
113             * @param tag ã‚¿ã‚°é…å?
114             */
115            public void printTag( final Tag[] tag ) {
116                    for( int i=0; i<tag.length; i++ ) {
117                            String tagName = tag[i].name();
118                            String data = "";
119                            // {@og.value package.class#field} ã®å‡¦ç?‚’行ã„ã¾ã™ã?
120                            if( tagName.equalsIgnoreCase( "@og.value" ) ) {
121    //                              outFile.print( DocletUtil.valueTag( tag[i]) );
122                                    data = DocletUtil.valueTag( tag[i] );
123                            }
124                            // 5.6.3.3 (2013/04/19) {@og.doc03Link ・・・} ã®å‡¦ç?‚’行ã„ã¾ã™ã?
125                            else if( tagName.equalsIgnoreCase( "@og.doc03Link" ) ) {
126    //                              outFile.print( DocletUtil.doc03LinkTag( tag[i]) );
127                                    data = DocletUtil.doc03LinkTag( tag[i] );
128                            }
129                            // 5.5.5.6 (2012/08/31) @og.tag ãªã©ã« @og.value ãŒå«ã¾ã‚Œã¦ã?‚‹å ´åˆã?処ç?‚’追åŠ?
130                            else if( ! tagName.equalsIgnoreCase( "Text" ) ) {
131                                    printTag( tag[i].inlineTags() );
132                            }
133                            else {
134    //                              String data = DocletUtil.htmlFilter( tag[i].text() );
135    //                              String data = StringUtil.htmlFilter( tag[i].text() ).trim();            // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil ã«å¤‰æ›´
136                                    data = StringUtil.htmlFilter( tag[i].text() ).trim();           // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil ã«å¤‰æ›´
137    
138    //                              if( rtn2br ) {
139    //                                      outFile.print( data.replaceAll( CR,BR ) );
140    ////                                    outFile.print( BR );
141    //                              }
142    //                              else {
143    ////                                    outFile.println( data );
144    //                                      outFile.print( data );
145    //                              }
146                            }
147                            if( rtn2br ) {
148                                    outFile.print( data.replaceAll( CR,BR ) );
149                            }
150                            else {
151                                    outFile.print( data );
152                            }
153                    }
154    //              outFile.println();                              // 5.5.4.2 (2012/07/13) タグ出力ã?æœ?¾Œã«æ”¹è¡?
155            }
156    
157            /**
158             * æ–?­—å?引数ã‚??’ã¤ã¨ã€ã‚¿ã‚°é…å?ã‚’å—ã‘å–りã?タグ出力ã—ã¾ã™ã?
159             *
160             * @param str1  第ä¸?–‡å­—å?
161             * @param tag   ã‚¿ã‚°é…å?
162             * @param str3  第三文字å?
163             */
164            public void printTag( final String str1,final Tag[] tag, final String str3 ) {
165                    outFile.print( str1 );
166                    printTag( tag );
167                    outFile.println( str3 );
168            }
169    
170            /**
171             * ã‚¿ã‚°é…å?ã‚’å—ã‘å–りã?タグ出力ã—ã¾ã™ã?
172             *
173             * @param tag Tag[]
174             */
175    //      public void printTag( final Tag[] tag ) {
176    //              if( tag.length == 1 ) {
177    //                      String data = DocletUtil.htmlFilter( tag[0].text() );
178    //                      if( rtn2br ) { outFile.print( data.replaceAll( CR,BR ) ); }
179    //                      else             { outFile.print( data ); }
180    //              }
181    //              else {
182    //                      for( int i=0; i<tag.length; i++ ) {
183    //                              String data = DocletUtil.htmlFilter( tag[i].text() );
184    //                              if( rtn2br ) {
185    //                                      outFile.print( data.replaceAll( CR,BR ) );
186    //                                      outFile.print( BR );
187    //                              }
188    //                              else {
189    //                                      outFile.println( data );
190    //                              }
191    //                      }
192    //              }
193    //      }
194    
195            /**
196             * ã‚¿ã‚°é…å?ã‚’å—ã‘å–りã?タグ出力ã—ã¾ã™ã?
197             * è¤?•°ã®ã‚¿ã‚°ã‚’å?力ã™ã‚‹å?åˆã«ã€ã‚«ãƒ³ãƒžåŒºåˆ?‚Šæ–?­—ã§é€£çµã—ã¾ã™ã?
198             *
199             * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter ã«å¤‰æ›´
200             *
201             * @param tag ã‚¿ã‚°é…å?
202             */
203            public void printCSVTag( final Tag[] tag ) {
204                    for( int i=0; i<tag.length; i++ ) {
205    //                      String data = DocletUtil.htmlFilter( tag[i].text() );
206                            String data = StringUtil.htmlFilter( tag[i].text() );           // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil ã«å¤‰æ›´
207                            if( i > 0 ) { outFile.print( "," ); }
208                            outFile.print( data );
209                    }
210            }
211    
212            /**
213             * ã‚¿ã‚°é…å?ã‚’å—ã‘å–りã?タグ出力ã—ã¾ã™ã?
214             * ã“ã“ã§ã¯ã€ã‚¿ã‚°æ¯Žã«ã‚¿ã‚°ã®åç§°ã¨å†?®¹ã‚’å?力ã—ã€æ”¹è¡Œã‚’行ã„ã¾ã™ã?
215             * 特殊å?ç?¼šã“ã“ã§ã¯ã€og.rev ã‚¿ã‚°ã¯å–り込ã¿ã¾ã›ã‚“ã€?
216             *
217             * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter ã«å¤‰æ›´
218             *
219             * @param tag ã‚¿ã‚°é…å?
220             */
221            public void printTagsInfo( final Tag[] tag ) {
222                    for( int i=0; i<tag.length; i++ ) {
223                            String tagName = tag[i].name();
224                            if( tagName.equalsIgnoreCase( "@og.rev" ) ) {
225                                    continue;
226                            }
227                            outFile.print( tagName );
228                            outFile.print( " " );
229    //                      outFile.print( DocletUtil.htmlFilter( tag[i].text() ) );
230                            outFile.print( StringUtil.htmlFilter( tag[i].text() ) );                // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil ã«å¤‰æ›´
231                            if( rtn2br ) { outFile.print( BR ); }
232                            else             { outFile.println(); }
233                    }
234            }
235    
236            /**
237             * æ–?­—å?引数ã‚??’ã¤ã¨ã€ã‚¿ã‚°é…å?ã‚’å—ã‘å–りã?å…ˆé?ä¸?–‡å­—ã?タグ出力ã—ã¾ã™ã?
238             *
239             * @param str1  第ä¸?–‡å­—å?
240             * @param tag   ã‚¿ã‚°é…å?
241             * @param str3  第三文字å?
242             */
243            public void printChar( final String str1,final Tag[] tag, final String str3 ) {
244                    outFile.print( str1 );
245                    if( tag.length > 0 ) {
246                            String str = tag[0].text();
247                            if( str != null && str.length() > 0 ) {
248                                    outFile.print( str.charAt(0) );
249                            }
250                    }
251                    outFile.println( str3 );
252            }
253    }