KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > jaxp > validation > XMLSchemaValidatorComponentManager


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.sun.org.apache.xerces.internal.jaxp.validation;
18
19 import java.util.HashMap JavaDoc;
20
21 import javax.xml.XMLConstants JavaDoc;
22
23 import com.sun.org.apache.xerces.internal.impl.Constants;
24 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
25 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
26 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
27 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
28 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
29 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;
30 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;
31 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
32 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
33 import com.sun.org.apache.xerces.internal.util.SecurityManager;
34 import com.sun.org.apache.xerces.internal.util.SymbolTable;
35 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
36 import com.sun.org.apache.xerces.internal.xni.XNIException;
37 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
38 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
39 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
40 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
41 import org.xml.sax.ErrorHandler JavaDoc;
42
43 /**
44  * <p>An implementation of XMLComponentManager for a schema validator.</p>
45  *
46  * @author Michael Glavassevich, IBM
47  * @version $Id: XMLSchemaValidatorComponentManager.java,v 1.1.4.1 2005/09/05 11:48:40 sunithareddy Exp $
48  */

49 final class XMLSchemaValidatorComponentManager extends ParserConfigurationSettings implements
50         XMLComponentManager {
51     
52     // feature identifiers
53

54     /** Feature identifier: schema validation. */
55     private static final String JavaDoc SCHEMA_VALIDATION =
56         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
57     
58     /** Feature identifier: validation. */
59     private static final String JavaDoc VALIDATION =
60         Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
61     
62     /** Feature identifier: use grammar pool only. */
63     private static final String JavaDoc USE_GRAMMAR_POOL_ONLY =
64         Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE;
65     
66     // property identifiers
67

68     /** Property identifier: entity manager. */
69     private static final String JavaDoc ENTITY_MANAGER =
70         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
71     
72     /** Property identifier: entity resolver. */
73     private static final String JavaDoc ENTITY_RESOLVER =
74         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
75     
76     /** Property identifier: error handler. */
77     private static final String JavaDoc ERROR_HANDLER =
78         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
79     
80     /** Property identifier: error reporter. */
81     private static final String JavaDoc ERROR_REPORTER =
82         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
83     
84     /** Property identifier: namespace context. */
85     private static final String JavaDoc NAMESPACE_CONTEXT =
86         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY;
87     
88     /** Property identifier: XML Schema validator. */
89     private static final String JavaDoc SCHEMA_VALIDATOR =
90         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
91     
92     /** Property identifier: security manager. */
93     private static final String JavaDoc SECURITY_MANAGER =
94         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
95     
96     /** Property identifier: symbol table. */
97     private static final String JavaDoc SYMBOL_TABLE =
98         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
99     
100     /** Property identifier: validation manager. */
101     private static final String JavaDoc VALIDATION_MANAGER =
102         Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
103     
104     /** Property identifier: grammar pool. */
105     private static final String JavaDoc XMLGRAMMAR_POOL =
106         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
107     
108     //
109
// Data
110
//
111

112     /**
113      * fConfigUpdated is set to true if there has been any change to the configuration settings,
114      * i.e a feature or a property was changed.
115      */

116     private boolean fConfigUpdated = true;
117     
118     /**
119      * Tracks whether the validator should use components from
120      * the grammar pool to the exclusion of all others.
121      */

122     private boolean fUseGrammarPoolOnly;
123     
124     /** Lookup map for components required for validation. **/
125     private final HashMap JavaDoc fComponents = new HashMap JavaDoc();
126     
127     //
128
// Components
129
//
130

131     /** Entity manager. */
132     private XMLEntityManager fEntityManager;
133     
134     /** Error reporter. */
135     private XMLErrorReporter fErrorReporter;
136     
137     /** Namespace context. */
138     private NamespaceContext fNamespaceContext;
139     
140     /** XML Schema validator. */
141     private XMLSchemaValidator fSchemaValidator;
142        
143     /** Validation manager. */
144     private ValidationManager fValidationManager;
145     
146     //
147
// User Objects
148
//
149

150     /** Application's ErrorHandler. **/
151     private ErrorHandler JavaDoc fErrorHandler = null;
152     
153     /** Application's LSResourceResolver. */
154     private LSResourceResolver JavaDoc fResourceResolver = null;
155     
156     /** Constructs a component manager suitable for Xerces' schema validator. */
157     public XMLSchemaValidatorComponentManager(XSGrammarPoolContainer grammarContainer) {
158         // setup components
159
fEntityManager = new XMLEntityManager();
160         fComponents.put(ENTITY_MANAGER, fEntityManager);
161         
162         fErrorReporter = new XMLErrorReporter();
163         fComponents.put(ERROR_REPORTER, fErrorReporter);
164         
165         fNamespaceContext = new NamespaceSupport();
166         fComponents.put(NAMESPACE_CONTEXT, fNamespaceContext);
167         
168         fSchemaValidator = new XMLSchemaValidator();
169         fComponents.put(SCHEMA_VALIDATOR, fSchemaValidator);
170         
171         fValidationManager = new ValidationManager();
172         fComponents.put(VALIDATION_MANAGER, fValidationManager);
173         
174         // setup other properties
175
fComponents.put(ENTITY_RESOLVER, null);
176         fComponents.put(ERROR_HANDLER, null);
177         fComponents.put(SECURITY_MANAGER, null);
178         fComponents.put(SYMBOL_TABLE, new SymbolTable());
179         
180         // setup grammar pool
181
fComponents.put(XMLGRAMMAR_POOL, grammarContainer.getGrammarPool());
182         fUseGrammarPoolOnly = grammarContainer.isFullyComposed();
183         
184         // add schema message formatter to error reporter
185
fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
186         
187         // add all recognized features and properties and apply their defaults
188
addRecognizedParamsAndSetDefaults(fEntityManager);
189         addRecognizedParamsAndSetDefaults(fErrorReporter);
190         addRecognizedParamsAndSetDefaults(fSchemaValidator);
191     }
192
193     /**
194      * Returns the state of a feature.
195      *
196      * @param featureId The feature identifier.
197      * @return true if the feature is supported
198      *
199      * @throws XMLConfigurationException Thrown for configuration error.
200      * In general, components should
201      * only throw this exception if
202      * it is <strong>really</strong>
203      * a critical error.
204      */

205     public boolean getFeature(String JavaDoc featureId)
206             throws XMLConfigurationException {
207         if (PARSER_SETTINGS.equals(featureId)) {
208             return fConfigUpdated;
209         }
210         else if (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId)) {
211             return true;
212         }
213         else if (USE_GRAMMAR_POOL_ONLY.equals(featureId)) {
214             return fUseGrammarPoolOnly;
215         }
216         else if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(featureId)) {
217             return getProperty(SECURITY_MANAGER) != null;
218         }
219         return super.getFeature(featureId);
220     }
221     
222     /**
223      * Set the state of a feature.
224      *
225      * @param featureId The unique identifier (URI) of the feature.
226      * @param state The requested state of the feature (true or false).
227      *
228      * @exception XMLConfigurationException If the requested feature is not known.
229      */

