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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.xml;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.util.Iterator;
\r
18 import javax.xml.parsers.DocumentBuilder;
\r
19 import javax.xml.parsers.DocumentBuilderFactory;
\r
20 import javax.xml.parsers.ParserConfigurationException;
\r
22 import org.w3c.dom.NodeList;
\r
23 import org.xml.sax.SAXException;
\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
40 private org.w3c.dom.Node node;
\r
42 public Node(org.w3c.dom.Node node) {
\r
46 public Node(File file) throws IOException {
\r
47 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
\r
48 DocumentBuilder builder;
\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
59 static class NodeIterator implements Iterator<Node> {
\r
64 public NodeIterator(NodeList list) {
\r
67 length = list.getLength();
\r
71 public boolean hasNext() {
\r
76 public Node next() {
\r
77 return new Node(list.item(i++));
\r
81 public void remove() {
\r
82 throw new UnsupportedOperationException();
\r
88 public Iterator<Node> iterator() {
\r
89 return new NodeIterator(node.getChildNodes());
\r
92 public String name() {
\r
93 return node.getNodeName();
\r
96 public String value() {
\r
97 return node.getNodeValue().trim();
\r
100 public int type() {
\r
101 return node.getNodeType();
\r
104 public Node parent() {
\r
105 return new Node(node.getParentNode());
\r
108 public Node get(String key) {
\r
109 return new Node(node.getAttributes().getNamedItem(key));
\r
112 public String getValue(String key) {
\r
113 return node.getAttributes().getNamedItem(key).getNodeValue().trim();
\r
116 public int childCount() {
\r
117 return node.getChildNodes().getLength();
\r
120 public String text() {
\r
121 return node.getTextContent().trim();
\r
124 public Node getSingleChild(String type) {
\r
125 NodeList nodeList = node.getChildNodes();
\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
131 child = new Node(c);
\r
133 System.out.println("Child of type " + type + " is not unique.");
\r
140 public boolean equals(Object obj) {
\r
141 return this == obj || (obj instanceof Node && node.equals(((Node)obj).node));
\r