3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
6 import java.util.Iterator;
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
15 import org.xml.sax.EntityResolver;
16 import org.xml.sax.InputSource;
17 import org.xml.sax.SAXException;
21 public static Document parse(InputStream is) throws SAXException, IOException
24 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
25 factory.setValidating(false);
26 factory.setExpandEntityReferences(false);
27 DocumentBuilder builder = factory.newDocumentBuilder();
28 builder.setEntityResolver(new EntityResolver() {
29 public InputSource resolveEntity(String publicId, String systemId)
30 throws SAXException, IOException
32 return new InputSource(new StringReader(""));
35 return builder.parse(is);
37 catch (ParserConfigurationException e) {
38 throw new AssertionError("SAX/DOM parser configuration error");
42 public static <E extends Node> Iterable<E> iterate(final NodeList nodes, final Class<E> type)
44 return new Iterable<E>() {
45 public Iterator<E> iterator()
47 return new NodeListIterator<E>(nodes, type);
52 static class NodeListIterator<E extends Node> implements Iterator<E>
59 public NodeListIterator(NodeList nodes, Class<E> nodeType)
67 public boolean hasNext()
83 throw new UnsupportedOperationException();
86 @SuppressWarnings("unchecked")
89 while (i_ < nodes_.getLength()) {
90 Node n = nodes_.item(i_);
92 if (nodeType_.isInstance(n)) {