]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/xml/Node.java
Merge remote-tracking branch 'origin/svn'
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / xml / Node.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.xml;\r
13 \r
14 import java.io.File;\r
15 import java.io.IOException;\r
16 import java.util.Iterator;\r
17 \r
18 import javax.xml.parsers.DocumentBuilder;\r
19 import javax.xml.parsers.DocumentBuilderFactory;\r
20 import javax.xml.parsers.ParserConfigurationException;\r
21 \r
22 import org.w3c.dom.NodeList;\r
23 import org.xml.sax.SAXException;\r
24 \r
25 \r
26 public class Node implements Iterable<Node> {\r
27     public static final short ELEMENT_NODE              = org.w3c.dom.Node.ELEMENT_NODE;\r
28     public static final short ATTRIBUTE_NODE            = org.w3c.dom.Node.ATTRIBUTE_NODE;\r
29     public static final short TEXT_NODE                 = org.w3c.dom.Node.TEXT_NODE;\r
30     public static final short CDATA_SECTION_NODE        = org.w3c.dom.Node.CDATA_SECTION_NODE;\r
31     public static final short ENTITY_REFERENCE_NODE     = org.w3c.dom.Node.ENTITY_REFERENCE_NODE;\r
32     public static final short ENTITY_NODE               = org.w3c.dom.Node.ENTITY_NODE;\r
33     public static final short PROCESSING_INSTRUCTION_NODE = org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;\r
34     public static final short COMMENT_NODE              = org.w3c.dom.Node.COMMENT_NODE;\r
35     public static final short DOCUMENT_NODE             = org.w3c.dom.Node.DOCUMENT_NODE;\r
36     public static final short DOCUMENT_TYPE_NODE        = org.w3c.dom.Node.DOCUMENT_TYPE_NODE;\r
37     public static final short DOCUMENT_FRAGMENT_NODE    = org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE;\r
38     public static final short NOTATION_NODE             = org.w3c.dom.Node.NOTATION_NODE;\r
39         \r
40         private org.w3c.dom.Node node;\r
41 \r
42         public Node(org.w3c.dom.Node node) {\r
43                 this.node = node;\r
44         }\r
45         \r
46         public Node(File file) throws IOException {\r
47                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
48                 DocumentBuilder builder;\r
49                 try {\r
50                         builder = factory.newDocumentBuilder();         \r
51                         node = builder.parse(file);\r
52                 } catch (ParserConfigurationException e) {\r
53                         throw new IOException(e);\r
54                 } catch (SAXException e) {\r
55                         throw new IOException(e);\r
56                 }\r
57         }\r
58 \r
59         static class NodeIterator implements Iterator<Node> {\r
60                 NodeList list;          \r
61                 int      i;\r
62                 int      length;\r
63 \r
64                 public NodeIterator(NodeList list) {\r
65                         this.list = list;\r
66                         i = 0;\r
67                         length = list.getLength();\r
68                 }\r
69 \r
70                 @Override\r
71                 public boolean hasNext() {\r
72                         return i<length;\r
73                 }\r
74 \r
75                 @Override\r
76                 public Node next() {\r
77                         return new Node(list.item(i++));\r
78                 }\r
79 \r
80                 @Override\r
81                 public void remove() {\r
82                         throw new UnsupportedOperationException();\r
83                 }               \r
84                 \r
85         }\r
86         \r
87         @Override\r
88         public Iterator<Node> iterator() {\r
89                 return new NodeIterator(node.getChildNodes());\r
90         }       \r
91         \r
92         public String name() {\r
93                 return node.getNodeName();\r
94         }\r
95         \r
96         public String value() {\r
97                 return node.getNodeValue().trim();              \r
98         }\r
99         \r
100         public int type() {\r
101                 return node.getNodeType();\r
102         }\r
103         \r
104         public Node parent() {\r
105                 return new Node(node.getParentNode());\r
106         }\r
107         \r
108         public Node get(String key) {\r
109                 return new Node(node.getAttributes().getNamedItem(key));\r
110         }\r
111         \r
112         public String getValue(String key) {\r
113                 return node.getAttributes().getNamedItem(key).getNodeValue().trim();            \r
114         }\r
115         \r
116         public int childCount() {\r
117                 return node.getChildNodes().getLength();\r
118         }\r
119         \r
120         public String text() {\r
121                 return node.getTextContent().trim();\r
122         }\r
123         \r
124         public Node getSingleChild(String type) {\r
125                 NodeList nodeList = node.getChildNodes();\r
126                 Node child = null;\r
127                 for(int i=0;i<nodeList.getLength();++i) {\r
128                         org.w3c.dom.Node c = nodeList.item(i);\r
129                         if(c.getNodeName().equals(type)) {\r
130                                 if(child == null)\r
131                                         child = new Node(c);\r
132                                 else\r
133                                         System.out.println("Child of type " + type + " is not unique.");\r
134                         }\r
135                 }\r
136                 return child;\r
137         }\r
138         \r
139         @Override\r
140         public boolean equals(Object obj) {\r
141                 return this == obj || (obj instanceof Node && node.equals(((Node)obj).node));\r
142         }\r
143         \r
144 }\r
145 \r