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.util.*;
006:
007: public class InMemoryPage extends CommitingPage {
008: private static final long serialVersionUID = 1L;
009:
010: protected static final String currentVersionName = "current_version";
011:
012: protected Map versions = new HashMap();
013:
014: protected Map children = new HashMap();
015:
016: protected InMemoryPage(String name, String content, WikiPage parent)
017: throws Exception {
018: super (name, parent);
019: addExtention(new VirtualCouplingExtension(this ));
020: versions.put(currentVersionName, new PageData(this , content));
021: }
022:
023: public WikiPage addChildPage(String name) throws Exception {
024: WikiPage page = createChildPage(name);
025: children.put(name, page);
026: return page;
027: }
028:
029: public static WikiPage makeRoot(String name) throws Exception {
030: InMemoryPage root = new InMemoryPage(name, "", null);
031: return root;
032: }
033:
034: public static WikiPage makeRoot(Properties props) throws Exception {
035: return makeRoot(props.getProperty("FitNesseRoot",
036: "FitNesseRoot"));
037: }
038:
039: protected WikiPage createChildPage(String name) throws Exception {
040: BaseWikiPage newPage = new InMemoryPage(name, "", this );
041: children.put(newPage.getName(), newPage);
042: return newPage;
043: }
044:
045: public void removeChildPage(String name) throws Exception {
046: children.remove(name);
047: }
048:
049: public boolean hasChildPage(String pageName) {
050: return children.containsKey(pageName);
051: }
052:
053: protected VersionInfo makeVersion() throws Exception {
054: PageData current = getDataVersion(currentVersionName);
055:
056: String name = String.valueOf(VersionInfo.nextId());
057: VersionInfo version = makeVersionInfo(current, name);
058: versions.put(version.getName(), current);
059: return version;
060: }
061:
062: protected WikiPage getNormalChildPage(String name) throws Exception {
063: return (WikiPage) children.get(name);
064: }
065:
066: public List getNormalChildren() throws Exception {
067: return new LinkedList(children.values());
068: }
069:
070: public PageData getData() throws Exception {
071: return new PageData(getDataVersion(currentVersionName));
072: }
073:
074: public void doCommit(PageData newData) throws Exception {
075: newData.setWikiPage(this );
076: newData.setLastModificationTime(new Date());
077: versions.put(currentVersionName, newData);
078: }
079:
080: public PageData getDataVersion(String versionName) throws Exception {
081: PageData version = (PageData) versions.get(versionName);
082: if (version == null)
083: throw new NoSuchVersionException("There is no version '"
084: + versionName + "'");
085:
086: Set names = new HashSet(versions.keySet());
087: names.remove(currentVersionName);
088: List pageVersions = new LinkedList();
089: for (Iterator iterator = names.iterator(); iterator.hasNext();) {
090: String name = (String) iterator.next();
091: PageData data = (PageData) versions.get(name);
092: pageVersions.add(makeVersionInfo(data, name));
093: }
094: version.addVersions(pageVersions);
095: return new PageData(version);
096: }
097:
098: public int numberOfVersions() {
099: return versions.size() - 1;
100: }
101:
102: protected VersionInfo makeVersionInfo(PageData current, String name)
103: throws Exception {
104: String author = current
105: .getAttribute(WikiPage.LAST_MODIFYING_USER);
106: if (author == null)
107: author = "";
108: Date date = current.getLastModificationTime();
109: VersionInfo version = new VersionInfo(name, author, date);
110: return version;
111: }
112: }
|