1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.util;
35
36 import info.magnolia.cms.core.Path;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import org.jdom.Attribute;
44 import org.jdom.Document;
45 import org.jdom.JDOMException;
46 import org.jdom.input.SAXBuilder;
47 import org.jdom.xpath.XPath;
48
49
50
51
52
53
54 public class WorkspaceXmlUtil {
55
56
57
58
59 public static List<String> getWorkspaceNamesWithIndexer() {
60 return getWorkspaceNames("/Workspace/SearchIndex/param[@name='textFilterClasses']/@value",
61 ".*\\.jackrabbit\\.extractor\\..*");
62 }
63
64 public static List<String> getWorkspaceNamesMatching(String xPathExpression) {
65 return getWorkspaceNames(xPathExpression, ".*");
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public static List<String> getWorkspaceNames(String xPathExpression, String expectation) {
82 final List<String> names = new ArrayList<String>();
83 final File sourceDir = new File(Path.getAppRootDir() + "/repositories/magnolia/workspaces/");
84 File[] files = sourceDir.listFiles();
85 if (files == null) {
86
87 return names;
88 }
89 final SAXBuilder builder = new SAXBuilder();
90 for (File f : files) {
91 if (!f.isDirectory()) {
92 continue;
93 }
94 final File wks = new File(f, "workspace.xml");
95 if (!wks.exists() || !wks.canRead()) {
96 continue;
97 }
98 try {
99
100 final List<Attribute> list = getElementsFromXPath(builder.build(wks), xPathExpression);
101 if (expectation == null) {
102 if (list.size() == 0) {
103 names.add(wks.getAbsolutePath());
104 }
105 } else {
106 if (list.size() > 0 && list.get(0).getValue().matches(expectation)) {
107 names.add(wks.getAbsolutePath());
108 }
109 }
110 } catch (JDOMException e) {
111 throw new RuntimeException(e);
112 } catch (IOException e) {
113 throw new RuntimeException(e);
114 }
115 }
116 return names;
117 }
118
119 @SuppressWarnings("unchecked")
120 private static List<Attribute> getElementsFromXPath(Document doc, String xpathExpr) throws JDOMException {
121 final XPath xpath = XPath.newInstance(xpathExpr);
122
123 xpath.addNamespace("ws", "file://workspace.xml");
124
125 return xpath.selectNodes(doc);
126 }
127 }