1 13 package info.magnolia.cms.util; 14 15 import info.magnolia.cms.core.SystemProperty; 16 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 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 ; 39 import org.xml.sax.EntityResolver ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 43 49 public class ConfigUtil { 50 56 public static final class MapDTDEntityResolver implements EntityResolver { 57 58 private final Map dtds; 59 60 public MapDTDEntityResolver(Map dtds) { 61 this.dtds = dtds; 62 } 63 64 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 65 String key = StringUtils.substringAfterLast(systemId, "/"); 66 if (dtds.containsKey(key)) { 67 Class clazz = getClass(); 68 InputStream in = clazz.getResourceAsStream((String )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 (in); 77 } 78 } 79 else { 80 return null; 81 } 82 } 83 } 84 85 88 public static Logger log = LoggerFactory.getLogger(ConfigUtil.class); 89 90 95 public static InputStream getConfigFile(String fileName) { 96 File log4jFile = new File (SystemProperty.getProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR), fileName); 97 InputStream stream; 98 if (log4jFile.exists()) { 99 URL url; 100 try { 101 url = new URL ("file:" + log4jFile.getAbsolutePath()); } 103 catch (MalformedURLException e) { 104 log.error("Unable to read config file from [" + fileName 106 + "], got a MalformedURLException: " , e); 108 return null; 109 } 110 try { 111 stream = url.openStream(); 112 } 113 catch (IOException e) { 114 log.error("Unable to read config file from [" + fileName 116 + "], got a IOException " ,e); 118 return null; 119 } 120 } 121 else { 122 try { 123 stream = new FileInputStream (fileName); 124 } 125 catch (FileNotFoundException e) { 126 log.error("Unable to read config file from [" + fileName 128 + "], got a FileNotFoundException " , e); 130 return null; 131 } 132 } 133 return stream; 134 } 135 136 142 public static String replaceTokens(InputStream stream) throws IOException { 143 String config; 144 config = IOUtils.toString(stream); 145 IOUtils.closeQuietly(stream); 146 return replaceTokens(config); 147 } 148 149 155 public static String replaceTokens(String config) throws IOException { 156 for (Iterator iter = SystemProperty.getProperties().keySet().iterator(); iter.hasNext();) { 157 String key = (String ) iter.next(); 158 config = StringUtils.replace(config, "${" + key + "}", SystemProperty.getProperty(key, "")); 159 160 } 161 return config; 162 } 163 164 172 public static Document string2DOM(String xml) throws ParserConfigurationException , SAXException , IOException { 173 return string2DOM(xml, Collections.EMPTY_MAP); 174 } 175 176 183 public static org.jdom.Document string2JDOM(String xml) throws JDOMException, IOException { 184 return string2JDOM(xml, Collections.EMPTY_MAP); 185 } 186 187 195 public static org.jdom.Document string2JDOM(String xml, final Map dtds) throws JDOMException, IOException { 196 SAXBuilder builder = new SAXBuilder(); 197 builder.setEntityResolver(new MapDTDEntityResolver(dtds)); 198 return builder.build(IOUtils.toInputStream(xml)); 199 } 200 201 210 public static Document string2DOM(String xml, final Map dtds) throws ParserConfigurationException , SAXException , IOException { 211 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 212 dbf.setValidating(true); 213 DocumentBuilder builder; 214 builder = dbf.newDocumentBuilder(); 215 builder.setEntityResolver(new MapDTDEntityResolver(dtds)); 216 return builder.parse(IOUtils.toInputStream(xml)); 217 } 218 219 } 220 | Popular Tags |