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.wikitext.widgets;
004:
005: import java.util.HashMap;
006: import java.util.LinkedList;
007: import java.util.List;
008: import java.util.Map;
009:
010: import fitnesse.wiki.PagePointer;
011: import fitnesse.wiki.WikiPage;
012: import fitnesse.wikitext.WidgetBuilder;
013:
014: public class WidgetRoot extends ParentWidget {
015: private Map variables = new HashMap();
016:
017: private WidgetBuilder builder;
018:
019: private WikiPage page;
020:
021: private boolean doEscaping = true;
022:
023: private List literals = new LinkedList();
024:
025: public WidgetRoot(WikiPage page) throws Exception {
026: this ("", page, WidgetBuilder.htmlWidgetBuilder);
027: }
028:
029: public WidgetRoot(String value, WikiPage page) throws Exception {
030: this (value, page, WidgetBuilder.htmlWidgetBuilder);
031: }
032:
033: public WidgetRoot(String value, WikiPage page, WidgetBuilder builder)
034: throws Exception {
035: super (null);
036: this .page = page;
037: this .builder = builder;
038: if (value != null)
039: buildWidgets(value);
040: }
041:
042: public WidgetRoot(PagePointer pagePointer) throws Exception {
043: this ("", pagePointer, WidgetBuilder.htmlWidgetBuilder);
044: }
045:
046: public WidgetRoot(String value, PagePointer pagePointer)
047: throws Exception {
048: this (value, pagePointer, WidgetBuilder.htmlWidgetBuilder);
049: }
050:
051: public WidgetRoot(String value, PagePointer pagePointer,
052: WidgetBuilder builder) throws Exception {
053: super (null);
054: this .page = pagePointer.getPage();
055: this .builder = builder;
056: if (value != null)
057: buildWidgets(value);
058: }
059:
060: public WidgetBuilder getBuilder() {
061: return builder;
062: }
063:
064: protected void buildWidgets(String value) throws Exception {
065: String nonLiteralContent = processLiterals(value);
066: addChildWidgets(nonLiteralContent);
067: }
068:
069: public String render() throws Exception {
070: return childHtml();
071: }
072:
073: public String getVariable(String key) throws Exception {
074: String value = (String) variables.get(key);
075:
076: WikiPage page = getWikiPage();
077: while (value == null && !page.getPageCrawler().isRoot(page)) {
078: page = page.getParent();
079: value = page.getData().getVariable(key);
080: }
081: if (value == null) {
082: value = System.getProperty(key);
083: }
084: return value;
085: }
086:
087: public void addVariable(String key, String value) {
088: variables.put(key, value);
089: }
090:
091: public int defineLiteral(String literal) {
092: int literalNumber = literals.size();
093: literals.add(literal);
094: return literalNumber;
095: }
096:
097: public String getLiteral(int literalNumber) {
098: if (literalNumber >= literals.size())
099: return "literal(" + literalNumber + ") not found.";
100: return (String) literals.get(literalNumber);
101: }
102:
103: public WikiPage getWikiPage() {
104: return page;
105: }
106:
107: public void setEscaping(boolean value) {
108: doEscaping = value;
109: }
110:
111: public boolean doEscaping() {
112: return doEscaping;
113: }
114:
115: public List getLiterals() {
116: return literals;
117: }
118:
119: public void setLiterals(List literals) {
120: this .literals = literals;
121: }
122:
123: public String asWikiText() throws Exception {
124: return childWikiText();
125: }
126: }
|