3737import java .io .PrintStream ;
3838import java .io .StringWriter ;
3939import java .net .URL ;
40- import java .util .ArrayList ;
4140
4241import javax .xml .parsers .DocumentBuilder ;
4342import javax .xml .parsers .DocumentBuilderFactory ;
5655
5756import org .scijava .common3 .Classes ;
5857import org .w3c .dom .Document ;
59- import org .w3c .dom .Element ;
6058import org .w3c .dom .Node ;
6159import org .w3c .dom .NodeList ;
6260import 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}
0 commit comments