View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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.module.model.reader;
35  
36  import info.magnolia.cms.util.ClasspathResourcesUtil;
37  import info.magnolia.module.ModuleManagementException;
38  import info.magnolia.module.model.ModuleDefinition;
39  import info.magnolia.module.model.Version;
40  import org.apache.commons.betwixt.io.BeanReader;
41  import org.apache.commons.io.IOUtils;
42  import org.jdom.DocType;
43  import org.jdom.Document;
44  import org.jdom.JDOMException;
45  import org.jdom.input.SAXBuilder;
46  import org.jdom.output.XMLOutputter;
47  import org.xml.sax.SAXException;
48  import org.xml.sax.SAXParseException;
49  
50  import java.beans.IntrospectionException;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.io.InputStreamReader;
54  import java.io.Reader;
55  import java.io.StringReader;
56  import java.io.StringWriter;
57  import java.net.URL;
58  import java.util.HashMap;
59  import java.util.Map;
60  import java.util.regex.Matcher;
61  import java.util.regex.Pattern;
62  import javax.inject.Singleton;
63  
64  /**
65   * This implementation of ModuleDefinitionReader uses Betwixt to read and convert module
66   * descriptor files.
67   *
68   * @author gjoseph
69   * @version $Revision: $ ($Author: $)
70   */
71  @Singleton
72  public class BetwixtModuleDefinitionReader implements ModuleDefinitionReader {
73      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BetwixtModuleDefinitionReader.class);
74  
75      private static final String DTD = "/info/magnolia/module/model/module.dtd";
76  
77      private final String dtdUrl;
78      private final BeanReader beanReader;
79  
80      public BetwixtModuleDefinitionReader() {
81          final URL dtd = getClass().getResource(DTD);
82          if (dtd == null) {
83              throw new IllegalStateException("DTD not found at " + DTD);
84          }
85          dtdUrl = dtd.toString();
86  
87          final BetwixtBindingStrategy bindingStrategy = new BetwixtBindingStrategy();
88          bindingStrategy.registerConverter(Version.class, new VersionConverter());
89  
90          beanReader = new BeanReader();
91          try {
92              beanReader.getXMLIntrospector().getConfiguration().setTypeBindingStrategy(bindingStrategy);
93              beanReader.setValidating(true);
94              beanReader.setErrorHandler(new ErrorHandler());
95              beanReader.registerBeanClass(ModuleDefinition.class);
96          } catch (IntrospectionException e) {
97              throw new RuntimeException(e); // TODO
98          }
99      }
100 
101     @Override
102     public Map<String, ModuleDefinition> readAll() throws ModuleManagementException {
103         final Map<String, ModuleDefinition> moduleDefinitions = new HashMap<String, ModuleDefinition>();
104 
105         final String[] defResources = findModuleDescriptors();
106 
107         for (final String resourcePath : defResources) {
108             log.debug("Parsing module file {}", resourcePath);
109             final ModuleDefinition def = readFromResource(resourcePath);
110             moduleDefinitions.put(def.getName(), def);
111         }
112         return moduleDefinitions;
113     }
114 
115     protected String[] findModuleDescriptors() {
116         return ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() {
117             @Override
118             public boolean accept(String name) {
119                 return name.startsWith("/META-INF/magnolia/") && name.endsWith(".xml");
120             }
121         });
122     }
123 
124     @Override
125     public ModuleDefinition read(Reader in) throws ModuleManagementException {
126         try {
127             final Reader replacedDtd = replaceDtd(in);
128             return (ModuleDefinition) beanReader.parse(replacedDtd);
129         } catch (IOException e) {
130             throw new ModuleManagementException("Can't read module definition file: " + e.getMessage(), e);
131         } catch (SAXException e) {
132             throw new ModuleManagementException(e.getMessage(), e);
133         }
134     }
135 
136     @Override
137     public ModuleDefinition readFromResource(String resourcePath) throws ModuleManagementException {
138         final InputStream resourceAsStream = getClass().getResourceAsStream(resourcePath);
139         if (resourceAsStream == null) {
140             throw new ModuleManagementException("Can't find module definition file at " + resourcePath);
141         }
142         final InputStreamReader reader = new InputStreamReader(resourceAsStream);
143         try {
144             return read(reader);
145         } finally {
146             try {
147                 reader.close();
148             } catch (IOException e) {
149                 log.error("Can't close input for " + resourcePath);
150             }
151         }
152     }
153 
154     /**
155      * @deprecated TODO very ugly hack to force documents to be validated against OUR DTD.
156      * We could use an EntityResolver, but it seems that with SAX, the documents will only be
157      * validated if they original have a doctype declaration.
158      * We could also use http://doctypechanger.sourceforge.net/
159      * ... or switch to XmlSchema.
160      */
161     private Reader replaceDtd(Reader reader) throws IOException {
162         String content = IOUtils.toString(reader);
163 
164         // remove doctype
165         Pattern pattern = Pattern.compile("<!DOCTYPE .*>");
166         Matcher matcher = pattern.matcher(content);
167         content = matcher.replaceFirst("");
168 
169         // set doctype to the dtd
170         try {
171             Document doc = new SAXBuilder().build(new StringReader(content));
172             doc.setDocType(new DocType("module", dtdUrl));
173             // write the xml to the string
174             XMLOutputter outputter = new XMLOutputter();
175             StringWriter writer = new StringWriter();
176             outputter.output(doc, writer);
177             final String replacedDtd = writer.toString();
178             return new StringReader(replacedDtd);
179         } catch (JDOMException e) {
180             throw new RuntimeException(e); // TODO
181         }
182     }
183 
184     private static class ErrorHandler implements org.xml.sax.ErrorHandler {
185         // TODO -- pass source (url, content, ...) for each parse ?
186         @Override
187         public void warning(SAXParseException e) throws SAXException {
188             log.warn("Warning on module definition " + getSaxParseExceptionMessage(e));
189         }
190 
191         @Override
192         public void error(SAXParseException e) throws SAXException {
193             throw new SAXException("Invalid module definition file, error " + getSaxParseExceptionMessage(e), e);
194         }
195 
196         @Override
197         public void fatalError(SAXParseException e) throws SAXException {
198             throw new SAXException("Invalid module definition file, fatal error " + getSaxParseExceptionMessage(e), e);
199         }
200     }
201 
202     private static String getSaxParseExceptionMessage(SAXParseException e) {
203         return "at line " + e.getLineNumber() + " column " + e.getColumnNumber() + ": " + e.getMessage();
204     }
205 }