KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > ConfigUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.cms.core.SystemProperty;
16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31
32 import org.apache.commons.io.IOUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.jdom.JDOMException;
35 import org.jdom.input.SAXBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document JavaDoc;
39 import org.xml.sax.EntityResolver JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * Util used to process config files
45  * @author Philipp Bracher
46  * @version $Id: ConfigUtil.java 7285 2006-11-09 18:57:39Z philipp $
47  *
48  */

49 public class ConfigUtil {
50     /**
51      * EntityResolver using a Map to resources
52      * @author Philipp Bracher
53      * @version $Id: ConfigUtil.java 7285 2006-11-09 18:57:39Z philipp $
54      *
55      */

56     public static final class MapDTDEntityResolver implements EntityResolver JavaDoc {
57
58         private final Map JavaDoc dtds;
59
60         public MapDTDEntityResolver(Map JavaDoc dtds) {
61             this.dtds = dtds;
62         }
63
64         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
65             String JavaDoc key = StringUtils.substringAfterLast(systemId, "/");
66             if (dtds.containsKey(key)) {
67                 Class JavaDoc clazz = getClass();
68                 InputStream JavaDoc in = clazz.getResourceAsStream((String JavaDoc)dtds.get(key));
69                 if (in == null) {
70                     log.error("Could not find [" + systemId + "]. Used ["
71                         + clazz.getClassLoader()
72                         + "] class loader in the search.");
73                     return null;
74                 }
75                 else {
76                     return new InputSource JavaDoc(in);
77                 }
78             }
79             else {
80                 return null;
81             }
82         }
83     }
84
85     /**
86      * Log
87      */

88     public static Logger log = LoggerFactory.getLogger(ConfigUtil.class);
89
90     /**
91      * Try to get the file. Get it first in the file system, then from the resources
92      * @param fileName
93      * @return the input stream
94      */

95     public static InputStream JavaDoc getConfigFile(String JavaDoc fileName) {
96         File JavaDoc log4jFile = new File JavaDoc(SystemProperty.getProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR), fileName);
97         InputStream JavaDoc stream;
98         if (log4jFile.exists()) {
99             URL JavaDoc url;
100             try {
101                 url = new URL JavaDoc("file:" + log4jFile.getAbsolutePath()); //$NON-NLS-1$
102
}
103             catch (MalformedURLException JavaDoc e) {
104                 log.error("Unable to read config file from [" //$NON-NLS-1$
105
+ fileName
106                     + "], got a MalformedURLException: " //$NON-NLS-1$
107
, e);
108                 return null;
109             }
110             try {
111                 stream = url.openStream();
112             }
113             catch (IOException JavaDoc e) {
114                 log.error("Unable to read config file from [" //$NON-NLS-1$
115
+ fileName
116                     + "], got a IOException " //$NON-NLS-1$
117
,e);
118                 return null;
119             }
120         }
121         else {
122             try {
123                 stream = new FileInputStream JavaDoc(fileName);
124             }
125             catch (FileNotFoundException JavaDoc e) {
126                 log.error("Unable to read config file from [" //$NON-NLS-1$
127
+ fileName
128                     + "], got a FileNotFoundException " //$NON-NLS-1$
129
, e);
130                 return null;
131             }
132         }
133         return stream;
134     }
135
136     /**
137      * Read the stream and replace tokens
138      * @param stream
139      * @return the string with replaced tokens
140      * @throws IOException
141      */

142     public static String JavaDoc replaceTokens(InputStream JavaDoc stream) throws IOException JavaDoc {
143         String JavaDoc config;
144         config = IOUtils.toString(stream);
145         IOUtils.closeQuietly(stream);
146         return replaceTokens(config);
147     }
148     
149     /**
150      * Replace tokens in a string
151      * @param config
152      * @return
153      * @throws IOException
154      */

155     public static String JavaDoc replaceTokens(String JavaDoc config) throws IOException JavaDoc {
156         for (Iterator JavaDoc iter = SystemProperty.getProperties().keySet().iterator(); iter.hasNext();) {
157             String JavaDoc key = (String JavaDoc) iter.next();
158             config = StringUtils.replace(config, "${" + key + "}", SystemProperty.getProperty(key, ""));
159             
160         }
161         return config;
162     }
163
164     /**
165      * Convert the string to an DOM Document
166      * @param xml
167      * @return
168      * @throws ParserConfigurationException
169      * @throws SAXException
170      * @throws IOException
171      */

172     public static Document JavaDoc string2DOM(String JavaDoc xml) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
173         return string2DOM(xml, Collections.EMPTY_MAP);
174     }
175
176     /**
177      * Convert the string to a JDOM Document
178      * @param xml
179      * @return
180      * @throws JDOMException
181      * @throws IOException
182      */

183     public static org.jdom.Document string2JDOM(String JavaDoc xml) throws JDOMException, IOException JavaDoc{
184         return string2JDOM(xml, Collections.EMPTY_MAP);
185     }
186
187     /**
188      * Uses a map to find the dtds in the resources
189      * @param xml
190      * @param dtds
191      * @return
192      * @throws JDOMException
193      * @throws IOException
194      */

195     public static org.jdom.Document string2JDOM(String JavaDoc xml, final Map JavaDoc dtds) throws JDOMException, IOException JavaDoc{
196         SAXBuilder builder = new SAXBuilder();
197         builder.setEntityResolver(new MapDTDEntityResolver(dtds));
198         return builder.build(IOUtils.toInputStream(xml));
199     }
200     
201     /**
202      * Uses a map to find dtds in the resources
203      * @param xml
204      * @param dtds
205      * @return
206      * @throws ParserConfigurationException
207      * @throws SAXException
208      * @throws IOException
209      */

210     public static Document JavaDoc string2DOM(String JavaDoc xml, final Map JavaDoc dtds) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
211         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
212         dbf.setValidating(true);
213         DocumentBuilder JavaDoc builder;
214         builder = dbf.newDocumentBuilder();
215         builder.setEntityResolver(new MapDTDEntityResolver(dtds));
216         return builder.parse(IOUtils.toInputStream(xml));
217     }
218
219 }
220
Popular Tags