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