-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.utils.xml;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.util.Iterator;\r
-\r
-import javax.xml.parsers.DocumentBuilder;\r
-import javax.xml.parsers.DocumentBuilderFactory;\r
-import javax.xml.parsers.ParserConfigurationException;\r
-\r
-import org.w3c.dom.NodeList;\r
-import org.xml.sax.SAXException;\r
-\r
-\r
-public class Node implements Iterable<Node> {\r
- public static final short ELEMENT_NODE = org.w3c.dom.Node.ELEMENT_NODE;\r
- public static final short ATTRIBUTE_NODE = org.w3c.dom.Node.ATTRIBUTE_NODE;\r
- public static final short TEXT_NODE = org.w3c.dom.Node.TEXT_NODE;\r
- public static final short CDATA_SECTION_NODE = org.w3c.dom.Node.CDATA_SECTION_NODE;\r
- public static final short ENTITY_REFERENCE_NODE = org.w3c.dom.Node.ENTITY_REFERENCE_NODE;\r
- public static final short ENTITY_NODE = org.w3c.dom.Node.ENTITY_NODE;\r
- public static final short PROCESSING_INSTRUCTION_NODE = org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;\r
- public static final short COMMENT_NODE = org.w3c.dom.Node.COMMENT_NODE;\r
- public static final short DOCUMENT_NODE = org.w3c.dom.Node.DOCUMENT_NODE;\r
- public static final short DOCUMENT_TYPE_NODE = org.w3c.dom.Node.DOCUMENT_TYPE_NODE;\r
- public static final short DOCUMENT_FRAGMENT_NODE = org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE;\r
- public static final short NOTATION_NODE = org.w3c.dom.Node.NOTATION_NODE;\r
- \r
- private org.w3c.dom.Node node;\r
-\r
- public Node(org.w3c.dom.Node node) {\r
- this.node = node;\r
- }\r
- \r
- public Node(File file) throws IOException {\r
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
- DocumentBuilder builder;\r
- try {\r
- builder = factory.newDocumentBuilder(); \r
- node = builder.parse(file);\r
- } catch (ParserConfigurationException e) {\r
- throw new IOException(e);\r
- } catch (SAXException e) {\r
- throw new IOException(e);\r
- }\r
- }\r
-\r
- static class NodeIterator implements Iterator<Node> {\r
- NodeList list; \r
- int i;\r
- int length;\r
-\r
- public NodeIterator(NodeList list) {\r
- this.list = list;\r
- i = 0;\r
- length = list.getLength();\r
- }\r
-\r
- @Override\r
- public boolean hasNext() {\r
- return i<length;\r
- }\r
-\r
- @Override\r
- public Node next() {\r
- return new Node(list.item(i++));\r
- }\r
-\r
- @Override\r
- public void remove() {\r
- throw new UnsupportedOperationException();\r
- } \r
- \r
- }\r
- \r
- @Override\r
- public Iterator<Node> iterator() {\r
- return new NodeIterator(node.getChildNodes());\r
- } \r
- \r
- public String name() {\r
- return node.getNodeName();\r
- }\r
- \r
- public String value() {\r
- return node.getNodeValue().trim(); \r
- }\r
- \r
- public int type() {\r
- return node.getNodeType();\r
- }\r
- \r
- public Node parent() {\r
- return new Node(node.getParentNode());\r
- }\r
- \r
- public Node get(String key) {\r
- return new Node(node.getAttributes().getNamedItem(key));\r
- }\r
- \r
- public String getValue(String key) {\r
- return node.getAttributes().getNamedItem(key).getNodeValue().trim(); \r
- }\r
- \r
- public int childCount() {\r
- return node.getChildNodes().getLength();\r
- }\r
- \r
- public String text() {\r
- return node.getTextContent().trim();\r
- }\r
- \r
- public Node getSingleChild(String type) {\r
- NodeList nodeList = node.getChildNodes();\r
- Node child = null;\r
- for(int i=0;i<nodeList.getLength();++i) {\r
- org.w3c.dom.Node c = nodeList.item(i);\r
- if(c.getNodeName().equals(type)) {\r
- if(child == null)\r
- child = new Node(c);\r
- else\r
- System.out.println("Child of type " + type + " is not unique.");\r
- }\r
- }\r
- return child;\r
- }\r
- \r
- @Override\r
- public boolean equals(Object obj) {\r
- return this == obj || (obj instanceof Node && node.equals(((Node)obj).node));\r
- }\r
- \r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.utils.xml;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+
+public class Node implements Iterable<Node> {
+ public static final short ELEMENT_NODE = org.w3c.dom.Node.ELEMENT_NODE;
+ public static final short ATTRIBUTE_NODE = org.w3c.dom.Node.ATTRIBUTE_NODE;
+ public static final short TEXT_NODE = org.w3c.dom.Node.TEXT_NODE;
+ public static final short CDATA_SECTION_NODE = org.w3c.dom.Node.CDATA_SECTION_NODE;
+ public static final short ENTITY_REFERENCE_NODE = org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
+ public static final short ENTITY_NODE = org.w3c.dom.Node.ENTITY_NODE;
+ public static final short PROCESSING_INSTRUCTION_NODE = org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
+ public static final short COMMENT_NODE = org.w3c.dom.Node.COMMENT_NODE;
+ public static final short DOCUMENT_NODE = org.w3c.dom.Node.DOCUMENT_NODE;
+ public static final short DOCUMENT_TYPE_NODE = org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
+ public static final short DOCUMENT_FRAGMENT_NODE = org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE;
+ public static final short NOTATION_NODE = org.w3c.dom.Node.NOTATION_NODE;
+
+ private org.w3c.dom.Node node;
+
+ public Node(org.w3c.dom.Node node) {
+ this.node = node;
+ }
+
+ public Node(File file) throws IOException {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder;
+ try {
+ builder = factory.newDocumentBuilder();
+ node = builder.parse(file);
+ } catch (ParserConfigurationException e) {
+ throw new IOException(e);
+ } catch (SAXException e) {
+ throw new IOException(e);
+ }
+ }
+
+ static class NodeIterator implements Iterator<Node> {
+ NodeList list;
+ int i;
+ int length;
+
+ public NodeIterator(NodeList list) {
+ this.list = list;
+ i = 0;
+ length = list.getLength();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return i<length;
+ }
+
+ @Override
+ public Node next() {
+ return new Node(list.item(i++));
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+
+ @Override
+ public Iterator<Node> iterator() {
+ return new NodeIterator(node.getChildNodes());
+ }
+
+ public String name() {
+ return node.getNodeName();
+ }
+
+ public String value() {
+ return node.getNodeValue().trim();
+ }
+
+ public int type() {
+ return node.getNodeType();
+ }
+
+ public Node parent() {
+ return new Node(node.getParentNode());
+ }
+
+ public Node get(String key) {
+ return new Node(node.getAttributes().getNamedItem(key));
+ }
+
+ public String getValue(String key) {
+ return node.getAttributes().getNamedItem(key).getNodeValue().trim();
+ }
+
+ public int childCount() {
+ return node.getChildNodes().getLength();
+ }
+
+ public String text() {
+ return node.getTextContent().trim();
+ }
+
+ public Node getSingleChild(String type) {
+ NodeList nodeList = node.getChildNodes();
+ Node child = null;
+ for(int i=0;i<nodeList.getLength();++i) {
+ org.w3c.dom.Node c = nodeList.item(i);
+ if(c.getNodeName().equals(type)) {
+ if(child == null)
+ child = new Node(c);
+ else
+ System.out.println("Child of type " + type + " is not unique.");
+ }
+ }
+ return child;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return this == obj || (obj instanceof Node && node.equals(((Node)obj).node));
+ }
+
+}
+