Skip to content

Commit dcc9558

Browse files
committed
scijava-meta: encapsulate javax.xml dep harder
981acc7 attempted to do it, but missed some spots. Hopefully this commit finishes the job.
1 parent aff72a5 commit dcc9558

2 files changed

Lines changed: 15 additions & 72 deletions

File tree

scijava-meta/src/main/java/org/scijava/meta/XML.java

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.io.PrintStream;
3838
import java.io.StringWriter;
3939
import java.net.URL;
40-
import java.util.ArrayList;
4140

4241
import javax.xml.parsers.DocumentBuilder;
4342
import javax.xml.parsers.DocumentBuilderFactory;
@@ -56,7 +55,6 @@
5655

5756
import org.scijava.common3.Classes;
5857
import org.w3c.dom.Document;
59-
import org.w3c.dom.Element;
6058
import org.w3c.dom.Node;
6159
import org.w3c.dom.NodeList;
6260
import org.xml.sax.SAXException;
@@ -101,12 +99,7 @@ public XML(final String s) throws IOException {
10199
}
102100

103101
/** Creates an XML object for an existing document. */
104-
public XML(final Document doc) {
105-
this(null, doc);
106-
}
107-
108-
/** Creates an XML object for an existing document. */
109-
public XML(final String path, final Document doc) {
102+
private XML(final String path, final Document doc) {
110103
this.path = path;
111104
this.doc = doc;
112105

@@ -169,35 +162,13 @@ public String path() {
169162
return path;
170163
}
171164

172-
/** Gets the XML's DOM representation. */
173-
public Document document() {
174-
return doc;
175-
}
176-
177165
/** Obtains the CDATA identified by the given XPath expression. */
178166
public String cdata(final String expression) {
179167
final NodeList nodes = xpath(expression);
180168
if (nodes == null || nodes.getLength() == 0) return null;
181169
return cdata(nodes.item(0));
182170
}
183171

184-
/** Obtains the elements identified by the given XPath expression. */
185-
public ArrayList<Element> elements(final String expression) {
186-
return elements(xpath(expression));
187-
}
188-
189-
/** Obtains the nodes identified by the given XPath expression. */
190-
public NodeList xpath(final String expression) {
191-
final Object result;
192-
try {
193-
result = xpath.evaluate(expression, doc, XPathConstants.NODESET);
194-
}
195-
catch (final XPathExpressionException e) {
196-
return null;
197-
}
198-
return (NodeList) result;
199-
}
200-
201172
// -- Object methods --
202173

203174
@Override
@@ -217,7 +188,7 @@ public String toString() {
217188
// -- Utility methods --
218189

219190
/** Gets the CData beneath the given node. */
220-
public static String cdata(final Node item) {
191+
private static String cdata(final Node item) {
221192
final NodeList children = item.getChildNodes();
222193
if (children.getLength() == 0) return null;
223194
for (int i = 0; i < children.getLength(); i++) {
@@ -228,32 +199,6 @@ public static String cdata(final Node item) {
228199
return null;
229200
}
230201

231-
/** Gets the CData beneath the given element's specified child. */
232-
public static String cdata(final Element el, final String child) {
233-
NodeList children = el.getElementsByTagName(child);
234-
if (children.getLength() == 0) return null;
235-
return cdata(children.item(0));
236-
}
237-
238-
/** Gets the element nodes from the given node list. */
239-
public static ArrayList<Element> elements(final NodeList nodes) {
240-
final ArrayList<Element> elements = new ArrayList<>();
241-
if (nodes != null) {
242-
for (int i = 0; i < nodes.getLength(); i++) {
243-
final Node node = nodes.item(i);
244-
if (node instanceof Element) elements.add((Element) node);
245-
}
246-
}
247-
return elements;
248-
}
249-
250-
/** Gets the given element's specified child elements. */
251-
public static ArrayList<Element> elements(final Element el,
252-
final String child)
253-
{
254-
return elements(el.getElementsByTagName(child));
255-
}
256-
257202
// -- Helper methods --
258203

259204
/** Loads an XML document from the given file. */
@@ -274,7 +219,7 @@ private static Document loadXML(final URL url) throws IOException {
274219
}
275220

276221
/** Loads an XML document from the given input stream. */
277-
protected static Document loadXML(final InputStream in) throws IOException {
222+
private static Document loadXML(final InputStream in) throws IOException {
278223
try {
279224
return createBuilder().parse(in);
280225
}
@@ -284,7 +229,7 @@ protected static Document loadXML(final InputStream in) throws IOException {
284229
}
285230

286231
/** Loads an XML document from the given input stream. */
287-
protected static Document loadXML(final String s) throws IOException {
232+
private static Document loadXML(final String s) throws IOException {
288233
try {
289234
return createBuilder().parse(new ByteArrayInputStream(s.getBytes()));
290235
}
@@ -313,4 +258,15 @@ private static String dumpXML(final Document doc)
313258
return stringWriter.getBuffer().toString();
314259
}
315260

261+
/** Obtains the nodes identified by the given XPath expression. */
262+
private NodeList xpath(final String expression) {
263+
final Object result;
264+
try {
265+
result = xpath.evaluate(expression, doc, XPathConstants.NODESET);
266+
}
267+
catch (final XPathExpressionException e) {
268+
return null;
269+
}
270+
return (NodeList) result;
271+
}
316272
}

scijava-meta/src/test/java/org/scijava/meta/POMTest.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@
3131

3232
import java.io.File;
3333
import java.io.IOException;
34-
import java.util.ArrayList;
3534

3635
import org.junit.jupiter.api.Test;
37-
import org.w3c.dom.Element;
3836

3937
import static org.junit.jupiter.api.Assertions.assertEquals;
4038
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -122,15 +120,4 @@ public void testCdata() throws IOException {
122120
assertEquals("https://github.com/scijava/scijava", //
123121
pom.cdata("//project/url"));
124122
}
125-
126-
@Test
127-
public void testElements() throws IOException {
128-
final POM pom = new POM(new File("pom.xml"));
129-
final ArrayList<Element> developers =
130-
pom.elements("//project/developers/developer");
131-
assertEquals(2, developers.size());
132-
assertEquals("ctrueden", XML.cdata(developers.get(0), "id"));
133-
assertEquals("gselzer", XML.cdata(developers.get(1), "id"));
134-
}
135-
136123
}

0 commit comments

Comments
 (0)