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.SystemProperty;
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 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56 public class WorkspaceXmlUtil {
57
58 private final static Logger log = LoggerFactory.getLogger(WorkspaceXmlUtil.class);
59
60
61
62
63 public static List<String> getWorkspaceNamesWithIndexer() {
64 return getWorkspaceNames("/Workspace/SearchIndex/param[@name='textFilterClasses']/@value",
65 ".*\\.jackrabbit\\.extractor\\..*");
66 }
67
68 public static List<String> getWorkspaceNamesMatching(String xPathExpression) {
69 return getWorkspaceNames(xPathExpression, ".*");
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public static List<String> getWorkspaceNames(String xPathExpression, String expectation) {
86 final List<String> names = new ArrayList<String>();
87 final String dir = SystemProperty.getProperty(SystemProperty.MAGNOLIA_REPOSITORIES_HOME) + "/magnolia/workspaces/";
88 log.debug("Checking directory " + dir);
89 final File sourceDir = new File(dir);
90 File[] files = sourceDir.listFiles();
91 if (files == null) {
92
93 return names;
94 }
95 final SAXBuilder builder = new SAXBuilder();
96 for (File f : files) {
97 if (!f.isDirectory()) {
98 continue;
99 }
100 final File wks = new File(f, "workspace.xml");
101 if (!wks.exists() || !wks.canRead()) {
102 continue;
103 }
104 try {
105 log.debug("Analysing file " + wks.getAbsolutePath());
106
107 final List<Attribute> list = getElementsFromXPath(builder.build(wks), xPathExpression);
108 if (expectation == null) {
109 if (list.size() == 0) {
110 names.add(wks.getAbsolutePath());
111 }
112 } else {
113 if (list.size() > 0 && list.get(0).getValue().matches(expectation)) {
114 names.add(wks.getAbsolutePath());
115 }
116 }
117 } catch (JDOMException e) {
118 throw new RuntimeException(e);
119 } catch (IOException e) {
120 throw new RuntimeException(e);
121 }
122 }
123 return names;
124 }
125
126 @SuppressWarnings("unchecked")
127 private static List<Attribute> getElementsFromXPath(Document doc, String xpathExpr) throws JDOMException {
128 final XPath xpath = XPath.newInstance(xpathExpr);
129
130 xpath.addNamespace("ws", "file://workspace.xml");
131
132 return xpath.selectNodes(doc);
133 }
134 }