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: import java.io.File;
007: import fitnesse.util.FileUtil;
008:
009: public abstract class BaseWikiPage implements WikiPage {
010: protected String name;
011: protected WikiPage parent;
012:
013: protected BaseWikiPage(String name, WikiPage parent) {
014: this .name = name;
015: this .parent = parent;
016: }
017:
018: public String getName() throws Exception {
019: return name;
020: }
021:
022: public PageCrawler getPageCrawler() {
023: return new PageCrawlerImpl();
024: }
025:
026: public WikiPage getParent() throws Exception {
027: return parent == null ? this : parent;
028: }
029:
030: protected abstract List<WikiPage> getNormalChildren()
031: throws Exception;
032:
033: public List<WikiPage> getChildren() throws Exception {
034: List<WikiPage> children = getNormalChildren();
035: WikiPageProperties props = getData().getProperties();
036: WikiPageProperty symLinksProperty = props
037: .getProperty(SymbolicPage.PROPERTY_NAME);
038: if (symLinksProperty != null) {
039: for (Iterator iterator = symLinksProperty.keySet()
040: .iterator(); iterator.hasNext();) {
041: String linkName = (String) iterator.next();
042: WikiPage page = createSymbolicPage(symLinksProperty,
043: linkName);
044: if (page != null)
045: children.add(page);
046: }
047: }
048: return children;
049: }
050:
051: private WikiPage createSymbolicPage(
052: WikiPageProperty symLinkProperty, String linkName)
053: throws Exception {
054: if (symLinkProperty == null)
055: return null;
056: String linkPath = symLinkProperty.get(linkName);
057: if (linkPath == null)
058: return null;
059: if (linkPath.startsWith("file://"))
060: return createExternalSymbolicLink(linkPath, linkName);
061: else
062: return createInternalSymbolicPage(linkPath, linkName);
063: }
064:
065: private WikiPage createExternalSymbolicLink(String linkPath,
066: String linkName) throws Exception {
067: String fullPagePath = linkPath.substring(7);
068: File file = new File(fullPagePath);
069: File parentDirectory = file.getParentFile();
070: if (parentDirectory.exists()) {
071: if (!file.exists())
072: FileUtil.makeDir(file.getPath());
073: if (file.isDirectory()) {
074: WikiPage externalRoot = FileSystemPage.makeRoot(
075: parentDirectory.getPath(), file.getName());
076: return new SymbolicPage(linkName, externalRoot, this );
077: }
078: }
079: return null;
080: }
081:
082: private WikiPage createInternalSymbolicPage(String linkPath,
083: String linkName) throws Exception {
084: WikiPagePath path = PathParser.parse(linkPath);
085: PageCrawler crawler = getPageCrawler();
086: WikiPage page = crawler.getPage(crawler.getRoot(this ), path);
087: if (page != null)
088: page = new SymbolicPage(linkName, page, this );
089: return page;
090: }
091:
092: protected abstract WikiPage getNormalChildPage(String name)
093: throws Exception;
094:
095: public WikiPage getChildPage(String name) throws Exception {
096: WikiPage page = getNormalChildPage(name);
097: if (page == null)
098: page = createSymbolicPage(getData().getProperties()
099: .getProperty(SymbolicPage.PROPERTY_NAME), name);
100: return page;
101: }
102:
103: public String toString() {
104: return this .getClass().getName() + ": " + name;
105: }
106:
107: public int compareTo(Object o) {
108: try {
109: return getName().compareTo(((WikiPage) o).getName());
110: } catch (Exception e) {
111: return 0;
112: }
113: }
114:
115: public boolean equals(Object o) {
116: if (this == o)
117: return true;
118: if (!(o instanceof WikiPage))
119: return false;
120: try {
121: PageCrawler crawler = getPageCrawler();
122: return crawler.getFullPath(this ).equals(
123: crawler.getFullPath(((WikiPage) o)));
124: } catch (Exception e) {
125: return false;
126: }
127: }
128:
129: public int hashCode() {
130: try {
131: return getPageCrawler().getFullPath(this ).hashCode();
132: } catch (Exception e) {
133: return 0;
134: }
135: }
136: }
|