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    }