001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.xmlutil;
017:
018: import org.w3c.dom.*;
019: import org.xml.sax.SAXException;
020:
021: import javax.xml.parsers.DocumentBuilderFactory;
022: import javax.xml.parsers.ParserConfigurationException;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.io.InputStream;
026: import java.io.IOException;
027: import java.io.File;
028: import java.io.FileInputStream;
029:
030: /**
031: * Utility methods for working with DOM documents.
032: */
033: public class DocumentHelper {
034: public static Document parse(InputStream is)
035: throws ParserConfigurationException, IOException,
036: SAXException {
037: DocumentBuilderFactory factory = DocumentBuilderFactory
038: .newInstance();
039: factory.setNamespaceAware(true);
040: Document document = factory.newDocumentBuilder().parse(is);
041: return document;
042: }
043:
044: public static Document parse(File file)
045: throws ParserConfigurationException, IOException,
046: SAXException {
047: InputStream is = null;
048: try {
049: is = new FileInputStream(file);
050: return parse(is);
051: } finally {
052: if (is != null)
053: is.close();
054: }
055: }
056:
057: public static Element getElementChild(Element element, String name,
058: boolean required) throws Exception {
059: NodeList nodes = element.getChildNodes();
060: for (int i = 0; i < nodes.getLength(); i++) {
061: Node node = nodes.item(i);
062: if (node instanceof Element
063: && node.getLocalName().equals(name)
064: && node.getNamespaceURI() == null) {
065: return (Element) node;
066: }
067: }
068: if (required)
069: throw new Exception("Missing element " + name + " in "
070: + element.getLocalName());
071: else
072: return null;
073: }
074:
075: public static Element[] getElementChildren(Element element) {
076: List<Element> elements = new ArrayList<Element>();
077: NodeList nodes = element.getChildNodes();
078: for (int i = 0; i < nodes.getLength(); i++) {
079: if (nodes.item(i) instanceof Element) {
080: elements.add((Element) nodes.item(i));
081: }
082: }
083: return elements.toArray(new Element[0]);
084: }
085:
086: public static Element[] getElementChildren(Element element,
087: String name) {
088: Element[] elements = getElementChildren(element);
089: List<Element> taggedElements = new ArrayList<Element>();
090: for (int i = 0; i < elements.length; i++) {
091: if (elements[i].getLocalName().equals(name)
092: && elements[i].getNamespaceURI() == null) {
093: taggedElements.add(elements[i]);
094: }
095: }
096: return taggedElements.toArray(new Element[0]);
097: }
098:
099: public static String getAttribute(Element element, String name,
100: boolean required) throws Exception {
101: if (!element.hasAttribute(name)) {
102: if (required)
103: throw new Exception("Missing attribute " + name
104: + " on element " + element.getLocalName());
105: else
106: return null;
107: }
108:
109: return element.getAttribute(name);
110: }
111:
112: public static boolean getBooleanAttribute(Element element,
113: String name, boolean defaultValue) throws Exception {
114: String value = element.getAttribute(name);
115: if (value.equals(""))
116: return defaultValue;
117: else
118: return value.equalsIgnoreCase("true");
119: }
120:
121: public static boolean getBooleanElement(Element element,
122: String name, boolean defaultValue) throws Exception {
123: Element child = getElementChild(element, name, false);
124: if (child == null)
125: return defaultValue;
126:
127: return getElementText(child, true).equalsIgnoreCase("true");
128: }
129:
130: public static String getStringElement(Element element, String name,
131: String defaultValue) throws Exception {
132: Element child = getElementChild(element, name, false);
133: if (child == null)
134: return defaultValue;
135:
136: return getElementText(child, true);
137: }
138:
139: public static int getIntegerElement(Element element, String name,
140: int defaultValue) throws Exception {
141: Element child = getElementChild(element, name, false);
142: if (child == null)
143: return defaultValue;
144:
145: String text = getElementText(child, true);
146: try {
147: return Integer.parseInt(text);
148: } catch (NumberFormatException e) {
149: throw new RuntimeException("Invalid integer value " + text
150: + " in element " + name);
151: }
152: }
153:
154: public static String getElementText(Element element,
155: boolean required) throws Exception {
156: StringBuilder text = new StringBuilder(30);
157: NodeList nodes = element.getChildNodes();
158: for (int i = 0; i < nodes.getLength(); i++) {
159: Node node = nodes.item(i);
160: if (node instanceof Text)
161: text.append(node.getNodeValue());
162: }
163:
164: if (required && text.length() == 0)
165: throw new Exception("Missing text content in element "
166: + element.getLocalName());
167: else if (text.length() == 0)
168: return null;
169: else
170: return text.toString();
171: }
172: }
|