Skip to content

Commit 0d07a8c

Browse files
committed
Merge branch 'xml'
Improve the XML and POM utility classes.
2 parents f1d2b4d + 25e9524 commit 0d07a8c

File tree

3 files changed

+124
-15
lines changed

3 files changed

+124
-15
lines changed

src/main/java/org/scijava/util/POM.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,26 @@ public POM(final String s) throws ParserConfigurationException, SAXException,
8484

8585
// -- POM methods --
8686

87+
/** Gets the POM's parent groupId. */
88+
public String getParentGroupId() {
89+
return cdata("//project/parent/groupId");
90+
}
91+
92+
/** Gets the POM's parent artifactId. */
93+
public String getParentArtifactId() {
94+
return cdata("//project/parent/artifactId");
95+
}
96+
97+
/** Gets the POM's parent artifactId. */
98+
public String getParentVersion() {
99+
return cdata("//project/parent/version");
100+
}
101+
87102
/** Gets the POM's groupId. */
88103
public String getGroupId() {
89104
final String groupId = cdata("//project/groupId");
90105
if (groupId != null) return groupId;
91-
return cdata("//project/parent/groupId");
106+
return getParentGroupId();
92107
}
93108

94109
/** Gets the POM's artifactId. */
@@ -191,7 +206,7 @@ public String getVersion() {
191206
synchronized (this) {
192207
if (version == null) {
193208
version = cdata("//project/version");
194-
if (version == null) version = cdata("//project/parent/version");
209+
if (version == null) version = getParentVersion();
195210
}
196211
}
197212
}

src/main/java/org/scijava/util/XML.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.PrintStream;
4040
import java.io.StringWriter;
4141
import java.net.URL;
42+
import java.util.ArrayList;
4243

4344
import javax.xml.parsers.DocumentBuilder;
4445
import javax.xml.parsers.DocumentBuilderFactory;
@@ -56,6 +57,7 @@
5657
import javax.xml.xpath.XPathFactory;
5758

5859
import org.w3c.dom.Document;
60+
import org.w3c.dom.Element;
5961
import org.w3c.dom.Node;
6062
import org.w3c.dom.NodeList;
6163
import org.xml.sax.SAXException;
@@ -181,7 +183,20 @@ public Document getDocument() {
181183
public String cdata(final String expression) {
182184
final NodeList nodes = xpath(expression);
183185
if (nodes == null || nodes.getLength() == 0) return null;
184-
return getCData(nodes.item(0));
186+
return cdata(nodes.item(0));
187+
}
188+
189+
/** Obtains the elements identified by the given XPath expression. */
190+
public ArrayList<Element> elements(final String expression) {
191+
final NodeList nodes = xpath(expression);
192+
final ArrayList<Element> elements = new ArrayList<Element>();
193+
if (nodes != null) {
194+
for (int i=0; i<nodes.getLength(); i++) {
195+
final Node node = nodes.item(i);
196+
if (node instanceof Element) elements.add((Element) node);
197+
}
198+
}
199+
return elements;
185200
}
186201

187202
/** Obtains the nodes identified by the given XPath expression. */
@@ -212,6 +227,27 @@ public String toString() {
212227
}
213228
}
214229

230+
// -- Utility methods --
231+
232+
/** Gets the CData beneath the given node. */
233+
public static String cdata(final Node item) {
234+
final NodeList children = item.getChildNodes();
235+
if (children == null || children.getLength() == 0) return null;
236+
for (int i = 0; i < children.getLength(); i++) {
237+
final Node child = children.item(i);
238+
if (child.getNodeType() != Node.TEXT_NODE) continue;
239+
return child.getNodeValue();
240+
}
241+
return null;
242+
}
243+
244+
/** Gets the CData beneath the given element's specified child. */
245+
public static String cdata(final Element el, final String child) {
246+
NodeList children = el.getElementsByTagName(child);
247+
if (children == null || children.getLength() == 0) return null;
248+
return cdata(children.item(0));
249+
}
250+
215251
// -- Helper methods --
216252

217253
/** Loads an XML document from the given file. */
@@ -252,18 +288,6 @@ private static DocumentBuilder createBuilder()
252288
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
253289
}
254290

