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