View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.themis.xanalysis.parser.proj;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisChar;
21  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisParserDef;
22  import io.github.tonywasher.joceanus.themis.xanalysis.parser.mod.ThemisXAnalysisModModule;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Objects;
28  
29  /**
30   * Module.
31   */
32  public class ThemisXAnalysisModule {
33      /**
34       * The path xtra.
35       */
36      static final String PATH_XTRA = ".src.main.java".replace(ThemisXAnalysisChar.PERIOD, ThemisXAnalysisChar.COMMENT);
37  
38      /**
39       * The module-info file.
40       */
41      private static final String MODULE_INFO = "module-info" + ThemisXAnalysisFile.SFX_JAVA;
42  
43      /**
44       * The module name.
45       */
46      private final String theName;
47  
48      /**
49       * The locations.
50       */
51      private final File theLocation;
52  
53      /**
54       * The package list.
55       */
56      private final List<ThemisXAnalysisPackage> thePackages;
57  
58      /**
59       * The module-info declaration.
60       */
61      private ThemisXAnalysisModModule theModuleInfo;
62  
63      /**
64       * Constructor.
65       *
66       * @param pLocation the location of the module
67       * @param pPom      the module Pom
68       * @throws OceanusException on error
69       */
70      ThemisXAnalysisModule(final File pLocation,
71                            final ThemisXAnalysisMaven pPom) throws OceanusException {
72          /* Create the list */
73          thePackages = new ArrayList<>();
74  
75          /* Store the name and location */
76          theLocation = new File(pLocation, PATH_XTRA);
77          theName = pLocation.getName();
78  
79          /* Initiate search for packages */
80          checkForPackage(theLocation, null);
81  
82          /* Add any extraDirs */
83          for (String myXtra : pPom.getXtraDirs()) {
84              final File myXtraDir = new File(pLocation, myXtra);
85              checkForPackage(myXtraDir, null);
86          }
87      }
88  
89      /**
90       * Obtain the name.
91       *
92       * @return the name
93       */
94      public String getName() {
95          return theName;
96      }
97  
98      @Override
99      public String toString() {
100         return getName();
101     }
102 
103     /**
104      * Obtain the packages.
105      *
106      * @return the packages
107      */
108     public List<ThemisXAnalysisPackage> getPackages() {
109         return thePackages;
110     }
111 
112     /**
113      * Obtain the module-info.
114      *
115      * @return the module-info
116      */
117     public ThemisXAnalysisModModule getModuleInfo() {
118         return theModuleInfo;
119     }
120 
121     /**
122      * Check for package.
123      *
124      * @param pLocation the location to search
125      * @param pPackage  the package name
126      * @throws OceanusException on error
127      */
128     void checkForPackage(final File pLocation,
129                          final String pPackage) throws OceanusException {
130         /* Assume not a package */
131         boolean isPackage = false;
132 
133         /* Determine the location to search */
134         final File myLocation = pPackage == null
135                 ? pLocation
136                 : new File(pLocation, pPackage.replace(ThemisXAnalysisChar.PERIOD, ThemisXAnalysisChar.COMMENT));
137 
138         /* Look for java files or further packages */
139         for (File myFile : Objects.requireNonNull(myLocation.listFiles())) {
140             /* Access file name */
141             final String myName = myFile.getName();
142 
143             /* If this is a directory */
144             if (myFile.isDirectory()) {
145                 final String myPackage = pPackage == null
146                         ? myName
147                         : pPackage + ThemisXAnalysisChar.PERIOD + myName;
148                 checkForPackage(pLocation, myPackage);
149             }
150 
151             /* If this is a Java file name */
152             if (myName.endsWith(ThemisXAnalysisFile.SFX_JAVA)
153                     && !MODULE_INFO.equals(myName)) {
154                 isPackage = pPackage != null;
155             }
156         }
157 
158         /* If this is a package */
159         if (isPackage) {
160             /* Add the package to the list */
161             thePackages.add(new ThemisXAnalysisPackage(pLocation, pPackage));
162         }
163     }
164 
165     /**
166      * Parse java code.
167      *
168      * @param pParser the parser
169      * @throws OceanusException on error
170      */
171     void parseJavaCode(final ThemisXAnalysisParserDef pParser) throws OceanusException {
172         /* Loop through the packages */
173         for (ThemisXAnalysisPackage myPackage : thePackages) {
174             /* Process the package */
175             myPackage.parseJavaCode(pParser);
176         }
177 
178         /* Check for and load the module-info file if found */
179         final File myModuleInfo = new File(theLocation, MODULE_INFO);
180         if (myModuleInfo.exists()) {
181             theModuleInfo = (ThemisXAnalysisModModule) pParser.parseModuleInfo(myModuleInfo);
182         }
183     }
184 }