PageRenderTime 95ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/fitnesse/wiki/PageData.java

https://github.com/emlang/fitnesse
Java | 312 lines | 245 code | 61 blank | 6 comment | 13 complexity | a5d99516fa02a974c61adb0c9d4ab28e MD5 | raw file
  1. // Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
  2. // Released under the terms of the CPL Common Public License version 1.0.
  3. package fitnesse.wiki;
  4. import static fitnesse.wiki.PageType.*;
  5. import java.io.Serializable;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.Date;
  9. import java.util.HashSet;
  10. import java.util.List;
  11. import java.util.Set;
  12. import util.StringUtil;
  13. import fitnesse.responders.run.ExecutionLog;
  14. import fitnesse.wikitext.WidgetBuilder;
  15. import fitnesse.wikitext.WikiWidget;
  16. import fitnesse.wikitext.widgets.ClasspathWidget;
  17. import fitnesse.wikitext.widgets.IncludeWidget;
  18. import fitnesse.wikitext.widgets.ParentWidget;
  19. import fitnesse.wikitext.widgets.PreformattedWidget;
  20. import fitnesse.wikitext.widgets.TextIgnoringWidgetRoot;
  21. import fitnesse.wikitext.widgets.VariableDefinitionWidget;
  22. import fitnesse.wikitext.widgets.VariableWidget;
  23. import fitnesse.wikitext.widgets.WidgetRoot;
  24. import fitnesse.wikitext.widgets.WidgetWithTextArgument;
  25. import fitnesse.wikitext.widgets.XRefWidget;
  26. @SuppressWarnings("unchecked")
  27. public class PageData implements Serializable {
  28. private static final long serialVersionUID = 1L;
  29. public static WidgetBuilder classpathWidgetBuilder = new WidgetBuilder(
  30. IncludeWidget.class, VariableDefinitionWidget.class,
  31. ClasspathWidget.class);
  32. public static WidgetBuilder xrefWidgetBuilder = new WidgetBuilder(
  33. XRefWidget.class);
  34. public static WidgetBuilder variableDefinitionWidgetBuilder = new WidgetBuilder(
  35. IncludeWidget.class, PreformattedWidget.class,
  36. VariableDefinitionWidget.class, VariableWidget.class);
  37. // TODO: Find a better place for us
  38. public static final String PropertyLAST_MODIFIED = "LastModified";
  39. public static final String PropertyHELP = "Help";
  40. public static final String PropertyPRUNE = "Prune";
  41. public static final String PropertySEARCH = "Search";
  42. public static final String PropertyRECENT_CHANGES = "RecentChanges";
  43. public static final String PropertyFILES = "Files";
  44. public static final String PropertyWHERE_USED = "WhereUsed";
  45. public static final String PropertyREFACTOR = "Refactor";
  46. public static final String PropertyPROPERTIES = "Properties";
  47. public static final String PropertyVERSIONS = "Versions";
  48. public static final String PropertyEDIT = "Edit";
  49. public static final String PropertySUITES = "Suites";
  50. public static final String PAGE_TYPE_ATTRIBUTE = "PageType";
  51. public static final String[] PAGE_TYPE_ATTRIBUTES = { NORMAL.toString(),
  52. TEST.toString(), SUITE.toString() };
  53. public static final String[] ACTION_ATTRIBUTES = { PropertyEDIT,
  54. PropertyVERSIONS, PropertyPROPERTIES, PropertyREFACTOR,
  55. PropertyWHERE_USED };
  56. public static final String[] NAVIGATION_ATTRIBUTES = {
  57. PropertyRECENT_CHANGES, PropertyFILES, PropertySEARCH, PropertyPRUNE };
  58. public static final String[] NON_SECURITY_ATTRIBUTES = StringUtil
  59. .combineArrays(ACTION_ATTRIBUTES, NAVIGATION_ATTRIBUTES);
  60. public static final String PropertySECURE_READ = "secure-read";
  61. public static final String PropertySECURE_WRITE = "secure-write";
  62. public static final String PropertySECURE_TEST = "secure-test";
  63. public static final String[] SECURITY_ATTRIBUTES = { PropertySECURE_READ,
  64. PropertySECURE_WRITE, PropertySECURE_TEST };
  65. public static final String LAST_MODIFYING_USER = "LastModifyingUser";
  66. public static final String SUITE_SETUP_NAME = "SuiteSetUp";
  67. public static final String SUITE_TEARDOWN_NAME = "SuiteTearDown";
  68. private transient WikiPage wikiPage;
  69. private String content;
  70. private WikiPageProperties properties = new WikiPageProperties();
  71. private Set<VersionInfo> versions;
  72. private ParentWidget variableRoot;
  73. private List<String> literals;
  74. public static final String COMMAND_PATTERN = "COMMAND_PATTERN";
  75. public static final String TEST_RUNNER = "TEST_RUNNER";
  76. public static final String PATH_SEPARATOR = "PATH_SEPARATOR";
  77. public PageData(WikiPage page) throws Exception {
  78. wikiPage = page;
  79. initializeAttributes();
  80. versions = new HashSet<VersionInfo>();
  81. }
  82. public PageData(WikiPage page, String content) throws Exception {
  83. this(page);
  84. setContent(content);
  85. }
  86. public PageData(PageData data) throws Exception {
  87. this(data.getWikiPage(), data.content);
  88. properties = new WikiPageProperties(data.properties);
  89. versions.addAll(data.versions);
  90. variableRoot = data.variableRoot;
  91. }
  92. public String getStringOfAllAttributes() {
  93. return properties.toString();
  94. }
  95. public void initializeAttributes() throws Exception {
  96. properties.set(PropertyEDIT, Boolean.toString(true));
  97. properties.set(PropertyVERSIONS, Boolean.toString(true));
  98. properties.set(PropertyPROPERTIES, Boolean.toString(true));
  99. properties.set(PropertyREFACTOR, Boolean.toString(true));
  100. properties.set(PropertyWHERE_USED, Boolean.toString(true));
  101. properties.set(PropertyFILES, Boolean.toString(true));
  102. properties.set(PropertyRECENT_CHANGES, Boolean.toString(true));
  103. properties.set(PropertySEARCH, Boolean.toString(true));
  104. properties.setLastModificationTime(new Date());
  105. initTestOrSuiteProperty();
  106. }
  107. private void initTestOrSuiteProperty() throws Exception {
  108. final String pageName = wikiPage.getName();
  109. if (pageName == null) {
  110. handleInvalidPageName(wikiPage);
  111. return;
  112. }
  113. if (isErrorLogsPage())
  114. return;
  115. PageType pageType = PageType.getPageTypeForPageName(pageName);
  116. if (NORMAL.equals(pageType))
  117. return;
  118. properties.set(pageType.toString(), Boolean.toString(true));
  119. }
  120. private boolean isErrorLogsPage() throws Exception {
  121. PageCrawler crawler = wikiPage.getPageCrawler();
  122. String relativePagePath = crawler.getRelativeName(
  123. crawler.getRoot(wikiPage), wikiPage);
  124. return relativePagePath.startsWith(ExecutionLog.ErrorLogName);
  125. }
  126. // TODO: Should be written to a real logger, but it doesn't like FitNesse's
  127. // logger is
  128. // really intended for general logging.
  129. private void handleInvalidPageName(WikiPage wikiPage) {
  130. try {
  131. String msg = "WikiPage " + wikiPage + " does not have a valid name!"
  132. + wikiPage.getName();
  133. System.err.println(msg);
  134. throw new RuntimeException(msg);
  135. } catch (Exception e) {
  136. throw new RuntimeException(e);
  137. }
  138. }
  139. public WikiPageProperties getProperties() throws Exception {
  140. return properties;
  141. }
  142. public String getAttribute(String key) throws Exception {
  143. return properties.get(key);
  144. }
  145. public void removeAttribute(String key) throws Exception {
  146. properties.remove(key);
  147. }
  148. public void setAttribute(String key, String value) throws Exception {
  149. properties.set(key, value);
  150. }
  151. public void setAttribute(String key) throws Exception {
  152. properties.set(key);
  153. }
  154. public boolean hasAttribute(String attribute) {
  155. return properties.has(attribute);
  156. }
  157. public void setProperties(WikiPageProperties properties) {
  158. this.properties = properties;
  159. }
  160. public String getContent() throws Exception {
  161. return content;
  162. }
  163. public void setContent(String content) {
  164. this.content = content;
  165. }
  166. public String getHtml() throws Exception {
  167. return processHTMLWidgets(getContent(), wikiPage);
  168. }
  169. public String getHtml(WikiPage context) throws Exception {
  170. return processHTMLWidgets(getContent(), context);
  171. }
  172. public String getHeaderPageHtml() throws Exception {
  173. WikiPage header = wikiPage.getHeaderPage();
  174. return header == null ? "" : header.getData().getHtml();
  175. }
  176. public String getFooterPageHtml() throws Exception {
  177. WikiPage footer = wikiPage.getFooterPage();
  178. return footer == null ? "" : footer.getData().getHtml();
  179. }
  180. public String getVariable(String name) throws Exception {
  181. return getInitializedVariableRoot().getVariable(name);
  182. }
  183. public void addVariable(String name, String value) throws Exception {
  184. getInitializedVariableRoot().addVariable(name, value);
  185. }
  186. public void setLiterals(List<String> literals) {
  187. this.literals = literals;
  188. }
  189. private ParentWidget getInitializedVariableRoot() throws Exception {
  190. if (variableRoot == null) {
  191. initializeVariableRoot();
  192. }
  193. return variableRoot;
  194. }
  195. private void initializeVariableRoot() throws Exception {
  196. variableRoot = new TextIgnoringWidgetRoot(getContentWithVariableRenderTriggers(), wikiPage,
  197. literals, variableDefinitionWidgetBuilder
  198. );
  199. variableRoot.render();
  200. }
  201. private String getContentWithVariableRenderTriggers() throws Exception {
  202. StringBuffer content = new StringBuffer(getContent())
  203. .append(getVariableRenderTrigger(PATH_SEPARATOR))
  204. .append(getVariableRenderTrigger(COMMAND_PATTERN))
  205. .append(getVariableRenderTrigger(TEST_RUNNER));
  206. return content.toString();
  207. }
  208. private String getVariableRenderTrigger(String variableName) {
  209. return "\n#${" + variableName + "}";
  210. }
  211. private String processHTMLWidgets(String content, WikiPage context)
  212. throws Exception {
  213. ParentWidget root = new WidgetRoot(content, context,
  214. WidgetBuilder.htmlWidgetBuilder);
  215. return root.render();
  216. }
  217. public void setWikiPage(WikiPage page) {
  218. wikiPage = page;
  219. }
  220. public WikiPage getWikiPage() {
  221. return wikiPage;
  222. }
  223. public List<String> getClasspaths() throws Exception {
  224. return getTextOfWidgets(classpathWidgetBuilder);
  225. }
  226. public List<String> getXrefPages() throws Exception {
  227. return getTextOfWidgets(xrefWidgetBuilder);
  228. }
  229. private List<String> getTextOfWidgets(WidgetBuilder builder) throws Exception {
  230. ParentWidget root = new TextIgnoringWidgetRoot(getContent(), wikiPage,
  231. builder);
  232. List<WikiWidget> widgets = root.getChildren();
  233. List<String> values = new ArrayList<String>();
  234. for (WikiWidget widget : widgets) {
  235. if (widget instanceof WidgetWithTextArgument)
  236. values.add(((WidgetWithTextArgument) widget).getText());
  237. else
  238. widget.render();
  239. }
  240. return values;
  241. }
  242. public Set<VersionInfo> getVersions() {
  243. return versions;
  244. }
  245. public void addVersions(Collection<VersionInfo> newVersions) {
  246. versions.addAll(newVersions);
  247. }
  248. public boolean isEmpty() throws Exception {
  249. return getContent() == null || getContent().length() == 0;
  250. }
  251. }