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