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