1 10 11 package org.mule.config; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.mule.util.IOUtils; 19 import org.mule.util.StringUtils; 20 import org.xml.sax.EntityResolver ; 21 import org.xml.sax.InputSource ; 22 import org.xml.sax.SAXException ; 23 24 39 public class MuleDtdResolver implements EntityResolver 40 { 41 44 protected static final Log logger = LogFactory.getLog(MuleDtdResolver.class); 45 46 public static final String DEFAULT_MULE_DTD = "mule-configuration.dtd"; 47 private String dtdName = null; 48 49 private static final String SEARCH_PATH = ""; 51 52 private EntityResolver delegate; 53 private String xsl; 54 private static String currentXsl; 55 56 public MuleDtdResolver() 57 { 58 this(DEFAULT_MULE_DTD); 59 } 60 61 public MuleDtdResolver(String dtdName) 62 { 63 this(dtdName, null, null); 64 } 65 66 public MuleDtdResolver(String dtdName, String xsl) 67 { 68 this(dtdName, xsl, null); 69 } 70 71 public MuleDtdResolver(String dtdName, EntityResolver delegate) 72 { 73 this(dtdName, null, delegate); 74 } 75 76 public MuleDtdResolver(String dtdName, String xsl, EntityResolver delegate) 77 { 78 this.dtdName = dtdName; 79 this.delegate = delegate; 80 this.xsl = xsl; 81 if (logger.isDebugEnabled()) 82 { 83 StringBuffer buffer = new StringBuffer (); 84 buffer.append("Created Mule Dtd Resolver: "); 85 buffer.append("dtd=").append(dtdName).append(", "); 86 buffer.append("xsl=").append(xsl).append(", "); 87 buffer.append("delegate resolver=").append(delegate).append(", "); 88 logger.debug(buffer.toString()); 89 } 90 } 91 92 public InputSource resolveEntity(String publicId, String systemId) throws IOException , SAXException 93 { 94 logger.debug("Trying to resolve XML entity with public ID: " + publicId + " and system ID: " 95 + systemId); 96 97 InputSource source = null; 98 currentXsl = null; 99 if (delegate != null) 100 { 101 source = delegate.resolveEntity(publicId, systemId); 102 } 103 if ((source == null) && StringUtils.isNotBlank(systemId) && systemId.endsWith(".dtd")) 104 { 105 String [] tokens = systemId.split("/"); 106 String dtdFile = tokens[tokens.length - 1]; 107 logger.debug("Looking on classpath for " + SEARCH_PATH + dtdFile); 108 109 InputStream is = IOUtils.getResourceAsStream(SEARCH_PATH + dtdFile, getClass(), 110 true, false); 111 if (is != null) 112 { 113 source = new InputSource (is); 114 source.setPublicId(publicId); 115 source.setSystemId(systemId); 116 logger.debug("Found on classpath mule DTD: " + systemId); 117 currentXsl = xsl; 118 return source; 119 } 120 logger.debug("Could not find dtd resource on classpath: " + SEARCH_PATH + dtdFile); 121 } 122 return source; 123 } 124 125 public String getXslForDtd() 126 { 127 return currentXsl; 128 } 129 } 130 | Popular Tags |