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.wiki;
004:
005: import java.io.Serializable;
006: import java.util.ArrayList;
007: import java.util.Collection;
008: import java.util.Date;
009: import java.util.HashSet;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Set;
013:
014: import fitnesse.components.SaveRecorder;
015: import fitnesse.responders.editing.EditResponder;
016: import fitnesse.responders.run.SuiteResponder;
017: import fitnesse.wikitext.WidgetBuilder;
018: import fitnesse.wikitext.WikiWidget;
019: import fitnesse.wikitext.widgets.ClasspathWidget;
020: import fitnesse.wikitext.widgets.FixtureWidget;
021: import fitnesse.wikitext.widgets.IncludeWidget;
022: import fitnesse.wikitext.widgets.TextIgnoringWidgetRoot;
023: import fitnesse.wikitext.widgets.VariableDefinitionWidget;
024: import fitnesse.wikitext.widgets.WidgetRoot;
025: import fitnesse.wikitext.widgets.WidgetWithTextArgument;
026: import fitnesse.wikitext.widgets.XRefWidget;
027:
028: public class PageData implements Serializable {
029: public static WidgetBuilder classpathWidgetBuilder = new WidgetBuilder(
030: new Class[] { IncludeWidget.class,
031: VariableDefinitionWidget.class,
032: ClasspathWidget.class });
033: public static WidgetBuilder fixtureWidgetBuilder = new WidgetBuilder(
034: new Class[] { FixtureWidget.class });
035: public static WidgetBuilder xrefWidgetBuilder = new WidgetBuilder(
036: new Class[] { XRefWidget.class });
037: public static WidgetBuilder variableDefinitionWidgetBuilder = new WidgetBuilder(
038: new Class[] { IncludeWidget.class,
039: VariableDefinitionWidget.class });
040:
041: private transient WikiPage wikiPage;
042: private String content;
043: private WikiPageProperties properties = new WikiPageProperties();
044: private Set versions;
045: private WidgetRoot variableRoot;
046:
047: public PageData(WikiPage page) throws Exception {
048: wikiPage = page;
049: initializeAttributes();
050: versions = new HashSet();
051: }
052:
053: public PageData(WikiPage page, String content) throws Exception {
054: this (page);
055: setContent(content);
056: }
057:
058: public PageData(PageData data) throws Exception {
059: this (data.getWikiPage());
060: wikiPage = data.wikiPage;
061: content = data.content;
062: properties = new WikiPageProperties(data.properties);
063: versions.addAll(data.versions);
064: }
065:
066: public String getStringOfAllAttributes() {
067: return properties.toString();
068: }
069:
070: public void initializeAttributes() throws Exception {
071: properties.set("Edit", "true");
072: properties.set("Versions", "true");
073: properties.set("Properties", "true");
074: properties.set("Refactor", "true");
075: properties.set("WhereUsed", "true");
076: properties.set("Files", "true");
077: properties.set("RecentChanges", "true");
078: properties.set("Search", "true");
079: properties.set(EditResponder.TICKET_ID, SaveRecorder
080: .newTicket()
081: + "");
082: properties.setLastModificationTime(new Date());
083:
084: final String pageName = wikiPage.getName();
085: if (pageName.startsWith("Test"))
086: properties.set("Test", "true");
087: if (pageName.startsWith("Suite")
088: && !pageName.equals(SuiteResponder.SUITE_SETUP_NAME)
089: && !pageName.equals(SuiteResponder.SUITE_TEARDOWN_NAME)) {
090: properties.set("Suite", "true");
091: }
092: }
093:
094: public WikiPageProperties getProperties() throws Exception {
095: return properties;
096: }
097:
098: public String getAttribute(String key) throws Exception {
099: return properties.get(key);
100: }
101:
102: public void removeAttribute(String key) throws Exception {
103: properties.remove(key);
104: }
105:
106: public void setAttribute(String key, String value) throws Exception {
107: properties.set(key, value);
108: }
109:
110: public void setAttribute(String key) throws Exception {
111: properties.set(key);
112: }
113:
114: public boolean hasAttribute(String attribute) throws Exception {
115: return properties.has(attribute);
116: }
117:
118: public void setProperties(WikiPageProperties properties) {
119: this .properties = properties;
120: }
121:
122: public String getContent() throws Exception {
123: return content;
124: }
125:
126: public void setContent(String content) throws Exception {
127: this .content = content;
128: }
129:
130: public String getHtml() throws Exception {
131: return processHTMLWidgets(getContent(), wikiPage);
132: }
133:
134: public String getHtml(WikiPage context) throws Exception {
135: return processHTMLWidgets(getContent(), context);
136: }
137:
138: public String getVariable(String name) throws Exception {
139: initializeVariableRoot();
140: return variableRoot.getVariable(name);
141: }
142:
143: private void initializeVariableRoot() throws Exception {
144: if (variableRoot == null) {
145: variableRoot = new TextIgnoringWidgetRoot(getContent(),
146: wikiPage, variableDefinitionWidgetBuilder);
147: variableRoot.render();
148: }
149: }
150:
151: public void addVariable(String name, String value) throws Exception {
152: initializeVariableRoot();
153: variableRoot.addVariable(name, value);
154: }
155:
156: private String processHTMLWidgets(String content, WikiPage context)
157: throws Exception {
158: WidgetRoot root = new WidgetRoot(content, context,
159: WidgetBuilder.htmlWidgetBuilder);
160: return root.render();
161: }
162:
163: public void setWikiPage(WikiPage page) {
164: wikiPage = page;
165: }
166:
167: public WikiPage getWikiPage() {
168: return wikiPage;
169: }
170:
171: public List getClasspaths() throws Exception {
172: return getTextOfWidgets(classpathWidgetBuilder);
173: }
174:
175: public List getFixtureNames() throws Exception {
176: return getTextOfWidgets(fixtureWidgetBuilder);
177: }
178:
179: public List getXrefPages() throws Exception {
180: return getTextOfWidgets(xrefWidgetBuilder);
181: }
182:
183: private List getTextOfWidgets(WidgetBuilder builder)
184: throws Exception {
185: WidgetRoot root = new TextIgnoringWidgetRoot(getContent(),
186: wikiPage, builder);
187: List widgets = root.getChildren();
188: List values = new ArrayList();
189: for (Iterator iterator = widgets.iterator(); iterator.hasNext();) {
190: Object widget = iterator.next();
191: if (widget instanceof WidgetWithTextArgument)
192: values.add(((WidgetWithTextArgument) widget).getText());
193: else if (widget instanceof WikiWidget)
194: ((WikiWidget) widget).render();
195: }
196: return values;
197: }
198:
199: public Set getVersions() {
200: return versions;
201: }
202:
203: public void addVersions(Collection newVersions) {
204: versions.addAll(newVersions);
205: }
206: }
|