1 16 17 package org.apache.xerces.jaxp.validation; 18 19 import java.util.HashMap ; 20 21 import javax.xml.XMLConstants ; 22 23 import org.apache.xerces.impl.Constants; 24 import org.apache.xerces.impl.XMLEntityManager; 25 import org.apache.xerces.impl.XMLErrorReporter; 26 import org.apache.xerces.impl.validation.ValidationManager; 27 import org.apache.xerces.impl.xs.XMLSchemaValidator; 28 import org.apache.xerces.impl.xs.XSMessageFormatter; 29 import org.apache.xerces.util.DOMEntityResolverWrapper; 30 import org.apache.xerces.util.ErrorHandlerWrapper; 31 import org.apache.xerces.util.NamespaceSupport; 32 import org.apache.xerces.util.ParserConfigurationSettings; 33 import org.apache.xerces.util.SecurityManager; 34 import org.apache.xerces.util.SymbolTable; 35 import org.apache.xerces.xni.NamespaceContext; 36 import org.apache.xerces.xni.XNIException; 37 import org.apache.xerces.xni.parser.XMLComponent; 38 import org.apache.xerces.xni.parser.XMLComponentManager; 39 import org.apache.xerces.xni.parser.XMLConfigurationException; 40 import org.w3c.dom.ls.LSResourceResolver ; 41 import org.xml.sax.ErrorHandler ; 42 43 49 final class XMLSchemaValidatorComponentManager extends ParserConfigurationSettings implements 50 XMLComponentManager { 51 52 54 55 private static final String SCHEMA_VALIDATION = 56 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 57 58 59 private static final String VALIDATION = 60 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 61 62 63 private static final String USE_GRAMMAR_POOL_ONLY = 64 Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE; 65 66 68 69 private static final String ENTITY_MANAGER = 70 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 71 72 73 private static final String ENTITY_RESOLVER = 74 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 75 76 77 private static final String ERROR_HANDLER = 78 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 79 80 81 private static final String ERROR_REPORTER = 82 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 83 84 85 private static final String NAMESPACE_CONTEXT = 86 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY; 87 88 89 private static final String SCHEMA_VALIDATOR = 90 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 91 92 93 private static final String SECURITY_MANAGER = 94 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 95 96 97 private static final String SYMBOL_TABLE = 98 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 99 100 101 private static final String VALIDATION_MANAGER = 102 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 103 104 105 private static final String XMLGRAMMAR_POOL = 106 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 107 108 112 116 private boolean fConfigUpdated = true; 117 118 122 private boolean fUseGrammarPoolOnly; 123 124 125 private final HashMap fComponents = new HashMap (); 126 127 131 132 private XMLEntityManager fEntityManager; 133 134 135 private XMLErrorReporter fErrorReporter; 136 137 138 private NamespaceContext fNamespaceContext; 139 140 141 private XMLSchemaValidator fSchemaValidator; 142 143 144 private ValidationManager fValidationManager; 145 146 150 151 private ErrorHandler fErrorHandler = null; 152 153 154 private LSResourceResolver fResourceResolver = null; 155 156 157 public XMLSchemaValidatorComponentManager(XSGrammarPoolContainer grammarContainer) { 158 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 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 fComponents.put(XMLGRAMMAR_POOL, grammarContainer.getGrammarPool()); 182 fUseGrammarPoolOnly = grammarContainer.isFullyComposed(); 183 184 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter()); 186 187 addRecognizedParamsAndSetDefaults(fEntityManager); 189 addRecognizedParamsAndSetDefaults(fErrorReporter); 190 addRecognizedParamsAndSetDefaults(fSchemaValidator); 191 } 192 193 205 public boolean getFeature(String 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 230 public void setFeature(String 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 () : 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 263 public Object getProperty(String propertyId) 264 throws XMLConfigurationException { 265 final Object 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 283 public void setProperty(String propertyId, Object 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 311 public void addRecognizedParamsAndSetDefaults(XMLComponent component) { 312 313 final String [] recognizedFeatures = component.getRecognizedFeatures(); 315 addRecognizedFeatures(recognizedFeatures); 316 317 final String [] recognizedProperties = component.getRecognizedProperties(); 319 addRecognizedProperties(recognizedProperties); 320 321 setFeatureDefaults(component, recognizedFeatures); 323 setPropertyDefaults(component, recognizedProperties); 324 } 325 326 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 fConfigUpdated = false; 335 } 336 337 void setErrorHandler(ErrorHandler errorHandler) { 338 fErrorHandler = errorHandler; 339 setProperty(ERROR_HANDLER, (errorHandler != null) ? new ErrorHandlerWrapper(errorHandler) : 340 new ErrorHandlerWrapper(DraconianErrorHandler.getInstance())); 341 } 342 343 ErrorHandler getErrorHandler() { 344 return fErrorHandler; 345 } 346 347 void setResourceResolver(LSResourceResolver resourceResolver) { 348 fResourceResolver = resourceResolver; 349 setProperty(ENTITY_RESOLVER, new DOMEntityResolverWrapper(resourceResolver)); 350 } 351 352 public LSResourceResolver getResourceResolver() { 353 return fResourceResolver; 354 } 355 356 357 void restoreInitialState() { 358 fConfigUpdated = true; 359 360 fFeatures.clear(); 362 fProperties.clear(); 363 364 fComponents.put(ENTITY_RESOLVER, null); 366 fComponents.put(ERROR_HANDLER, null); 367 368 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 378 private void setFeatureDefaults(final XMLComponent component, final String [] recognizedFeatures) { 379 if (recognizedFeatures != null) { 380 for (int i = 0; i < recognizedFeatures.length; ++i) { 381 String featureId = recognizedFeatures[i]; 382 Boolean state = component.getFeatureDefault(featureId); 383 if (state != null) { 384 if (!fFeatures.containsKey(featureId)) { 386 fFeatures.put(featureId, state); 387 fConfigUpdated = true; 392 } 393 } 394 } 395 } 396 } 397 398 399 private void setPropertyDefaults(final XMLComponent component, final String [] recognizedProperties) { 400 if (recognizedProperties != null) { 401 for (int i = 0; i < recognizedProperties.length; ++i) { 402 String propertyId = recognizedProperties[i]; 403 Object value = component.getPropertyDefault(propertyId); 404 if (value != null) { 405 if (!fProperties.containsKey(propertyId)) { 407 fProperties.put(propertyId, value); 408 fConfigUpdated = true; 413 } 414 } 415 } 416 } 417 } 418 419 } | Popular Tags |