PageData.java :  » Testing » StoryTestIQ » fitnesse » wiki » Java Open Source

Java Open Source » Testing » StoryTestIQ 
StoryTestIQ » fitnesse » wiki » PageData.java
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
package fitnesse.wiki;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import fitnesse.responders.run.SuiteResponder;
import fitnesse.wikitext.WidgetBuilder;
import fitnesse.wikitext.widgets.ClasspathWidget;
import fitnesse.wikitext.widgets.FixtureWidget;
import fitnesse.wikitext.widgets.TextIgnoringWidgetRoot;
import fitnesse.wikitext.widgets.VariableDefinitionWidget;
import fitnesse.wikitext.widgets.WidgetRoot;
import fitnesse.wikitext.widgets.WidgetWithTextArgument;
import fitnesse.wikitext.widgets.XRefWidget;

public class PageData implements Serializable {
  private static final long serialVersionUID = 1L;

  public static WidgetBuilder classpathWidgetBuilder = new WidgetBuilder(
      new Class[] { ClasspathWidget.class });

  public static WidgetBuilder fixtureWidgetBuilder = new WidgetBuilder(new Class[] { FixtureWidget.class });

  public static WidgetBuilder xrefWidgetBuilder = new WidgetBuilder(new Class[] { XRefWidget.class });

  public static WidgetBuilder variableDefinitionWidgetBuilder = new WidgetBuilder(
      new Class[] { VariableDefinitionWidget.class });

  private transient WikiPage wikiPage;

  private String content;

  private WikiPageProperties properties = new WikiPageProperties();

  private Set versions;

  private WidgetRoot variableRoot;

  public static SimpleDateFormat makeVersionTimeFormat() {
    // SimpleDateFormat is not thread safe, so we need to create each
    // instance independently.
    return new SimpleDateFormat("yyyyMMddHHmmss");
  }

  public PageData(WikiPage page) throws Exception {
    wikiPage = page;
    initializeAttributes();
    versions = new HashSet();
  }

  public PageData(WikiPage page, String content) throws Exception {
    this(page);
    setContent(content);
  }

  public PageData(PageData data) throws Exception {
    this(data.getWikiPage());
    wikiPage = data.wikiPage;
    content = data.content;
    properties = new WikiPageProperties(data.properties);
    versions.addAll(data.versions);
  }

  public String getStringOfAllAttributes() {
    return properties.toString();
  }

  public void initializeAttributes() throws Exception {
    properties.set(WikiPage.ACTION_EDIT, "true");
    properties.set(WikiPage.ACTION_PROPERTIES, "true");
    properties.set(WikiPage.ACTION_REFACTOR, "true");

    // Set properties for FitNesse test pages based on name
    final String pageName = wikiPage.getName();
    if (pageName.startsWith(WikiPage.ACTION_TEST))
      properties.set(WikiPage.ACTION_TEST, "true");
    if (pageName.startsWith(WikiPage.ACTION_SUITE) && !pageName.equals(SuiteResponder.SUITE_SETUP_NAME)
        && !pageName.equals(SuiteResponder.SUITE_TEARDOWN_NAME)) {
      properties.set(WikiPage.ACTION_SUITE, "true");
    }
  }

  public WikiPageProperties getProperties() throws Exception {
    return properties;
  }

  public String getAttribute(String key) throws Exception {
    return properties.get(key);
  }

  public void removeAttribute(String key) throws Exception {
    properties.remove(key);
  }

  public void setAttribute(String key, String value) throws Exception {
    properties.set(key, value);
  }

  public void setAttribute(String key) throws Exception {
    properties.set(key);
  }

  public boolean hasAttribute(String attribute) throws Exception {
    return properties.has(attribute);
  }

  public void setProperties(WikiPageProperties properties) {
    this.properties = properties;
  }

  public String getContent() throws Exception {
    return content;
  }

  public void setContent(String content) throws Exception {
    this.content = content;
  }

  public String getHtml() throws Exception {
    return processHTMLWidgets(getContent(), wikiPage);
  }

  public String getHtml(WikiPage context) throws Exception {
    return processHTMLWidgets(getContent(), context);
  }

  public String getVariable(String name) throws Exception {
    if (variableRoot == null) {
      variableRoot = new TextIgnoringWidgetRoot(getContent(), wikiPage, variableDefinitionWidgetBuilder);
      variableRoot.render();
    }
    return variableRoot.getVariable(name);
  }

  private String processHTMLWidgets(String content, WikiPage context) throws Exception {
    WidgetRoot root = new WidgetRoot(content, context, WidgetBuilder.htmlWidgetBuilder);
    return root.render();
  }

  public void setWikiPage(WikiPage page) {
    wikiPage = page;
  }

  public WikiPage getWikiPage() {
    return wikiPage;
  }

  public List getClasspaths() throws Exception {
    return getTextOfWidgets(classpathWidgetBuilder);
  }

  public List getFixtureNames() throws Exception {
    return getTextOfWidgets(fixtureWidgetBuilder);
  }

  public List getXrefPages() throws Exception {
    return getTextOfWidgets(xrefWidgetBuilder);
  }

  private List getTextOfWidgets(WidgetBuilder builder) throws Exception {
    WidgetRoot root = new TextIgnoringWidgetRoot(getContent(), wikiPage, builder);
    List widgets = root.getChildren();
    List values = new ArrayList();
    for (Iterator iterator = widgets.iterator(); iterator.hasNext();) {
      WidgetWithTextArgument widget = (WidgetWithTextArgument) iterator.next();
      values.add(widget.getText());
    }
    return values;
  }

  public Set getVersions() {
    return versions;
  }

  public void addVersions(Collection newVersions) {
    versions.addAll(newVersions);
  }

  public Date getLastModificationTime() throws Exception {
    String dateStr = (String) properties.get("LastModified");
    if (dateStr == null)
      return new Date();
    else
      return makeVersionTimeFormat().parse(dateStr);
  }

  public void setLastModificationTime(Date date) {
    properties.set("LastModified", makeVersionTimeFormat().format(date));
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.