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    
020    import com.sun.javadoc.RootDoc;
021    import com.sun.javadoc.ClassDoc;
022    import com.sun.javadoc.Type;
023    import com.sun.javadoc.Tag;
024    import java.util.Map;
025    import java.util.HashMap;
026    import java.io.IOException;
027    
028    /**
029     * ソースコメントからã?属æ?æƒ??を取りå?ã�?Doclet クラスですã?
030     *
031     * @version  4.0
032     * @author   Kazuhiko Hasegawa
033     * @since    JDK5.0,
034     */
035    public final class DocletPlugin {
036            private static Map<String,AttKeySet> map = new HashMap<String,AttKeySet>();
037    
038            private static final String OG_FOR_SMPL  = "og.formSample";
039            private static final String ENCODE = "UTF-8";
040    
041            /**
042             * すべてã�?staticメソãƒ?ƒ‰なので、コンストラクタを呼び出さなくしておきますã?
043             *
044             */
045            private DocletPlugin() {}
046    
047            /**
048             * Doclet のエントリポイントメソãƒ?ƒ‰ですã?
049             *
050             * @param root ドキュメントルートオブジェクãƒ?
051             *
052             * @return 正常実行時 true
053             */
054            public static boolean start( final RootDoc root ) {
055                    String version = DocletUtil.getOption( "-version" , root.options() );
056                    String file    = DocletUtil.getOption( "-outfile" , root.options() );
057    
058                    mapInit();
059    
060                    DocletTagWriter writer = null;
061                    try {
062                            writer = new DocletTagWriter( file,ENCODE );
063    
064                            writer.printTag( "<?xml version=\"1.0\" encoding=\"", ENCODE, "\" ?>" );
065                            writer.printTag( "<javadoc>" );
066                            writer.printTag( "  <version>",version,"</version>" );
067                            writer.printTag( "  <description></description>" );
068                            writeContents( root.classes(),writer );
069                            writer.printTag( "</javadoc>" );
070                    }
071                    catch( IOException ex ) {
072                            LogWriter.log( ex );
073                    }
074                    finally {
075                            if( writer != null ) { writer.close(); }
076                    }
077                    return true;
078            }
079    
080            /**
081             * ClassDoc 配å?よりコンãƒ?ƒ³ãƒ?‚’作æ?しますã?
082             * インターフェースもå?ç�??対象としますã?
083             *
084             * @og.rev 5.5.4.1 (2012/07/06) コメントã?æ–?­—å?でなくã?Tag配å?として処ç�?�•せるã€?
085             *
086             * @param classes       ClassDoc配å?
087             * @param writer        DocletTagWriterオブジェクãƒ?
088             */
089            private static void writeContents( final ClassDoc[] classes,final DocletTagWriter writer ) {
090                    for(int i=0; i< classes.length; i++) {
091                            ClassDoc classDoc      = classes[i] ;
092                            if( ! classDoc.isPublic() ) { continue; }
093    
094                            AttKeySet attSet = getAttGroupName( classDoc ) ;
095    
096                            if( attSet == null ) { continue; }              // map に登録されてã�?�ªã�??
097    
098                            String attKey   = attSet.getAttKey( classDoc.name() );
099    
100                            if( attKey == null ) { continue; }              // 対象クラス名が、ä¸??しなã�??
101    
102                            String attClass = classDoc.qualifiedName() ;    // Class Full Name
103                            Tag[]  desc     = classDoc.firstSentenceTags();
104    //                      String cmnt     = DocletUtil.htmlFilter( classDoc.commentText() );                      // 5.5.4.1 (2012/07/06)
105                            Tag[]  cmnt     = classDoc.inlineTags();                                                                        // 5.5.4.1 (2012/07/06)
106                            Tag[] smplTags  = classDoc.tags(OG_FOR_SMPL);
107    
108                            writer.printTag( "<classDoc>" );
109                            writer.printTag( "  <attClass>"           ,attClass                               ,"</attClass>"            );
110                            writer.printTag( "  <seq>"                        ,attSet.getSeq()                ,"</seq>"                 );
111                            writer.printTag( "  <attKey>"             ,attKey                                 ,"</attKey>"              );
112                            writer.printTag( "  <valueName>"  ,attSet.getValueName()  ,"</valueName>"           );
113                            writer.printTag( "  <description>"        ,desc                                   ,"</description>" );
114                            writer.printTag( "  <contents>"           ,cmnt                                   ,"</contents>"            );
115                            writer.printTag( "  <formSample>" ,smplTags                               ,"</formSample>"  );
116    
117                            writer.printTag( "</classDoc>" );
118                    }
119            }
120    
121            /**
122             * 処ç�?�™る属æ?クラスのMapをå?期化しますã?
123             * æŒ?®šできるのは、親クラスかã?直接のインターフェースですã?
124             *
125             * @og.rev 4.3.5.0 (2008/02/01) daemonパッケージ追åŠ?
126             * @og.rev 5.5.3.5 (2012/06/21) ChartWriter 削除、TransferExec,TransferRead 追åŠ?
127             * @og.rev 5.6.3.3 (2013/04/19) DBTableReport,CalendarData,DBConstValue,JspParserFilter,ConnectIF 追åŠ?
128             *
129             */
130            private static void mapInit() {
131                    map.put( "org.opengion.hayabusa.db.Query"                               , new AttKeySet( "Query"                        ,0, "queryType"         ));
132                    map.put( "org.opengion.hayabusa.db.CellRenderer"                , new AttKeySet( "Renderer"                     ,1, "renderer"          ));
133                    map.put( "org.opengion.hayabusa.db.CellEditor"                  , new AttKeySet( "Editor"                       ,2, "editor"            ));
134                    map.put( "org.opengion.hayabusa.db.DBType"                              , new AttKeySet( "DBType"                       ,3, "dbType"            ));
135                    map.put( "org.opengion.hayabusa.db.TableFilter"                 , new AttKeySet( "TableFilter"          ,4, "tableFilter"       ));
136                    map.put( "org.opengion.hayabusa.db.Selection"                   , new AttKeySet( "Selection"            ,5, "selection"         ));
137                    map.put( "org.opengion.hayabusa.html.ViewForm"                  , new AttKeySet( "ViewForm"                     ,6, "viewFormType"      ));
138                    map.put( "org.opengion.hayabusa.io.TableWriter"                 , new AttKeySet( "TableWriter"          ,7, "writerClass"       ));
139                    map.put( "org.opengion.hayabusa.io.TableReader"                 , new AttKeySet( "TableReader"          ,8, "readerClass"       ));
140    //              map.put( "org.opengion.hayabusa.io.ChartWriter"                 , new AttKeySet( "ChartWriter"          ,9, "chartClass"        ));             // 5.5.3.5 (2012/06/21)
141                    map.put( "org.opengion.hayabusa.resource.CalendarQuery" , new AttKeySet( "CalendarQuery"        ,10, "calDB"            ));
142                    map.put( "org.opengion.fukurou.process.HybsProcess"             , new AttKeySet( "Process"                      ,11, "process"          ));
143                    map.put( "org.opengion.fukurou.transfer.TransferExec"   , new AttKeySet( "TransferExec"         ,12, "kbExec"           ));             // 5.5.3.5 (2012/06/21)
144                    map.put( "org.opengion.fukurou.transfer.TransferRead"   , new AttKeySet( "TransferRead"         ,13, "kbRead"           ));             // 5.5.3.5 (2012/06/21)
145                    map.put( "org.opengion.fukurou.util.HybsTimerTask"              , new AttKeySet( "Daemon"                       ,14, "daemon"           ));             // 4.3.4.4 (2009/01/01)
146    
147                    map.put( "org.opengion.hayabusa.report.DBTableReport"   , new AttKeySet( "DBTableReport"        ,15, "tableReport"      ));             // 5.6.3.3 (2013/04/19)
148                    map.put( "org.opengion.hayabusa.resource.CalendarData"  , new AttKeySet( "CalendarData"         ,16, "calData"          ));             // 5.6.3.3 (2013/04/19)
149                    map.put( "org.opengion.hayabusa.db.DBConstValue"                , new AttKeySet( "DBConstValue"         ,17, "cnstVal"          ));             // 5.6.3.3 (2013/04/19)
150                    map.put( "org.opengion.fukurou.xml.JspParserFilter"             , new AttKeySet( "JspCreate"            ,18, "jspParser"        ));             // 5.6.3.3 (2013/04/19)
151                    map.put( "org.opengion.fukurou.util.ConnectIF   "               , new AttKeySet( "ConnectIF"            ,19, "connIF"           ));             // 5.6.3.3 (2013/04/19)
152            }
153    
154            /**
155             * æŒ?®šã? ClassDoc がã?処ç�?�™る属æ?クラスのMapに含まれてã�?‚‹場合ã?
156             * そã? AttKeySet クラスのオブジェクトを返しますã?
157             * 存在しなã�??合ã?null を返しますã?
158             *
159             * @param       classDoc ClassDocオブジェクãƒ?
160             *
161             * @return      ClassDocに対応すã‚?AttKeySetオブジェクãƒ?
162             */
163            private static AttKeySet getAttGroupName( final ClassDoc classDoc ) {
164                    if( classDoc == null ) { return null; }
165    
166                    String classFullName = classDoc.qualifiedName() ;
167                    AttKeySet attKey = map.get( classFullName );
168                    if( attKey == null ) {
169                            Type type = classDoc.superclassType();  // 親クラスタイãƒ?
170                            if( type != null ) {
171                                    attKey = getAttGroupName( type.asClassDoc() );  // 親クラス
172                            }
173    
174                            if( attKey == null ) {
175                                    Type[] itface = classDoc.interfaceTypes();              // 直近インターフェース
176                                    for( int i=0; i<itface.length; i++ ) {
177                                            attKey = getAttGroupName( itface[i].asClassDoc() );
178                                            if( attKey != null ) { break; }
179                                    }
180                            }
181                    }
182                    return attKey;
183            }
184    
185            /**
186             * カスタãƒ?‚ªプションを使用するドックレãƒ?ƒˆのå¿??メソãƒ?ƒ‰ optionLength(String) ですã?
187             *
188             * ドックレãƒ?ƒˆに認識させるå�?‚«スタãƒ?‚ªプションにã€?optionLength がその
189             * オプションを構æ?する要ç´?(トã?クン) の数を返さなければなりませんã€?
190             * こã?カスタãƒ?‚ªプションではã€?-tag オプションそã?もã?と
191             * そã?値の 2 つの要ç´?�§構æ?されるã?で、作æ?するドックレãƒ?ƒˆの
192             * optionLengthメソãƒ?ƒ‰はã€?-tag オプションに対して 2 を返さなくては
193             * なりません。またã?認識できなã�?‚ªプションに対してはã€? を返しますã?
194             *
195             * @param option オプションæ–?­—å?
196             *
197             * @return 要ç´?(トã?クン) の数
198             */
199            public static int optionLength( final String option ) {
200                    if(option.equalsIgnoreCase("-version")) {
201                            return 2;
202                    }
203                    else if(option.equalsIgnoreCase("-outfile")) {
204                            return 2;
205                    }
206                    return 0;
207            }
208    }