View | Details | Raw Unified | Return to bug 55166
Collapse All | Expand All

(-)test/javax/servlet/TestSchemaValidation.java (+113 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package javax.servlet;
18
19
import java.io.File;
20
import java.net.URL;
21
import javax.xml.XMLConstants;
22
import javax.xml.parsers.DocumentBuilder;
23
import javax.xml.parsers.DocumentBuilderFactory;
24
import javax.xml.validation.Schema;
25
import javax.xml.validation.SchemaFactory;
26
27
import org.apache.catalina.deploy.WebXml;
28
import org.apache.catalina.startup.DigesterFactory;
29
import org.apache.catalina.startup.WebRuleSet;
30
import org.apache.tomcat.util.digester.Digester;
31
import org.junit.Assert;
32
import org.junit.Ignore;
33
import org.junit.Test;
34
import org.w3c.dom.Document;
35
import org.w3c.dom.ls.LSInput;
36
import org.w3c.dom.ls.LSResourceResolver;
37
38
public class TestSchemaValidation {
39
40
    /**
41
     * Test for https://issues.apache.org/bugzilla/show_bug.cgi?id=55166
42
     */
43
    @Test
44
    @Ignore
45
    public void testValidation() throws Exception {
46
        LSResourceResolver resolver = new LSResourceResolver() {
47
            @Override
48
            public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
49
                return null;  //To change body of implemented methods use File | Settings | File Templates.
50
            }
51
        };
52
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
53
        schemaFactory.setResourceResolver(resolver);
54
        URL url = ServletContext.class.getResource("/javax/servlet/resources/web-app_3_1.xsd");
55
        System.out.println("url = " + url);
56
        Schema schema = schemaFactory.newSchema(url);
57
58
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
59
        factory.setNamespaceAware(true);
60
        factory.setSchema(schema);
61
        DocumentBuilder builder = factory.newDocumentBuilder();
62
        Document document = builder.parse(new File("test/webapp/WEB-INF/web.xml"));
63
        Assert.assertEquals("web-app", document.getDocumentElement().getLocalName());
64
    }
65
66
    @Test
67
    public void testWebapp() throws Exception {
68
        Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false));
69
        digester.push(new WebXml());
70
        WebXml desc = (WebXml) digester.parse(new File("test/webapp/WEB-INF/web.xml"));
71
        Assert.assertEquals("3.1", desc.getVersion());
72
    }
73
74
    @Test
75
    public void testWebapp_2_3() throws Exception {
76
        Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false));
77
        digester.push(new WebXml());
78
        WebXml desc = (WebXml) digester.parse(new File("test/webapp-2.3/WEB-INF/web.xml"));
79
        Assert.assertEquals("2.3", desc.getVersion());
80
    }
81
82
    @Test
83
    public void testWebapp_2_4() throws Exception {
84
        Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false));
85
        digester.push(new WebXml());
86
        WebXml desc = (WebXml) digester.parse(new File("test/webapp-2.4/WEB-INF/web.xml"));
87
        Assert.assertEquals("2.4", desc.getVersion());
88
    }
89
90
    @Test
91
    public void testWebapp_2_5() throws Exception {
92
        Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false));
93
        digester.push(new WebXml());
94
        WebXml desc = (WebXml) digester.parse(new File("test/webapp-2.5/WEB-INF/web.xml"));
95
        Assert.assertEquals("2.5", desc.getVersion());
96
    }
97
98
    @Test
99
    public void testWebapp_3_0() throws Exception {
100
        Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false));
101
        digester.push(new WebXml());
102
        WebXml desc = (WebXml) digester.parse(new File("test/webapp-3.0/WEB-INF/web.xml"));
103
        Assert.assertEquals("3.0", desc.getVersion());
104
    }
105
106
    @Test
107
    public void testWebapp_3_1() throws Exception {
108
        Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false));
109
        digester.push(new WebXml());
110
        WebXml desc = (WebXml) digester.parse(new File("test/webapp-3.1/WEB-INF/web.xml"));
111
        Assert.assertEquals("3.1", desc.getVersion());
112
    }
113
}
(-)java/org/apache/tomcat/util/digester/Digester.java (-3 / +32 lines)
Lines 45-50 Link Here
45
import org.xml.sax.SAXNotSupportedException;
45
import org.xml.sax.SAXNotSupportedException;
46
import org.xml.sax.SAXParseException;
46
import org.xml.sax.SAXParseException;
47
import org.xml.sax.XMLReader;
47
import org.xml.sax.XMLReader;
48
import org.xml.sax.ext.LexicalHandler;
48
import org.xml.sax.helpers.AttributesImpl;
49
import org.xml.sax.helpers.AttributesImpl;
49
import org.xml.sax.helpers.DefaultHandler;
50
import org.xml.sax.helpers.DefaultHandler;
50
51
Lines 791-796 Link Here
791
            reader.setEntityResolver(entityResolver);
792
            reader.setEntityResolver(entityResolver);
792
        }
793
        }
793
794
795
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", new LexicalHandler() {
796
            @Override
