1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package io.github.tonywasher.joceanus.themis.parser.maven;
19
20 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
23 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
24 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
25 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34 public class ThemisMavenPom
35 implements MetisFieldItem {
36
37
38
39 interface ThemisXMavenControl {
40
41
42
43
44
45
46
47 ThemisMavenPom loadPomViaId(ThemisMavenId pId) throws OceanusException;
48
49
50
51
52
53
54
55 ThemisMavenPom loadPomAtLocation(File pLocation) throws OceanusException;
56 }
57
58
59
60
61 private static final MetisFieldSet<ThemisMavenPom> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisMavenPom.class);
62
63
64
65
66 static {
67 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_ID, ThemisMavenPom::getId);
68 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PARENT, ThemisMavenPom::getParent);
69 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PROPERTIES, ThemisMavenPom::getProperties);
70 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_XTRADIRS, ThemisMavenPom::getXtraDirs);
71 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_MODULES, ThemisMavenPom::getModules);
72 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_VERSIONS, ThemisMavenPom::getTheVersions);
73 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_DIRECTDEPENDENCIES, ThemisMavenPom::getTheDirectDependencies);
74 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_DEPENDENCIES, ThemisMavenPom::getTheDependencies);
75 }
76
77
78
79
80 private final ThemisXMavenControl theControl;
81
82
83
84
85 private final File theLocation;
86
87
88
89
90 private final ThemisMavenPomParser theParser;
91
92
93
94
95 private final ThemisMavenId theId;
96
97
98
99
100 private final ThemisMavenPom theParent;
101
102
103
104
105 private final ThemisMavenPropertyCache theProperties;
106
107
108
109
110 private final List<String> theXtraDirs;
111
112
113
114
115 private final List<ThemisMavenPom> theModules;
116
117
118
119
120 private final boolean isJarPackage;
121
122
123
124
125 private ThemisMavenVersionCache theVersions;
126
127
128
129
130 private List<ThemisMavenPom> theDirectDependencies;
131
132
133
134
135 private List<ThemisMavenId> theDependencies;
136
137
138
139
140
141
142
143
144 ThemisMavenPom(final ThemisXMavenControl pController,
145 final File pLocation) throws OceanusException {
146
147 theControl = pController;
148 theLocation = new File(pLocation.getParent());
149
150
151 theProperties = new ThemisMavenPropertyCache();
152
153
154 theParser = new ThemisMavenPomParser(pLocation, theProperties);
155
156
157 theModules = new ArrayList<>();
158 theXtraDirs = new ArrayList<>();
159
160
161 final ThemisMavenId myParentId = theParser.getParent();
162
163
164 if (myParentId != null) {
165
166 theParent = theControl.loadPomViaId(myParentId);
167
168
169 theProperties.setParent(theParent.getProperties());
170
171 } else {
172 theParent = null;
173 }
174
175
176 theParser.readProperties();
177
178
179 theId = theParser.getId(theParent == null ? null : theParent.getId());
180
181
182 isJarPackage = theParser.isJarPackaging();
183 }
184
185
186
187
188
189
190
191 ThemisMavenPom(final ThemisMavenId pId) throws OceanusException {
192
193 theId = pId;
194
195
196 theControl = null;
197 theLocation = null;
198 theParent = null;
199
200
201 theProperties = new ThemisMavenPropertyCache();
202 theVersions = new ThemisMavenVersionCache();
203
204
205 theParser = null;
206
207
208 isJarPackage = false;
209
210
211 theModules = new ArrayList<>();
212 theXtraDirs = new ArrayList<>();
213 theDirectDependencies = new ArrayList<>();
214 theDependencies = new ArrayList<>();
215 }
216
217 @Override
218 public MetisFieldSet<ThemisMavenPom> getDataFieldSet() {
219 return FIELD_DEFS;
220 }
221
222 @Override
223 public String formatObject(final OceanusDataFormatter pFormatter) {
224 return getId().formatObject(pFormatter);
225 }
226
227
228
229
230
231
232 public ThemisMavenId getId() {
233 return theId;
234 }
235
236
237
238
239
240
241 public String getArtifactId() {
242 return theId.getArtifactId();
243 }
244
245
246
247
248
249
250 public File getLocation() {
251 return theLocation;
252 }
253
254
255
256
257
258
259 private ThemisMavenPom getParent() {
260 return theParent;
261 }
262
263
264
265
266
267
268
269 public boolean isJarPackaging() throws OceanusException {
270 return isJarPackage;
271 }
272
273
274
275
276
277
278 public List<ThemisMavenPom> getModules() {
279 return theModules;
280 }
281
282
283
284
285
286
287
288 public List<ThemisMavenPom> getDirectDependencies() throws OceanusException {
289
290 if (theDirectDependencies == null) {
291
292 theDirectDependencies = processDependencies();
293 }
294
295
296 return theDirectDependencies;
297 }
298
299
300
301
302
303
304 private List<ThemisMavenPom> getTheDirectDependencies() {
305
306 return theDirectDependencies;
307 }
308
309
310
311
312
313
314
315 public List<ThemisMavenId> getDependencies() throws OceanusException {
316
317 if (theDependencies == null) {
318
319 theDependencies = combineDependencies();
320 }
321
322
323 return theDependencies;
324 }
325
326
327
328
329
330
331 public List<ThemisMavenId> getTheDependencies() {
332
333 return theDependencies;
334 }
335
336
337
338
339
340
341 public List<String> getXtraDirs() {
342 return theXtraDirs;
343 }
344
345
346
347
348
349
350
351 public ThemisMavenVersionCache getVersions() throws OceanusException {
352
353 if (theVersions == null) {
354
355 theVersions = processDependencyManagement();
356 }
357
358
359 return theVersions;
360 }
361
362
363
364
365
366
367 private ThemisMavenVersionCache getTheVersions() {
368
369 return theVersions;
370 }
371
372
373
374
375
376
377 ThemisMavenPropertyCache getProperties() {
378 return theProperties;
379 }
380
381
382
383
384
385
386 void processLocalDetails() throws OceanusException {
387
388 final List<String> myModules = theParser.getModules();
389 for (String myModule : myModules) {
390
391 final ThemisMavenPom myLoaded = theControl.loadPomAtLocation(new File(theLocation, myModule));
392 theModules.add(myLoaded);
393 }
394
395
396 final List<String> myXtraDirs = theParser.getXtraDirs();
397 theXtraDirs.addAll(myXtraDirs);
398 }
399
400
401
402
403
404
405
406 private ThemisMavenVersionCache processDependencyManagement() throws OceanusException {
407
408 final ThemisMavenVersionCache myCache = new ThemisMavenVersionCache();
409
410
411 if (theParent != null) {
412 myCache.setParent(theParent.getVersions());
413 }
414
415
416 final List<ThemisMavenId> myDependencyMgmt = theParser.getDependencyManagement();
417 for (ThemisMavenId myDependency : myDependencyMgmt) {
418
419 if ("import".equals(myDependency.getScope())) {
420 final ThemisMavenPom myLoaded = theControl.loadPomViaId(myDependency);
421 myCache.importDependencies(myLoaded.getVersions());
422 } else {
423 myCache.addToCache(myDependency);
424 }
425 }
426
427
428 return myCache;
429 }
430
431
432
433
434
435
436
437 private List<ThemisMavenPom> processDependencies() throws OceanusException {
438
439 final List<ThemisMavenPom> myPoms = new ArrayList<>();
440
441
442 final List<ThemisMavenId> myDependencies = theParser.getDependencies();
443 for (ThemisMavenId myDependency : myDependencies) {
444
445 final ThemisMavenId myId = theVersions.lookUpVersion(myDependency);
446 final ThemisMavenPom myLoaded = theControl.loadPomViaId(myId);
447 myPoms.add(myLoaded);
448 }
449
450
451 return myPoms;
452 }
453
454
455
456
457
458
459
460 private List<ThemisMavenId> combineDependencies() throws OceanusException {
461
462 final List<ThemisMavenId> myIds = new ArrayList<>();
463
464
465 combineDependencies(myIds, this);
466
467
468 return myIds;
469 }
470
471
472
473
474
475
476
477
478 private static void combineDependencies(final List<ThemisMavenId> pIds,
479 final ThemisMavenPom pPom) throws OceanusException {
480
481 for (ThemisMavenPom myDependency : pPom.getDirectDependencies()) {
482
483 final ThemisMavenId myId = myDependency.getId();
484 if (checkDependencyId(pIds, myId)) {
485
486 combineDependencies(pIds, myDependency);
487 }
488 }
489 }
490
491
492
493
494
495
496
497
498
499 static boolean checkDependencyId(final List<ThemisMavenId> pDependencies,
500 final ThemisMavenId pId) throws OceanusException {
501
502 for (ThemisMavenId myDependency : pDependencies) {
503
504 if (myDependency.equalsPrefix(pId)) {
505
506 if (myDependency.getVersion().equals(pId.getVersion())) {
507 return false;
508 }
509
510
511 throw new ThemisDataException("Mismatch of dependency versions " + myDependency + " vs " + pId);
512 }
513 }
514
515
516 pDependencies.add(pId);
517 return true;
518 }
519 }