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:
018: package org.apache.catalina.startup;
019:
020: import java.net.URL;
021:
022: import org.apache.catalina.util.SchemaResolver;
023: import org.apache.tomcat.util.digester.Digester;
024: import org.apache.tomcat.util.digester.RuleSet;
025:
026: /**
027: * Wrapper class around the Digester that hide Digester's initialization details
028: *
029: * @author Jean-Francois Arcand
030: */
031: public class DigesterFactory {
032: /**
033: * The log.
034: */
035: protected static org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory
036: .getLog(DigesterFactory.class);
037:
038: /**
039: * The XML entiry resolver used by the Digester.
040: */
041: private static SchemaResolver schemaResolver;
042:
043: /**
044: * Create a <code>Digester</code> parser with no <code>Rule</code>
045: * associated and XML validation turned off.
046: */
047: public static Digester newDigester() {
048: return newDigester(false, false, null);
049: }
050:
051: /**
052: * Create a <code>Digester</code> parser with XML validation turned off.
053: * @param rule an instance of <code>RuleSet</code> used for parsing the xml.
054: */
055: public static Digester newDigester(RuleSet rule) {
056: return newDigester(false, false, rule);
057: }
058:
059: /**
060: * Create a <code>Digester</code> parser.
061: * @param xmlValidation turn on/off xml validation
062: * @param xmlNamespaceAware turn on/off namespace validation
063: * @param rule an instance of <code>RuleSet</code> used for parsing the xml.
064: */
065: public static Digester newDigester(boolean xmlValidation,
066: boolean xmlNamespaceAware, RuleSet rule) {
067: Digester digester = new Digester();
068: digester.setNamespaceAware(xmlNamespaceAware);
069: digester.setValidating(xmlValidation);
070: digester.setUseContextClassLoader(true);
071:
072: if (xmlValidation || xmlNamespaceAware) {
073: configureSchema(digester);
074: }
075:
076: schemaResolver = new SchemaResolver(digester);
077: registerLocalSchema();
078:
079: digester.setEntityResolver(schemaResolver);
080: if (rule != null) {
081: digester.addRuleSet(rule);
082: }
083:
084: return (digester);
085: }
086:
087: /**
088: * Utilities used to force the parser to use local schema, when available,
089: * instead of the <code>schemaLocation</code> XML element.
090: */
091: protected static void registerLocalSchema() {
092: // J2EE
093: register(Constants.J2eeSchemaResourcePath_14,
094: Constants.J2eeSchemaPublicId_14);
095: // W3C
096: register(Constants.W3cSchemaResourcePath_10,
097: Constants.W3cSchemaPublicId_10);
098: // JSP
099: register(Constants.JspSchemaResourcePath_20,
100: Constants.JspSchemaPublicId_20);
101:
102: register(Constants.JspSchemaResourcePath_21,
103: Constants.JspSchemaPublicId_21);
104:
105: // TLD
106: register(Constants.TldDtdResourcePath_11,
107: Constants.TldDtdPublicId_11);
108:
109: register(Constants.TldDtdResourcePath_12,
110: Constants.TldDtdPublicId_12);
111:
112: register(Constants.TldSchemaResourcePath_20,
113: Constants.TldSchemaPublicId_20);
114:
115: register(Constants.TldSchemaResourcePath_21,
116: Constants.TldSchemaPublicId_21);
117:
118: // web.xml
119: register(Constants.WebDtdResourcePath_22,
120: Constants.WebDtdPublicId_22);
121:
122: register(Constants.WebDtdResourcePath_23,
123: Constants.WebDtdPublicId_23);
124:
125: register(Constants.WebSchemaResourcePath_24,
126: Constants.WebSchemaPublicId_24);
127:
128: register(Constants.WebSchemaResourcePath_25,
129: Constants.WebSchemaPublicId_25);
130:
131: // Web Service
132: register(Constants.J2eeWebServiceSchemaResourcePath_11,
133: Constants.J2eeWebServiceSchemaPublicId_11);
134:
135: register(Constants.J2eeWebServiceClientSchemaResourcePath_11,
136: Constants.J2eeWebServiceClientSchemaPublicId_11);
137:
138: }
139:
140: /**
141: * Load the resource and add it to the resolver.
142: */
143: protected static void register(String resourceURL,
144: String resourcePublicId) {
145: URL url = DigesterFactory.class.getResource(resourceURL);
146:
147: if (url == null) {
148: log.warn("Could not get url for " + resourceURL);
149: } else {
150: schemaResolver.register(resourcePublicId, url.toString());
151: }
152: }
153:
154: /**
155: * Turn on DTD and/or validation (based on the parser implementation)
156: */
157: protected static void configureSchema(Digester digester) {
158: URL url = DigesterFactory.class
159: .getResource(Constants.WebSchemaResourcePath_24);
160:
161: if (url == null) {
162: log.error("Could not get url for "
163: + Constants.WebSchemaResourcePath_24);
164: } else {
165: digester.setSchema(url.toString());
166: }
167: }
168: }
|