001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.util;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.InputStream;
008:
009: import javax.xml.parsers.DocumentBuilder;
010: import javax.xml.parsers.DocumentBuilderFactory;
011:
012: import org.w3c.dom.CDATASection;
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017: import org.w3c.dom.Text;
018:
019: import fitnesse.components.XmlWriter;
020:
021: public class XmlUtil {
022: private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
023: .newInstance();
024:
025: public static DocumentBuilder getDocumentBuilder() throws Exception {
026: return documentBuilderFactory.newDocumentBuilder();
027: }
028:
029: public static Document newDocument() throws Exception {
030: return getDocumentBuilder().newDocument();
031: }
032:
033: public static Document newDocument(InputStream input)
034: throws Exception {
035: return getDocumentBuilder().parse(input);
036: }
037:
038: public static Document newDocument(String input) throws Exception {
039: ByteArrayInputStream is = new ByteArrayInputStream(input
040: .getBytes("UTF-8"));
041: return newDocument(is);
042: }
043:
044: public static Element getElementByTagName(Element element,
045: String name) throws Exception {
046: NodeList nodes = element.getElementsByTagName(name);
047: if (nodes.getLength() == 0)
048: return null;
049: else
050: return (Element) nodes.item(0);
051: }
052:
053: public static Element getLocalElementByTagName(Element context,
054: String tagName) throws Exception {
055: NodeList childNodes = context.getChildNodes();
056: for (int i = 0; i < childNodes.getLength(); i++) {
057: Node node = childNodes.item(i);
058: if (node instanceof Element
059: && tagName.equals(node.getNodeName()))
060: return (Element) node;
061: }
062: return null;
063: }
064:
065: public static String getTextValue(Element element, String name)
066: throws Exception {
067: Element namedElement = getElementByTagName(element, name);
068: return getElementText(namedElement, name);
069: }
070:
071: public static String getLocalTextValue(Element element, String name)
072: throws Exception {
073: Element namedElement = getLocalElementByTagName(element, name);
074: return getElementText(namedElement, name);
075: }
076:
077: private static String getElementText(Element namedElement,
078: String name) throws Exception {
079: if (namedElement == null)
080: return null;
081: Node candidateTextNode = namedElement.getFirstChild();
082: if (candidateTextNode instanceof Text)
083: return candidateTextNode.getNodeValue();
084: else
085: throw new Exception("The first child of " + name
086: + " is not a Text node");
087: }
088:
089: public static void addTextNode(Document document, Element element,
090: String tagName, String value) {
091: if (value != null && !(value.equals(""))) {
092: Element titleElement = document.createElement(tagName);
093: Text titleText = document.createTextNode(value);
094: titleElement.appendChild(titleText);
095: element.appendChild(titleElement);
096: }
097: }
098:
099: public static void addCdataNode(Document document, Element element,
100: String tagName, String value) {
101: if (value != null && !(value.equals(""))) {
102: Element titleElement = document.createElement(tagName);
103: CDATASection cData = document.createCDATASection(value);
104: titleElement.appendChild(cData);
105: element.appendChild(titleElement);
106: }
107: }
108:
109: public static String xmlAsString(Document doc) throws Exception {
110: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
111: XmlWriter writer = new XmlWriter(outputStream);
112: writer.write(doc);
113: writer.flush();
114: writer.close();
115: String value = outputStream.toString();
116: return value;
117: }
118: }
|