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.parser.proj;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.themis.parser.base.ThemisChar;
21  import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
22  import io.github.tonywasher.joceanus.themis.parser.mod.ThemisModModule;
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 ThemisModule {
33      /**
34       * The module-info file.
35       */
36      private static final String MODULE_INFO = "module-info" + ThemisFile.SFX_JAVA;
37  
38      /**
39       * The module name.
40       */
41      private final String theName;
42  
43      /**
44       * The locations.
45       */
46      private final File theLocation;
47  
48      /**
49       * The package list.
50       */
51      private final List<ThemisPackage> thePackages;
52  
53      /**
54       * The module-info declaration.
55       */
56      private ThemisModModule theModuleInfo;
57  
58      /**
59       * Constructor.
60       *
61       * @param pLocation the location of the module
62       * @param pPom      the module Pom
63       */
64      ThemisModule(final File pLocation,
65                   final ThemisMaven pPom) {
66          /* Create the list */
67          thePackages = new ArrayList<>();
68  
69          /* Store the name and location */
70          theLocation = new File(pLocation, ThemisPackage.PATH_XTRA);
71          theName = pLocation.getName();
72  
73          /* Initiate search for packages */
74          checkForPackage(theLocation, null);
75  
76          /* Add any extraDirs */
77          for (String myXtra : pPom.getXtraDirs()) {
78              final File myXtraDir = new File(pLocation, myXtra);
79              checkForPackage(myXtraDir, null);
80          }
81      }
82  
83      /**
84       * Obtain the name.
85       *
86       * @return the name
87       */
88      public String getName() {
89          return theName;
90      }
91  
92      @Override
93      public String toString() {
94          return getName();
95      }
96  
97      /**
98       * Obtain the packages.
99       *
100      * @return the packages
101      */
102     public List<ThemisPackage> getPackages() {
103         return thePackages;
104     }
105 
106     /**
107      * Obtain the module-info.
108      *
109      * @return the module-info
110      */
111     public ThemisModModule getModuleInfo() {
112         return theModuleInfo;
113     }
114 
115     /**
116      * Check for package.
117      *
118      * @param pLocation the location to search
119      * @param pPackage  the package name
120      */
121     void checkForPackage(final File pLocation,
122                          final String pPackage) {
123         /* Assume not a package */
124         boolean isPackage = false;
125 
126         /* Determine the location to search */
127         final File myLocation = pPackage == null
128                 ? pLocation
129                 : new File(pLocation, pPackage.replace(ThemisChar.PERIOD, ThemisChar.COMMENT));
130 
131         /* Look for java files or further packages */
132         for (File myFile : Objects.requireNonNull(myLocation.listFiles())) {
133             /* Access file name */
134             final String myName = myFile.getName();
135 
136             /* If this is a directory */
137             if (myFile.isDirectory()) {
138                 final String myPackage = pPackage == null
139                         ? myName
140                         : pPackage + ThemisChar.PERIOD + myName;
141                 checkForPackage(pLocation, myPackage);
142             }
143 
144             /* If this is a Java file name */
145             if (myName.endsWith(ThemisFile.SFX_JAVA)
146                     && !MODULE_INFO.equals(myName)) {
147                 isPackage = pPackage != null;
148             }
149         }
150 
151         /* If this is a package */
152         if (isPackage) {
153             /* Add the package to the list */
154             thePackages.add(new ThemisPackage(pLocation, pPackage));
155         }
156     }
157 
158     /**
159      * Parse java code.
160      *
161      * @param pParser the parser
162      * @throws OceanusException on error
163      */
164     void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
165         /* Loop through the packages */
166         for (ThemisPackage myPackage : thePackages) {
167             /* Process the package */
168             myPackage.parseJavaCode(pParser);
169         }
170 
171         /* Check for and load the module-info file if found */
172         final File myModuleInfo = new File(theLocation, MODULE_INFO);
173         if (myModuleInfo.exists()) {
174             theModuleInfo = (ThemisModModule) pParser.parseModuleInfo(myModuleInfo);
175         }
176     }
177 }