--- /dev/null
+package de.j32.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.util.Iterator;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+public class XmlUtil
+{
+ public static Document parse(InputStream is) throws SAXException, IOException
+ {
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setValidating(false);
+ factory.setExpandEntityReferences(false);
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setEntityResolver(new EntityResolver() {
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ return new InputSource(new StringReader(""));
+ }
+ });
+ return builder.parse(is);
+ }
+ catch (ParserConfigurationException e) {
+ throw new AssertionError("SAX/DOM parser configuration error");
+ }
+ }
+
+ public static <E extends Node> Iterable<E> iterate(final NodeList nodes, final Class<E> type)
+ {
+ return new Iterable<E>() {
+ public Iterator<E> iterator()
+ {
+ return new NodeListIterator<E>(nodes, type);
+ }
+ };
+ }
+
+ static class NodeListIterator<E extends Node> implements Iterator<E>
+ {
+ Class<E> nodeType_;
+ NodeList nodes_;
+ int i_ = 0;
+ E next_;
+
+ public NodeListIterator(NodeList nodes, Class<E> nodeType)
+ {
+ nodes_ = nodes;
+ nodeType_ = nodeType;
+ advance();
+ }
+
+ @Override
+ public boolean hasNext()
+ {
+ return next_ != null;
+ }
+
+ @Override
+ public E next()
+ {
+ E rv = next_;
+ advance();
+ return rv;
+ }
+
+ @Override
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @SuppressWarnings("unchecked")
+ void advance()
+ {
+ while (i_ < nodes_.getLength()) {
+ Node n = nodes_.item(i_);
+ ++i_;
+ if (nodeType_.isInstance(n)) {
+ next_ = (E) n;
+ return;
+ }
+ }
+ next_ = null;
+ }
+ }
+}