15
15
import com .aspose .wizards .maven .AsposeMavenModuleBuilderHelper ;
16
16
import com .intellij .openapi .progress .ProgressIndicator ;
17
17
import com .intellij .openapi .project .Project ;
18
+ import com .intellij .openapi .vfs .LocalFileSystem ;
19
+ import com .intellij .openapi .vfs .VfsUtil ;
20
+ import com .intellij .openapi .vfs .VirtualFile ;
21
+ import com .sun .xml .internal .messaging .saaj .util .ByteOutputStream ;
18
22
import org .jetbrains .annotations .NotNull ;
19
23
import org .w3c .dom .Document ;
24
+ import org .w3c .dom .Node ;
20
25
import org .w3c .dom .NodeList ;
21
26
import org .xml .sax .SAXException ;
22
27
26
31
import javax .xml .parsers .DocumentBuilder ;
27
32
import javax .xml .parsers .DocumentBuilderFactory ;
28
33
import javax .xml .parsers .ParserConfigurationException ;
34
+ import javax .xml .transform .*;
35
+ import javax .xml .transform .dom .DOMSource ;
36
+ import javax .xml .transform .stream .StreamResult ;
29
37
import javax .xml .transform .stream .StreamSource ;
30
38
import javax .xml .xpath .*;
31
39
import java .io .*;
@@ -94,7 +102,7 @@ public Metadata getProductMavenDependency(String productMavenRepositoryUrl) {
94
102
return data ;
95
103
}
96
104
97
- public String getResolveSupportedJDK (String ProductURL ) {
105
+ public String getResolveSupportedJDK (String ProductURL ) {
98
106
String supportedJDKs [] = {"jdk17" , "jdk16" , "jdk15" , "jdk14" , "jdk18" };
99
107
String classifier = null ;
100
108
for (String jdkCheck : supportedJDKs ) {
@@ -125,6 +133,130 @@ public boolean remoteFileExists(String URLName) {
125
133
}
126
134
}
127
135
136
+ public void addMavenDependenciesInProject (NodeList addTheseDependencies ) {
137
+ String mavenPomXmlfile = AsposeMavenUtil .getPOMXmlFile (projectHandle );
138
+
139
+ VirtualFile vf_mavenPomXmlfilel = LocalFileSystem .getInstance ().findFileByPath (mavenPomXmlfile );
140
+
141
+
142
+ try {
143
+ Document pomDocument = getXmlDocument (mavenPomXmlfile );
144
+
145
+ Node dependenciesNode = pomDocument .getElementsByTagName ("dependencies" ).item (0 );
146
+
147
+
148
+ if (addTheseDependencies != null && addTheseDependencies .getLength () > 0 ) {
149
+ for (int n = 0 ; n < addTheseDependencies .getLength (); n ++) {
150
+ String artifactId = addTheseDependencies .item (n ).getFirstChild ().getNextSibling ().getNextSibling ().getNextSibling ().getFirstChild ().getNodeValue ();
151
+
152
+ XPathFactory xPathfactory = XPathFactory .newInstance ();
153
+ XPath xpath = xPathfactory .newXPath ();
154
+ String expression = "//artifactId[text()='" + artifactId + "']" ;
155
+
156
+ XPathExpression xPathExpr = xpath .compile (expression );
157
+
158
+ Node dependencyAlreadyExist = (Node ) xPathExpr .evaluate (pomDocument , XPathConstants .NODE );
159
+
160
+ if (dependencyAlreadyExist != null ) {
161
+ Node dependencies =pomDocument .getElementsByTagName ("dependencies" ).item (0 );
162
+ dependencies .removeChild (dependencyAlreadyExist .getParentNode ());
163
+ }
164
+
165
+ Node importedNode = pomDocument .importNode (addTheseDependencies .item (n ), true );
166
+ dependenciesNode .appendChild (importedNode );
167
+
168
+
169
+ }
170
+ }
171
+ removeEmptyLinesfromDOM (pomDocument );
172
+ writeXmlDocumentToVirtualFile (vf_mavenPomXmlfilel , pomDocument );
173
+
174
+ } catch (IOException io ) {
175
+ io .printStackTrace ();
176
+ } catch (ParserConfigurationException pce ) {
177
+ pce .printStackTrace ();
178
+ } catch (SAXException sae ) {
179
+ sae .printStackTrace ();
180
+ } catch (TransformerException tfe ) {
181
+ tfe .printStackTrace ();
182
+ } catch (XPathExpressionException e ) {
183
+ e .printStackTrace ();
184
+ } catch (Exception ex ) {
185
+ ex .printStackTrace ();
186
+ }
187
+ }
188
+ public void addMavenRepositoriesInProject (NodeList addTheseRepositories ) {
189
+ String mavenPomXmlfile = AsposeMavenUtil .getPOMXmlFile (projectHandle );
190
+
191
+ VirtualFile vf_mavenPomXmlfilel = LocalFileSystem .getInstance ().findFileByPath (mavenPomXmlfile );
192
+
193
+
194
+ try {
195
+ Document pomDocument = getXmlDocument (mavenPomXmlfile );
196
+
197
+ Node repositoriesNode = pomDocument .getElementsByTagName ("repositories" ).item (0 );
198
+
199
+
200
+ if (addTheseRepositories != null && addTheseRepositories .getLength () > 0 ) {
201
+ for (int n = 0 ; n < addTheseRepositories .getLength (); n ++) {
202
+ String repositoryId = addTheseRepositories .item (n ).getFirstChild ().getNextSibling ().getFirstChild ().getNodeValue ();
203
+
204
+ XPathFactory xPathfactory = XPathFactory .newInstance ();
205
+ XPath xpath = xPathfactory .newXPath ();
206
+ String expression = "//id[text()='" + repositoryId + "']" ;
207
+
208
+ XPathExpression xPathExpr = xpath .compile (expression );
209
+
210
+ Boolean repositoryAlreadyExist = (Boolean ) xPathExpr .evaluate (pomDocument , XPathConstants .BOOLEAN );
211
+
212
+ if (!repositoryAlreadyExist ) {
213
+ Node importedNode = pomDocument .importNode (addTheseRepositories .item (n ), true );
214
+ repositoriesNode .appendChild (importedNode );
215
+ }
216
+
217
+ }
218
+ }
219
+ removeEmptyLinesfromDOM (pomDocument );
220
+ writeXmlDocumentToVirtualFile (vf_mavenPomXmlfilel , pomDocument );
221
+
222
+ } catch (IOException io ) {
223
+ io .printStackTrace ();
224
+ } catch (ParserConfigurationException pce ) {
225
+ pce .printStackTrace ();
226
+ } catch (SAXException sae ) {
227
+ sae .printStackTrace ();
228
+ } catch (TransformerException tfe ) {
229
+ tfe .printStackTrace ();
230
+ } catch (XPathExpressionException e ) {
231
+ e .printStackTrace ();
232
+ } catch (Exception ex ) {
233
+ ex .printStackTrace ();
234
+ }
235
+ }
236
+
237
+ private void removeEmptyLinesfromDOM (Document doc ) throws XPathExpressionException {
238
+ XPath xp = XPathFactory .newInstance ().newXPath ();
239
+ NodeList nl = (NodeList ) xp .evaluate ("//text()[normalize-space(.)='']" , doc , XPathConstants .NODESET );
240
+
241
+ for (int i =0 ; i < nl .getLength (); ++i ) {
242
+ Node node = nl .item (i );
243
+ node .getParentNode ().removeChild (node );
244
+ }
245
+ }
246
+ public static void writeXmlDocumentToVirtualFile (VirtualFile pom , Document pomDocument ) throws TransformerConfigurationException , TransformerException , IOException {
247
+ TransformerFactory transformerFactory = TransformerFactory .newInstance ();
248
+ Transformer transformer = transformerFactory .newTransformer ();
249
+ transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
250
+ transformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , "4" );
251
+ DOMSource source = new DOMSource (pomDocument );
252
+
253
+ ByteOutputStream bytes = new ByteOutputStream ();
254
+
255
+ StreamResult result = new StreamResult (bytes );
256
+ transformer .transform (source , result );
257
+
258
+ VfsUtil .saveText (pom , bytes .toString ());
259
+ }
128
260
129
261
public boolean retrieveAsposeMavenDependencies (@ NotNull ProgressIndicator progressIndicator ) {
130
262
try {
@@ -181,9 +313,9 @@ private Document getXmlDocument(String mavenPomXmlfile) throws ParserConfigurati
181
313
return pomDocument ;
182
314
}
183
315
184
- public String getDependencyVersionFromPOM (String dependencyName , Project project ) {
316
+ public String getDependencyVersionFromPOM (String dependencyName ) {
185
317
try {
186
- String mavenPomXmlfile = AsposeMavenUtil .getPOMXmlFile (project );
318
+ String mavenPomXmlfile = AsposeMavenUtil .getPOMXmlFile (projectHandle );
187
319
188
320
Document pomDocument = getXmlDocument (mavenPomXmlfile );
189
321
@@ -192,7 +324,6 @@ public String getDependencyVersionFromPOM(String dependencyName, Project project
192
324
String expression = "//version[ancestor::dependency/artifactId[text()='" + dependencyName + "']]" ;
193
325
XPathExpression xPathExpr = xpath .compile (expression );
194
326
NodeList nl = (NodeList ) xPathExpr .evaluate (pomDocument , XPathConstants .NODESET );
195
- AsposeConstants .println ("NodeList length: " + nl .getLength ());
196
327
if (nl != null && nl .getLength () > 0 ) {
197
328
return nl .item (0 ).getTextContent ();
198
329
}
@@ -208,10 +339,63 @@ public String getDependencyVersionFromPOM(String dependencyName, Project project
208
339
return null ;
209
340
}
210
341
342
+ public NodeList getDependenciesFromPOM (String mavenPomXmlfile , String excludeGroup ) {
343
+
344
+ try {
345
+
346
+ Document pomDocument = getXmlDocument (mavenPomXmlfile );
347
+
348
+ XPathFactory xPathfactory = XPathFactory .newInstance ();
349
+ XPath xpath = xPathfactory .newXPath ();
350
+ String expression = "//dependency[child::groupId[text()!='" + excludeGroup + "']]" ;
351
+ XPathExpression xPathExpr = xpath .compile (expression );
352
+ NodeList nl = (NodeList ) xPathExpr .evaluate (pomDocument , XPathConstants .NODESET );
353
+ if (nl != null && nl .getLength () > 0 ) {
354
+ return nl ;
355
+ }
356
+ } catch (IOException io ) {
357
+ io .printStackTrace ();
358
+ } catch (ParserConfigurationException pce ) {
359
+ pce .printStackTrace ();
360
+ } catch (SAXException sae ) {
361
+ sae .printStackTrace ();
362
+ } catch (XPathExpressionException e ) {
363
+ e .printStackTrace ();
364
+ }
365
+ return null ;
366
+ }
367
+
368
+ public NodeList getRepositoriesFromPOM (String mavenPomXmlfile , String excludeURL ) {
369
+
370
+ try {
371
+
372
+ Document pomDocument = getXmlDocument (mavenPomXmlfile );
373
+
374
+ XPathFactory xPathfactory = XPathFactory .newInstance ();
375
+ XPath xpath = xPathfactory .newXPath ();
376
+ String expression = "//repository[child::url[not(starts-with(.,'" + excludeURL + "'))]]" ;
377
+ XPathExpression xPathExpr = xpath .compile (expression );
378
+ NodeList nl = (NodeList ) xPathExpr .evaluate (pomDocument , XPathConstants .NODESET );
379
+ if (nl != null && nl .getLength () > 0 ) {
380
+ return nl ;
381
+ }
382
+ } catch (IOException io ) {
383
+ io .printStackTrace ();
384
+ } catch (ParserConfigurationException pce ) {
385
+ pce .printStackTrace ();
386
+ } catch (SAXException sae ) {
387
+ sae .printStackTrace ();
388
+ } catch (XPathExpressionException e ) {
389
+ e .printStackTrace ();
390
+ }
391
+ return null ;
392
+ }
393
+
211
394
public String getAsposeHomePath () {
395
+
212
396
String path = "" ;
213
397
path = System .getProperty ("user.home" );
214
- path = path + File .separator + "aspose" + File .separator ;
398
+ path = path + File .separator + "aspose" + File .separator ;
215
399
return path ;
216
400
}
217
401
@@ -257,18 +441,21 @@ public static void checkAndCreateFolder(String folderPath) {
257
441
folder .mkdirs ();
258
442
}
259
443
}
444
+
260
445
// Singleton instance
261
- private static AsposeMavenProjectManager asposeMavenProjectManager = new AsposeMavenProjectManager ();
446
+ private static AsposeMavenProjectManager asposeMavenProjectManager = new AsposeMavenProjectManager ();
262
447
263
448
public static AsposeMavenProjectManager getInstance () {
264
449
return asposeMavenProjectManager ;
265
450
}
451
+
266
452
public static AsposeMavenProjectManager initialize (Project projectId ) {
267
- asposeMavenProjectManager = new AsposeMavenProjectManager ();
268
- asposeMavenProjectManager .projectHandle =projectId ;
453
+ asposeMavenProjectManager = new AsposeMavenProjectManager ();
454
+ asposeMavenProjectManager .projectHandle = projectId ;
269
455
return asposeMavenProjectManager ;
270
456
}
271
457
272
- private AsposeMavenProjectManager (){}
458
+ private AsposeMavenProjectManager () {
459
+ }
273
460
274
461
}
0 commit comments