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.lethe.analysis;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Objects;
25
26 /**
27 * Module.
28 */
29 public class ThemisAnalysisModule
30 implements ThemisAnalysisElement {
31 /**
32 * The path xtra.
33 */
34 static final String PATH_XTRA = "/src/main/java";
35
36 /**
37 * The module-info file.
38 */
39 private static final String MODULE_INFO = "module-info" + ThemisAnalysisPackage.SFX_JAVA;
40
41 /**
42 * The module name.
43 */
44 private final String theName;
45
46 /**
47 * The location.
48 */
49 private final File theLocation;
50
51 /**
52 * The package list.
53 */
54 private final List<ThemisAnalysisPackage> thePackages;
55
56 /**
57 * The initial dataMap.
58 */
59 private final ThemisAnalysisDataMap theDataMap;
60
61 /**
62 * Constructor.
63 *
64 * @param pProject the project
65 * @param pLocation the module location
66 * @throws OceanusException on error
67 */
68 ThemisAnalysisModule(final ThemisAnalysisProject pProject,
69 final File pLocation) throws OceanusException {
70 /* Initialise class */
71 this(pLocation, new ThemisAnalysisDataMap(pProject.getDataMap()));
72 }
73
74 /**
75 * Constructor.
76 *
77 * @param pLocation the module location
78 * @throws OceanusException on error
79 */
80 ThemisAnalysisModule(final File pLocation) throws OceanusException {
81 /* Initialise class */
82 this(pLocation, new ThemisAnalysisDataMap());
83
84 /* initialPass */
85 performInitialPass();
86
87 /* consolidationPass */
88 performConsolidationPass();
89
90 /* finalPass */
91 performFinalPass();
92 }
93
94 /**
95 * Constructor.
96 *
97 * @param pLocation the module location
98 * @param pDataMap the dataMap
99 * @throws OceanusException on error
100 */
101 private ThemisAnalysisModule(final File pLocation,
102 final ThemisAnalysisDataMap pDataMap) throws OceanusException {
103 /* Store the name and location */
104 theLocation = new File(pLocation, PATH_XTRA);
105 theName = pLocation.getName();
106 theDataMap = pDataMap;
107
108 /* Create the list */
109 thePackages = new ArrayList<>();
110
111 /* Initiate search for packages */
112 checkForPackage(null);
113 }
114
115 /**
116 * Obtain the name.
117 *
118 * @return the name
119 */
120 public String getName() {
121 return theName;
122 }
123
124 @Override
125 public String toString() {
126 return getName();
127 }
128
129 /**
130 * Obtain the location.
131 *
132 * @return the location
133 */
134 File getLocation() {
135 return theLocation;
136 }
137
138 /**
139 * Obtain the packages.
140 *
141 * @return the packages
142 */
143 public List<ThemisAnalysisPackage> getPackages() {
144 return thePackages;
145 }
146
147 /**
148 * Obtain the dataMap.
149 *
150 * @return the map
151 */
152 ThemisAnalysisDataMap getDataMap() {
153 return theDataMap;
154 }
155
156 /**
157 * Check for package.
158 *
159 * @param pPackage the package name
160 * @throws OceanusException on error
161 */
162 void checkForPackage(final String pPackage) throws OceanusException {
163 /* Assume not a package */
164 boolean isPackage = false;
165
166 /* Determine the location to search */
167 final File myLocation = pPackage == null
168 ? theLocation
169 : new File(theLocation, pPackage.replace(ThemisAnalysisChar.PERIOD, ThemisAnalysisChar.COMMENT));
170
171 /* Look for java files or further packages */
172 for (File myFile : Objects.requireNonNull(myLocation.listFiles())) {
173 /* Access file name */
174 final String myName = myFile.getName();
175
176 /* If this is a directory */
177 if (myFile.isDirectory()) {
178 final String myPackage = pPackage == null
179 ? myName
180 : pPackage + ThemisAnalysisChar.PERIOD + myName;
181 checkForPackage(myPackage);
182 }
183
184 /* If this is aAccess file name */
185 if (myName.endsWith(ThemisAnalysisPackage.SFX_JAVA)
186 && !MODULE_INFO.equals(myName)) {
187 isPackage = pPackage != null;
188 }
189 }
190
191 /* If this is a package */
192 if (isPackage) {
193 /* Add the package to the list */
194 thePackages.add(new ThemisAnalysisPackage(this, pPackage));
195 }
196 }
197
198 /**
199 * initialPass.
200 *
201 * @throws OceanusException on error
202 */
203 void performInitialPass() throws OceanusException {
204 /* Loop through the packages */
205 for (ThemisAnalysisPackage myPackage : thePackages) {
206 /* Process the package */
207 myPackage.performInitialPass();
208 }
209 }
210
211 /**
212 * consolidationPass.
213 *
214 * @throws OceanusException on error
215 */
216 void performConsolidationPass() throws OceanusException {
217 /* Loop through the packages */
218 for (ThemisAnalysisPackage myPackage : thePackages) {
219 /* Process the package */
220 myPackage.performConsolidationPass();
221 }
222 }
223
224 /**
225 * finalPass.
226 *
227 * @throws OceanusException on error
228 */
229 void performFinalPass() throws OceanusException {
230 /* Loop through the packages */
231 for (ThemisAnalysisPackage myPackage : thePackages) {
232 /* Process the package */
233 myPackage.performFinalPass();
234 }
235 }
236 }