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