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 fit.Counts;
006: import java.io.InputStream;
007: import fitnesse.components.ContentBuffer;
008: import fitnesse.html.*;
009: import fitnesse.responders.run.SuiteHtmlFormatter;
010:
011: public class HtmlResultFormatter implements ResultFormatter {
012: private ContentBuffer buffer;
013: private boolean closed = false;
014: private SuiteHtmlFormatter suiteFormatter;
015: private String host;
016: private String rootPath;
017: private HtmlPage page;
018:
019: public HtmlResultFormatter(HtmlPageFactory pageFactory,
020: String host, String rootPath) throws Exception {
021: this .host = host;
022: this .rootPath = rootPath;
023:
024: buffer = new ContentBuffer(".html");
025:
026: createPage(pageFactory, rootPath);
027: suiteFormatter = new SuiteHtmlFormatter(page);
028: buffer.append(suiteFormatter.head());
029: }
030:
031: private void createPage(HtmlPageFactory pageFactory, String rootPath)
032: throws Exception {
033: page = pageFactory.newPage();
034: page.head.use(makeBaseTag());
035: page.head.add(makeContentTypeMetaTag());
036: page.title.use(rootPath);
037: page.head.add(page.title);
038: page.head.add(page.makeCssLink("/files/css/fitnesse_print.css",
039: "screen"));
040:
041: HtmlTag script = new HtmlTag("script", scriptContent);
042: script.addAttribute("language", "javascript");
043: page.head.add(script);
044: page.body.addAttribute("onload", "localizeInPageLinks()");
045:
046: page.header.use(HtmlUtil.makeBreadCrumbsWithPageType(rootPath,
047: "Command Line Test Results"));
048: }
049:
050: private HtmlTag makeContentTypeMetaTag() {
051: HtmlTag meta = new HtmlTag("meta");
052: meta.addAttribute("http-equiv", "Content-Type");
053: meta.addAttribute("content", "text/html; charset=utf-8");
054: return meta;
055: }
056:
057: private HtmlTag makeBaseTag() {
058: HtmlTag base = new HtmlTag("base");
059: StringBuffer href = new StringBuffer("http://");
060: href.append(host);
061: href.append("/");
062: base.addAttribute("href", href.toString());
063: return base;
064: }
065:
066: public void acceptResult(PageResult result) throws Exception {
067: String relativePageName = result.title();
068: suiteFormatter.startOutputForNewTest(relativePageName, rootPath
069: + "." + relativePageName);
070: suiteFormatter.acceptOutput(result.content());
071: String resultRow = suiteFormatter.acceptResults(
072: relativePageName, result.counts());
073: buffer.append(resultRow);
074: }
075:
076: public void acceptFinalCount(Counts count) throws Exception {
077: buffer.append(suiteFormatter.testSummary(count));
078: buffer.append(suiteFormatter.testOutput());
079: }
080:
081: private void close() throws Exception {
082: if (!closed) {
083: buffer.append(suiteFormatter.tail());
084: closed = true;
085: }
086: }
087:
088: public int getByteCount() throws Exception {
089: close();
090: return buffer.getSize();
091: }
092:
093: public InputStream getResultStream() throws Exception {
094: close();
095: return buffer.getInputStream();
096: }
097:
098: public static final String scriptContent = "\n"
099: + "function localizeInPageLinks()\n"
100: + "{\n"
101: + "\tvar base = document.getElementsByTagName('base')[0].href;\n"
102: + "\tvar inPageBase = base + \"#\";\n"
103: + "\tvar baseLength = inPageBase.length\n"
104: + "\tvar aTags = document.getElementsByTagName('a');\n"
105: + "\tfor(var i=0; i < aTags.length; i++)\n"
106: + "\t{\n"
107: + "\t\tvar tag = aTags[i];\n"
108: + "\t\tif(tag.href && tag.href.substring(0, baseLength) == inPageBase)\n"
109: + "\t\t\ttag.href = location.href + '#' + tag.href.substring(baseLength);\n"
110: + "\t}\n" + "}\n";
111: }
|