Implement spring Conduit bean registry and lots of 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.Element;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16 import org.xml.sax.EntityResolver;
17 import org.xml.sax.InputSource;
18 import org.xml.sax.SAXException;
19
20 public class XmlUtil
21 {
22         public static Document parse(InputStream is)
23                 throws SAXException, IOException
24         {
25                 try {
26                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
27                         factory.setValidating(false);
28                         factory.setExpandEntityReferences(false);
29                         DocumentBuilder builder = factory.newDocumentBuilder();
30                         builder.setEntityResolver(new EntityResolver() {
31                                 public InputSource resolveEntity(String publicId, String systemId)
32                                 throws SAXException, IOException
33                                 {
34                                         return new InputSource(new StringReader(""));
35                                 }});            
36                         return builder.parse(is);
37                 }
38                 catch (ParserConfigurationException e) {
39                         throw new AssertionError("SAX/DOM parser configuration error");
40                 }
41         }
42         
43         public static Iterable<Element> iterateElements(final NodeList nodes)
44         {
45                 return new Iterable<Element>() {
46                         public Iterator<Element> iterator() {
47                                 return new NodeListIterator<Element>(nodes, Element.class);
48                         }
49                 };
50         }
51
52         static class NodeListIterator<E extends Node>
53                 implements Iterator<E>
54         {
55                 Class<?> nodeType_;
56                 NodeList nodes_;
57                 int i_ = 0;
58                 E next_;
59                 
60                 public NodeListIterator(NodeList nodes, Class<?> nodeType)
61                 {
62                         nodes_ = nodes;
63                         nodeType_ = nodeType;
64                         advance();
65                 }
66                 
67                 @Override
68                 public boolean hasNext()
69                 {
70                         return next_ != null;
71                 }
72
73                 @Override
74                 public E next()
75                 {
76                         E rv = next_;
77                         advance();
78                         return rv;
79                 }
80
81                 @Override
82                 public void remove()
83                 {
84                         throw new UnsupportedOperationException();
85                 }
86                 
87                 @SuppressWarnings("unchecked")
88                 void advance()
89                 {
90                         while (i_ < nodes_.getLength()) {
91                                 Node n = nodes_.item(i_);
92                                 ++ i_;
93                                 if (nodeType_.isInstance(n)) {
94                                         next_ = (E) n;
95                                         return;
96                                 }
97                         }
98                         next_ = null;
99                 }
100         }       
101 }