~ubuntu-branches/ubuntu/trusty/tomcat6/trusty

« back to all changes in this revision

Viewing changes to java/org/apache/tomcat/util/descriptor/DigesterFactory.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2014-02-17 00:02:00 UTC
  • mfrom: (1.2.10)
  • Revision ID: package-import@ubuntu.com-20140217000200-qs6ki7bhqnfhkas7
Tags: 6.0.39-1
* Team upload.
* New upstream release.
  - Refreshed the patches
* Standards-Version updated to 3.9.5 (no changes)
* Switch to debhelper level 9
* Use XZ compression for the upstream tarball
* Use canonical URL for the Vcs-Git field

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 org.apache.tomcat.util.descriptor;
 
18
 
 
19
import java.net.URL;
 
20
import java.util.Collections;
 
21
import java.util.HashMap;
 
22
import java.util.Map;
 
23
 
 
24
import javax.servlet.ServletContext;
 
25
 
 
26
import org.apache.tomcat.util.digester.Digester;
 
27
import org.apache.tomcat.util.digester.RuleSet;
 
28
import org.xml.sax.ext.EntityResolver2;
 
29
 
 
30
/**
 
31
 * Wrapper class around the Digester that hide Digester's initialization
 
32
 * details.
 
33
 */
 
34
public class DigesterFactory {
 
35
 
 
36
    /**
 
37
     * Mapping of well-known public IDs used by the Servlet API to the matching
 
38
     * local resource.
 
39
     */
 
40
    public static final Map<String,String> SERVLET_API_PUBLIC_IDS;
 
41
 
 
42
    /**
 
43
     * Mapping of well-known system IDs used by the Servlet API to the matching
 
44
     * local resource.
 
45
     */
 
46
    public static final Map<String,String> SERVLET_API_SYSTEM_IDS;
 
47
 
 
48
    static {
 
49
        Map<String, String> publicIds = new HashMap<String, String>();
 
50
        Map<String, String> systemIds = new HashMap<String, String>();
 
51
 
 
52
        // W3C
 
53
        publicIds.put(XmlIdentifiers.XSD_10_PUBLIC, idFor("XMLSchema.dtd"));
 
54
        publicIds.put(XmlIdentifiers.DATATYPES_PUBLIC, idFor("datatypes.dtd"));
 
55
        systemIds.put(XmlIdentifiers.XML_2001_XSD, idFor("xml.xsd"));
 
56
 
 
57
        // from J2EE 1.2
 
58
        publicIds.put(XmlIdentifiers.WEB_22_PUBLIC, idFor("web-app_2_2.dtd"));
 
59
        publicIds.put(XmlIdentifiers.TLD_11_PUBLIC, idFor("web-jsptaglibrary_1_1.dtd"));
 
60
 
 
61
        // from J2EE 1.3
 
62
        publicIds.put(XmlIdentifiers.WEB_23_PUBLIC, idFor("web-app_2_3.dtd"));
 
63
        publicIds.put(XmlIdentifiers.TLD_12_PUBLIC, idFor("web-jsptaglibrary_1_2.dtd"));
 
64
 
 
65
        // from J2EE 1.4
 
66
        systemIds.put("http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd",
 
67
                idFor("j2ee_web_services_1_1.xsd"));
 
68
        systemIds.put("http://www.ibm.com/webservices/xsd/j2ee_web_services_client_1_1.xsd",
 
69
                idFor("j2ee_web_services_client_1_1.xsd"));
 
70
        systemIds.put(XmlIdentifiers.WEB_24_XSD, idFor("web-app_2_4.xsd"));
 
71
        systemIds.put(XmlIdentifiers.TLD_20_XSD, idFor("web-jsptaglibrary_2_0.xsd"));
 
72
        addSelf(systemIds, "j2ee_1_4.xsd");
 
73
        addSelf(systemIds, "jsp_2_0.xsd");
 
74
 
 
75
        // from JavaEE 5
 
76
        systemIds.put(XmlIdentifiers.WEB_25_XSD, idFor("web-app_2_5.xsd"));
 
77
        systemIds.put(XmlIdentifiers.TLD_21_XSD, idFor("web-jsptaglibrary_2_1.xsd"));
 
78
        addSelf(systemIds, "javaee_5.xsd");
 
79
        addSelf(systemIds, "jsp_2_1.xsd");
 
80
        addSelf(systemIds, "javaee_web_services_1_2.xsd");
 
81
        addSelf(systemIds, "javaee_web_services_client_1_2.xsd");
 
82
 
 
83
        SERVLET_API_PUBLIC_IDS = Collections.unmodifiableMap(publicIds);
 
84
        SERVLET_API_SYSTEM_IDS = Collections.unmodifiableMap(systemIds);
 
85
    }
 
86
 
 
87
    private static void addSelf(Map<String, String> ids, String id) {
 
88
        String systemId = idFor(id);
 
89
        ids.put(systemId, systemId);
 
90
        ids.put(id, systemId);
 
91
    }
 
92
 
 
93
    private static String idFor(String url) {
 
94
        URL id = ServletContext.class.getResource("resources/" + url);
 
95
        if (id == null) {
 
96
            id = ServletContext.class.getResource("jsp/resources/" + url);
 
97
        }
 
98
        return id.toExternalForm();
 
99
    }
 
100
 
 
101
 
 
102
    /**
 
103
     * Create a <code>Digester</code> parser.
 
104
     * @param xmlValidation turn on/off xml validation
 
105
     * @param xmlNamespaceAware turn on/off namespace validation
 
106
     * @param rule an instance of <code>RuleSet</code> used for parsing the xml.
 
107
     * @param blockExternal turn on/off the blocking of external resources
 
108
     */
 
109
    public static Digester newDigester(boolean xmlValidation,
 
110
                                       boolean xmlNamespaceAware,
 
111
                                       RuleSet rule,
 
112
                                       boolean blockExternal) {
 
113
        Digester digester = new Digester();
 
114
        digester.setNamespaceAware(xmlNamespaceAware);
 
115
        digester.setValidating(xmlValidation);
 
116
        digester.setUseContextClassLoader(true);
 
117
        EntityResolver2 resolver = new LocalResolver(SERVLET_API_PUBLIC_IDS,
 
118
                SERVLET_API_SYSTEM_IDS, blockExternal);
 
119
        digester.setEntityResolver(resolver);
 
120
        if (rule != null) {
 
121
            digester.addRuleSet(rule);
 
122
        }
 
123
 
 
124
        return digester;
 
125
    }
 
126
}