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.SystemProperty;
37 import info.magnolia.cms.core.Path;
38
39 import java.io.File;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.Map;
49
50 import javax.xml.parsers.DocumentBuilder;
51 import javax.xml.parsers.DocumentBuilderFactory;
52 import javax.xml.parsers.ParserConfigurationException;
53
54 import org.apache.commons.io.IOUtils;
55 import org.apache.commons.lang.StringUtils;
56 import org.jdom.JDOMException;
57 import org.jdom.input.SAXBuilder;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60 import org.w3c.dom.Document;
61 import org.xml.sax.EntityResolver;
62 import org.xml.sax.InputSource;
63 import org.xml.sax.SAXException;
64
65
66
67
68
69
70 public class ConfigUtil {
71 private static final Logger log = LoggerFactory.getLogger(ConfigUtil.class);
72
73
74
75
76
77
78 public static final class MapDTDEntityResolver implements EntityResolver {
79
80 private final Map<String, String> dtds;
81
82 public MapDTDEntityResolver(Map<String, String> dtds) {
83 this.dtds = dtds;
84 }
85
86 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
87 String key = StringUtils.substringAfterLast(systemId, "/");
88 if (dtds.containsKey(key)) {
89 Class<? extends MapDTDEntityResolver> clazz = getClass();
90 InputStream in = clazz.getResourceAsStream(dtds.get(key));
91 if (in == null) {
92 log.error("Could not find [" + systemId + "]. Used ["
93 + clazz.getClassLoader()
94 + "] class loader in the search.");
95 return null;
96 } else {
97 return new InputSource(in);
98 }
99 } else {
100 return null;
101 }
102 }
103 }
104
105 public static String getTokenizedConfigFile(String fileName) throws IOException {
106 final InputStream stream = getConfigFile(fileName);
107 if (stream == null) {
108 throw new IOException("Can't load file: " + fileName);
109 }
110 return replaceTokens(stream);
111 }
112
113
114
115
116
117
118
119 public static InputStream getConfigFile(String fileName) {
120 File file = new File(Path.getAppRootDir(), fileName);
121 InputStream stream;
122
123 if (file.exists()) {
124 URL url;
125 try {
126 url = new URL("file:" + file.getAbsolutePath());
127 }
128 catch (MalformedURLException e) {
129 log.error("Unable to read config file from [" + fileName + "], got a MalformedURLException: ", e);
130 return null;
131 }
132 try {
133 stream = url.openStream();
134 }
135 catch (IOException e) {
136 log.error("Unable to read config file from [" + fileName + "], got a IOException ", e);
137 return null;
138 }
139 return stream;
140 }
141
142
143 file = new File(fileName);
144 if(file.exists()) {
145 try {
146 stream = new FileInputStream(file);
147 }
148 catch (FileNotFoundException e) {
149 log.error("Unable to read config file from [" + fileName + "], got a FileNotFoundException ", e);
150 return null;
151 }
152 return stream;
153 }
154
155 try {
156 return ClasspathResourcesUtil.getStream(fileName);
157 } catch (IOException e) {
158 log.error("Unable to read config file from the resources [" + fileName + "]", e);
159 }
160
161 return null;
162 }
163
164
165
166
167
168 public static String replaceTokens(InputStream stream) throws IOException {
169 final String config = IOUtils.toString(stream);
170 IOUtils.closeQuietly(stream);
171 return replaceTokens(config);
172 }
173
174
175
176
177
178
179
180 public static String replaceTokens(String config) throws IOException {
181 for (Iterator iter = SystemProperty.getProperties().keySet().iterator(); iter.hasNext();) {
182 String key = (String) iter.next();
183 config = StringUtils.replace(config, "${" + key + "}", SystemProperty.getProperty(key, ""));
184 }
185 return config;
186 }
187
188
189
190
191
192 public static Document string2DOM(String xml) throws ParserConfigurationException, SAXException, IOException {
193 return string2DOM(xml, Collections.EMPTY_MAP);
194 }
195
196
197
198
199 public static org.jdom.Document string2JDOM(String xml) throws JDOMException, IOException{
200 return string2JDOM(xml, Collections.EMPTY_MAP);
201 }
202
203
204
205
206
207 public static org.jdom.Document string2JDOM(String xml, final Map dtds) throws JDOMException, IOException{
208 SAXBuilder builder = new SAXBuilder();
209 builder.setEntityResolver(new MapDTDEntityResolver(dtds));
210 return builder.build(IOUtils.toInputStream(xml));
211 }
212
213
214
215
216
217
218
219
220
221
222 public static Document string2DOM(String xml, final Map<String, String> dtds) throws ParserConfigurationException, SAXException, IOException {
223 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
224 dbf.setValidating(true);
225 DocumentBuilder builder;
226 builder = dbf.newDocumentBuilder();
227 builder.setEntityResolver(new MapDTDEntityResolver(dtds));
228 return builder.parse(IOUtils.toInputStream(xml));
229 }
230
231 }