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.fixtures;
04:
05: import fit.Fixture;
06:
07: import java.io.PrintWriter;
08: import java.io.StringWriter;
09: import java.util.StringTokenizer;
10: import fitnesse.wiki.*;
11:
12: public class PageBuilder extends Fixture {
13: private PrintWriter writer;
14:
15: private StringWriter stringWriter;
16:
17: private String pageAttributes = null;
18:
19: public PageBuilder() {
20: stringWriter = new StringWriter();
21: writer = new PrintWriter(stringWriter);
22: }
23:
24: public void line(String line) {
25: if (line.startsWith("\\"))
26: line = line.substring(1);
27: writer.println(line);
28: }
29:
30: public void page(String name) throws Exception {
31: String content = stringWriter.toString();
32: PageCrawler crawler = FitnesseFixtureContext.root
33: .getPageCrawler();
34: WikiPagePath path = PathParser.parse(name);
35: WikiPage page = crawler.addPage(FitnesseFixtureContext.root,
36: path, content);
37: if (pageAttributes != null) {
38: PageData data = page.getData();
39: setAttributes(data);
40: page.commit(data);
41: pageAttributes = null;
42: }
43: }
44:
45: public void attributes(String attributes) {
46: pageAttributes = attributes;
47: }
48:
49: private void setAttributes(PageData data) throws Exception {
50: StringTokenizer tokenizer = new StringTokenizer(pageAttributes,
51: ",");
52: while (tokenizer.hasMoreTokens()) {
53: String nameValuePair = tokenizer.nextToken();
54: int equals = nameValuePair.indexOf("=");
55: if (equals < 0)
56: throw new Exception(
57: "Attribute must have form name=value");
58: String name = nameValuePair.substring(0, equals);
59: String value = nameValuePair.substring(equals + 1);
60: data.setAttribute(name, value);
61: }
62: }
63: }
|