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 org.opengion.fukurou.util.LogWriter;
019 import org.opengion.fukurou.util.StringUtil;
020
021 import com.sun.javadoc.RootDoc;
022 import com.sun.javadoc.ClassDoc;
023 import com.sun.javadoc.FieldDoc;
024 import com.sun.javadoc.Tag;
025 import java.io.IOException;
026
027 /**
028 * ソースコメントからã?パラメータæƒ??を取りå?ã�?Doclet クラスですã?
029 * og.paramLevel タグと og.cryptography タグをå?りå?しますã?
030 * これらã?ã€?½¼?½??¾‘パラメータとしてGE12??½°?Œ゙ルに設定される値をクラスより抽出する
031 * のに使用しますã?
032 *
033 * @version 4.0
034 * @author Kazuhiko Hasegawa
035 * @since JDK5.0,
036 */
037 public final class DocletParam {
038 private static final String OG_PARAM_LVL = "og.paramLevel";
039 private static final String OG_CRYPTOGRAPHY = "og.cryptography";
040 private static final String ENCODE = "UTF-8";
041 private static final int CNST = 1000;
042
043 /**
044 * すべてã�?staticメソãƒ?ƒ‰なので、コンストラクタを呼び出さなくしておきますã?
045 *
046 */
047 private DocletParam() {}
048
049 /**
050 * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
051 *
052 * @og.rev 5.5.2.0 (2012/05/01) systemIdのbuild.xmlの引数がã?** の場合にエラーになるためã?対å¿?
053 *
054 * @param root ドキュメントルートオブジェクãƒ?
055 *
056 * @return 正常実行時 true
057 */
058 public static boolean start( final RootDoc root ) {
059 String systemId = DocletUtil.getOption( "-systemId" , root.options() );
060 String file = DocletUtil.getOption( "-outfile" , root.options() );
061
062 if( systemId == null || systemId.isEmpty() ) { systemId = "**"; } // 5.5.2.0 (2012/05/01)
063
064 DocletTagWriter writer = null;
065 try {
066 writer = new DocletTagWriter( file,ENCODE );
067
068 writer.printTag( "<?xml version=\"1.0\" encoding=\"", ENCODE, "\" ?>" );
069 writer.printTag( "<javadoc>" );
070 writer.printTag( " <systemId>",systemId,"</systemId>" );
071 writeContents( root.classes(),writer );
072 writer.printTag( "</javadoc>" );
073 }
074 catch( IOException ex ) {
075 LogWriter.log( ex );
076 }
077 finally {
078 if( writer != null ) { writer.close(); }
079 }
080 return true;
081 }
082
083 /**
084 * ClassDoc 配å?よりコンãƒ?ƒ³ãƒ?‚’作æ?しますã?
085 *
086 * @og.rev 5.5.4.1 (2012/07/06) コメントã?æ–?—å?でなくã?Tag配å?として処ç�?�•せるã€?
087 * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter â†?StringUtil.htmlFilter に変更
088 *
089 * @param classes ClassDoc配å?
090 * @param writer DocletTagWriterオブジェクãƒ?
091 */
092 private static void writeContents( final ClassDoc[] classes,final DocletTagWriter writer ) {
093 for(int i=0; i< classes.length; i++) {
094 ClassDoc classDoc = classes[i] ;
095 FieldDoc[] fields = classDoc.fields();
096
097 for( int j=0; j<fields.length; j++ ) {
098 FieldDoc field = fields[j];
099 String param = field.constantValueExpression() ;
100 if( param != null && param.length() >=2 &&
101 param.charAt(0) == '"' && param.charAt( param.length()-1 ) == '"' ) {
102 param = param.substring( 1,param.length()-1 );
103 }
104 // param = DocletUtil.htmlFilter( param );
105 param = StringUtil.htmlFilter( param ); // 5.5.4.1 (2012/07/06) DocletUtil â†?StringUtil に変更
106
107 String paramId = field.name();
108 String seq = String.valueOf(j*10 + CNST);
109 Tag[] title = field.firstSentenceTags();
110 // String cmnt = DocletUtil.commentText( field.commentText() ); // 5.5.4.1 (2012/07/06)
111 Tag[] cmnt = field.inlineTags(); // 5.5.4.1 (2012/07/06)
112 Tag[] paramLvl = field.tags(OG_PARAM_LVL);
113 Tag[] fgcrypt = field.tags(OG_CRYPTOGRAPHY);
114
115 writer.printTag( "<fieldDoc>" );
116 writer.printTag( " <paramId>" ,paramId ,"</paramId>" );
117 writer.printTag( " <seq>" ,seq ,"</seq>" );
118 writer.printTag( " <param>" ,param ,"</param>" );
119 writer.printTag( " <title>" ,title ,"</title>" );
120 writer.printTag( " <contents>" ,cmnt ,"</contents>" );
121 writer.printChar( " <paramLevel>" ,paramLvl ,"</paramLevel>" );
122 writer.printChar( " <fgcrypt>" ,fgcrypt ,"</fgcrypt>" );
123 writer.printTag( "</fieldDoc>" );
124 }
125
126 }
127 }
128
129 /**
130 * カスタãƒ?‚ªプションを使用するドックレãƒ?ƒˆのå¿??メソãƒ?ƒ‰ optionLength(String) ですã?
131 *
132 * ドックレãƒ?ƒˆに認識させるå�?‚«スタãƒ?‚ªプションにã€?optionLength がその
133 * オプションを構æ?する要ç´?(トã?クン) の数を返さなければなりませんã€?
134 * こã?カスタãƒ?‚ªプションではã€?-tag オプションそã?もã?と
135 * そã?値の 2 つの要ç´?�§構æ?されるã?で、作æ?するドックレãƒ?ƒˆの
136 * optionLengthメソãƒ?ƒ‰はã€?-tag オプションに対して 2 を返さなくては
137 * なりません。またã?認識できなã�?‚ªプションに対してはã€? を返しますã?
138 *
139 * @param option オプションæ–?—å?
140 *
141 * @return 要ç´?(トã?クン) の数
142 */
143 public static int optionLength( final String option ) {
144 if(option.equalsIgnoreCase("-outfile")) {
145 return 2;
146 }
147 else if(option.equalsIgnoreCase("-systemId")) {
148 return 2;
149 }
150 return 0;
151 }
152 }