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 */
016package org.opengion.fukurou.taglet;
017
018import com.sun.javadoc.Tag;
019
020import org.opengion.fukurou.util.FileUtil;
021import org.opengion.fukurou.util.StringUtil;
022
023import java.io.File;
024import java.io.PrintWriter;
025import java.io.IOException;
026
027/**
028 * Tag 情報を出力する PrintWriter 相当クラスです。
029 *
030 * @version  4.0
031 * @author   Kazuhiko Hasegawa
032 * @since    JDK5.0,
033 */
034public 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が、1つの場合と配列の場合で改行出力を分けていましたが、改行しないことにします。
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( "@og.value".equalsIgnoreCase( tagName ) ) {
124                                data = DocletUtil.valueTag( tag[i] );
125                        }
126                        // 5.6.3.3 (2013/04/19) {@og.doc03Link ・・・} の処理を行います。
127                        else if( "@og.doc03Link".equalsIgnoreCase( tagName ) ) {
128                                data = DocletUtil.doc03LinkTag( tag[i] );
129                        }
130                        // 5.5.5.6 (2012/08/31) @og.tag などに @og.value が含まれている場合の処理を追加
131                        else if( ! "Text".equalsIgnoreCase( tagName ) ) {
132                                printTag( tag[i].inlineTags() );
133                        }
134                        else {
135                                data = StringUtil.htmlFilter( tag[i].text() ).trim();           // 5.5.4.1 (2012/07/06) DocletUtil → StringUtil に変更
136                        }
137                        if( rtn2br ) {
138                                outFile.print( data.replaceAll( CR,BR ) );
139                        }
140                        else {
141                                outFile.print( data );
142                        }
143                }
144        }
145
146        /**
147         * 文字列引数を 2つと、タグ配列を受け取り、タグ出力します。
148         *
149         * @param str1  第一文字列
150         * @param tag   タグ配列
151         * @param str3  第三文字列
152         */
153        public void printTag( final String str1,final Tag[] tag, final String str3 ) {
154                outFile.print( str1 );
155                printTag( tag );
156                outFile.println( str3 );
157        }
158
159        /**
160         * タグ配列を受け取り、タグ出力します。
161         * 複数のタグを出力する場合に、カンマ区切り文字で連結します。
162         *
163         * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter → StringUtil.htmlFilter に変更
164         *
165         * @param tag タグ配列
166         */
167        public void printCSVTag( final Tag[] tag ) {
168                for( int i=0; i<tag.length; i++ ) {
169                        String data = StringUtil.htmlFilter( tag[i].text() );           // 5.5.4.1 (2012/07/06) DocletUtil → StringUtil に変更
170                        if( i > 0 ) { outFile.print( "," ); }
171                        outFile.print( data );
172                }
173        }
174
175        /**
176         * タグ配列を受け取り、タグ出力します。
177         * ここでは、タグ毎にタグの名称と内容を出力し、改行を行います。
178         * 特殊処理:ここでは、og.rev タグは取り込みません。
179         *
180         * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter → StringUtil.htmlFilter に変更
181         *
182         * @param tag タグ配列
183         */
184        public void printTagsInfo( final Tag[] tag ) {
185                for( int i=0; i<tag.length; i++ ) {
186                        String tagName = tag[i].name();
187                        if( "@og.rev".equalsIgnoreCase( tagName ) ) {
188                                continue;
189                        }
190                        outFile.print( tagName );
191                        outFile.print( " " );
192                        outFile.print( StringUtil.htmlFilter( tag[i].text() ) );                // 5.5.4.1 (2012/07/06) DocletUtil → StringUtil に変更
193                        if( rtn2br ) { outFile.print( BR ); }
194                        else             { outFile.println(); }
195                }
196        }
197
198        /**
199         * 文字列引数を 2つと、タグ配列を受け取り、先頭一文字のタグ出力します。
200         *
201         * @param str1  第一文字列
202         * @param tag   タグ配列
203         * @param str3  第三文字列
204         */
205        public void printChar( final String str1,final Tag[] tag, final String str3 ) {
206                outFile.print( str1 );
207                if( tag.length > 0 ) {
208                        String str = tag[0].text();
209                        if( str != null && str.length() > 0 ) {
210                                outFile.print( str.charAt(0) );
211                        }
212                }
213                outFile.println( str3 );
214        }
215}