230     public void setFeature(String JavaDoc featureId, boolean value) throws XMLConfigurationException {
231         if (PARSER_SETTINGS.equals(featureId)) {
232             throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, featureId);
233         }
234         else if (value == false && (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId))) {
235             throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, featureId);
236         }
237         else if (USE_GRAMMAR_POOL_ONLY.equals(featureId) && value != fUseGrammarPoolOnly) {
238             throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, featureId);
239         }
240         fConfigUpdated = true;
241         if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(featureId)) {
242             setProperty(SECURITY_MANAGER, value ? new SecurityManager JavaDoc() : null);
243             return;
244         }
245         fEntityManager.setFeature(featureId, value);
246         fErrorReporter.setFeature(featureId, value);
247         fSchemaValidator.setFeature(featureId, value);
248         super.setFeature(featureId, value);
249     }
250     
251     /**
252      * Returns the value of a property.
253      *
254      * @param propertyId The property identifier.
255      * @return the value of the property
256      *
257      * @throws XMLConfigurationException Thrown for configuration error.
258      * In general, components should
259      * only throw this exception if
260      * it is <strong>really</strong>
261      * a critical error.
262      */

263     public Object JavaDoc getProperty(String JavaDoc propertyId)
264             throws XMLConfigurationException {
265         final Object JavaDoc component = fComponents.get(propertyId);
266         if (component != null) {
267             return component;
268         }
269         else if (fComponents.containsKey(propertyId)) {
270             return null;
271         }
272         return super.getProperty(propertyId);
273     }
274     
275     /**
276      * Sets the state of a property.
277      *
278      * @param propertyId The unique identifier (URI) of the property.
279      * @param value The requested state of the property.
280      *
281      * @exception XMLConfigurationException If the requested property is not known.
282      */

283     public void setProperty(String JavaDoc propertyId, Object JavaDoc value) throws XMLConfigurationException {
284         if ( ENTITY_MANAGER.equals(propertyId) || ERROR_REPORTER.equals(propertyId) ||
285              NAMESPACE_CONTEXT.equals(propertyId) || SCHEMA_VALIDATOR.equals(propertyId) ||
286              SYMBOL_TABLE.equals(propertyId) || VALIDATION_MANAGER.equals(propertyId) ||
287              XMLGRAMMAR_POOL.equals(propertyId)) {
288             throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, propertyId);
289         }
290         fConfigUpdated = true;
291         fEntityManager.setProperty(propertyId, value);
292         fErrorReporter.setProperty(propertyId, value);
293         fSchemaValidator.setProperty(propertyId, value);
294         if (ENTITY_RESOLVER.equals(propertyId) || ERROR_HANDLER.equals(propertyId) ||
295                 SECURITY_MANAGER.equals(propertyId)) {
296             fComponents.put(propertyId, value);
297             return;
298         }
299         super.setProperty(propertyId, value);
300     }
301     
302     /**
303      * Adds all of the component's recognized features and properties
304      * to the list of default recognized features and properties, and
305      * sets default values on the configuration for features and
306      * properties which were previously absent from the configuration.
307      *
308      * @param component The component whose recognized features
309      * and properties will be added to the configuration
310      */

