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