1 /*
2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xerces.internal.jaxp.validation;
22
23 import java.util.HashMap;
24 import java.util.Locale;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import javax.xml.XMLConstants;
29
30 import com.sun.org.apache.xerces.internal.impl.Constants;
31 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
32 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
33 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
34 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
35 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
36 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;
37 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;
38 import com.sun.org.apache.xerces.internal.util.FeatureState;
39 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
40 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
41 import com.sun.org.apache.xerces.internal.util.PropertyState;
42 import com.sun.org.apache.xerces.internal.util.Status;
43 import com.sun.org.apache.xerces.internal.util.SymbolTable;
44 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
45 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
46 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
47 import com.sun.org.apache.xerces.internal.xni.XNIException;
48 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
49 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
50 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
51 import org.w3c.dom.ls.LSResourceResolver;
52 import org.xml.sax.ErrorHandler;
53
54 /**
55 * <p>An implementation of XMLComponentManager for a schema validator.</p>
56 *
57 * @author Michael Glavassevich, IBM
58 */
59 final class XMLSchemaValidatorComponentManager extends ParserConfigurationSettings implements
60 XMLComponentManager {
61
62 // feature identifiers
63
64 /** Feature identifier: schema validation. */
65 private static final String SCHEMA_VALIDATION =
66 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
67
68 /** Feature identifier: validation. */
69 private static final String VALIDATION =
70 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
159 // Data
160 //
161 /**
162 * <p>State of secure mode.</p>
163 */
164 private boolean _isSecureMode = false;
165
166 /**
167 * fConfigUpdated is set to true if there has been any change to the configuration settings,
168 * i.e a feature or a property was changed.
169 */
170 private boolean fConfigUpdated = true;
171
172 /**
173 * Tracks whether the validator should use components from
174 * the grammar pool to the exclusion of all others.
175 */
176 private boolean fUseGrammarPoolOnly;
177
178 /** Lookup map for components required for validation. **/
179 private final HashMap fComponents = new HashMap();
180
181 //
182 // Components
183 //
184
185 /** Entity manager. */
186 private XMLEntityManager fEntityManager;
187
188 /** Error reporter. */
189 private XMLErrorReporter fErrorReporter;
190
191 /** Namespace context. */
192 private NamespaceContext fNamespaceContext;
193
194 /** XML Schema validator. */
195 private XMLSchemaValidator fSchemaValidator;
196
197 /** Validation manager. */
198 private ValidationManager fValidationManager;
199
200 //
201 // Configuration
202 //
203
204 /** Stores initial feature values for validator reset. */
205 private final HashMap fInitFeatures = new HashMap();
206
207 /** Stores initial property values for validator reset. */
208 private final HashMap fInitProperties = new HashMap();
209
210 /** Stores the initial security manager. */
211 private XMLSecurityManager fInitSecurityManager;
212
213 /** Stores the initial security property manager. */
214 private final XMLSecurityPropertyManager fSecurityPropertyMgr;
215
216 //
217 // User Objects
218 //
219
220 /** Application's ErrorHandler. **/
221 private ErrorHandler fErrorHandler = null;
222
223 /** Application's LSResourceResolver. */
224 private LSResourceResolver fResourceResolver = null;
225
226 /** Locale chosen by the application. */
227 private Locale fLocale = null;
228
245 fComponents.put(VALIDATION_MANAGER, fValidationManager);
246
247 // setup other properties
248 fComponents.put(ENTITY_RESOLVER, null);
249 fComponents.put(ERROR_HANDLER, null);
250
251 fComponents.put(SYMBOL_TABLE, new SymbolTable());
252
253 // setup grammar pool
254 fComponents.put(XMLGRAMMAR_POOL, grammarContainer.getGrammarPool());
255 fUseGrammarPoolOnly = grammarContainer.isFullyComposed();
256
257 // add schema message formatter to error reporter
258 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
259
260 // add all recognized features and properties and apply their defaults
261 final String [] recognizedFeatures = {
262 DISALLOW_DOCTYPE_DECL_FEATURE,
263 NORMALIZE_DATA,
264 SCHEMA_ELEMENT_DEFAULT,
265 SCHEMA_AUGMENT_PSVI
266 };
267 addRecognizedFeatures(recognizedFeatures);
268 fFeatures.put(DISALLOW_DOCTYPE_DECL_FEATURE, Boolean.FALSE);
269 fFeatures.put(NORMALIZE_DATA, Boolean.FALSE);
270 fFeatures.put(SCHEMA_ELEMENT_DEFAULT, Boolean.FALSE);
271 fFeatures.put(SCHEMA_AUGMENT_PSVI, Boolean.TRUE);
272
273 addRecognizedParamsAndSetDefaults(fEntityManager, grammarContainer);
274 addRecognizedParamsAndSetDefaults(fErrorReporter, grammarContainer);
275 addRecognizedParamsAndSetDefaults(fSchemaValidator, grammarContainer);
276
277 /* TODO: are other XMLSchemaValidator default values never set?
278 * Initial investigation indicates that they aren't set, but
279 * that they all have default values of false, so it works out
280 * anyway -PM
281 */
282 fFeatures.put(IGNORE_XSI_TYPE, Boolean.FALSE);
283 fFeatures.put(ID_IDREF_CHECKING, Boolean.TRUE);
284 fFeatures.put(IDENTITY_CONSTRAINT_CHECKING, Boolean.TRUE);
285 fFeatures.put(UNPARSED_ENTITY_CHECKING, Boolean.TRUE);
286
287 boolean secureProcessing = grammarContainer.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
288 if (System.getSecurityManager() != null) {
289 _isSecureMode = true;
290 secureProcessing = true;
291 }
292
293 fInitSecurityManager = (XMLSecurityManager)
294 grammarContainer.getProperty(SECURITY_MANAGER);
295 if (fInitSecurityManager != null ) {
296 fInitSecurityManager.setSecureProcessing(secureProcessing);
297 } else {
298 fInitSecurityManager = new XMLSecurityManager(secureProcessing);
299 }
300
301 setProperty(SECURITY_MANAGER, fInitSecurityManager);
302
303 //pass on properties set on SchemaFactory
304 fSecurityPropertyMgr = (XMLSecurityPropertyManager)
305 grammarContainer.getProperty(Constants.XML_SECURITY_PROPERTY_MANAGER);
306 setProperty(XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
307 }
308
309 /**
310 * Returns the state of a feature.
311 *
312 * @param featureId The feature identifier.
313 * @return true if the feature is supported
314 *
315 * @throws XMLConfigurationException Thrown for configuration error.
316 * In general, components should
317 * only throw this exception if
318 * it is <strong>really</strong>
319 * a critical error.
320 */
321 public FeatureState getFeatureState(String featureId)
322 throws XMLConfigurationException {
323 if (PARSER_SETTINGS.equals(featureId)) {
324 return FeatureState.is(fConfigUpdated);
325 }
326 else if (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId)) {
524 fConfigUpdated = true;
525
526 // Remove error resolver and error handler
527 fComponents.put(ENTITY_RESOLVER, null);
528 fComponents.put(ERROR_HANDLER, null);
529
530 // Set the Locale back to null.
531 setLocale(null);
532 fComponents.put(LOCALE, null);
533
534 // Restore initial security manager
535 fInitSecurityManager.setSecureProcessing(true);
536 fComponents.put(SECURITY_MANAGER, fInitSecurityManager);
537
538 // Set the Locale back to null.
539 setLocale(null);
540 fComponents.put(LOCALE, null);
541
542 // Reset feature and property values to their initial values
543 if (!fInitFeatures.isEmpty()) {
544 Iterator iter = fInitFeatures.entrySet().iterator();
545 while (iter.hasNext()) {
546 Map.Entry entry = (Map.Entry) iter.next();
547 String name = (String) entry.getKey();
548 boolean value = ((Boolean) entry.getValue()).booleanValue();
549 super.setFeature(name, value);
550 }
551 fInitFeatures.clear();
552 }
553 if (!fInitProperties.isEmpty()) {
554 Iterator iter = fInitProperties.entrySet().iterator();
555 while (iter.hasNext()) {
556 Map.Entry entry = (Map.Entry) iter.next();
557 String name = (String) entry.getKey();
558 Object value = entry.getValue();
559 super.setProperty(name, value);
560 }
561 fInitProperties.clear();
562 }
563 }
564
565 /** Sets feature defaults for the given component on this configuration. */
566 private void setFeatureDefaults(final XMLComponent component,
567 final String [] recognizedFeatures, XSGrammarPoolContainer grammarContainer) {
568 if (recognizedFeatures != null) {
569 for (int i = 0; i < recognizedFeatures.length; ++i) {
570 String featureId = recognizedFeatures[i];
571 Boolean state = grammarContainer.getFeature(featureId);
572 if (state == null) {
573 state = component.getFeatureDefault(featureId);
574 }
575 if (state != null) {
576 // Do not overwrite values already set on the configuration.
577 if (!fFeatures.containsKey(featureId)) {
|
1 /*
2 * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xerces.internal.jaxp.validation;
22
23 import java.util.HashMap;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import javax.xml.XMLConstants;
28
29 import com.sun.org.apache.xerces.internal.impl.Constants;
30 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
31 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
32 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
33 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
34 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
35 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;
36 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;
37 import com.sun.org.apache.xerces.internal.util.FeatureState;
38 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
39 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
40 import com.sun.org.apache.xerces.internal.util.PropertyState;
41 import com.sun.org.apache.xerces.internal.util.Status;
42 import com.sun.org.apache.xerces.internal.util.SymbolTable;
43 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
44 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
45 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
46 import com.sun.org.apache.xerces.internal.xni.XNIException;
47 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
48 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
49 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
50 import javax.xml.catalog.CatalogFeatures;
51 import org.w3c.dom.ls.LSResourceResolver;
52 import org.xml.sax.ErrorHandler;
53
54 /**
55 * <p>An implementation of XMLComponentManager for a schema validator.</p>
56 *
57 * @author Michael Glavassevich, IBM
58 */
59 final class XMLSchemaValidatorComponentManager extends ParserConfigurationSettings implements
60 XMLComponentManager {
61
62 // feature identifiers
63
64 /** Feature identifier: schema validation. */
65 private static final String SCHEMA_VALIDATION =
66 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
67
68 /** Feature identifier: validation. */
69 private static final String VALIDATION =
70 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
159 // Data
160 //
161 /**
162 * <p>State of secure mode.</p>
163 */
164 private boolean _isSecureMode = false;
165
166 /**
167 * fConfigUpdated is set to true if there has been any change to the configuration settings,
168 * i.e a feature or a property was changed.
169 */
170 private boolean fConfigUpdated = true;
171
172 /**
173 * Tracks whether the validator should use components from
174 * the grammar pool to the exclusion of all others.
175 */
176 private boolean fUseGrammarPoolOnly;
177
178 /** Lookup map for components required for validation. **/
179 private final HashMap<String, Object> fComponents = new HashMap<>();
180
181 //
182 // Components
183 //
184
185 /** Entity manager. */
186 private XMLEntityManager fEntityManager;
187
188 /** Error reporter. */
189 private XMLErrorReporter fErrorReporter;
190
191 /** Namespace context. */
192 private NamespaceContext fNamespaceContext;
193
194 /** XML Schema validator. */
195 private XMLSchemaValidator fSchemaValidator;
196
197 /** Validation manager. */
198 private ValidationManager fValidationManager;
199
200 //
201 // Configuration
202 //
203
204 /** Stores initial feature values for validator reset. */
205 private final HashMap<String, Boolean> fInitFeatures = new HashMap<>();
206
207 /** Stores initial property values for validator reset. */
208 private final HashMap<String, Object> fInitProperties = new HashMap<>();
209
210 /** Stores the initial security manager. */
211 private XMLSecurityManager fInitSecurityManager;
212
213 /** Stores the initial security property manager. */
214 private final XMLSecurityPropertyManager fSecurityPropertyMgr;
215
216 //
217 // User Objects
218 //
219
220 /** Application's ErrorHandler. **/
221 private ErrorHandler fErrorHandler = null;
222
223 /** Application's LSResourceResolver. */
224 private LSResourceResolver fResourceResolver = null;
225
226 /** Locale chosen by the application. */
227 private Locale fLocale = null;
228
245 fComponents.put(VALIDATION_MANAGER, fValidationManager);
246
247 // setup other properties
248 fComponents.put(ENTITY_RESOLVER, null);
249 fComponents.put(ERROR_HANDLER, null);
250
251 fComponents.put(SYMBOL_TABLE, new SymbolTable());
252
253 // setup grammar pool
254 fComponents.put(XMLGRAMMAR_POOL, grammarContainer.getGrammarPool());
255 fUseGrammarPoolOnly = grammarContainer.isFullyComposed();
256
257 // add schema message formatter to error reporter
258 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
259
260 // add all recognized features and properties and apply their defaults
261 final String [] recognizedFeatures = {
262 DISALLOW_DOCTYPE_DECL_FEATURE,
263 NORMALIZE_DATA,
264 SCHEMA_ELEMENT_DEFAULT,
265 SCHEMA_AUGMENT_PSVI,
266 XMLConstants.USE_CATALOG
267 };
268 addRecognizedFeatures(recognizedFeatures);
269 fFeatures.put(DISALLOW_DOCTYPE_DECL_FEATURE, Boolean.FALSE);
270 fFeatures.put(NORMALIZE_DATA, Boolean.FALSE);
271 fFeatures.put(SCHEMA_ELEMENT_DEFAULT, Boolean.FALSE);
272 fFeatures.put(SCHEMA_AUGMENT_PSVI, Boolean.TRUE);
273 fFeatures.put(XMLConstants.USE_CATALOG, grammarContainer.getFeature(XMLConstants.USE_CATALOG));
274
275 addRecognizedParamsAndSetDefaults(fEntityManager, grammarContainer);
276 addRecognizedParamsAndSetDefaults(fErrorReporter, grammarContainer);
277 addRecognizedParamsAndSetDefaults(fSchemaValidator, grammarContainer);
278
279 /* TODO: are other XMLSchemaValidator default values never set?
280 * Initial investigation indicates that they aren't set, but
281 * that they all have default values of false, so it works out
282 * anyway -PM
283 */
284 fFeatures.put(IGNORE_XSI_TYPE, Boolean.FALSE);
285 fFeatures.put(ID_IDREF_CHECKING, Boolean.TRUE);
286 fFeatures.put(IDENTITY_CONSTRAINT_CHECKING, Boolean.TRUE);
287 fFeatures.put(UNPARSED_ENTITY_CHECKING, Boolean.TRUE);
288
289 boolean secureProcessing = grammarContainer.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
290 if (System.getSecurityManager() != null) {
291 _isSecureMode = true;
292 secureProcessing = true;
293 }
294
295 fInitSecurityManager = (XMLSecurityManager)
296 grammarContainer.getProperty(SECURITY_MANAGER);
297 if (fInitSecurityManager != null ) {
298 fInitSecurityManager.setSecureProcessing(secureProcessing);
299 } else {
300 fInitSecurityManager = new XMLSecurityManager(secureProcessing);
301 }
302
303 setProperty(SECURITY_MANAGER, fInitSecurityManager);
304
305 //pass on properties set on SchemaFactory
306 fSecurityPropertyMgr = (XMLSecurityPropertyManager)
307 grammarContainer.getProperty(Constants.XML_SECURITY_PROPERTY_MANAGER);
308 setProperty(XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
309
310 //initialize Catalog properties
311 for( CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
312 setProperty(f.getPropertyName(), grammarContainer.getProperty(f.getPropertyName()));
313 }
314 }
315
316 /**
317 * Returns the state of a feature.
318 *
319 * @param featureId The feature identifier.
320 * @return true if the feature is supported
321 *
322 * @throws XMLConfigurationException Thrown for configuration error.
323 * In general, components should
324 * only throw this exception if
325 * it is <strong>really</strong>
326 * a critical error.
327 */
328 public FeatureState getFeatureState(String featureId)
329 throws XMLConfigurationException {
330 if (PARSER_SETTINGS.equals(featureId)) {
331 return FeatureState.is(fConfigUpdated);
332 }
333 else if (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId)) {
531 fConfigUpdated = true;
532
533 // Remove error resolver and error handler
534 fComponents.put(ENTITY_RESOLVER, null);
535 fComponents.put(ERROR_HANDLER, null);
536
537 // Set the Locale back to null.
538 setLocale(null);
539 fComponents.put(LOCALE, null);
540
541 // Restore initial security manager
542 fInitSecurityManager.setSecureProcessing(true);
543 fComponents.put(SECURITY_MANAGER, fInitSecurityManager);
544
545 // Set the Locale back to null.
546 setLocale(null);
547 fComponents.put(LOCALE, null);
548
549 // Reset feature and property values to their initial values
550 if (!fInitFeatures.isEmpty()) {
551 for (Map.Entry<String, Boolean> entry : fInitFeatures.entrySet()) {
552 String name = entry.getKey();
553 boolean value = entry.getValue();
554 super.setFeature(name, value);
555 }
556 fInitFeatures.clear();
557 }
558 if (!fInitProperties.isEmpty()) {
559 for (Map.Entry<String, Object> entry : fInitProperties.entrySet()) {
560 String name = entry.getKey();
561 Object value = entry.getValue();
562 super.setProperty(name, value);
563 }
564 fInitProperties.clear();
565 }
566 }
567
568 /** Sets feature defaults for the given component on this configuration. */
569 private void setFeatureDefaults(final XMLComponent component,
570 final String [] recognizedFeatures, XSGrammarPoolContainer grammarContainer) {
571 if (recognizedFeatures != null) {
572 for (int i = 0; i < recognizedFeatures.length; ++i) {
573 String featureId = recognizedFeatures[i];
574 Boolean state = grammarContainer.getFeature(featureId);
575 if (state == null) {
576 state = component.getFeatureDefault(featureId);
577 }
578 if (state != null) {
579 // Do not overwrite values already set on the configuration.
580 if (!fFeatures.containsKey(featureId)) {
|