01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.runner;
04:
05: import fit.Counts;
06: import java.util.regex.*;
07:
08: public class PageResult {
09: private static final Pattern countsPattern = Pattern
10: .compile("(\\d+)[^,]*, (\\d+)[^,]*, (\\d+)[^,]*, (\\d+)[^,]*");
11:
12: private StringBuffer contentBuffer = new StringBuffer();
13:
14: private Counts counts;
15:
16: private String title;
17:
18: public PageResult(String title) {
19: this .title = title;
20: }
21:
22: public PageResult(String title, Counts counts,
23: String startingContent) throws Exception {
24: this (title);
25: this .counts = counts;
26: append(startingContent);
27: }
28:
29: public String content() {
30: return contentBuffer.toString();
31: }
32:
33: public void append(String data) throws Exception {
34: contentBuffer.append(data);
35: }
36:
37: public String title() {
38: return title;
39: }
40:
41: public Counts counts() {
42: return counts;
43: }
44:
45: public void setCounts(Counts counts) {
46: this .counts = counts;
47: }
48:
49: public String toString() {
50: StringBuffer buffer = new StringBuffer();
51: buffer.append(title).append("\n");
52: buffer.append(counts.toString()).append("\n");
53: buffer.append(contentBuffer);
54: return buffer.toString();
55: }
56:
57: public static PageResult parse(String resultString)
58: throws Exception {
59: int firstEndlIndex = resultString.indexOf('\n');
60: int secondEndlIndex = resultString.indexOf('\n',
61: firstEndlIndex + 1);
62:
63: String title = resultString.substring(0, firstEndlIndex);
64: Counts counts = parseCounts(resultString.substring(
65: firstEndlIndex + 1, secondEndlIndex));
66: String content = resultString.substring(secondEndlIndex + 1);
67:
68: return new PageResult(title, counts, content);
69: }
70:
71: private static Counts parseCounts(String countString) {
72: Matcher matcher = countsPattern.matcher(countString);
73: if (matcher.find()) {
74: int right = Integer.parseInt(matcher.group(1));
75: int wrong = Integer.parseInt(matcher.group(2));
76: int ignores = Integer.parseInt(matcher.group(3));
77: int exceptions = Integer.parseInt(matcher.group(4));
78: return new Counts(right, wrong, ignores, exceptions);
79: } else
80: return null;
81: }
82: }
|