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             *
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    }