138 lines
4.1 KiB
Java
138 lines
4.1 KiB
Java
package util;
|
|
|
|
// Java imports
|
|
import java.io.*;
|
|
import java.util.*;
|
|
import javax.xml.parsers.*;
|
|
import org.w3c.dom.*;
|
|
|
|
// Utility methods for managing XML documents
|
|
public class XML {
|
|
|
|
// This class cannot be instantiated
|
|
private XML() { }
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// Constants //
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Parsing instance
|
|
private static final DocumentBuilder XML_PARSER;
|
|
|
|
// Static initializer
|
|
static {
|
|
DocumentBuilder parser = null;
|
|
try { parser =
|
|
DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
|
} catch (Exception e) { }
|
|
XML_PARSER = parser;
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// Static Methods //
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Retrieve an attribute value from an XML element
|
|
public static String attribute(Element element, String attribute) {
|
|
return element.hasAttribute(attribute) ?
|
|
element.getAttribute(attribute) : null;
|
|
}
|
|
|
|
// Retrieve the child elements of an XML node
|
|
public static Element[] children(Node node) {
|
|
var nodes = node.getChildNodes();
|
|
int count = nodes.getLength();
|
|
var ret = new ArrayList<Element>();
|
|
for (int x = 0; x < count; x++) {
|
|
node = nodes.item(x);
|
|
if (node.getNodeType() == Node.ELEMENT_NODE)
|
|
ret.add((Element) node);
|
|
}
|
|
return ret.toArray(new Element[ret.size()]);
|
|
}
|
|
|
|
// Retrieve the first node matching the given hierarchy
|
|
public static Element element(Node node, String hierarchy) {
|
|
|
|
// Error checking
|
|
if (node == null || hierarchy == null)
|
|
return null;
|
|
|
|
// Propagate down the hierarchy
|
|
outer: for (String name : hierarchy.split("\\.")) {
|
|
var nodes = node.getChildNodes();
|
|
int count = nodes.getLength();
|
|
for (int x = 0; x < count; x++) {
|
|
node = nodes.item(x);
|
|
if (
|
|
node.getNodeType() == Node.ELEMENT_NODE &&
|
|
node.getNodeName().equals(name)
|
|
) continue outer;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
return (Element) node;
|
|
}
|
|
|
|
// Retrieve all nodes matching the endpoint of a given hierarchy
|
|
public static Element[] elements(Node node, String hierarchy) {
|
|
|
|
// Find the first matching node
|
|
node = element(node, hierarchy);
|
|
if (node == null)
|
|
return new Element[0];
|
|
|
|
// Working variables
|
|
var names = hierarchy.split("\\.");
|
|
String name = names[names.length - 1];
|
|
var ret = new ArrayList<Element>();
|
|
|
|
// Include all siblings with the same tag name
|
|
ret.add((Element) node);
|
|
for (;;) {
|
|
node = node.getNextSibling();
|
|
if (node == null)
|
|
break;
|
|
if (
|
|
node.getNodeType() == Node.ELEMENT_NODE &&
|
|
node.getNodeName().equals(name)
|
|
) ret.add((Element) node);
|
|
}
|
|
|
|
return ret.toArray(new Element[ret.size()]);
|
|
}
|
|
|
|
// Read and parse an XML file
|
|
public static Node read(String filename) {
|
|
try { return XML_PARSER.parse(
|
|
new ByteArrayInputStream(Util.fileRead(filename)));
|
|
} catch (Exception e) { return null; }
|
|
}
|
|
|
|
// Retrieve the text content of an element
|
|
public static String text(Node node) {
|
|
|
|
// Error checking
|
|
if (node == null)
|
|
return null;
|
|
|
|
// Find the child node called #text
|
|
var nodes = node.getChildNodes();
|
|
int count = nodes.getLength();
|
|
for (int x = 0; x < count; x++) {
|
|
node = nodes.item(x);
|
|
if (node.getNodeType() != Node.TEXT_NODE)
|
|
continue;
|
|
String text = node.getNodeValue();
|
|
return text == null ? "" : text.trim();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
}
|