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.components;
004:
005: import java.io.*;
006: import org.w3c.dom.*;
007:
008: public class XmlWriter {
009: private static String endl = System.getProperty("line.separator");
010:
011: private Writer writer;
012: private boolean isNewLine;
013:
014: public XmlWriter(OutputStream os) throws Exception {
015: writer = new OutputStreamWriter(os, "UTF-8");
016: }
017:
018: public void write(Document doc) throws Exception {
019: write("<?xml version=\"1.0\"?>");
020: write(endl);
021: write(doc.getDocumentElement(), 0);
022: }
023:
024: public void write(NodeList nodes) throws Exception {
025: write(nodes, 0);
026: }
027:
028: public void write(Element element, int tabs) throws Exception {
029: if (!isNewLine)
030: write(endl);
031: if (!element.hasChildNodes()) {
032: writeTabs(tabs);
033: write("<" + element.getTagName() + writeAttributes(element)
034: + "/>");
035: } else {
036: writeTabs(tabs);
037: write("<" + element.getTagName() + writeAttributes(element)
038: + ">");
039: write(element.getChildNodes(), tabs + 1);
040: if (isNewLine)
041: writeTabs(tabs);
042: write("</" + element.getTagName() + ">");
043: }
044: write(endl);
045: }
046:
047: private String writeAttributes(Element element) {
048: StringBuffer attributeString = new StringBuffer();
049: NamedNodeMap attributeMap = element.getAttributes();
050: int length = attributeMap.getLength();
051: for (int i = 0; i < length; i++) {
052: Attr attributeNode = (Attr) attributeMap.item(i);
053: String name = attributeNode.getName();
054: String value = attributeNode.getValue();
055: attributeString.append(" ").append(name).append("=\"")
056: .append(value).append("\"");
057: }
058: return attributeString.toString();
059: }
060:
061: private void write(NodeList nodes, int tabs) throws Exception {
062: for (int i = 0; i < nodes.getLength(); i++) {
063: Node node = nodes.item(i);
064: write(node, tabs);
065: }
066: }
067:
068: private void writeText(Text text) throws Exception {
069: String nodeValue = text.getNodeValue();
070: write(nodeValue.trim());
071: }
072:
073: private void writeCdata(CDATASection cData) throws Exception {
074: String cDataText = "<![CDATA[" + cData.getNodeValue() + "]]>";
075: write(cDataText);
076: }
077:
078: private void write(Node node, int tabs) throws Exception {
079: if (node instanceof Element)
080: write((Element) node, tabs);
081: else if (node instanceof CDATASection)
082: writeCdata((CDATASection) node);
083: else if (node instanceof Text)
084: writeText((Text) node);
085: else
086: throw new Exception("XmlWriter: unsupported node type: "
087: + node.getClass());
088: }
089:
090: private void writeTabs(int tabs) throws Exception {
091: for (int i = 0; i < tabs; i++)
092: write("\t");
093: }
094:
095: private void write(String value) throws Exception {
096: if (value == null || "".equals(value)) {
097: return;
098: }
099: isNewLine = endl.equals(value);
100: writer.write(value);
101: }
102:
103: public void flush() throws Exception {
104: writer.flush();
105: }
106:
107: public void close() throws Exception {
108: writer.close();
109: }
110: }
|