01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.wiki;
04:
05: import java.util.Iterator;
06: import java.util.regex.Pattern;
07:
08: import fitnesse.wikitext.widgets.WikiWordWidget;
09:
10: public class PathParser {
11: public static final String PATH_SEPARATOR = ".";
12:
13: private static final Pattern wikiWordPattern = Pattern
14: .compile(WikiWordWidget.REGEXP);
15:
16: private WikiPagePath path;
17:
18: public static WikiPagePath parse(String pathName) {
19: return new PathParser().makePath(pathName);
20: }
21:
22: private WikiPagePath makePath(String pathName) {
23: path = new WikiPagePath();
24: if (pathName.equals("")) {
25: return path;
26: } else if (pathName.equals("root")
27: || pathName.equals(PATH_SEPARATOR)) {
28: path.addName(WikiPagePath.ROOT);
29: return path;
30: } else {
31: return parsePathName(pathName);
32: }
33: }
34:
35: private WikiPagePath parsePathName(String pathName) {
36: if (pathName.startsWith(PATH_SEPARATOR)) {
37: path.addName(WikiPagePath.ROOT);
38: pathName = pathName.substring(1);
39: }
40: String[] names = pathName.split("\\" + PATH_SEPARATOR);
41: for (int i = 0; i < names.length; i++) {
42: String pageName = names[i];
43: if (nameIsValid(pageName))
44: path.addName(pageName);
45: else
46: return null;
47: }
48: return path;
49: }
50:
51: private static boolean nameIsValid(String name) {
52: return wikiWordPattern.matcher(name).matches();
53: }
54:
55: public static String render(WikiPagePath path) {
56: StringBuffer renderedPath = new StringBuffer();
57: Iterator i = path.getNames().iterator();
58: if (i.hasNext()) {
59: String name = (String) i.next();
60: if (!name.equals(WikiPagePath.ROOT))
61: renderedPath.append(name);
62: else if (!i.hasNext())
63: renderedPath.append(PATH_SEPARATOR);
64: }
65: while (i.hasNext()) {
66: renderedPath.append(PATH_SEPARATOR).append(i.next());
67: }
68: return renderedPath.toString();
69: }
70: }
|