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.runner;
004:
005: import java.io.*;
006: import fit.Counts;
007: import fitnesse.components.*;
008: import fitnesse.util.*;
009: import fitnesse.components.XmlWriter;
010: import org.w3c.dom.*;
011:
012: public class XmlResultFormatter implements ResultFormatter {
013: private ContentBuffer buffer;
014:
015: private Document document;
016:
017: private boolean closed = false;
018:
019: private byte[] tailBytes;
020:
021: public XmlResultFormatter(String host, String rootPath)
022: throws Exception {
023: buffer = new ContentBuffer(".xml");
024: createDocument(host, rootPath);
025: writeDocumentHeader();
026: }
027:
028: private void writeDocumentHeader() throws Exception {
029: ByteArrayOutputStream output = new ByteArrayOutputStream();
030: XmlWriter writer = new XmlWriter(output);
031: writer.write(document);
032: writer.close();
033: String xmlText = output.toString();
034: int endIndex = xmlText.indexOf("</testResults>");
035: String head = xmlText.substring(0, endIndex);
036: String tail = xmlText.substring(endIndex);
037: tailBytes = tail.getBytes();
038: buffer.append(head);
039: }
040:
041: private void createDocument(String host, String rootPath)
042: throws Exception {
043: document = XmlUtil.newDocument();
044: Element root = document.createElement("testResults");
045: document.appendChild(root);
046: XmlUtil.addTextNode(document, root, "host", host);
047: XmlUtil.addTextNode(document, root, "rootPath", rootPath);
048: }
049:
050: public void acceptResult(PageResult result) throws Exception {
051: Element resultElement = document.createElement("result");
052: XmlUtil.addTextNode(document, resultElement,
053: "relativePageName", result.title());
054: XmlUtil.addCdataNode(document, resultElement, "content", result
055: .content());
056: resultElement.appendChild(makeCountsElement("counts", result
057: .counts()));
058: writeElement(resultElement);
059: }
060:
061: public void acceptFinalCount(Counts count) throws Exception {
062: Element countsElement = makeCountsElement("finalCounts", count);
063: writeElement(countsElement);
064: }
065:
066: public int getByteCount() throws Exception {
067: close();
068: return buffer.getSize();
069: }
070:
071: public InputStream getResultStream() throws Exception {
072: close();
073: return buffer.getInputStream();
074: }
075:
076: private void close() throws Exception {
077: if (!closed) {
078: buffer.append(tailBytes);
079: closed = true;
080: }
081: }
082:
083: private void writeElement(Element resultElement) throws Exception {
084: ByteArrayOutputStream output = new ByteArrayOutputStream();
085: XmlWriter writer = new XmlWriter(output);
086: writer.write(resultElement, 1);
087: writer.close();
088: buffer.append(output.toByteArray());
089: }
090:
091: private Element makeCountsElement(String name, Counts counts) {
092: Element countsElement = document.createElement(name);
093: XmlUtil.addTextNode(document, countsElement, "right",
094: counts.right + "");
095: XmlUtil.addTextNode(document, countsElement, "wrong",
096: counts.wrong + "");
097: XmlUtil.addTextNode(document, countsElement, "ignores",
098: counts.ignores + "");
099: XmlUtil.addTextNode(document, countsElement, "exceptions",
100: counts.exceptions + "");
101: return countsElement;
102: }
103: }
|