311     public void addRecognizedParamsAndSetDefaults(XMLComponent component) {
312         
313         // register component's recognized features
314
final String JavaDoc[] recognizedFeatures = component.getRecognizedFeatures();
315         addRecognizedFeatures(recognizedFeatures);
316         
317         // register component's recognized properties
318
final String JavaDoc[] recognizedProperties = component.getRecognizedProperties();
319         addRecognizedProperties(recognizedProperties);
320
321         // set default values
322
setFeatureDefaults(component, recognizedFeatures);
323         setPropertyDefaults(component, recognizedProperties);
324     }
325     
326     /** Calls reset on each of the components owned by this component manager. **/
327     public void reset() throws XNIException {
328         fNamespaceContext.reset();
329         fValidationManager.reset();
330         fEntityManager.reset(this);
331         fErrorReporter.reset(this);
332         fSchemaValidator.reset(this);
333         // Mark configuration as fixed.
334
fConfigUpdated = false;
335     }
336     
337     void setErrorHandler(ErrorHandler JavaDoc errorHandler) {
338         fErrorHandler = errorHandler;
339         setProperty(ERROR_HANDLER, (errorHandler != null) ? new ErrorHandlerWrapper(errorHandler) :
340                 new ErrorHandlerWrapper(DraconianErrorHandler.getInstance()));
341     }
342     
343     ErrorHandler JavaDoc getErrorHandler() {
344         return fErrorHandler;
345     }
346     
347     void setResourceResolver(LSResourceResolver JavaDoc resourceResolver) {
348         fResourceResolver = resourceResolver;
349         setProperty(ENTITY_RESOLVER, new DOMEntityResolverWrapper(resourceResolver));
350     }
351     
352     public LSResourceResolver JavaDoc getResourceResolver() {
353         return fResourceResolver;
354     }
355     
356     /** Cleans out configuration, restoring it to its initial state. */
357     void restoreInitialState() {
358         fConfigUpdated = true;
359         
360         // Clear feature and property tables.
361
fFeatures.clear();
362         fProperties.clear();
363         
364         // Remove error resolver and error handler
365
fComponents.put(ENTITY_RESOLVER, null);
366         fComponents.put(ERROR_HANDLER, null);
367         
368         // Restore component defaults.
369
setFeatureDefaults(fEntityManager, fEntityManager.getRecognizedFeatures());
370         setPropertyDefaults(fEntityManager, fEntityManager.getRecognizedProperties());
371         setFeatureDefaults(fErrorReporter, fErrorReporter.getRecognizedFeatures());
372         setPropertyDefaults(fErrorReporter, fErrorReporter.getRecognizedProperties());
373         setFeatureDefaults(fSchemaValidator, fSchemaValidator.getRecognizedFeatures());
374         setPropertyDefaults(fSchemaValidator, fSchemaValidator.getRecognizedProperties());
375     }
376     
377     /** Sets feature defaults for the given component on this configuration. */
378     private void setFeatureDefaults(final XMLComponent component, final String JavaDoc [] recognizedFeatures) {
379         if (recognizedFeatures != null) {
380             for (int i = 0; i < recognizedFeatures.length; ++i) {
381                 String JavaDoc featureId = recognizedFeatures[i];
382                 Boolean JavaDoc state = component.getFeatureDefault(featureId);
383                 if (state != null) {
384                     // Do not overwrite values already set on the configuration.
385
if (!fFeatures.containsKey(featureId)) {
386                         fFeatures.put(featureId, state);
387                         // For newly added components who recognize this feature
388
// but did not offer a default value, we need to make
389
// sure these components will get an opportunity to read
390
// the value before parsing begins.
391
fConfigUpdated = true;
392                     }
393                 }
394             }
395         }
396     }
397     
398     /** Sets property defaults for the given component on this configuration. */
399     private void setPropertyDefaults(final XMLComponent component, final String JavaDoc [] recognizedProperties) {
400         if (recognizedProperties != null) {
401             for (int i = 0; i < recognizedProperties.length; ++i) {
402                 String JavaDoc propertyId = recognizedProperties[i];
403                 Object JavaDoc value = component.getPropertyDefault(propertyId);
404                 if (value != null) {
405                     // Do not overwrite values already set on the configuration.
406
if (!fProperties.containsKey(propertyId)) {
407                         fProperties.put(propertyId, value);
408                         // For newly added components who recognize this property
409
// but did not offer a default value, we need to make
410
// sure these components will get an opportunity to read
411
// the value before parsing begins.
412
fConfigUpdated = true;
413                     }
414                 }
415             }
416         }
417     }
418     
419 } // XMLSchemaValidatorComponentManager
420
Popular Tags