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             * @og.rev 5.7.1.1 (2013/12/13) タグのイン?トを止める?
054             *
055             * @param root ドキュメントルートオブジェク?
056             *
057             * @return 正常実行時 true
058             */
059            public static boolean start( final RootDoc root ) {
060                    String systemId = DocletUtil.getOption( "-systemId" , root.options() );
061                    String file     = DocletUtil.getOption( "-outfile"  , root.options() );
062    
063                    if( systemId == null || systemId.isEmpty() ) { systemId = "**"; }       // 5.5.2.0 (2012/05/01)
064    
065                    DocletTagWriter writer = null;
066                    try {
067                            writer = new DocletTagWriter( file,ENCODE );
068    
069                            // 5.7.1.1 (2013/12/13) タグのイン?トを止める?
070                            writer.printTag( "<?xml version=\"1.0\" encoding=\"", ENCODE, "\" ?>" );
071                            writer.printTag( "<javadoc>" );
072                            writer.printTag(   "<systemId>",systemId,"</systemId>" );
073                            writeContents( root.classes(),writer );
074                            writer.printTag( "</javadoc>" );
075                    }
076                    catch( IOException ex ) {
077                            LogWriter.log( ex );
078                    }
079                    finally {
080                            if( writer != null ) { writer.close(); }
081                    }
082                    return true;
083            }
084    
085            /**
086             * ClassDoc 配?よりコン??作?します?
087             *
088             * @og.rev 5.5.4.1 (2012/07/06) コメント???でなく?Tag配?として処?せる?
089             * @og.rev 5.5.4.1 (2012/07/06) DocletUtil.htmlFilter ?StringUtil.htmlFilter に変更
090             * @og.rev 5.7.1.1 (2013/12/13) タグのイン?トを止める?
091             *
092             * @param classes       ClassDoc配?
093             * @param writer        DocletTagWriterオブジェク?
094             */
095            private static void writeContents( final ClassDoc[] classes,final DocletTagWriter writer ) {
096                    for(int i=0; i< classes.length; i++) {
097                            ClassDoc classDoc      = classes[i] ;
098                            FieldDoc[] fields = classDoc.fields();
099    
100                            for( int j=0; j<fields.length; j++ ) {
101                                    FieldDoc field = fields[j];
102                                    String param = field.constantValueExpression() ;
103                                    if( param != null && param.length() >=2 &&
104                                            param.charAt(0) == '"' && param.charAt( param.length()-1 ) == '"' ) {
105                                                    param = param.substring( 1,param.length()-1 );
106                                    }
107    //                              param = DocletUtil.htmlFilter( param );
108                                    param = StringUtil.htmlFilter( param );         // 5.5.4.1 (2012/07/06) DocletUtil ?StringUtil に変更
109    
110                                    String paramId  = field.name();
111                                    String seq      = String.valueOf(j*10 + CNST);
112                                    Tag[] title     = field.firstSentenceTags();
113    //                              String cmnt     = DocletUtil.commentText( field.commentText() );        // 5.5.4.1 (2012/07/06)
114                                    Tag[] cmnt      = field.inlineTags();                                                           // 5.5.4.1 (2012/07/06)
115                                    Tag[] paramLvl  = field.tags(OG_PARAM_LVL);
116                                    Tag[] fgcrypt   = field.tags(OG_CRYPTOGRAPHY);
117    
118                                    // 5.7.1.1 (2013/12/13) タグのイン?トを止める?
119                                    writer.printTag(  "<fieldDoc>" );
120                                    writer.printTag(    "<paramId>"           ,paramId        ,"</paramId>"             );
121                                    writer.printTag(    "<seq>"                       ,seq            ,"</seq>"                 );
122                                    writer.printTag(    "<param>"             ,param          ,"</param>"                       );
123                                    writer.printTag(    "<title>"             ,title          ,"</title>"                       );
124                                    writer.printTag(    "<contents>"  ,cmnt           ,"</contents>"            );
125                                    writer.printChar(   "<paramLevel>"        ,paramLvl       ,"</paramLevel>"  );
126                                    writer.printChar(   "<fgcrypt>"           ,fgcrypt        ,"</fgcrypt>"             );
127                                    writer.printTag(  "</fieldDoc>" );
128                            }
129    
130                    }
131            }
132    
133            /**
134             * カスタ?プションを使用するドックレ?の??メソ? optionLength(String) です?
135             *
136             * ドックレ?に認識させる?スタ?プションに?optionLength がその
137             * オプションを構?する要?(ト?クン) の数を返さなければなりません?
138             * こ?カスタ?プションでは?-tag オプションそ?も?と
139             * そ?値の 2 つの要?構?される?で、作?するドックレ?の
140             * optionLengthメソ?は?-tag オプションに対して 2 を返さなくては
141             * なりません。また?認識できな?プションに対しては? を返します?
142             *
143             * @param option オプション??
144             *
145             * @return 要?(ト?クン) の数
146             */
147            public static int optionLength( final String option ) {
148                    if(option.equalsIgnoreCase("-outfile")) {
149                            return 2;
150                    }
151                    else if(option.equalsIgnoreCase("-systemId")) {
152                            return 2;
153                    }
154                    return 0;
155            }
156    }