KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > DigesterFactory


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
18
19 package org.apache.catalina.startup;
20
21 import java.net.URL JavaDoc;
22
23 import org.apache.catalina.util.SchemaResolver;
24 import org.apache.tomcat.util.digester.Digester;
25 import org.apache.tomcat.util.digester.RuleSet;
26
27 /**
28  * Wrapper class around the Digester that hide Digester's initialization details
29  *
30  * @author Jean-Francois Arcand
31  */

32 public class DigesterFactory {
33     /**
34      * The log.
35      */

36    protected static org.apache.commons.logging.Log log =
37        org.apache.commons.logging.LogFactory.getLog(DigesterFactory.class);
38
39     /**
40      * The XML entiry resolver used by the Digester.
41      */

42     private static SchemaResolver schemaResolver;
43
44
45     /**
46      * Create a <code>Digester</code> parser with no <code>Rule</code>
47      * associated and XML validation turned off.
48      */

49     public static Digester newDigester(){
50         return newDigester(false, false, null);
51     }
52
53     
54     /**
55      * Create a <code>Digester</code> parser with XML validation turned off.
56      * @param rule an instance of <code>RuleSet</code> used for parsing the xml.
57      */

58     public static Digester newDigester(RuleSet rule){
59         return newDigester(false,false,rule);
60     }
61
62     
63     /**
64      * Create a <code>Digester</code> parser.
65      * @param xmlValidation turn on/off xml validation
66      * @param xmlNamespaceAware turn on/off namespace validation
67      * @param rule an instance of <code>RuleSet</code> used for parsing the xml.
68      */

69     public static Digester newDigester(boolean xmlValidation,
70                                        boolean xmlNamespaceAware,
71                                        RuleSet rule) {
72         Digester digester = new Digester();
73         digester.setNamespaceAware(xmlNamespaceAware);
74         digester.setValidating(xmlValidation);
75         digester.setUseContextClassLoader(true);
76
77         if (xmlValidation || xmlNamespaceAware){
78             configureSchema(digester);
79         }
80
81         schemaResolver = new SchemaResolver(digester);
82         registerLocalSchema();
83         
84         digester.setEntityResolver(schemaResolver);
85         if ( rule != null ) {
86             digester.addRuleSet(rule);
87         }
88
89         return (digester);
90     }
91
92
93     /**
94      * Utilities used to force the parser to use local schema, when available,
95      * instead of the <code>schemaLocation</code> XML element.
96      */

97     protected static void registerLocalSchema(){
98         // J2EE
99
register(Constants.J2eeSchemaResourcePath_14,
100                  Constants.J2eeSchemaPublicId_14);
101         // W3C
102
register(Constants.W3cSchemaResourcePath_10,
103                  Constants.W3cSchemaPublicId_10);
104         // JSP
105
register(Constants.JspSchemaResourcePath_20,
106                  Constants.JspSchemaPublicId_20);
107         // TLD
108
register(Constants.TldDtdResourcePath_11,
109                  Constants.TldDtdPublicId_11);
110         
111         register(Constants.TldDtdResourcePath_12,
112                  Constants.TldDtdPublicId_12);
113
114         register(Constants.TldSchemaResourcePath_20,
115                  Constants.TldSchemaPublicId_20);
116
117         // web.xml
118
register(Constants.WebDtdResourcePath_22,
119                  Constants.WebDtdPublicId_22);
120
121         register(Constants.WebDtdResourcePath_23,
122                  Constants.WebDtdPublicId_23);
123
124         register(Constants.WebSchemaResourcePath_24,
125                  Constants.WebSchemaPublicId_24);
126
127         // Web Service
128
register(Constants.J2eeWebServiceSchemaResourcePath_11,
129                  Constants.J2eeWebServiceSchemaPublicId_11);
130
131         register(Constants.J2eeWebServiceClientSchemaResourcePath_11,
132                  Constants.J2eeWebServiceClientSchemaPublicId_11);
133
134     }
135
136
137     /**
138      * Load the resource and add it to the resolver.
139      */

140     protected static void register(String JavaDoc resourceURL, String JavaDoc resourcePublicId){
141         URL JavaDoc url = DigesterFactory.class.getResource(resourceURL);
142    
143         if(url == null) {
144             log.warn("Could not get url for " + resourceURL);
145         } else {
146             schemaResolver.register(resourcePublicId , url.toString() );
147         }
148     }
149
150
151     /**
152      * Turn on DTD and/or validation (based on the parser implementation)
153      */

154     protected static void configureSchema(Digester digester){
155         URL JavaDoc url = DigesterFactory.class
156                         .getResource(Constants.WebSchemaResourcePath_24);
157   
158         if(url == null) {
159             log.error("Could not get url for "
160                                         + Constants.WebSchemaResourcePath_24);
161         } else {
162             digester.setSchema(url.toString());
163         }
164     }
165 }
166
Popular Tags