001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.util;
018:
019: import java.util.HashMap;
020:
021: import org.apache.tomcat.util.digester.Digester;
022: import org.xml.sax.InputSource;
023: import org.xml.sax.EntityResolver;
024: import org.xml.sax.SAXException;
025:
026: /**
027: * This class implements a local SAX's <code>EntityResolver</code>. All
028: * DTDs and schemas used to validate the web.xml file will re-directed
029: * to a local file stored in the servlet-api.jar and jsp-api.jar.
030: *
031: * @author Jean-Francois Arcand
032: */
033: public class SchemaResolver implements EntityResolver {
034:
035: /**
036: * The disgester instance for which this class is the entity resolver.
037: */
038: protected Digester digester;
039:
040: /**
041: * The URLs of dtds and schemas that have been registered, keyed by the
042: * public identifier that corresponds.
043: */
044: protected HashMap entityValidator = new HashMap();
045:
046: /**
047: * The public identifier of the DTD we are currently parsing under
048: * (if any).
049: */
050: protected String publicId = null;
051:
052: /**
053: * Extension to make the difference between DTD and Schema.
054: */
055: protected String schemaExtension = "xsd";
056:
057: /**
058: * Create a new <code>EntityResolver</code> that will redirect
059: * all remote dtds and schema to a locat destination.
060: * @param digester The digester instance.
061: */
062: public SchemaResolver(Digester digester) {
063: this .digester = digester;
064: }
065:
066: /**
067: * Register the specified DTD/Schema URL for the specified public
068: * identifier. This must be called before the first call to
069: * <code>parse()</code>.
070: *
071: * When adding a schema file (*.xsd), only the name of the file
072: * will get added. If two schemas with the same name are added,
073: * only the last one will be stored.
074: *
075: * @param publicId Public identifier of the DTD to be resolved
076: * @param entityURL The URL to use for reading this DTD
077: */
078: public void register(String publicId, String entityURL) {
079: String key = publicId;
080: if (publicId.indexOf(schemaExtension) != -1)
081: key = publicId.substring(publicId.lastIndexOf('/') + 1);
082: entityValidator.put(key, entityURL);
083: }
084:
085: /**
086: * Resolve the requested external entity.
087: *
088: * @param publicId The public identifier of the entity being referenced
089: * @param systemId The system identifier of the entity being referenced
090: *
091: * @exception SAXException if a parsing exception occurs
092: *
093: */
094: public InputSource resolveEntity(String publicId, String systemId)
095: throws SAXException {
096:
097: if (publicId != null) {
098: this .publicId = publicId;
099: digester.setPublicId(publicId);
100: }
101:
102: // Has this system identifier been registered?
103: String entityURL = null;
104: if (publicId != null) {
105: entityURL = (String) entityValidator.get(publicId);
106: }
107:
108: // Redirect the schema location to a local destination
109: String key = null;
110: if (entityURL == null && systemId != null) {
111: key = systemId.substring(systemId.lastIndexOf('/') + 1);
112: entityURL = (String) entityValidator.get(key);
113: }
114:
115: if (entityURL == null) {
116: return (null);
117: }
118:
119: try {
120: return (new InputSource(entityURL));
121: } catch (Exception e) {
122: throw new SAXException(e);
123: }
124:
125: }
126:
127: }
|