797
            public void startDTD(String name, String publicId, String systemId) throws SAXException {
798
                setPublicId(publicId);
799
            }
800
801
            @Override
802
            public void endDTD() throws SAXException {
803
            }
804
805
            @Override
806
            public void startEntity(String name) throws SAXException {
807
            }
808
809
            @Override
810
            public void endEntity(String name) throws SAXException {
811
            }
812
813
            @Override
814
            public void startCDATA() throws SAXException {
815
            }
816
817
            @Override
818
            public void endCDATA() throws SAXException {
819
            }
820
821
            @Override
822
            public void comment(char[] ch, int start, int length) throws SAXException {
823
            }
824
        });
825
794
        reader.setErrorHandler(this);
826
        reader.setErrorHandler(this);
795
        return reader;
827
        return reader;
796
    }
828
    }
Lines 1292-1300 Link Here
1292
            saxLog.debug("resolveEntity('" + publicId + "', '" + systemId + "')");
1324
            saxLog.debug("resolveEntity('" + publicId + "', '" + systemId + "')");
1293
        }
1325
        }
1294
1326
1295
        if (publicId != null)
1296
            this.publicId = publicId;
1297
1298
        // Has this system identifier been registered?
1327
        // Has this system identifier been registered?
1299
        String entityURL = null;
1328
        String entityURL = null;
