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.Iterator;
006: import java.util.List;
007:
008: import fitnesse.components.FitNesseTraversalListener;
009:
010: public class PageCrawlerImpl implements PageCrawler {
011: private PageCrawlerDeadEndStrategy deadEndStrategy;
012:
013: protected PageCrawlerImpl() {
014: }
015:
016: public WikiPage getPage(WikiPage context, WikiPagePath path)
017: throws Exception {
018: if (path == null)
019: return null;
020:
021: if (path.isEmpty())
022: return context;
023:
024: String firstPathElement = path.getFirst();
025: WikiPagePath restOfPath = path.getRest();
026: if (firstPathElement.equals(WikiPagePath.ROOT))
027: return getPage(getRoot(context), restOfPath);
028:
029: WikiPage childPage = context.getChildPage(firstPathElement);
030: if (childPage != null)
031: return getPage(childPage, restOfPath);
032: else
033: return getPageAfterDeadEnd(context, firstPathElement,
034: restOfPath);
035: }
036:
037: protected WikiPage getPageAfterDeadEnd(WikiPage context,
038: String first, WikiPagePath rest) throws Exception {
039: rest.addNameToFront(first);
040: if (deadEndStrategy != null)
041: return deadEndStrategy.getPageAfterDeadEnd(context, rest,
042: this );
043: else
044: return null;
045: }
046:
047: public void setDeadEndStrategy(PageCrawlerDeadEndStrategy strategy) {
048: deadEndStrategy = strategy;
049: }
050:
051: public boolean pageExists(WikiPage context, WikiPagePath path)
052: throws Exception {
053: return getPage(context, path) != null;
054: }
055:
056: public WikiPagePath getFullPathOfChild(WikiPage parent,
057: WikiPagePath childPath) throws Exception {
058: WikiPagePath fullPathOfChild;
059: if (childPath.isAbsolute())
060: fullPathOfChild = childPath.relativePath();
061: else {
062: WikiPagePath absolutePathOfParent = new WikiPagePath(parent);
063: fullPathOfChild = absolutePathOfParent.append(childPath);
064: }
065: return fullPathOfChild;
066: }
067:
068: public WikiPagePath getFullPath(WikiPage page) throws Exception {
069: return new WikiPagePath(page);
070: }
071:
072: public WikiPage addPage(WikiPage context, WikiPagePath path,
073: String content) throws Exception {
074: WikiPage page = addPage(context, path);
075: if (page != null) {
076: PageData data = new PageData(page);
077: data.setContent(content);
078: page.commit(data);
079: }
080: return page;
081: }
082:
083: public WikiPage addPage(WikiPage context, WikiPagePath path)
084: throws Exception {
085: return getOrMakePage(context, path.getNames());
086: }
087:
088: private WikiPage getOrMakePage(WikiPage context, List namePieces)
089: throws Exception {
090: String first = (String) namePieces.get(0);
091: List rest = namePieces.subList(1, namePieces.size());
092: WikiPage current;
093: if (context.getChildPage(first) == null)
094: current = context.addChildPage(first);
095: else
096: current = context.getChildPage(first);
097: if (rest.size() == 0)
098: return current;
099: return getOrMakePage(current, rest);
100: }
101:
102: public String getRelativeName(WikiPage base, WikiPage page)
103: throws Exception {
104: StringBuffer qualName = new StringBuffer();
105: for (WikiPage p = page; !isRoot(p) && p != base; p = p
106: .getParent()) {
107: if (p != page)
108: qualName.insert(0, ".");
109: qualName.insert(0, p.getName());
110: }
111: return qualName.toString();
112: }
113:
114: // TODO this doesn't belong here
115: public static WikiPage getInheritedPage(String pageName,
116: WikiPage context) throws Exception {
117: List ancestors = WikiPageUtil.getAncestorsStartingWith(context);
118: for (Iterator iterator = ancestors.iterator(); iterator
119: .hasNext();) {
120: WikiPage ancestor = (WikiPage) iterator.next();
121: WikiPage namedPage = ancestor.getChildPage(pageName);
122: if (namedPage != null)
123: return namedPage;
124: }
125: return null;
126: }
127:
128: public boolean isRoot(WikiPage page) throws Exception {
129: WikiPage parent = page.getParent();
130: return parent == null || parent == page;
131: }
132:
133: public WikiPage getRoot(WikiPage page) throws Exception {
134: if (isRoot(page))
135: return page;
136: else
137: return getRoot(page.getParent());
138: }
139:
140: public void traverse(WikiPage context,
141: FitNesseTraversalListener listener) throws Exception {
142: if (context.getClass() == SymbolicPage.class)
143: return;
144: listener.processPage(context);
145: List children = context.getChildren();
146: for (Iterator iterator = children.iterator(); iterator
147: .hasNext();) {
148: WikiPage wikiPage = (WikiPage) iterator.next();
149: traverse(wikiPage, listener);
150: }
151: }
152: }
|