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