View Javadoc

1   /**
2    * This file Copyright (c) 2007-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Contains utility methods to check workspace.xml.
51   *
52   * @version $Id$
53   */
54  public class WorkspaceXmlUtil {
55  
56      /**
57       * @deprecated since 4.5 - directly use {@link #getWorkspaceNamesMatching(String, String, boolean)} instead.
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       * Create and return list of workspaces names. If expectation is not null, all workspace with configs containing
70       * entries identified by the xPathExpression will be contained if they match the expectation. If expectation is
71       * null, all workspace names with configs that don't contain anything matching the xPathExpression will be returned.
72       *
73       * @param xPathExpression
74       *            the xpath expression
75       * @param expectation
76       *            value the matches of the xPathExpression should be compared with - special meaning of 'null': in that
77       *            case xPathExpression should not be contained!
78       *
79       * @return the list of workspace names
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              // new repo
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                  // check for the xPathExpression in wks
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         // must add the namespace and use it: there is no default namespace elsewise
123         xpath.addNamespace("ws", "file://workspace.xml");
124 
125         return xpath.selectNodes(doc);
126     }
127 }