1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.themis.parser.project;
18
19 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
20 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
21 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
22 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
23 import io.github.tonywasher.joceanus.themis.parser.base.ThemisChar;
24 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
25 import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Objects;
31
32
33
34
35 public class ThemisPackage
36 implements MetisFieldItem {
37
38
39
40 private static final MetisFieldSet<ThemisPackage> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisPackage.class);
41
42
43
44
45 static {
46 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_NAME, ThemisPackage::getShortName);
47 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_FILES, ThemisPackage::getFiles);
48 }
49
50
51
52
53 static final String PATH_XTRA = ".src.main.java".replace(ThemisChar.PERIOD, File.separatorChar);
54
55
56
57
58 private static final String PACKAGE_INFO = "package-info" + ThemisFile.SFX_JAVA;
59
60
61
62
63 private final String thePackage;
64
65
66
67
68 private final String theShortName;
69
70
71
72
73 private final String theParentName;
74
75
76
77
78 private final File theLocation;
79
80
81
82
83 private final List<ThemisFile> theFiles;
84
85
86
87
88 private final boolean isStandard;
89
90
91
92
93 private final boolean isPlaceHolder;
94
95
96
97
98
99
100
101 ThemisPackage(final File pLocation,
102 final String pPackage) {
103
104 theLocation = pLocation;
105 thePackage = pPackage;
106 isStandard = pLocation.getAbsolutePath().endsWith(PATH_XTRA);
107 isPlaceHolder = false;
108
109
110 final int iIndex = thePackage.lastIndexOf(ThemisChar.PERIOD);
111 theShortName = iIndex == -1 ? thePackage : thePackage.substring(iIndex + 1);
112 theParentName = iIndex == -1 ? null : thePackage.substring(0, iIndex);
113
114
115 final String myPath = pPackage.replace(ThemisChar.PERIOD, File.separatorChar);
116 final File myLocation = new File(pLocation, myPath);
117
118
119 theFiles = listFiles(myLocation);
120 }
121
122 @Override
123 public MetisFieldSet<ThemisPackage> getDataFieldSet() {
124 return FIELD_DEFS;
125 }
126
127 @Override
128 public String formatObject(final OceanusDataFormatter pFormatter) {
129 return toString();
130 }
131
132
133
134
135
136
137 public ThemisPackage(final String pPackage) {
138
139 theLocation = null;
140 thePackage = pPackage;
141 isStandard = false;
142 isPlaceHolder = true;
143
144
145 final int iIndex = thePackage.lastIndexOf(ThemisChar.PERIOD);
146 theShortName = iIndex == -1 ? thePackage : thePackage.substring(iIndex + 1);
147 theParentName = iIndex == -1 ? null : thePackage.substring(0, iIndex);
148
149
150 theFiles = new ArrayList<>();
151 }
152
153
154
155
156
157
158 public String getPackage() {
159 return thePackage;
160 }
161
162
163
164
165
166
167 public String getShortName() {
168 return theShortName;
169 }
170
171
172
173
174
175
176 public String getParentName() {
177 return theParentName;
178 }
179
180
181
182
183
184
185 public boolean isRoot() {
186 return theShortName.isEmpty();
187 }
188
189
190
191
192
193
194 public File getLocation() {
195 return theLocation;
196 }
197
198
199
200
201
202
203 public List<ThemisFile> getFiles() {
204 return theFiles;
205 }
206
207
208
209
210
211
212 public boolean isStandard() {
213 return isStandard;
214 }
215
216
217
218
219
220
221 public boolean isPlaceHolder() {
222 return isPlaceHolder;
223 }
224
225
226
227
228
229
230
231 List<ThemisFile> listFiles(final File pLocation) {
232
233 final List<ThemisFile> myFiles = new ArrayList<>();
234
235
236 for (File myEntry : Objects.requireNonNull(pLocation.listFiles())) {
237
238 if (!myEntry.isDirectory()) {
239
240 final String myName = myEntry.getName();
241
242
243 if (myName.endsWith(ThemisFile.SFX_JAVA)
244 && !PACKAGE_INFO.equals(myName)) {
245
246 final ThemisFile myFile = new ThemisFile(myEntry);
247 myFiles.add(myFile);
248 }
249 }
250 }
251
252
253 return myFiles;
254 }
255
256
257
258
259
260
261
262 void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
263
264 pParser.setCurrentPackage(thePackage);
265
266
267 for (ThemisFile myFile : theFiles) {
268
269 myFile.parseJavaCode(pParser);
270 }
271 }
272
273 @Override
274 public String toString() {
275 return thePackage;
276 }
277 }