import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
-import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
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 Iterable<Element> iterateElements(final NodeList nodes)
- {
- return new Iterable<Element>() {
- public Iterator<Element> iterator() {
- return new NodeListIterator<Element>(nodes, Element.class);
- }
- };
- }
+ 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");
+ }
+ }
- static class NodeListIterator<E extends Node>
- implements Iterator<E>
- {
- Class<?> nodeType_;
- NodeList nodes_;
- int i_ = 0;
- E next_;
-
- public NodeListIterator(NodeList nodes, Class<?> nodeType)
- {
- nodes_ = nodes;
- nodeType_ = nodeType;
- advance();
- }
-
- @Override
- public boolean hasNext()
- {
- return next_ != null;
- }
+ 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);
+ }
+ };
+ }
- @Override
- public E next()
- {
- E rv = next_;
- advance();
- return rv;
- }
+ static class NodeListIterator<E extends Node> implements Iterator<E>
+ {
+ Class<E> nodeType_;
+ NodeList nodes_;
+ int i_ = 0;
+ E next_;
- @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;
- }
- }
+ 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;
+ }
+ }
}