Skip to content

Commit 7596f0f

Browse files
committedSep 17, 2015
Merge pull request #8 from AdeelIlyas2014/master
Auto import pom conf. handling added for examples
2 parents b1fb7af + f47d85a commit 7596f0f

File tree

6 files changed

+355
-215
lines changed

6 files changed

+355
-215
lines changed
 

‎Plugins/Aspose_Slides_Java_for_IntelliJ(Maven)/.idea/workspace.xml

Lines changed: 142 additions & 190 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Plugins/Aspose_Slides_Java_for_IntelliJ(Maven)/src/com/aspose/examples/AsposeExampleDialog.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.aspose.utils.*;
1616
import com.intellij.openapi.diagnostic.Logger;
1717
import com.intellij.openapi.ui.DialogWrapper;
18+
import org.w3c.dom.NodeList;
1819

1920
import javax.swing.*;
2021
import java.io.File;
@@ -90,6 +91,15 @@ private boolean createExample() {
9091
String sourceRepositoryExamplePath = comp.getExPath();
9192

9293
String repositorylocation = AsposeSlidesJavaAPI.getInstance().getLocalRepositoryPath();
94+
String repositoryPOM_XML=repositorylocation+File.separator+"Examples"+File.separator+AsposeConstants.MAVEN_POM_XML;
95+
96+
NodeList examplesNoneAsposeDependencies=AsposeMavenProjectManager.getInstance().getDependenciesFromPOM(repositoryPOM_XML,AsposeConstants.ASPOSE_GROUP_ID);
97+
98+
AsposeMavenProjectManager.getInstance().addMavenDependenciesInProject(examplesNoneAsposeDependencies);
99+
100+
NodeList examplesNoneAsposeRepositories=AsposeMavenProjectManager.getInstance().getRepositoriesFromPOM(repositoryPOM_XML, AsposeConstants.ASPOSE_MAVEN_REPOSITORY);
101+
102+
AsposeMavenProjectManager.getInstance().addMavenRepositoriesInProject(examplesNoneAsposeRepositories);
93103

94104
String sourceExamplesUtilsPath= repositorylocation+File.separator+AsposeConstants.REPOSITORY_UTIL;
95105
String destinationExamplesUtilsPath = projectPath + File.separator + sourceExamplesUtilsPath.replace(repositorylocation + File.separator + AsposeConstants.SOURCE_API_EXAMPLES_LOCATION, AsposeConstants.DESTINATION_API_EXAMPLES_LOCATION);

‎Plugins/Aspose_Slides_Java_for_IntelliJ(Maven)/src/com/aspose/examples/AsposeExamplePanel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void read() {
167167
//=========================================================================
168168
private boolean retrieveAPIDependency() {
169169
getComponentSelection().removeAllItems();
170-
String versionNo = AsposeMavenProjectManager.getInstance().getDependencyVersionFromPOM(AsposeConstants.API_MAVEN_DEPENDENCY, AsposeMavenProjectManager.getInstance().getProjectHandle());
170+
String versionNo = AsposeMavenProjectManager.getInstance().getDependencyVersionFromPOM(AsposeConstants.API_MAVEN_DEPENDENCY);
171171
if (versionNo == null) {
172172
getComponentSelection().addItem(AsposeConstants.API_DEPENDENCY_NOT_FOUND);
173173
} else {
@@ -183,7 +183,6 @@ private void retrieveAPIExamples() {
183183
DefaultTreeModel model = (DefaultTreeModel) getExamplesTree().getModel();
184184
model.setRoot(top);
185185
model.reload(top);
186-
System.out.println("ITEM: " + item);
187186
if (item != null && !item.equals(AsposeConstants.API_DEPENDENCY_NOT_FOUND)) {
188187

189188
AsposeExampleCallback callback = new AsposeExampleCallback(this, top);

‎Plugins/Aspose_Slides_Java_for_IntelliJ(Maven)/src/com/aspose/utils/AsposeConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
public class AsposeConstants {
2222

23+
public static final String ASPOSE_MAVEN_REPOSITORY = "http://maven.aspose.com";
2324
public static final String API_NAME = "Aspose.Slides";
25+
public static final String ASPOSE_GROUP_ID = "com.aspose";
2426
public static final String API_MAVEN_DEPENDENCY = "aspose-slides";
2527
public static final String API_EXAMPLES_PACKAGE ="com"+File.separator+API_MAVEN_DEPENDENCY.replace("-",File.separator)+File.separator+"examples";
2628
public static final String SOURCE_API_EXAMPLES_LOCATION ="Examples"+File.separator+"src"+File.separator+"main"+File.separator+"java"+File.separator+API_EXAMPLES_PACKAGE;
@@ -29,6 +31,7 @@ public class AsposeConstants {
2931
public static final String DESTINATION_API_EXAMPLES_RESOURCES_LOCATION="src"+File.separator+"main"+File.separator+"resources"+File.separator+ API_EXAMPLES_PACKAGE;
3032

3133
public static final String REPOSITORY_UTIL=SOURCE_API_EXAMPLES_LOCATION+File.separator+"Utils.java";
34+
3235
public static final String API_DEPENDENCY_NOT_FOUND = "Dependency not found!";
3336

3437
public static final String MAVEN_POM_XML = "pom.xml";

‎Plugins/Aspose_Slides_Java_for_IntelliJ(Maven)/src/com/aspose/utils/AsposeMavenProjectManager.java

Lines changed: 196 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
import com.aspose.wizards.maven.AsposeMavenModuleBuilderHelper;
1616
import com.intellij.openapi.progress.ProgressIndicator;
1717
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;
1822
import org.jetbrains.annotations.NotNull;
1923
import org.w3c.dom.Document;
24+
import org.w3c.dom.Node;
2025
import org.w3c.dom.NodeList;
2126
import org.xml.sax.SAXException;
2227

@@ -26,6 +31,9 @@
2631
import javax.xml.parsers.DocumentBuilder;
2732
import javax.xml.parsers.DocumentBuilderFactory;
2833
import javax.xml.parsers.ParserConfigurationException;
34+
import javax.xml.transform.*;
35+
import javax.xml.transform.dom.DOMSource;
36+
import javax.xml.transform.stream.StreamResult;
2937
import javax.xml.transform.stream.StreamSource;
3038
import javax.xml.xpath.*;
3139
import java.io.*;
@@ -94,7 +102,7 @@ public Metadata getProductMavenDependency(String productMavenRepositoryUrl) {
94102
return data;
95103
}
96104

97-
public String getResolveSupportedJDK(String ProductURL) {
105+
public String getResolveSupportedJDK(String ProductURL) {
98106
String supportedJDKs[] = {"jdk17", "jdk16", "jdk15", "jdk14", "jdk18"};
99107
String classifier = null;
100108
for (String jdkCheck : supportedJDKs) {
@@ -125,6 +133,130 @@ public boolean remoteFileExists(String URLName) {
125133
}
126134
}
127135

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+
}
128260

129261
public boolean retrieveAsposeMavenDependencies(@NotNull ProgressIndicator progressIndicator) {
130262
try {
@@ -181,9 +313,9 @@ private Document getXmlDocument(String mavenPomXmlfile) throws ParserConfigurati
181313
return pomDocument;
182314
}
183315

184-
public String getDependencyVersionFromPOM(String dependencyName, Project project) {
316+
public String getDependencyVersionFromPOM(String dependencyName) {
185317
try {
186-
String mavenPomXmlfile = AsposeMavenUtil.getPOMXmlFile(project);
318+
String mavenPomXmlfile = AsposeMavenUtil.getPOMXmlFile(projectHandle);
187319

188320
Document pomDocument = getXmlDocument(mavenPomXmlfile);
189321

@@ -192,7 +324,6 @@ public String getDependencyVersionFromPOM(String dependencyName, Project project
192324
String expression = "//version[ancestor::dependency/artifactId[text()='" + dependencyName + "']]";
193325
XPathExpression xPathExpr = xpath.compile(expression);
194326
NodeList nl = (NodeList) xPathExpr.evaluate(pomDocument, XPathConstants.NODESET);
195-
AsposeConstants.println("NodeList length: " + nl.getLength());
196327
if (nl != null && nl.getLength() > 0) {
197328
return nl.item(0).getTextContent();
198329
}
@@ -208,10 +339,63 @@ public String getDependencyVersionFromPOM(String dependencyName, Project project
208339
return null;
209340
}
210341

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+
211394
public String getAsposeHomePath() {
395+
212396
String path = "";
213397
path = System.getProperty("user.home");
214-
path = path + File.separator+"aspose"+File.separator;
398+
path = path + File.separator + "aspose" + File.separator;
215399
return path;
216400
}
217401

@@ -257,18 +441,21 @@ public static void checkAndCreateFolder(String folderPath) {
257441
folder.mkdirs();
258442
}
259443
}
444+
260445
// Singleton instance
261-
private static AsposeMavenProjectManager asposeMavenProjectManager= new AsposeMavenProjectManager();
446+
private static AsposeMavenProjectManager asposeMavenProjectManager = new AsposeMavenProjectManager();
262447

263448
public static AsposeMavenProjectManager getInstance() {
264449
return asposeMavenProjectManager;
265450
}
451+
266452
public static AsposeMavenProjectManager initialize(Project projectId) {
267-
asposeMavenProjectManager= new AsposeMavenProjectManager();
268-
asposeMavenProjectManager.projectHandle =projectId;
453+
asposeMavenProjectManager = new AsposeMavenProjectManager();
454+
asposeMavenProjectManager.projectHandle = projectId;
269455
return asposeMavenProjectManager;
270456
}
271457

272-
private AsposeMavenProjectManager(){}
458+
private AsposeMavenProjectManager() {
459+
}
273460

274461
}

‎Plugins/Aspose_Slides_Java_for_IntelliJ(Maven)/src/com/aspose/wizards/maven/AsposeMavenModuleBuilderHelper.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.w3c.dom.Document;
3434
import org.w3c.dom.Element;
3535
import org.w3c.dom.Node;
36+
import org.w3c.dom.NodeList;
3637
import org.xml.sax.SAXException;
3738

3839
import javax.xml.parsers.DocumentBuilder;
@@ -106,19 +107,7 @@ public void run() {
106107
});
107108
}
108109

109-
private void writeXmlDocumentToVirtualFile(VirtualFile pom, Document pomDocument) throws TransformerConfigurationException, TransformerException, IOException {
110-
TransformerFactory transformerFactory = TransformerFactory.newInstance();
111-
Transformer transformer = transformerFactory.newTransformer();
112-
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
113-
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
114-
DOMSource source = new DOMSource(pomDocument);
115110

116-
ByteOutputStream bytes = new ByteOutputStream();
117-
118-
StreamResult result = new StreamResult(bytes);
119-
transformer.transform(source, result);
120-
VfsUtil.saveText(pom, bytes.toString());
121-
}
122111

123112
private Document getXmlDocument(String xmlfile) throws ParserConfigurationException, SAXException, IOException {
124113
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
@@ -165,7 +154,7 @@ private void addMavenConfiguration(final VirtualFile miscxml, String mavenMiscXm
165154
list.appendChild(listOption);
166155

167156
// Write the content into misc xml file
168-
writeXmlDocumentToVirtualFile(miscxml, pomDocument);
157+
AsposeMavenProjectManager.writeXmlDocumentToVirtualFile(miscxml, pomDocument);
169158
} catch (IOException io) {
170159
io.printStackTrace();
171160
} catch (ParserConfigurationException pce) {
@@ -222,7 +211,7 @@ private void updateProjectPom(final Project project, final VirtualFile pom) {
222211

223212
// Write the content into maven pom xml file
224213

225-
writeXmlDocumentToVirtualFile(pom, pomDocument);
214+
AsposeMavenProjectManager.writeXmlDocumentToVirtualFile(pom, pomDocument);
226215

227216

228217
} catch (IOException io) {

0 commit comments

Comments
 (0)