1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.exc.ThemisIOException;
21 import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31
32
33
34
35 public class ThemisProject {
36
37
38
39 private final String theName;
40
41
42
43
44 private final File theLocation;
45
46
47
48
49 private final Map<ThemisMavenId, ThemisModule> theModules;
50
51
52
53
54 private final List<ThemisMavenId> theDependencies;
55
56
57
58
59
60
61
62 public ThemisProject(final File pLocation) throws OceanusException {
63
64 theLocation = pLocation;
65
66
67 theModules = new LinkedHashMap<>();
68 theDependencies = new ArrayList<>();
69
70
71 final ThemisMaven myPom = parseProjectFile(null, new File(theLocation, ThemisMaven.POM));
72 theName = myPom == null ? null : myPom.getMavenId().getArtifactId();
73
74
75 theDependencies.removeIf(theModules::containsKey);
76
77
78 final List<ThemisMavenId> myDependencies = new ArrayList<>(theDependencies);
79 for (ThemisMavenId myId : myDependencies) {
80 processDependency(myId);
81 }
82 }
83
84
85
86
87
88
89 private void processDependency(final ThemisMavenId pId) throws OceanusException {
90 final File myFile = pId.getMavenPomPath();
91
92 try (InputStream myInStream = new FileInputStream(myFile)) {
93
94 final ThemisMaven myPom = new ThemisMaven(null, myInStream);
95
96
97 for (final ThemisMavenId myDepId : myPom.getDependencies()) {
98 if (!theDependencies.contains(myDepId)) {
99 theDependencies.add(myDepId);
100 if (myDepId.getClassifier() == null) {
101 processDependency(myDepId);
102 }
103 }
104 }
105 } catch (IOException e) {
106 throw new ThemisIOException("Failed to parse pom file", e);
107 }
108 }
109
110
111
112
113
114
115 public String getName() {
116 return theName;
117 }
118
119
120
121
122
123
124 File getLocation() {
125 return theLocation;
126 }
127
128
129
130
131
132
133 public List<ThemisModule> getModules() {
134 return new ArrayList<>(theModules.values());
135 }
136
137
138
139
140
141
142 public List<ThemisMavenId> getDependencies() {
143 return theDependencies;
144 }
145
146 @Override
147 public String toString() {
148 return theName;
149 }
150
151
152
153
154
155
156
157
158
159 private ThemisMaven parseProjectFile(final ThemisMaven pParent,
160 final File pPom) throws OceanusException {
161
162 if (!pPom.exists()) {
163 return null;
164 }
165
166
167 try (InputStream myInStream = new FileInputStream(pPom)) {
168
169 final ThemisMaven myPom = new ThemisMaven(pParent, myInStream);
170
171
172 final File mySrc = new File(pPom.getParent(), ThemisPackage.PATH_XTRA);
173 if (mySrc.exists()
174 && mySrc.isDirectory()) {
175
176 theModules.put(myPom.getMavenId(), new ThemisModule(new File(pPom.getParent()), myPom));
177
178
179 for (final ThemisMavenId myDepId : myPom.getDependencies()) {
180 if (!theDependencies.contains(myDepId)) {
181 theDependencies.add(myDepId);
182 }
183 }
184 }
185
186
187 for (final String myModuleName : myPom.getModules()) {
188
189 final File myModuleDir = new File(pPom.getParentFile(), myModuleName);
190
191
192 parseProjectFile(myPom, new File(myModuleDir, ThemisMaven.POM));
193 }
194
195
196 return myPom;
197
198
199 } catch (IOException e) {
200
201 throw new ThemisIOException("Failed to parse Project file", e);
202 }
203 }
204
205
206
207
208
209
210
211 public void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
212
213 for (ThemisModule myModule : theModules.values()) {
214
215 myModule.parseJavaCode(pParser);
216 }
217 }
218 }