1300
        if (publicId != null) {
1329
        if (publicId != null) {
(-)java/org/apache/catalina/startup/DigesterFactory.java (-175 / +1 lines)
Lines 18-28 Link Here
18
18
19
package org.apache.catalina.startup;
19
package org.apache.catalina.startup;
20
20
21
import java.net.URL;
22
23
import org.apache.catalina.util.SchemaResolver;
21
import org.apache.catalina.util.SchemaResolver;
24
import org.apache.juli.logging.Log;
25
import org.apache.juli.logging.LogFactory;
26
import org.apache.tomcat.util.digester.Digester;
22
import org.apache.tomcat.util.digester.Digester;
27
import org.apache.tomcat.util.digester.RuleSet;
23
import org.apache.tomcat.util.digester.RuleSet;
28
24
Lines 33-44 Link Here
33
 */
29
 */
34
public class DigesterFactory {
30
public class DigesterFactory {
35
    /**
31
    /**
36
     * The log.
37
     */
38
    private static final Log log = LogFactory.getLog(DigesterFactory.class);
39
40
41
    /**
42
     * Create a <code>Digester</code> parser.
32
     * Create a <code>Digester</code> parser.
43
     * @param xmlValidation turn on/off xml validation
33
     * @param xmlValidation turn on/off xml validation
44
     * @param xmlNamespaceAware turn on/off namespace validation
34
     * @param xmlNamespaceAware turn on/off namespace validation
Lines 51-225 Link Here
51
        digester.setNamespaceAware(xmlNamespaceAware);
41
        digester.setNamespaceAware(xmlNamespaceAware);
52
        digester.setValidating(xmlValidation);
42
        digester.setValidating(xmlValidation);
53
        digester.setUseContextClassLoader(true);
43
        digester.setUseContextClassLoader(true);
54
44
        digester.setEntityResolver(SchemaResolver.getEntityResolver());
55
        SchemaResolver schemaResolver = new SchemaResolver(digester);
56
        registerLocalSchema(schemaResolver);
57
58
        digester.setEntityResolver(schemaResolver);
59
        if ( rule != null ) {
45
        if ( rule != null ) {
60
            digester.addRuleSet(rule);
46
            digester.addRuleSet(rule);
61
        }
47
        }
62
48
63
        return (digester);
49
        return (digester);
64
    }
50
    }
65
66
67
    /**
68
     * Utilities used to force the parser to use local schema, when available,
69
     * instead of the <code>schemaLocation</code> XML element.
70
     */
71
    protected static void registerLocalSchema(SchemaResolver schemaResolver){
72
        // J2EE
73
        register(Constants.J2eeSchemaResourcePath_14,
74
                 Constants.J2eeSchemaPublicId_14,
75
                 schemaResolver);
76
77
        register(Constants.JavaeeSchemaResourcePath_5,
78
                Constants.JavaeeSchemaPublicId_5,
79
                schemaResolver);
80
81
        register(Constants.JavaeeSchemaResourcePath_6,
82
                Constants.JavaeeSchemaPublicId_6,
83
                schemaResolver);
84
85
        register(Constants.JavaeeSchemaResourcePath_7,
86
                Constants.JavaeeSchemaPublicId_7,
87
                schemaResolver);
88
89
        // W3C
90
        register(Constants.W3cSchemaResourcePath_10,
91
                 Constants.W3cSchemaPublicId_10,
92
                 schemaResolver);
93
94
        register(Constants.W3cSchemaDTDResourcePath_10,
95
                Constants.W3cSchemaDTDPublicId_10,
96
                schemaResolver);
97
98
        register(Constants.W3cDatatypesDTDResourcePath_10,
99
                Constants.W3cDatatypesDTDPublicId_10,
100
                schemaResolver);
101
102
        // JSP
103
        register(Constants.JspSchemaResourcePath_20,
104
                 Constants.JspSchemaPublicId_20,
105
                 schemaResolver);
106
107
        register(Constants.JspSchemaResourcePath_21,
108
                Constants.JspSchemaPublicId_21,
109
                schemaResolver);
110
111
        register(Constants.JspSchemaResourcePath_22,
112
                Constants.JspSchemaPublicId_22,
113
                schemaResolver);
114
115
        register(Constants.JspSchemaResourcePath_23,
116
                Constants.JspSchemaPublicId_23,
117
                schemaResolver);
118
119
        // TLD
120
        register(Constants.TldDtdResourcePath_11,
121
                 Constants.TldDtdPublicId_11,
122
                 schemaResolver);
123
124
        register(Constants.TldDtdResourcePath_12,
125
                 Constants.TldDtdPublicId_12,
126
                 schemaResolver);
127
128
        register(Constants.TldSchemaResourcePath_20,
129
                 Constants.TldSchemaPublicId_20,
130
                 schemaResolver);
131
132
        register(Constants.TldSchemaResourcePath_21,
133
                Constants.TldSchemaPublicId_21,
134
                schemaResolver);
135
136
        // web.xml
137
        register(Constants.WebDtdResourcePath_22,
138
                 Constants.WebDtdPublicId_22,
139
                 schemaResolver);
140
141
        register(Constants.WebDtdResourcePath_23,
142
                 Constants.WebDtdPublicId_23,
143
                 schemaResolver);
144
145
        register(Constants.WebSchemaResourcePath_24,
146
                 Constants.WebSchemaPublicId_24,
147
                 schemaResolver);
148
149
        register(Constants.WebSchemaResourcePath_25,
150
                Constants.WebSchemaPublicId_25,
151
                schemaResolver);
152
153
        register(Constants.WebSchemaResourcePath_30,
154
                Constants.WebSchemaPublicId_30,
155
                schemaResolver);
156
157
        register(Constants.WebCommonSchemaResourcePath_30,
158
                Constants.WebCommonSchemaPublicId_30,
159
                schemaResolver);
160
161
        register(Constants.WebFragmentSchemaResourcePath_30,
162
                Constants.WebFragmentSchemaPublicId_30,
163
                schemaResolver);
164
165
        register(Constants.WebSchemaResourcePath_31,
166
                Constants.WebSchemaPublicId_31,
167
                schemaResolver);
168
169
        register(Constants.WebCommonSchemaResourcePath_31,
170
                Constants.WebCommonSchemaPublicId_31,
171
                schemaResolver);
172
173
        register(Constants.WebFragmentSchemaResourcePath_31,
174
                Constants.WebFragmentSchemaPublicId_31,
175
                schemaResolver);
176
177
        // Web Service
178
        register(Constants.J2eeWebServiceSchemaResourcePath_11,
179
                 Constants.J2eeWebServiceSchemaPublicId_11,
180
                 schemaResolver);
181
182
        register(Constants.J2eeWebServiceClientSchemaResourcePath_11,
183
                 Constants.J2eeWebServiceClientSchemaPublicId_11,
184
                 schemaResolver);
185
186
        register(Constants.JavaeeWebServiceSchemaResourcePath_12,
187
                Constants.JavaeeWebServiceSchemaPublicId_12,
188
                schemaResolver);
189
190
        register(Constants.JavaeeWebServiceClientSchemaResourcePath_12,
191
                Constants.JavaeeWebServiceClientSchemaPublicId_12,
192
                schemaResolver);
193
194
        register(Constants.JavaeeWebServiceSchemaResourcePath_13,
195
                Constants.JavaeeWebServiceSchemaPublicId_13,
196
                schemaResolver);
197
198
        register(Constants.JavaeeWebServiceClientSchemaResourcePath_13,
199
                Constants.JavaeeWebServiceClientSchemaPublicId_13,
200
                schemaResolver);
201
202
        register(Constants.JavaeeWebServiceSchemaResourcePath_14,
203
                Constants.JavaeeWebServiceSchemaPublicId_14,
204
                schemaResolver);
205
206
        register(Constants.JavaeeWebServiceClientSchemaResourcePath_14,
207
                Constants.JavaeeWebServiceClientSchemaPublicId_14,
208
                schemaResolver);
209
    }
210
211
212
    /**
213
     * Load the resource and add it to the resolver.
214
     */
215
    protected static void register(String resourceURL, String resourcePublicId,
216
            SchemaResolver schemaResolver){
217
        URL url = DigesterFactory.class.getResource(resourceURL);
218
219
        if(url == null) {
220
            log.warn("Could not get url for " + resourceURL);
221
        } else {
222
            schemaResolver.register(resourcePublicId , url.toString() );
223
        }
224
    }
225
}
51
}
(-)java/org/apache/catalina/util/SchemaResolver.java (-69 / +137 lines)
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
}

Return to bug 55166