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