0bc9ebc9248b0d15245c04268e56ef6f85ddc167
[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)
22                 throws SAXException, IOException
23         {
24                 try {
25                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
26                         factory.setValidating(false);
27                         factory.setExpandEntityReferences(false);
28                         DocumentBuilder builder = factory.newDocumentBuilder();
29                         builder.setEntityResolver(new EntityResolver() {
30                                 public InputSource resolveEntity(String publicId, String systemId)
31                                 throws SAXException, IOException
32                                 {
33                                         return new InputSource(new StringReader(""));
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                                 return new NodeListIterator<E>(nodes, type);
47                         }
48                 };
49         }
50         
51         static class NodeListIterator<E extends Node>
52                 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 }