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// 7.4.4.0 (2021/06/30) openGionV8事前準備(taglet2→taglet)
017//package org.opengion.fukurou.taglet2;
018package org.opengion.fukurou.taglet;
019
020
021import jdk.javadoc.doclet.DocletEnvironment      ;
022// import jdk.javadoc.doclet.Doclet  ;
023// import jdk.javadoc.doclet.Reporter ;
024// import javax.lang.model.element.Element      ;
025import javax.lang.model.element.Modifier ;
026import javax.lang.model.element.TypeElement;
027// import javax.lang.model.element.ElementKind  ;
028import javax.lang.model.element.VariableElement;
029// import javax.lang.model.SourceVersion ;
030import javax.lang.model.util.ElementFilter ;
031// import javax.lang.model.util.Elements ;
032import javax.tools.Diagnostic.Kind ;
033import com.sun.source.doctree.DocCommentTree  ;
034import com.sun.source.util.DocTrees  ;
035import com.sun.source.doctree.DocTree  ;
036
037// import java.util.Locale ;
038import java.util.Set;
039import java.util.List;
040import java.util.HashSet;
041import java.util.Arrays;
042import java.util.Map;
043
044// import java.io.IOException;
045// import java.io.File;
046// import java.io.PrintWriter;
047
048// import org.opengion.fukurou.util.FileUtil;
049// import org.opengion.fukurou.util.StringUtil;
050
051/**
052 * ソースコメントから、パラメータ情報を取り出す Doclet クラスです。
053 * og.paramLevel タグと og.cryptography タグを切り出します。
054 * これらは、システムパラメータとしてGE12テーブルに設定される値をクラスより抽出する
055 * のに使用します。
056 *
057 * @version  7.3
058 * @author      Kazuhiko Hasegawa
059 * @since        JDK11.0,
060 */
061public class DocTreeParam extends AbstractDocTree {
062        private static final String OG_PARAM_LVL    = "og.paramLevel";
063        private static final String OG_CRYPTOGRAPHY = "og.cryptography";
064
065        private static final int    CNST   = 1000;
066
067        private String systemId = "**" ;
068        private String outfile;
069
070//      private DocTrees docUtil;
071
072        /**
073         * デフォルトコンストラクター
074         *
075         * @og.rev 7.3.0.0 (2021/01/06) PMD refactoring. Each class should declare at least one constructor.
076         */
077        public DocTreeParam() { super(); }              // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
078
079        /**
080         * Doclet のエントリポイントメソッドです(昔の startメソッド)。
081         *
082         * @og.rev 7.3.0.0 (2021/01/06) 新しいJavaDoc対応
083         *
084         * @param docEnv ドックレットを1回呼び出す操作環境
085         *
086         * @return 正常実行時 true
087         */
088        @Override
089        public boolean run( final DocletEnvironment docEnv ) {
090                try( DocTreeWriter writer = new DocTreeWriter( outfile,ENCODE ) ) {
091                        writer.printTag( "<?xml version=\"1.0\" encoding=\"", ENCODE , "\" ?>" );
092                        writer.printTag( "<javadoc>" );
093                        writer.printTag( " <systemId>" , systemId , "</systemId>" );
094                        writeContents( docEnv,writer );
095                        writer.printTag( "</javadoc>" );
096                }
097                catch( final Throwable th ) {
098                        reporter.print(Kind.ERROR, th.getMessage());
099                }
100
101                return true;
102        }
103
104        /**
105         * DocletEnvironmentよりコンテンツを作成します。
106         *
107         * @og.rev 7.3.0.0 (2021/01/06) 新しいJavaDoc対応
108         *
109         * @param docEnv        ドックレットの最上位
110         * @param writer        DocTreeWriterオブジェクト
111         */
112        private void writeContents( final DocletEnvironment docEnv, final DocTreeWriter writer ) {
113                // get the DocTrees utility class to access document comments
114//              docUtil = docEnv.getDocTrees();
115                final DocTrees docUtil = docEnv.getDocTrees();
116//              final Elements eleUtil  = docEnv.getElementUtils();
117
118                // クラス単位にループする。
119                for( final TypeElement typEle : ElementFilter.typesIn(docEnv.getIncludedElements())) {
120                        int cnt = 0;
121                        final String fullName = String.valueOf( typEle.getQualifiedName() ) ;
122//                      final String fullName = String.valueOf(typEle);
123//                      System.out.println(typEle.getKind() + ":" + fullName);
124                        writer.setClassName( fullName );
125
126                        // フィールドのみフィルタリングして取得する
127                        for( final VariableElement varEle : ElementFilter.fieldsIn(typEle.getEnclosedElements())) {             // フィールドだけに絞る
128                                if( varEle.getModifiers().contains( Modifier.PUBLIC )) {                                                                                // public だけに絞る
129                                        final DocCommentTree docTree = docUtil.getDocCommentTree(varEle);               // ドキュメンテーション・コメントが見つからない場合、null が返る。
130
131                                        final String paramId                            = String.valueOf(varEle);
132                                        final String seq                                        = String.valueOf(cnt*10 + CNST);
133                                        final List<? extends DocTree> title     = docTree == null ? EMPTY_LIST : docTree.getFirstSentence();
134                                        final List<? extends DocTree> cmnt      = docTree == null ? EMPTY_LIST : docTree.getFullBody();
135
136                                        final String param = String.valueOf( varEle.getConstantValue() );
137
138                                        final Map<String,List<String>> blkTagMap = blockTagsMap(docTree);
139                                        final String paramLvl = getBlockTag( OG_PARAM_LVL   , blkTagMap, "" ).split(":")[0];    // 取得した値を ':' で分割した最初の文字列。
140                                        final String fgcrypt  = getBlockTag( OG_CRYPTOGRAPHY, blkTagMap, "" ).split(":")[0];
141
142//                                      String paramLvl = "";
143//                                      String fgcrypt  = "";
144//                                      if( docTree != null ) {
145//                                              for( final DocTree dt : docTree.getBlockTags() ) {
146//                                                      final String tag = String.valueOf(dt);
147//                                                      if( tag.contains( OG_PARAM_LVL ) ) {
148//                                                              paramLvl = cut( tag,' ',':' );
149//                                                      }
150//                                                      else if( tag.contains( OG_CRYPTOGRAPHY ) ) {
151//                                                              fgcrypt = cut( tag,' ',':' );
152//                                                      }
153//                                              }
154//                                      }
155
156                                        writer.printTag( " <fieldDoc>" );
157                                        writer.printTag( "   <paramId>"         ,paramId        ,"</paramId>"           );
158                                        writer.printTag( "   <seq>"                     ,seq            ,"</seq>"                       );
159                                        writer.printTag( "   <param>"           ,param          ,"</param>"                     );
160                                        writer.printTag( "   <title>"           ,title          ,"</title>"                     );
161                                        writer.printTag( "   <contents>"        ,cmnt           ,"</contents>"          );
162                                        writer.printTag( "   <paramLevel>"      ,paramLvl       ,"</paramLevel>"        );
163                                        writer.printTag( "   <fgcrypt>"         ,fgcrypt        ,"</fgcrypt>"           );
164                                        writer.printTag( " </fieldDoc>" );
165
166                                        cnt++;
167                                }
168                        }
169                }
170        }
171
172        /**
173         * サポートされているすべてのオプションを返します。
174         *
175         * @return サポートされているすべてのオプションを含むセット、存在しない場合は空のセット
176         */
177        @Override
178        public Set<? extends Option> getSupportedOptions() {
179                final Option[] options = {
180                        new AbstractOption( "-outfile", "-systemId" ) {
181
182                                /**
183                                 * 必要に応じてオプションと引数を処理します。
184                                 *
185                                 * @param  opt オプション名
186                                 * @param  arguments 引数をカプセル化したリスト
187                                 * @return 操作が成功した場合はtrue、そうでない場合はfalse
188                                 */
189                                @Override
190                                public boolean process(final String opt, final List<String> arguments) {
191                                        if( "-outfile".equalsIgnoreCase(opt) ) {
192                                                outfile = arguments.get(0);
193                                        }
194                                        else if( "-systemId".equalsIgnoreCase(opt) ) {
195                                                systemId = arguments.get(0);
196                                        }
197                                        return true;
198                                }
199                        }
200                };
201                return new HashSet<>(Arrays.asList(options));
202        }
203}