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 fitnesse.util.StringUtil;
006: import java.util.*;
007: import java.io.Serializable;
008:
009: public class WikiPagePath implements Comparable, Cloneable,
010: Serializable {
011: private static final long serialVersionUID = 1L;
012:
013: public static final String ROOT = "_root";
014:
015: private LinkedList names = new LinkedList();
016:
017: public WikiPagePath() {
018: }
019:
020: protected Object clone() throws CloneNotSupportedException {
021: WikiPagePath clone = new WikiPagePath();
022: clone.names = (LinkedList) names.clone();
023: return clone;
024: }
025:
026: public WikiPagePath copy() {
027: try {
028: return (WikiPagePath) clone();
029: } catch (CloneNotSupportedException e) {
030: return null;
031: }
032: }
033:
034: public WikiPagePath(WikiPage page) throws Exception {
035: PageCrawler crawler = page.getPageCrawler();
036: while (!crawler.isRoot(page)) {
037: names.addFirst(page.getName());
038: page = page.getParent();
039: }
040: }
041:
042: public WikiPagePath(WikiPagePath path) {
043: for (WikiPagePath p = path; !p.isEmpty(); p = p.getRest())
044: addName(p.getFirst());
045: }
046:
047: private WikiPagePath(List names) {
048: this .names = new LinkedList(names);
049: }
050:
051: public String getFirst() {
052: return isEmpty() ? null : (String) names.get(0);
053: }
054:
055: public WikiPagePath addName(String name) {
056: names.add(name);
057: return this ;
058: }
059:
060: public WikiPagePath addNameToFront(String name) {
061: names.addFirst(name);
062: return this ;
063: }
064:
065: public WikiPagePath getRest() {
066: int size = names.size();
067: return (size <= 1) ? new WikiPagePath() : new WikiPagePath(
068: names.subList(1, size));
069: }
070:
071: public boolean isEmpty() {
072: return names.size() == 0;
073: }
074:
075: public String last() {
076: return (String) (names.size() == 0 ? null : names.get(names
077: .size() - 1));
078: }
079:
080: public List getNames() {
081: return names;
082: }
083:
084: public String toString() {
085: return "(" + StringUtil.join(names, ".") + ")";
086: }
087:
088: public void pop() {
089: if (names.size() > 0)
090: names.removeLast();
091: }
092:
093: public WikiPagePath append(WikiPagePath childPath) {
094: WikiPagePath newPath = new WikiPagePath(this );
095: for (WikiPagePath p = childPath; !p.isEmpty(); p = p.getRest())
096: newPath.addName(p.getFirst());
097: return newPath;
098: }
099:
100: public boolean isAbsolute() {
101: return (!isEmpty() && ROOT.equals(getFirst()));
102: }
103:
104: public void makeAbsolute() {
105: if (!isAbsolute())
106: addNameToFront(ROOT);
107: }
108:
109: public int hashCode() {
110: return StringUtil.join(names, "").hashCode();
111: }
112:
113: public WikiPagePath relativePath() {
114: if (isAbsolute())
115: return getRest();
116: else
117: return this ;
118: }
119:
120: public int compareTo(Object o) {
121: if (o instanceof WikiPagePath) {
122: WikiPagePath p = (WikiPagePath) o;
123: String compressedName = StringUtil.join(names, "");
124: String compressedArgumentName = StringUtil
125: .join(p.names, "");
126: return compressedName.compareTo(compressedArgumentName);
127: }
128: return 1; // we are greater because we are the right type.
129: }
130:
131: public boolean equals(Object o) {
132: if (o instanceof WikiPagePath) {
133: WikiPagePath that = (WikiPagePath) o;
134: return this .names.equals(that.names);
135: }
136: return false;
137: }
138:
139: public WikiPagePath parentPath() {
140: WikiPagePath parentPath = new WikiPagePath(this );
141: parentPath.pop();
142: return parentPath;
143: }
144:
145: public boolean startsWith(WikiPagePath that) {
146: if (that.names.size() > names.size())
147: return false;
148:
149: Iterator this Iterator = names.iterator();
150: Iterator thatIterator = that.names.iterator();
151: while (thatIterator.hasNext()) {
152: Object this Next = this Iterator.next();
153: Object thatNext = thatIterator.next();
154: if (!this Next.equals(thatNext))
155: return false;
156: }
157: return true;
158: }
159:
160: public WikiPagePath withNameAdded(String name) {
161: WikiPagePath path = new WikiPagePath(this);
162: path.addName(name);
163: return path;
164: }
165: }
|