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.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   * Contains utility methods to check workspace.xml.
53   *
54   * @version $Id$
55   */
56  public class WorkspaceXmlUtil {
57  
58      private final static Logger log = LoggerFactory.getLogger(WorkspaceXmlUtil.class);
59  
60      /**
61       * @deprecated since 4.5 - directly use {@link #getWorkspaceNamesMatching(String, String, boolean)} instead.
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       * Create and return list of workspaces names. If expectation is not null, all workspace with configs containing
74       * entries identified by the xPathExpression will be contained if they match the expectation. If expectation is
75       * null, all workspace names with configs that don't contain anything matching the xPathExpression will be returned.
76       *
77       * @param xPathExpression
78       *            the xpath expression
79       * @param expectation
80       *            value the matches of the xPathExpression should be compared with - special meaning of 'null': in that
81       *            case xPathExpression should not be contained!
82       *
83       * @return the list of workspace names
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              // new repo
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                 // check for the xPathExpression in wks
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         // must add the namespace and use it: there is no default namespace elsewise
130         xpath.addNamespace("ws", "file://workspace.xml");
131 
132         return xpath.selectNodes(doc);
133     }
134 }