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