1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
66 @Deprecated
67 public class ConfigUtil {
68 private static final Logger log = LoggerFactory.getLogger(ConfigUtil.class);
69
70
71
72
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
100
101 public static String getTokenizedConfigFile(String fileName) throws IOException {
102 return Components.getComponent(FileSystemHelper.class).getTokenizedConfigFile(fileName);
103 }
104
105
106
107
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
119
120
121 public static org.jdom.Document string2JDOM(String xml) throws JDOMException, IOException {
122 SAXBuilder builder = new SAXBuilder();
123 builder.setValidation(false);
124
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
136
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
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 }