001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.util;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.InputStream;
026: import java.io.PrintWriter;
027: import java.io.StringWriter;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.FactoryConfigurationError;
035: import javax.xml.parsers.ParserConfigurationException;
036: import javax.xml.transform.Result;
037: import javax.xml.transform.Source;
038: import javax.xml.transform.Transformer;
039: import javax.xml.transform.TransformerFactory;
040: import javax.xml.transform.dom.DOMSource;
041: import javax.xml.transform.stream.StreamResult;
042:
043: import org.w3c.dom.CharacterData;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046: import org.w3c.dom.Node;
047: import org.w3c.dom.NodeList;
048: import org.xml.sax.InputSource;
049:
050: public abstract class XmlUtil {
051:
052: public static Document parseXmlText(String xml) {
053: ByteArrayInputStream bais = new ByteArrayInputStream(xml
054: .getBytes());
055: return parseXmlInputSource(new InputSource(bais));
056: }
057:
058: public static Document parseXmlResource(String resource) {
059: InputStream inputStream = ClassLoaderUtil.getStream(resource);
060: InputSource inputSource = new InputSource(inputStream);
061: return parseXmlInputSource(inputSource);
062: }
063:
064: public static Document parseXmlInputStream(InputStream inputStream) {
065: Document document = null;
066: try {
067: document = getDocumentBuilder().parse(inputStream);
068: } catch (Exception e) {
069: throw new XmlException("couldn't parse xml", e);
070: }
071: return document;
072: }
073:
074: public static Document parseXmlInputSource(InputSource inputSource) {
075: Document document = null;
076: try {
077: document = getDocumentBuilder().parse(inputSource);
078: } catch (Exception e) {
079: throw new XmlException("couldn't parse xml", e);
080: }
081: return document;
082: }
083:
084: public static DocumentBuilder getDocumentBuilder()
085: throws FactoryConfigurationError,
086: ParserConfigurationException {
087: DocumentBuilderFactory factory = DocumentBuilderFactory
088: .newInstance();
089: return factory.newDocumentBuilder();
090: }
091:
092: public static Iterator elementIterator(Element element,
093: String tagName) {
094: return elements(element, tagName).iterator();
095: }
096:
097: public static List elements(Element element, String tagName) {
098: NodeList nodeList = element.getElementsByTagName(tagName);
099: List elements = new ArrayList(nodeList.getLength());
100: for (int i = 0; i < nodeList.getLength(); i++) {
101: Node child = nodeList.item(i);
102: if (child.getParentNode() == element) {
103: elements.add(child);
104: }
105: }
106: return elements;
107: }
108:
109: public static Element element(Element element, String name) {
110: Element childElement = null;
111: NodeList nodeList = element.getElementsByTagName(name);
112: if (nodeList.getLength() > 0) {
113: childElement = (Element) nodeList.item(0);
114: }
115: return childElement;
116: }
117:
118: public static Iterator elementIterator(Element element) {
119: return elements(element).iterator();
120: }
121:
122: public static List elements(Element element) {
123: List elements = new ArrayList();
124: NodeList nodeList = element.getChildNodes();
125: for (int i = 0; i < nodeList.getLength(); i++) {
126: Node node = nodeList.item(i);
127: if ((node instanceof Element)
128: && (element == node.getParentNode())) {
129: elements.add(node);
130: }
131: }
132: return elements;
133: }
134:
135: public static Element element(Element element) {
136: Element onlyChild = null;
137: List elements = elements(element);
138: if (!elements.isEmpty()) {
139: onlyChild = (Element) elements.get(0);
140: }
141: return onlyChild;
142: }
143:
144: public static String toString(Element element) {
145: if (element == null)
146: return "null";
147:
148: Source source = new DOMSource(element);
149:
150: StringWriter stringWriter = new StringWriter();
151: PrintWriter printWriter = new PrintWriter(stringWriter);
152: Result result = new StreamResult(printWriter);
153:
154: try {
155: Transformer transformer = TransformerFactory.newInstance()
156: .newTransformer();
157: transformer.transform(source, result);
158: } catch (Exception e) {
159: throw new XmlException("couldn't write element '"
160: + element.getTagName() + "' to string", e);
161: }
162:
163: printWriter.close();
164:
165: return stringWriter.toString();
166: }
167:
168: public static String getContentText(Element element) {
169: StringBuffer buffer = new StringBuffer();
170: NodeList nodeList = element.getChildNodes();
171: for (int i = 0; i < nodeList.getLength(); i++) {
172: Node node = nodeList.item(i);
173: if (node instanceof CharacterData) {
174: CharacterData characterData = (CharacterData) node;
175: buffer.append(characterData.getData());
176: }
177: }
178: return buffer.toString();
179: }
180: }
|