255-
/** Gets the CData beneath the given node. */
256-
private static String getCData(final Node item) {
257-
final NodeList children = item.getChildNodes();
258-
if (children == null || children.getLength() == 0) return null;
259-
for (int i = 0; i < children.getLength(); i++) {
260-
final Node child = children.item(i);
261-
if (child.getNodeType() != Node.TEXT_NODE) continue;
262-
return child.getNodeValue();
263-
}
264-
return null;
265-
}
266-
267291
/** Converts the given DOM to a string. */
268292
private static String dumpXML(final Document doc)
269293
throws TransformerException

src/test/java/org/scijava/util/POMTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,20 @@
3131

3232
package org.scijava.util;
3333

34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
36+
import static org.junit.Assert.assertNull;
3437
import static org.junit.Assert.assertTrue;
3538

39+
import java.io.File;
40+
import java.io.IOException;
41+
import java.util.ArrayList;
42+
43+
import javax.xml.parsers.ParserConfigurationException;
44+
3645
import org.junit.Test;
46+
import org.w3c.dom.Element;
47+
import org.xml.sax.SAXException;
3748

3849
/**
3950
* Tests methods of {@link POM}.
@@ -73,4 +84,63 @@ public void testCompareVersions() {
7384
assertTrue(POM.compareVersions("2.0.0", "2.0.0-beta-1") > 0);
7485
}
7586

87+
@Test
88+
public void testAccessors() throws ParserConfigurationException,
89+
SAXException, IOException
90+
{
91+
final POM pom = new POM(new File("pom.xml"));
92+
assertEquals("org.scijava", pom.getParentGroupId());
93+
assertEquals("pom-scijava", pom.getParentArtifactId());
94+
assertNotNull(pom.getParentVersion());
95+
assertEquals("org.scijava", pom.getGroupId());
96+
assertEquals("scijava-common", pom.getArtifactId());
97+
assertNotNull(pom.getVersion());
98+
assertEquals("Jenkins", pom.getCIManagementSystem());
99+
final String ciManagementURL = pom.getCIManagementURL();
100+
assertEquals("http://jenkins.imagej.net/job/SciJava-common/",
101+
ciManagementURL);
102+
assertEquals("GitHub Issues", pom.getIssueManagementSystem());
103+
final String issueManagementURL = pom.getIssueManagementURL();
104+
assertEquals("https://github.com/scijava/scijava-common/issues",
105+
issueManagementURL);
106+
assertNull(pom.getOrganizationName());
107+
assertNull(pom.getOrganizationURL());
108+
assertTrue(pom.getPath().endsWith("pom.xml"));
109+
assertTrue(pom.getProjectDescription().startsWith(
110+
"SciJava Common is a shared library for SciJava software."));
111+
assertEquals("2009", pom.getProjectInceptionYear());
112+
assertEquals("SciJava Common", pom.getProjectName());
113+
assertEquals("http://scijava.org/", pom.getProjectURL());
114+
final String scmConnection = pom.getSCMConnection();
115+
assertEquals("scm:git:git://github.com/scijava/scijava-common",
116+
scmConnection);
117+
final String scmDeveloperConnection = pom.getSCMDeveloperConnection();
118+
assertEquals("scm:git:git@github.com:scijava/scijava-common",
119+
scmDeveloperConnection);
120+
assertNotNull(pom.getSCMTag()); // won't be HEAD for release tags
121+
assertEquals("https://github.com/scijava/scijava-common", pom.getSCMURL());
122+
}
123+
124+
@Test
125+
public void testCdata() throws ParserConfigurationException,
126+
SAXException, IOException
127+
{
128+
final POM pom = new POM(new File("pom.xml"));
129+
assertEquals("repo", pom.cdata("//project/licenses/license/distribution"));
130+
assertEquals("http://scijava.org/", pom.cdata("//project/url"));
131+
}
132+
133+
@Test
134+
public void testElements() throws ParserConfigurationException,
135+
SAXException, IOException
136+
{
137+
final POM pom = new POM(new File("pom.xml"));
138+
final ArrayList<Element> developers =
139+
pom.elements("//project/developers/developer");
140+
assertEquals(3, developers.size());
141+
assertEquals("ctrueden", XML.cdata(developers.get(0), "id"));
142+
assertEquals("dscho", XML.cdata(developers.get(1), "id"));
143+
assertEquals("hinerm", XML.cdata(developers.get(2), "id"));
144+
}
145+
76146
}

0 commit comments

Comments
 (0)