View Javadoc

1   /**
2    * This file Copyright (c) 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.repository.definition;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import org.apache.commons.lang.BooleanUtils;
43  import org.jdom.Document;
44  import org.jdom.Element;
45  import org.jdom.JDOMException;
46  import org.jdom.input.SAXBuilder;
47  
48  /**
49   * Reads InputStream created out of repositories.xml into pojos using JDom.
50   *
51   * @version $Id$
52   */
53  public class RepositoryMappingDefinitionReader {
54  
55      /**
56       * repository element string.
57       */
58      private static final String ELEMENT_REPOSITORY = "Repository";
59  
60      private static final String ELEMENT_REPOSITORYMAPPING = "RepositoryMapping";
61  
62      private static final String ELEMENT_PARAM = "param";
63  
64      private static final String ELEMENT_WORKSPACE = "workspace";
65  
66      /**
67       * Attribute names.
68       */
69      private static final String ATTRIBUTE_NAME = "name";
70  
71      private static final String ATTRIBUTE_LOAD_ON_STARTUP = "loadOnStartup";
72  
73      private static final String ATTRIBUTE_PROVIDER = "provider";
74  
75      private static final String ATTRIBUTE_VALUE = "value";
76  
77      private static final String ATTRIBUTE_REPOSITORY_NAME = "repositoryName";
78  
79      private static final String ATTRIBUTE_WORKSPACE_NAME = "workspaceName";
80  
81      private static final String DEFAULT_WORKSPACE_NAME = "default";
82  
83  
84      /**
85       * Load repository mappings and params using repositories.xml.
86       */
87      public RepositoryMappingDefinition read(InputStream stream) throws JDOMException, IOException {
88          SAXBuilder builder = new SAXBuilder();
89          Document document = builder.build(stream);
90  
91          Element root = document.getRootElement();
92          RepositoryMappingDefinition definition = new RepositoryMappingDefinition();
93          parseRepositoryMapping(root, definition);
94          parseRepositories(root, definition);
95  
96          return definition;
97      }
98  
99      /**
100      * Parses <Repository>.
101      */
102     private void parseRepositories(Element root, RepositoryMappingDefinition definition) {
103         @SuppressWarnings("unchecked")
104         List<Element> repositoryElements = root.getChildren(ELEMENT_REPOSITORY);
105         for (Element repositoryElement : repositoryElements) {
106             parseRepository(repositoryElement, definition);
107         }
108     }
109 
110     private void parseRepository(Element repositoryElement, RepositoryMappingDefinition definition) {
111 
112         String name = repositoryElement.getAttributeValue(ATTRIBUTE_NAME);
113         String loadOnStartup = repositoryElement.getAttributeValue(ATTRIBUTE_LOAD_ON_STARTUP);
114         String provider = repositoryElement.getAttributeValue(ATTRIBUTE_PROVIDER);
115 
116         RepositoryDefinition repository = new RepositoryDefinition();
117         repository.setName(name);
118         repository.setProvider(provider);
119         repository.setLoadOnStartup(BooleanUtils.toBoolean(loadOnStartup));
120 
121         // parse repository parameters
122         Map<String, String> parameters = new HashMap<String, String>();
123         for (Object element : repositoryElement.getChildren(ELEMENT_PARAM)) {
124             Element parameterElement = (Element) element;
125             String parameterName = parameterElement.getAttributeValue(ATTRIBUTE_NAME);
126             String value = parameterElement.getAttributeValue(ATTRIBUTE_VALUE);
127             parameters.put(parameterName, value);
128         }
129         // TODO : it looks like params here are not interpolated
130         repository.setParameters(parameters);
131 
132         // parse workspace names
133         @SuppressWarnings("unchecked")
134         List<Element> workspaces = repositoryElement.getChildren(ELEMENT_WORKSPACE);
135         if (workspaces == null || workspaces.isEmpty()) {
136             // TODO this does not belong in the parser
137             repository.addWorkspace(DEFAULT_WORKSPACE_NAME);
138         } else {
139             for (Element element : workspaces) {
140                 String workspaceName = element.getAttributeValue(ATTRIBUTE_NAME);
141                 repository.addWorkspace(workspaceName);
142             }
143         }
144 
145         definition.addRepository(repository);
146     }
147 
148     /**
149      * Parses &lt;RepositoryMapping&gt;.
150      */
151     private void parseRepositoryMapping(Element root, RepositoryMappingDefinition definition) {
152         Element repositoryMappingElement = root.getChild(ELEMENT_REPOSITORYMAPPING);
153         @SuppressWarnings("unchecked")
154         List<Element> children = repositoryMappingElement.getChildren();
155         for (Element child : children) {
156             definition.addMapping(
157                     child.getAttributeValue(ATTRIBUTE_NAME),
158                     child.getAttributeValue(ATTRIBUTE_REPOSITORY_NAME),
159                     child.getAttributeValue(ATTRIBUTE_WORKSPACE_NAME));
160         }
161     }
162 
163 }