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 * Package.
28 */
29 public class ThemisAnalysisPackage
30 implements ThemisAnalysisElement {
31 /**
32 * The java suffix.
33 */
34 public static final String SFX_JAVA = ".java";
35
36 /**
37 * The package-info file.
38 */
39 private static final String PACKAGE_INFO = "package-info" + SFX_JAVA;
40
41 /**
42 * The package name.
43 */
44 private final String thePackage;
45
46 /**
47 * The list of files in this package.
48 */
49 private final List<ThemisAnalysisFile> theFiles;
50
51 /**
52 * The initial dataMap.
53 */
54 private final ThemisAnalysisDataMap theDataMap;
55
56 /**
57 * Module Constructor.
58 *
59 * @param pModule the owning module
60 * @param pPackage the package name.
61 * @throws OceanusException on error
62 */
63 ThemisAnalysisPackage(final ThemisAnalysisModule pModule,
64 final String pPackage) throws OceanusException {
65 /* Initialise class */
66 this(pModule.getLocation(), new ThemisAnalysisDataMap(pModule.getDataMap()), pPackage);
67 }
68
69 /**
70 * Constructor.
71 *
72 * @param pLocation the base location for the package
73 * @param pPackage the package name.
74 * @throws OceanusException on error
75 */
76 ThemisAnalysisPackage(final File pLocation,
77 final String pPackage) throws OceanusException {
78 /* Initialise class */
79 this(pLocation, new ThemisAnalysisDataMap(), pPackage);
80
81 /* initialPass */
82 performConsolidationPass();
83
84 /* consolidationPass */
85 performConsolidationPass();
86
87 /* finalPass */
88 performFinalPass();
89 }
90
91 /**
92 * Constructor.
93 *
94 * @param pLocation the base location for the package
95 * @param pDataMap the dataMap
96 * @param pPackage the package name.
97 */
98 private ThemisAnalysisPackage(final File pLocation,
99 final ThemisAnalysisDataMap pDataMap,
100 final String pPackage) {
101 /* Store package name and dataMap */
102 thePackage = pPackage;
103 theDataMap = pDataMap;
104
105 /* Create directory path and record the location */
106 final String myPath = pPackage.replace(ThemisAnalysisChar.PERIOD, File.separatorChar);
107 final File myLocation = new File(pLocation, myPath);
108
109 /* Build list of files */
110 theFiles = listFiles(myLocation);
111 }
112
113 /**
114 * Obtain the files.
115 *
116 * @return the files
117 */
118 public List<ThemisAnalysisFile> getFiles() {
119 return theFiles;
120 }
121
122 /**
123 * Obtain the dataMap.
124 *
125 * @return the map
126 */
127 ThemisAnalysisDataMap getDataMap() {
128 return theDataMap;
129 }
130
131 /**
132 * Build list of files.
133 *
134 * @param pLocation the location
135 * @return the list of files
136 */
137 List<ThemisAnalysisFile> listFiles(final File pLocation) {
138 /* Allocate the list */
139 final List<ThemisAnalysisFile> myClasses = new ArrayList<>();
140
141 /* Loop through the entries in the directory */
142 for (File myFile : Objects.requireNonNull(pLocation.listFiles())) {
143 /* Handle files */
144 if (!myFile.isDirectory()) {
145 /* Access the name of the file */
146 final String myName = myFile.getName();
147
148 /* If this is a .java that is not package-info */
149 if (myName.endsWith(SFX_JAVA)
150 && !PACKAGE_INFO.equals(myName)) {
151 /* Add the class */
152 final ThemisAnalysisFile myClass = new ThemisAnalysisFile(this, myFile);
153 myClasses.add(myClass);
154 }
155 }
156 }
157
158 /* Return the classes */
159 return myClasses;
160 }
161
162 /**
163 * initialPass process files.
164 *
165 * @throws OceanusException on error
166 */
167 void performInitialPass() throws OceanusException {
168 /* Loop through the classes */
169 for (ThemisAnalysisFile myFile : theFiles) {
170 /* Process the file */
171 myFile.processFile();
172 }
173 }
174
175 /**
176 * consolidationPass process files.
177 *
178 * @throws OceanusException on error
179 */
180 void performConsolidationPass() throws OceanusException {
181 /* Loop through the classes */
182 for (ThemisAnalysisFile myFile : theFiles) {
183 /* Process the file */
184 myFile.consolidationProcessingPass();
185 }
186 }
187
188 /**
189 * finalPass process files.
190 *
191 * @throws OceanusException on error
192 */
193 void performFinalPass() throws OceanusException {
194 /* Loop through the classes */
195 for (ThemisAnalysisFile myFile : theFiles) {
196 /* Process the file */
197 myFile.finalProcessingPass();
198 }
199 }
200
201 /**
202 * Obtain the package.
203 *
204 * @return the package
205 */
206 public String getPackage() {
207 return thePackage;
208 }
209
210 /**
211 * Is the line a package?
212 *
213 * @param pLine the line
214 * @return true/false
215 * @throws OceanusException on error
216 */
217 static boolean isPackage(final ThemisAnalysisLine pLine) throws OceanusException {
218 /* If we are ended by a semi-colon and this is a package line */
219 if (pLine.endsWithChar(ThemisAnalysisChar.SEMICOLON)
220 && pLine.isStartedBy(ThemisAnalysisKeyWord.PACKAGE.getKeyWord())) {
221 /* Strip the semi-colon and return true */
222 pLine.stripEndChar(ThemisAnalysisChar.SEMICOLON);
223 return true;
224 }
225
226 /* return false */
227 return false;
228 }
229
230 @Override
231 public String toString() {
232 return thePackage;
233 }
234 }