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.components;
04:
05: import fitnesse.wiki.*;
06: import fitnesse.util.Wildcard;
07: import java.util.*;
08: import java.io.File;
09:
10: public class ClassPathBuilder extends InheritedItemBuilder {
11: public String getClasspath(WikiPage page) throws Exception {
12: List paths = getInheritedPathElements(page, new HashSet(89));
13: String classPathString = createClassPathString(paths,
14: getPathSeparator(page));
15: return classPathString;
16: }
17:
18: public String getPathSeparator(WikiPage page) throws Exception {
19: String separator = page.getData().getVariable("PATH_SEPARATOR");
20: if (separator == null)
21: separator = (String) System.getProperties().get(
22: "path.separator");
23:
24: return separator;
25: }
26:
27: public List getInheritedPathElements(WikiPage page, Set visitedPages)
28: throws Exception {
29: return getInheritedItems(page, visitedPages);
30: }
31:
32: public String createClassPathString(List paths, String separator) {
33: if (paths.isEmpty())
34: return "defaultPath";
35: StringBuffer pathsString = new StringBuffer();
36:
37: paths = expandWildcards(paths);
38: Set addedPaths = new HashSet();
39: for (Iterator i = paths.iterator(); i.hasNext();) {
40: String path = (String) i.next();
41: if (path.matches(".*\\s.*") && path.indexOf("\"") == -1)
42: path = "\"" + path + "\"";
43:
44: if (!addedPaths.contains(path)) {
45: addedPaths.add(path);
46: addSeparatorIfNecessary(pathsString, separator);
47: pathsString.append(path);
48: }
49: }
50: return pathsString.toString();
51: }
52:
53: private List expandWildcards(List paths) {
54: List newPaths = new ArrayList();
55: for (Iterator iterator = paths.iterator(); iterator.hasNext();) {
56: String path = (String) iterator.next();
57: File file = new File(path);
58: File dir = new File(file.getAbsolutePath()).getParentFile();
59: if (file.getName().indexOf('*') != -1 && dir.exists()) {
60: File[] files = dir.listFiles(new Wildcard(file
61: .getName()));
62: for (int i = 0; i < files.length; i++)
63: newPaths.add(files[i].getPath());
64: } else
65: newPaths.add(path);
66: }
67:
68: return newPaths;
69: }
70:
71: private void addSeparatorIfNecessary(StringBuffer pathsString,
72: String separator) {
73: if (pathsString.length() > 0)
74: pathsString.append(separator);
75: }
76:
77: protected List getItemsFromPage(WikiPage page) throws Exception {
78: return page.getData().getClasspaths();
79: }
80: }
|