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.taglet2;
017
018
019import jdk.javadoc.doclet.DocletEnvironment      ;
020// import jdk.javadoc.doclet.Doclet  ;
021// import jdk.javadoc.doclet.Reporter ;
022// import javax.lang.model.element.Element      ;
023import javax.lang.model.element.Modifier ;
024import javax.lang.model.element.TypeElement;
025// import javax.lang.model.element.ElementKind  ;
026// import javax.lang.model.element.VariableElement;
027// import javax.lang.model.SourceVersion         ;
028import javax.lang.model.type.TypeMirror;
029// import javax.lang.model.type.PrimitiveType;
030import javax.lang.model.util.ElementFilter       ;
031import javax.lang.model.util.Elements ;
032// import javax.lang.model.util.Types    ;
033import javax.tools.Diagnostic.Kind       ;
034import com.sun.source.doctree.DocCommentTree  ;
035import com.sun.source.util.DocTrees  ;
036import com.sun.source.doctree.DocTree  ;
037
038// import java.util.Locale ;
039import java.util.Set;
040import java.util.List;
041import java.util.HashSet;
042import java.util.Arrays;
043import java.util.Map;
044import java.util.HashMap;
045
046// import java.io.IOException;
047// import java.io.File;
048// import java.io.PrintWriter;
049
050// import org.opengion.fukurou.util.FileUtil;
051// import org.opengion.fukurou.util.StringUtil;
052
053/**
054 * ソースコメントから、パラメータ情報を取り出す Doclet クラスです。
055 * og.paramLevel タグと og.cryptography タグを切り出します。
056 * これらは、システムパラメータとしてGE12テーブルに設定される値をクラスより抽出する
057 * のに使用します。
058 *
059 * @version  7.3
060 * @author      Kazuhiko Hasegawa
061 * @since        JDK11.0,
062 */
063public class DocTreePlugin extends AbstractDocTree {
064        private static final String OG_FOR_SMPL  = "og.formSample";
065
066        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
067        // 6.4.3.3 (2016/03/04) 匿名クラスとインスタンス初期化子による、Mapの初期化処理。
068        /** staticイニシャライザ後、読み取り専用にするので、ConcurrentHashMap を使用しません。 */
069        private static final Map<String,AttKeySet> ATT_KEY_MAP = new HashMap<>();
070        static {
071                int no = 0;                             // 7.2.2.0 (2020/03/27) 連番の自動生成
072                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.Query"                                       , new AttKeySet( "Query"                        ,no++, "queryType"              ));
073                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.CellRenderer"                        , new AttKeySet( "Renderer"                     ,no++, "renderer"               ));
074                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.CellEditor"                          , new AttKeySet( "Editor"                       ,no++, "editor"                 ));
075                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.DBType"                                      , new AttKeySet( "DBType"                       ,no++, "dbType"                 ));
076                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.TableFilter"                         , new AttKeySet( "TableFilter"          ,no++, "tableFilter"    ));
077                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.Selection"                           , new AttKeySet( "Selection"            ,no++, "selection"              ));
078                ATT_KEY_MAP.put( "org.opengion.hayabusa.db.DBConstValue"                        , new AttKeySet( "DBConstValue"         ,no++, "cnstVal"                ));             // 5.6.3.3 (2013/04/19)
079                ATT_KEY_MAP.put( "org.opengion.hayabusa.html.ViewForm"                          , new AttKeySet( "ViewForm"                     ,no++, "viewFormType"   ));
080                ATT_KEY_MAP.put( "org.opengion.hayabusa.io.TableWriter"                         , new AttKeySet( "TableWriter"          ,no++, "writerClass"    ));
081                ATT_KEY_MAP.put( "org.opengion.hayabusa.io.TableReader"                         , new AttKeySet( "TableReader"          ,no++, "readerClass"    ));
082//              ATT_KEY_MAP.put( "org.opengion.hayabusa.report.DBTableReport"           , new AttKeySet( "DBTableReport"        ,no++, "tableReport"    ));             // 7.0.1.5 (2018/12/10) 削除
083                ATT_KEY_MAP.put( "org.opengion.hayabusa.resource.CalendarQuery"         , new AttKeySet( "CalendarQuery"        ,no++, "calDB"                  ));
084                ATT_KEY_MAP.put( "org.opengion.hayabusa.resource.CalendarData"          , new AttKeySet( "CalendarData"         ,no++, "calData"                ));             // 5.6.3.3 (2013/04/19)
085                ATT_KEY_MAP.put( "org.opengion.fukurou.process.HybsProcess"                     , new AttKeySet( "Process"                      ,no++, "process"                ));
086//              ATT_KEY_MAP.put( "org.opengion.fukurou.transfer.TransferExec"           , new AttKeySet( "TransferExec"         ,no++, "kbExec"                 ));             // 5.5.3.5 (2012/06/21)
087//              ATT_KEY_MAP.put( "org.opengion.fukurou.transfer.TransferRead"           , new AttKeySet( "TransferRead"         ,no++, "kbRead"                 ));             // 5.5.3.5 (2012/06/21)
088                ATT_KEY_MAP.put( "org.opengion.fukurou.util.HybsTimerTask"                      , new AttKeySet( "Daemon"                       ,no++, "daemon"                 ));             // 4.3.4.4 (2009/01/01)
089                ATT_KEY_MAP.put( "org.opengion.fukurou.util.ConnectIF   "                       , new AttKeySet( "ConnectIF"            ,no++, "connIF"                 ));             // 5.6.3.3 (2013/04/19)
090                ATT_KEY_MAP.put( "org.opengion.fukurou.xml.JspParserFilter"                     , new AttKeySet( "JspCreate"            ,no++, "jspParser"              ));             // 5.6.3.3 (2013/04/19)
091        }
092
093        private String version  ;
094        private String outfile  ;
095
096//      private DocTrees docUtil;
097//      private Elements eleUtil ;
098
099        /**
100         * Doclet のエントリポイントメソッドです(昔の startメソッド)。
101         *
102         * @og.rev 7.3.0.0 (2021/01/06) 新しいJavaDoc対応
103         *
104         * @param docEnv ドックレットを1回呼び出す操作環境
105         *
106         * @return 正常実行時 true
107         */
108        @Override
109        public boolean run( final DocletEnvironment docEnv ) {
110                try( DocTreeWriter writer = new DocTreeWriter( outfile,ENCODE ) ) {
111                        // 5.7.1.1 (2013/12/13) タグのインデントを止める。
112                        writer.printTag( "<?xml version=\"1.0\" encoding=\"", ENCODE, "\" ?>" );
113                        writer.printTag( "<javadoc>" );
114                        writer.printTag( "  <version>",version,"</version>" );
115                        writer.printTag( "  <description></description>" );
116                        writeContents( docEnv,writer );
117                        writer.printTag( "</javadoc>" );
118                }
119                catch( final Throwable th ) {
120                        reporter.print(Kind.ERROR, th.getMessage());
121                }
122
123                return true;
124        }
125
126        /**
127         * DocletEnvironmentよりコンテンツを作成します。
128         *
129         * @og.rev 7.3.0.0 (2021/01/06) 新しいJavaDoc対応
130         *
131         * @param docEnv        ドックレットの最上位
132         * @param writer        DocTreeWriterオブジェクト
133         */
134        private void writeContents( final DocletEnvironment docEnv, final DocTreeWriter writer ) {
135//              docUtil = docEnv.getDocTrees();
136//              eleUtil = docEnv.getElementUtils();
137
138//              // get the DocTrees utility class to access document comments
139                final DocTrees docUtil = docEnv.getDocTrees();
140                final Elements eleUtil  = docEnv.getElementUtils();
141
142                // クラス単位にループする。
143                for( final TypeElement typEle : ElementFilter.typesIn(docEnv.getIncludedElements())) {
144                        if( !typEle.getModifiers().contains( Modifier.PUBLIC ) ) { continue; }
145
146                        final String fullName   = String.valueOf( typEle.getQualifiedName() ) ;
147//                      final String fullName = String.valueOf(typEle);
148        //              System.out.println(typEle.getKind() + ":" + fullName);
149                        writer.setClassName( fullName );
150
151                        final AttKeySet attSet = getAttGroupName( typEle,eleUtil ) ;
152                        if( attSet == null ) { continue; }              // map に登録されていない。
153
154                        final int ed = fullName.lastIndexOf( '.' );
155                        String attKey = null;
156                        if( ed > 0 ) {
157                                final String name = fullName.substring( ed+1 );
158                                attKey = attSet.getAttKey( name );
159                        }
160                        if( attKey == null ) { continue; }              // 対象クラス名が、一致しない。
161
162                        final DocCommentTree docTree = docUtil.getDocCommentTree(typEle);               // ドキュメンテーション・コメントが見つからない場合、null が返る。
163
164                        final List<? extends DocTree> desc      = docTree == null ? EMPTY_LIST : docTree.getFirstSentence();
165                        final List<? extends DocTree> cmnt      = docTree == null ? EMPTY_LIST : docTree.getFullBody();
166
167                        final Map<String,List<String>> blkTagMap = blockTagsMap(docTree);
168                        final String smplTags   = getBlockTag( OG_FOR_SMPL, blkTagMap, "" );
169
170//                      String smplTags = "";
171//                      if( docTree != null ) {
172//                              for( final DocTree dt : docTree.getBlockTags() ) {
173//                                      final String tag = String.valueOf(dt);
174//                                      if( tag.contains( OG_FOR_SMPL ) ) {
175//                                              smplTags = tag.substring( OG_FOR_SMPL.length() + 1 ).trim();
176//                                      }
177//                              }
178//                      }
179
180                        // 5.7.1.1 (2013/12/13) タグのインデントを止める。
181                        writer.printTag( "<classDoc>" );
182                        writer.printTag( "  <attClass>"         ,fullName                               ,"</attClass>"          );
183                        writer.printTag( "  <seq>"                      ,attSet.getSeq()                ,"</seq>"                       );
184                        writer.printTag( "  <attKey>"           ,attKey                                 ,"</attKey>"            );
185                        writer.printTag( "  <valueName>"        ,attSet.getValueName()  ,"</valueName>"         );
186                        writer.printTag( "  <description>"      ,desc                                   ,"</description>"       );
187                        writer.printTag( "  <contents>"         ,cmnt                                   ,"</contents>"          );
188                        writer.printTag( "  <formSample>"       ,smplTags                               ,"</formSample>"        );
189                        writer.printTag( "</classDoc>" );
190                }
191        }
192
193        /**
194         * 指定の ClassDoc が、処理する属性クラスのMapに含まれている場合、
195         * その AttKeySet クラスのオブジェクトを返します。
196         * 存在しない場合、null を返します。
197         *
198         * @og.rev 7.3.0.0 (2021/01/06) 新しいJavaDoc対応
199         *
200         * @param       typEle TypeElementオブジェクト
201         * @param       eleUtil Elementsユーティリティ
202         *
203         * @return      typEleに対応する AttKeySetオブジェクト
204         */
205        private static AttKeySet getAttGroupName( final TypeElement typEle,final Elements eleUtil ) {
206                if( typEle == null ) { return null; }
207
208                final String fullName = String.valueOf(typEle);
209                AttKeySet attKey = ATT_KEY_MAP.get( fullName );
210                if( attKey == null ) {
211                        for( final TypeElement tp : eleUtil.getAllTypeElements(fullName) ) {
212                                if( typEle.equals( tp ) ) { continue; }
213                                attKey = getAttGroupName( tp,eleUtil );
214                                if( attKey != null ) { return attKey; }
215                        }
216
217                        final TypeMirror stype = typEle.getSuperclass();                        // 親クラスタイプ
218                        if( stype != null ) {
219                                final String suCls = String.valueOf( stype );
220                                attKey = ATT_KEY_MAP.get( suCls );      // 親クラス
221                                if( attKey == null ) {
222                                        for( final TypeElement tp : eleUtil.getAllTypeElements(suCls) ) {
223                                                if( typEle.equals( tp ) ) { continue; }
224                                                attKey = getAttGroupName( tp,eleUtil );
225                                                if( attKey != null ) { return attKey; }
226                                        }
227                                }
228                        }
229
230                        if( attKey == null ) {
231                                for( final TypeMirror itype : typEle.getInterfaces() ) {                // 直近インターフェース
232                                        final String intFce = String.valueOf( itype );
233                                        attKey = ATT_KEY_MAP.get( intFce );
234                                        if( attKey != null ) { return attKey; }
235                                        else {
236                                                for( final TypeElement tp : eleUtil.getAllTypeElements(intFce) ) {
237                                                        if( typEle.equals( tp ) ) { continue; }
238                                                        attKey = getAttGroupName( tp,eleUtil );
239                                                        if( attKey != null ) { return attKey; }
240                                                }
241                                        }
242                                }
243                        }
244                }
245
246                return attKey;
247        }
248
249        /**
250         * 属性情報を管理する、AttKeySet クラスです。
251         *
252         * @og.rev 7.3.0.0 (2021/01/06) 新しいJavaDoc対応
253         *
254         * @version  4.0
255         * @author   Kazuhiko Hasegawa
256         * @since    JDK5.0,
257         */
258        private static final class AttKeySet {
259                private final String searchKey ;
260                private final int    len ;
261                private final String seq ;
262                private final String valueName ;
263
264                /**
265                 * コンストラクター
266                 *
267                 * @param searchKey  検索キー
268                 * @param seq        シーケンス番号
269                 * @param valueName  属性名
270                 *
271                 */
272                /* default */ AttKeySet( final String searchKey,final int seq,final String valueName ) {
273                        this.searchKey          = searchKey ;
274                        this.seq                = String.valueOf( seq );
275                        this.valueName          = valueName ;
276
277                        len = searchKey.length();
278                }
279
280                /**
281                 * シーケンス番号を返します。
282                 *
283                 * @return シーケンス番号
284                 *
285                 */
286                /* default */ String getSeq() {
287                        return seq;
288                }
289
290                /**
291                 * 属性名を返します。
292                 *
293                 * @return 属性名
294                 *
295                 */
296                /* default */ String getValueName() {
297                        return valueName;
298                }
299
300                /**
301                 * クラス名の先頭一致の場合の、**** 部分を返します。
302                 * インターフェースも扱えるように修正しましたので、先頭が _ の場合は、
303                 * _ を削除して返します。
304                 *
305                 * @param name クラスの名称 (例:DBCellEditor_**** , ViewForm_****)
306                 * @return クラス名の**** 部分
307                 */
308                /* default */ String getAttKey( final String name ) {
309                        // 6.1.0.0 (2014/12/26) refactoring
310                        String rtn = null;                                                              // 一致しなかった。
311
312                        if( name.equals( searchKey ) ) {                                // 完全一致:インターフェース
313                                rtn =  "(Interface)" + name ;
314                        }
315                        else if( name.indexOf( searchKey ) == 0 ) {             // 先頭一致した。
316                                rtn = name.substring( len );
317                                if( rtn.charAt(0) == '_' ) { rtn = rtn.substring( 1 ); }        // 先頭が _ の場合は、_ を削除
318                        }
319
320                        return rtn ;
321                }
322        }
323
324        /**
325         * サポートされているすべてのオプションを返します。
326         *
327         * @return サポートされているすべてのオプションを含むセット、存在しない場合は空のセット
328         */
329        @Override
330        public Set<? extends Option> getSupportedOptions() {
331                final Option[] options = {
332                        new AbstractOption( "-outfile", "-version" ) {
333
334                                /**
335                                 * 必要に応じてオプションと引数を処理します。
336                                 *
337                                 * @param  opt オプション名
338                                 * @param  arguments 引数をカプセル化したリスト
339                                 * @return 操作が成功した場合はtrue、そうでない場合はfalse
340                                 */
341                                @Override
342                                public boolean process(final String opt, final List<String> arguments) {
343                                        if( "-outfile".equalsIgnoreCase(opt) ) {
344                                                outfile = arguments.get(0);
345                                        }
346                                        else if( "-version".equalsIgnoreCase(opt) ) {
347                                                version = arguments.get(0);
348                                        }
349                                        return true;
350                                }
351                        }
352                };
353                return new HashSet<>(Arrays.asList(options));
354        }
355}