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上のブレーク <br> + CR */ 043 public static final String BR = "<br>" + 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) {@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 * 文字列引数を 2つと、タグ配列を受け取り、タグ出力します。 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 * 文字列引数を 2つと、タグ配列を受け取り、先頭一文字のタグ出力します。 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}