Code formating and whitespace cleanup
[jpim.git] / src / de / j32 / util / XmlUtil.java
1 package de.j32.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
6 import java.util.Iterator;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
11
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;
18
19 public class XmlUtil
20 {
21     public static Document parse(InputStream is) throws SAXException, IOException
22     {
23         try {
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
31                 {
32                     return new InputSource(new StringReader(""));
33                 }
34             });
35             return builder.parse(is);
36         }
37         catch (ParserConfigurationException e) {
38             throw new AssertionError("SAX/DOM parser configuration error");
39         }
40     }
41
42     public static <E extends Node> Iterable<E> iterate(final NodeList nodes, final Class<E> type)
43     {
44         return new Iterable<E>() {
45             public Iterator<E> iterator()
46             {
47                 return new NodeListIterator<E>(nodes, type);
48             }
49         };
50     }
51
52     static class NodeListIterator<E extends Node> implements Iterator<E>
53     {
54         Class<E> nodeType_;
55         NodeList nodes_;
56         int i_ = 0;
57         E next_;
58
59         public NodeListIterator(NodeList nodes, Class<E> nodeType)
60         {
61             nodes_ = nodes;
62             nodeType_ = nodeType;
63             advance();
64         }
65
66         @Override
67         public boolean hasNext()
68         {
69             return next_ != null;
70         }
71
72         @Override
73         public E next()
74         {
75             E rv = next_;
76             advance();
77             return rv;
78         }
79
80         @Override
81         public void remove()
82         {
83             throw new UnsupportedOperationException();
84         }
85
86         @SuppressWarnings("unchecked")
87         void advance()
88         {
89             while (i_ < nodes_.getLength()) {
90                 Node n = nodes_.item(i_);
91                 ++i_;
92                 if (nodeType_.isInstance(n)) {
93                     next_ = (E) n;
94                     return;
95                 }
96             }
97             next_ = null;
98         }
99     }
100 }