Lines 17-30
Link Here
|
17 |
package org.apache.catalina.util; |
17 |
package org.apache.catalina.util; |
18 |
|
18 |
|
19 |
|
19 |
|
|
|
20 |
import java.io.IOException; |
21 |
import java.net.URI; |
22 |
import java.net.URISyntaxException; |
23 |
import java.net.URL; |
20 |
import java.util.HashMap; |
24 |
import java.util.HashMap; |
21 |
import java.util.Map; |
25 |
import java.util.Map; |
|
|
26 |
import javax.servlet.ServletContext; |
27 |
import javax.servlet.jsp.JspFactory; |
22 |
|
28 |
|
23 |
import org.apache.tomcat.util.digester.Digester; |
|
|
24 |
import org.xml.sax.EntityResolver; |
25 |
import org.xml.sax.InputSource; |
29 |
import org.xml.sax.InputSource; |
26 |
import org.xml.sax.SAXException; |
30 |
import org.xml.sax.SAXException; |
|
|
31 |
import org.xml.sax.ext.EntityResolver2; |
27 |
|
32 |
|
|
|
33 |
import static org.apache.catalina.startup.Constants.J2eeSchemaPublicId_14; |
34 |
import static org.apache.catalina.startup.Constants.J2eeWebServiceClientSchemaPublicId_11; |
35 |
import static org.apache.catalina.startup.Constants.J2eeWebServiceSchemaPublicId_11; |
36 |
import static org.apache.catalina.startup.Constants.JavaeeSchemaPublicId_5; |
37 |
import static org.apache.catalina.startup.Constants.JavaeeSchemaPublicId_6; |
38 |
import static org.apache.catalina.startup.Constants.JavaeeSchemaPublicId_7; |
39 |
import static org.apache.catalina.startup.Constants.JavaeeWebServiceClientSchemaPublicId_12; |
40 |
import static org.apache.catalina.startup.Constants.JavaeeWebServiceClientSchemaPublicId_13; |
41 |
import static org.apache.catalina.startup.Constants.JavaeeWebServiceClientSchemaPublicId_14; |
42 |
import static org.apache.catalina.startup.Constants.JavaeeWebServiceSchemaPublicId_12; |
43 |
import static org.apache.catalina.startup.Constants.JavaeeWebServiceSchemaPublicId_13; |
44 |
import static org.apache.catalina.startup.Constants.JavaeeWebServiceSchemaPublicId_14; |
45 |
import static org.apache.catalina.startup.Constants.JspSchemaPublicId_20; |
46 |
import static org.apache.catalina.startup.Constants.JspSchemaPublicId_21; |
47 |
import static org.apache.catalina.startup.Constants.JspSchemaPublicId_22; |
48 |
import static org.apache.catalina.startup.Constants.JspSchemaPublicId_23; |
49 |
import static org.apache.catalina.startup.Constants.TldDtdPublicId_11; |
50 |
import static org.apache.catalina.startup.Constants.TldDtdPublicId_12; |
51 |
import static org.apache.catalina.startup.Constants.TldDtdResourcePath_11; |
52 |
import static org.apache.catalina.startup.Constants.TldDtdResourcePath_12; |
53 |
import static org.apache.catalina.startup.Constants.W3cDatatypesDTDResourcePath_10; |
54 |
import static org.apache.catalina.startup.Constants.W3cSchemaDTDResourcePath_10; |
55 |
import static org.apache.catalina.startup.Constants.W3cSchemaResourcePath_10; |
56 |
import static org.apache.catalina.startup.Constants.WebCommonSchemaPublicId_30; |
57 |
import static org.apache.catalina.startup.Constants.WebCommonSchemaPublicId_31; |
58 |
import static org.apache.catalina.startup.Constants.WebDtdPublicId_22; |
59 |
import static org.apache.catalina.startup.Constants.WebDtdPublicId_23; |
60 |
import static org.apache.catalina.startup.Constants.WebDtdResourcePath_22; |
61 |
import static org.apache.catalina.startup.Constants.WebDtdResourcePath_23; |
62 |
import static org.apache.catalina.startup.Constants.WebFragmentSchemaPublicId_30; |
63 |
import static org.apache.catalina.startup.Constants.WebFragmentSchemaPublicId_31; |
64 |
import static org.apache.catalina.startup.Constants.WebSchemaPublicId_24; |
65 |
import static org.apache.catalina.startup.Constants.WebSchemaPublicId_25; |
66 |
import static org.apache.catalina.startup.Constants.WebSchemaPublicId_30; |
67 |
import static org.apache.catalina.startup.Constants.WebSchemaPublicId_31; |
68 |
|
28 |
/** |
69 |
/** |
29 |
* This class implements a local SAX's <code>EntityResolver</code>. All |
70 |
* This class implements a local SAX's <code>EntityResolver</code>. All |
30 |
* DTDs and schemas used to validate the web.xml file will re-directed |
71 |
* DTDs and schemas used to validate the web.xml file will re-directed |
Lines 32-128
Link Here
|
32 |
* |
73 |
* |
33 |
* @author Jean-Francois Arcand |
74 |
* @author Jean-Francois Arcand |
34 |
*/ |
75 |
*/ |
35 |
public class SchemaResolver implements EntityResolver { |
76 |
public class SchemaResolver implements EntityResolver2 { |
36 |
|
77 |
|
37 |
/** |
78 |
private static final String SUN_1_4 = "http://java.sun.com/xml/ns/j2ee/"; |
38 |
* The digester instance for which this class is the entity resolver. |
79 |
private static final String SUN = "http://java.sun.com/xml/ns/javaee/"; |
39 |
*/ |
80 |
private static final String JCP = "http://xmlns.jcp.org/xml/ns/javaee/"; |
40 |
protected final Digester digester; |
81 |
private static final String IBM = "http://www.ibm.com/webservices/xsd/"; |
41 |
|
82 |
|
|
|
83 |
private static final SchemaResolver INSTANCE = new SchemaResolver(); |
84 |
private static final Map<String, URL> PUBLIC = new HashMap<>(); |
85 |
private static final Map<String, URL> SYSTEM = new HashMap<>(); |
86 |
static { |
87 |
Class<?> base = ServletContext.class; |
42 |
|
88 |
|
43 |
/** |
89 |
// W3C |
44 |
* The URLs of dtds and schemas that have been registered, keyed by the |
90 |
PUBLIC.put("-//W3C//DTD XMLSCHEMA 200102//EN", base.getResource(W3cSchemaDTDResourcePath_10)); |
45 |
* public identifier that corresponds. |
91 |
PUBLIC.put("datatypes", base.getResource(W3cDatatypesDTDResourcePath_10)); |
46 |
*/ |
92 |
SYSTEM.put("http://www.w3.org/2001/xml.xsd", base.getResource(W3cSchemaResourcePath_10)); |
47 |
protected final Map<String,String> entityValidator = new HashMap<>(); |
|
|
48 |
|
93 |
|
|
|
94 |
// from J2EE 1.2 |
95 |
PUBLIC.put(WebDtdPublicId_22, base.getResource(WebDtdResourcePath_22)); |
96 |
PUBLIC.put(TldDtdPublicId_11, base.getResource(TldDtdResourcePath_11)); |
49 |
|
97 |
|
50 |
/** |
98 |
// from J2EE 1.3 |
51 |
* Extension to make the difference between DTD and Schema. |
99 |
PUBLIC.put(WebDtdPublicId_23, base.getResource(WebDtdResourcePath_23)); |
52 |
*/ |
100 |
PUBLIC.put(TldDtdPublicId_12, base.getResource(TldDtdResourcePath_12)); |
53 |
protected final String schemaExtension = "xsd"; |
|
|
54 |
|
101 |
|
|
|
102 |
// from J2EE 1.4 |
103 |
register(IBM, J2eeWebServiceSchemaPublicId_11, base); |
104 |
register(IBM, J2eeWebServiceClientSchemaPublicId_11, base); |
105 |
register(SUN_1_4, J2eeSchemaPublicId_14, base); |
106 |
register(SUN_1_4, JspSchemaPublicId_20, JspFactory.class); |
107 |
register(SUN_1_4, WebSchemaPublicId_24, base); |
55 |
|
108 |
|
56 |
/** |
109 |
// from JavaEE 5 |
57 |
* Create a new <code>EntityResolver</code> that will redirect |
110 |
register(SUN, JavaeeWebServiceSchemaPublicId_12, base); |
58 |
* all remote dtds and schema to a local destination. |
111 |
register(SUN, JavaeeWebServiceClientSchemaPublicId_12, base); |
59 |
* @param digester The digester instance. |
112 |
register(SUN, JavaeeSchemaPublicId_5, base); |
60 |
*/ |
113 |
register(SUN, JspSchemaPublicId_21, JspFactory.class); |
61 |
public SchemaResolver(Digester digester) { |
114 |
register(SUN, WebSchemaPublicId_25, base); |
62 |
this.digester = digester; |
115 |
|
|
|
116 |
// from JavaEE 6 |
117 |
register(SUN, JavaeeWebServiceSchemaPublicId_13, base); |
118 |
register(SUN, JavaeeWebServiceClientSchemaPublicId_13, base); |
119 |
register(SUN, JavaeeSchemaPublicId_6, base); |
120 |
register(SUN, JspSchemaPublicId_22, JspFactory.class); |
121 |
register(SUN, WebSchemaPublicId_30, base); |
122 |
register(SUN, WebFragmentSchemaPublicId_30, base); |
123 |
register(SUN, WebCommonSchemaPublicId_30, base); |
124 |
|
125 |
// from JavaEE 7 |
126 |
register(JCP, JavaeeWebServiceSchemaPublicId_14, base); |
127 |
register(JCP, JavaeeWebServiceClientSchemaPublicId_14, base); |
128 |
register(JCP, JavaeeSchemaPublicId_7, base); |
129 |
register(JCP, JspSchemaPublicId_23, JspFactory.class); |
130 |
register(JCP, WebSchemaPublicId_31, base); |
131 |
register(JCP, WebFragmentSchemaPublicId_31, base); |
132 |
register(JCP, WebCommonSchemaPublicId_31, base); |
63 |
} |
133 |
} |
64 |
|
134 |
|
|
|
135 |
private static void register(String origin, String file, Class<?> base) { |
136 |
SYSTEM.put(origin + file, base.getResource("resources/" + file)); |
137 |
} |
65 |
|
138 |
|
66 |
/** |
139 |
/** |
67 |
* Register the specified DTD/Schema URL for the specified public |
140 |
* Returns an EntityResolver that resolves resources from local jars. |
68 |
* identifier. This must be called before the first call to |
|
|
69 |
* <code>parse()</code>. |
70 |
* |
141 |
* |
71 |
* When adding a schema file (*.xsd), only the name of the file |
142 |
* @return an EntityResolver that resolves resources from local jars |
72 |
* will get added. If two schemas with the same name are added, |
|
|
73 |
* only the last one will be stored. |
74 |
* |
75 |
* @param publicId Public identifier of the DTD to be resolved |
76 |
* @param entityURL The URL to use for reading this DTD |
77 |
*/ |
143 |
*/ |
78 |
public void register(String publicId, String entityURL) { |
144 |
public static SchemaResolver getEntityResolver() { |
79 |
String key = publicId; |
145 |
return INSTANCE; |
80 |
if (publicId.indexOf(schemaExtension) != -1) |
146 |
} |
81 |
key = publicId.substring(publicId.lastIndexOf('/')+1); |
|
|
82 |
entityValidator.put(key, entityURL); |
83 |
} |
84 |
|
147 |
|
85 |
|
|
|
86 |
/** |
148 |
/** |
87 |
* Resolve the requested external entity. |
149 |
* Constructor allowing sub-classing to handle additional mappings. |
88 |
* |
|
|
89 |
* @param publicId The public identifier of the entity being referenced |
90 |
* @param systemId The system identifier of the entity being referenced |
91 |
* |
92 |
* @exception SAXException if a parsing exception occurs |
93 |
* |
94 |
*/ |
150 |
*/ |
|
|
151 |
protected SchemaResolver() { |
152 |
} |
153 |
|
95 |
@Override |
154 |
@Override |
96 |
public InputSource resolveEntity(String publicId, String systemId) |
155 |
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { |
97 |
throws SAXException { |
156 |
return resolveEntity(null, publicId, null, systemId); |
|
|
157 |
} |
98 |
|
158 |
|
99 |
if (publicId != null) { |
159 |
@Override |
100 |
digester.setPublicId(publicId); |
160 |
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) |
101 |
} |
161 |
throws SAXException, IOException { |
102 |
|
162 |
|
103 |
// Has this system identifier been registered? |
163 |
// resolve the systemId against the baseURI |
104 |
String entityURL = null; |
164 |
try { |
105 |
if (publicId != null) { |
165 |
if (systemId != null && baseURI != null) { |
106 |
entityURL = entityValidator.get(publicId); |
166 |
URI systemUri = new URI(systemId); |
|
|
167 |
if (!systemUri.isAbsolute()) { |
168 |
systemId = new URI(baseURI).resolve(systemUri).toString(); |
169 |
} |
170 |
} |
171 |
} catch (URISyntaxException e) { |
172 |
throw new SAXException(e); |
107 |
} |
173 |
} |
108 |
|
174 |
|
109 |
// Redirect the schema location to a local destination |
175 |
// try resolving using the publicId |
110 |
String key = null; |
176 |
URL url = PUBLIC.get(publicId); |
111 |
if (entityURL == null && systemId != null) { |
|
|
112 |
key = systemId.substring(systemId.lastIndexOf('/')+1); |
113 |
entityURL = entityValidator.get(key); |
114 |
} |
115 |
|
177 |
|
116 |
if (entityURL == null) { |
178 |
// if not found, try resolving using the systemId |
117 |
return (null); |
179 |
if (url == null) { |
|
|
180 |
url = SYSTEM.get(systemId); |
118 |
} |
181 |
} |
119 |
|
182 |
if (url == null) { |
120 |
try { |
183 |
return null; |
121 |
return (new InputSource(entityURL)); |
184 |
} else { |
122 |
} catch (Exception e) { |
185 |
InputSource is = new InputSource(url.openStream()); |
123 |
throw new SAXException(e); |
186 |
is.setPublicId(publicId); |
|
|
187 |
is.setSystemId(systemId); |
188 |
return is; |
124 |
} |
189 |
} |
|
|
190 |
} |
125 |
|
191 |
|
|
|
192 |
@Override |
193 |
public InputSource getExternalSubset(String name, String baseURI) throws SAXException, IOException { |
194 |
return null; |
126 |
} |
195 |
} |
127 |
|
|
|
128 |
} |
196 |
} |