View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.cms.util;
35  
36  import info.magnolia.cms.core.FileSystemHelper;
37  import info.magnolia.cms.core.SystemProperty;
38  import info.magnolia.objectfactory.Components;
39  
40  import java.io.ByteArrayInputStream;
41  import java.io.IOException;
42  import java.io.InputStream;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.Map;
46  
47  import javax.xml.parsers.DocumentBuilder;
48  import javax.xml.parsers.DocumentBuilderFactory;
49  import javax.xml.parsers.ParserConfigurationException;
50  
51  import org.apache.commons.io.IOUtils;
52  import org.apache.commons.lang3.StringUtils;
53  import org.jdom.JDOMException;
54  import org.jdom.input.SAXBuilder;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  import org.w3c.dom.Document;
58  import org.xml.sax.EntityResolver;
59  import org.xml.sax.InputSource;
60  import org.xml.sax.SAXException;
61  
62  /**
63   * Util used to process config files.
64   * @deprecated since 5.5.3, please see methods description to find out what to use instead
65   */
66  @Deprecated
67  public class ConfigUtil {
68      private static final Logger log = LoggerFactory.getLogger(ConfigUtil.class);
69  
70      /**
71       * EntityResolver using a Map to resources.
72       * @deprecated since 5.5.3 without replacement
73       */
74      public static final class MapDTDEntityResolver implements EntityResolver {
75  
76          private final Map<String, String> dtds;
77  
78          public MapDTDEntityResolver(Map<String, String> dtds) {
79              this.dtds = dtds;
80          }
81  
82          @Override
83          public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
84              String key = StringUtils.substringAfterLast(systemId, "/");
85              if (dtds.containsKey(key)) {
86                  Class<? extends MapDTDEntityResolver> clazz = getClass();
87                  InputStream in = clazz.getResourceAsStream(dtds.get(key));
88                  if (in == null) {
89                      log.error("Could not find [{}]. Used [{}] class loader in the search, parsed without DTD.", systemId, clazz.getClassLoader());
90                      return new InputSource(new ByteArrayInputStream(new byte[]{}));
91                  }
92                  return new InputSource(in);
93              }
94              return new InputSource(new ByteArrayInputStream(new byte[]{}));
95          }
96      }
97  
98      /**
99       * @deprecated since 5.5.3, use {@link FileSystemHelper#getTokenizedConfigFile(String)} instead.
100      */
101     public static String getTokenizedConfigFile(String fileName) throws IOException {
102         return Components.getComponent(FileSystemHelper.class).getTokenizedConfigFile(fileName);
103     }
104 
105     /**
106      * Replace tokens in a string.
107      * @deprecated since 5.5.3 without replacement
108      */
109     public static String replaceTokens(String config) throws IOException {
110         for (Iterator iter = SystemProperty.getProperties().keySet().iterator(); iter.hasNext(); ) {
111             String key = (String) iter.next();
112             config = StringUtils.replace(config, "${" + key + "}", SystemProperty.getProperty(key, ""));
113         }
114         return config;
115     }
116 
117     /**
118      * Convert the string to a JDOM Document.
119      * @deprecated since 5.5.3 without replacement
120      */
121     public static org.jdom.Document string2JDOM(String xml) throws JDOMException, IOException {
122         SAXBuilder builder = new SAXBuilder();
123         builder.setValidation(false);
124         // when dtds are not specified, prevent from loading external entities
125             builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
126             builder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
127             builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
128             builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
129 
130         builder.setEntityResolver(new MapDTDEntityResolver(Collections.EMPTY_MAP));
131         return builder.build(IOUtils.toInputStream(xml));
132     }
133 
134     /**
135      * Uses a map to find dtds in the resources.
136      * @deprecated since 5.5.3 without replacement
137      */
138     public static Document string2DOM(String xml, final Map<String, String> dtds) throws ParserConfigurationException, SAXException, IOException {
139         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
140         dbf.setValidating(false);
141         // when dtds are not specified, prevent from loading external entities
142         if (dtds.size() == 0) {
143             dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
144             dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
145             dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
146             dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
147             dbf.setNamespaceAware(true);
148         }
149         DocumentBuilder builder = dbf.newDocumentBuilder();
150         builder.setEntityResolver(new MapDTDEntityResolver(dtds));
151         return builder.parse(IOUtils.toInputStream(xml));
152     }
153 
154 }