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 package org.apache.xerces.parsers; 19 20 import org.apache.xerces.impl.Constants; 21 import org.apache.xerces.impl.xs.XMLSchemaValidator; 22 import org.apache.xerces.impl.xs.XSMessageFormatter; 23 import org.apache.xerces.util.SymbolTable; 24 import org.apache.xerces.xni.grammars.XMLGrammarPool; 25 import org.apache.xerces.xni.parser.XMLComponentManager; 26 import org.apache.xerces.xni.parser.XMLConfigurationException; 27 28 /** 29 * This is the "standard" parser configuration. It extends the DTD 30 * configuration with the standard set of parser components. 31 * The standard set of parser components include those needed 32 * to parse and validate with DTD's, and those needed for XML 33 * Schema.</p> 34 * <p> 35 * In addition to the features and properties recognized by the base 36 * parser configuration, this class recognizes these additional 37 * features and properties: 38 * <ul> 39 * <li>Features 40 * <ul> 41 * <li>http://apache.org/xml/features/validation/schema</li> 42 * <li>http://apache.org/xml/features/validation/schema-full-checking</li> 43 * <li>http://apache.org/xml/features/validation/schema/normalized-value</li> 44 * <li>http://apache.org/xml/features/validation/schema/element-default</li> 45 * </ul> 46 * <li>Properties 47 * <ul> 48 * <li>http://apache.org/xml/properties/internal/error-reporter</li> 49 * <li>http://apache.org/xml/properties/internal/entity-manager</li> 50 * <li>http://apache.org/xml/properties/internal/document-scanner</li> 51 * <li>http://apache.org/xml/properties/internal/dtd-scanner</li> 52 * <li>http://apache.org/xml/properties/internal/grammar-pool</li> 53 * <li>http://apache.org/xml/properties/internal/validator/dtd</li> 54 * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li> 55 * </ul> 56 * </ul> 57 * 58 * @author Arnaud Le Hors, IBM 59 * @author Andy Clark, IBM 60 * 61 * @version $Id: StandardParserConfiguration.java 447239 2006-09-18 05:08:26Z mrglavas $ 62 */ 63 public class StandardParserConfiguration 64 extends DTDConfiguration { 65 66 // 67 // Constants 68 // 69 70 // feature identifiers 71 72 /** Feature identifier: expose schema normalized value */ 73 protected static final String NORMALIZE_DATA = 74 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 75 76 77 /** Feature identifier: send element default value via characters() */ 78 protected static final String SCHEMA_ELEMENT_DEFAULT = 79 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 80 81 82 /** Feature identifier: augment PSVI */ 83 protected static final String SCHEMA_AUGMENT_PSVI = 84 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; 85 86 87 /** feature identifier: XML Schema validation */ 88 protected static final String XMLSCHEMA_VALIDATION = 89 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 90 91 /** feature identifier: XML Schema validation -- full checking */ 92 protected static final String XMLSCHEMA_FULL_CHECKING = 93 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 94 95 /** Feature: generate synthetic annotations */ 96 protected static final String GENERATE_SYNTHETIC_ANNOTATIONS = 97 Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE; 98 99 /** Feature identifier: validate annotations */ 100 protected static final String VALIDATE_ANNOTATIONS = 101 Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE; 102 103 /** Feature identifier: honour all schemaLocations */ 104 protected static final String HONOUR_ALL_SCHEMALOCATIONS = 105 Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE; 106 107 /** Feature identifier: whether to ignore xsi:type attributes until a global element declaration is encountered */ 108 protected static final String IGNORE_XSI_TYPE = 109 Constants.XERCES_FEATURE_PREFIX + Constants.IGNORE_XSI_TYPE_FEATURE; 110 111 /** Feature identifier: whether to ignore ID/IDREF errors */ 112 protected static final String ID_IDREF_CHECKING = 113 Constants.XERCES_FEATURE_PREFIX + Constants.ID_IDREF_CHECKING_FEATURE; 114 115 /** Feature identifier: whether to ignore unparsed entity errors */ 116 protected static final String UNPARSED_ENTITY_CHECKING = 117 Constants.XERCES_FEATURE_PREFIX + Constants.UNPARSED_ENTITY_CHECKING_FEATURE; 118 119 /** Feature identifier: whether to ignore identity constraint errors */ 120 protected static final String IDENTITY_CONSTRAINT_CHECKING = 121 Constants.XERCES_FEATURE_PREFIX + Constants.IDC_CHECKING_FEATURE; 122 123 // property identifiers 124 125 /** Property identifier: XML Schema validator. */ 126 protected static final String SCHEMA_VALIDATOR = 127 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 128 129 /** Property identifier: schema location. */ 130 protected static final String SCHEMA_LOCATION = 131 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION; 132 133 /** Property identifier: no namespace schema location. */ 134 protected static final String SCHEMA_NONS_LOCATION = 135 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION; 136 137 /** Property identifier: root type definition. */ 138 protected static final String ROOT_TYPE_DEF = 139 Constants.XERCES_PROPERTY_PREFIX + Constants.ROOT_TYPE_DEFINITION_PROPERTY; 140 141 // 142 // Data 143 // 144 145 // components (non-configurable) 146 147 /** XML Schema Validator. */ 148 protected XMLSchemaValidator fSchemaValidator; 149 150 // 151 // Constructors 152 // 153 154 /** Default constructor. */ 155 public StandardParserConfiguration() { 156 this(null, null, null); 157 } // <init>() 158 159 /** 160 * Constructs a parser configuration using the specified symbol table. 161 * 162 * @param symbolTable The symbol table to use. 163 */ 164 public StandardParserConfiguration(SymbolTable symbolTable) { 165 this(symbolTable, null, null); 166 } // <init>(SymbolTable) 167 168 /** 169 * Constructs a parser configuration using the specified symbol table and 170 * grammar pool. 171 * <p> 172 * <strong>REVISIT:</strong> 173 * Grammar pool will be updated when the new validation engine is 174 * implemented. 175 * 176 * @param symbolTable The symbol table to use. 177 * @param grammarPool The grammar pool to use. 178 */ 179 public StandardParserConfiguration(SymbolTable symbolTable, 180 XMLGrammarPool grammarPool) { 181 this(symbolTable, grammarPool, null); 182 } // <init>(SymbolTable,XMLGrammarPool) 183 184 /** 185 * Constructs a parser configuration using the specified symbol table, 186 * grammar pool, and parent settings. 187 * <p> 188 * <strong>REVISIT:</strong> 189 * Grammar pool will be updated when the new validation engine is 190 * implemented. 191 * 192 * @param symbolTable The symbol table to use. 193 * @param grammarPool The grammar pool to use. 194 * @param parentSettings The parent settings. 195 */ 196 public StandardParserConfiguration(SymbolTable symbolTable, 197 XMLGrammarPool grammarPool, 198 XMLComponentManager parentSettings) { 199 super(symbolTable, grammarPool, parentSettings); 200 201 // add default recognized features 202 final String[] recognizedFeatures = { 203 NORMALIZE_DATA, 204 SCHEMA_ELEMENT_DEFAULT, 205 SCHEMA_AUGMENT_PSVI, 206 GENERATE_SYNTHETIC_ANNOTATIONS, 207 VALIDATE_ANNOTATIONS, 208 HONOUR_ALL_SCHEMALOCATIONS, 209 // NOTE: These shouldn't really be here but since the XML Schema 210 // validator is constructed dynamically, its recognized 211 // features might not have been set and it would cause a 212 // not-recognized exception to be thrown. -Ac 213 XMLSCHEMA_VALIDATION, 214 XMLSCHEMA_FULL_CHECKING, 215 IGNORE_XSI_TYPE, 216 ID_IDREF_CHECKING, 217 IDENTITY_CONSTRAINT_CHECKING, 218 UNPARSED_ENTITY_CHECKING, 219 }; 220 addRecognizedFeatures(recognizedFeatures); 221 222 // set state for default features 223 setFeature(SCHEMA_ELEMENT_DEFAULT, true); 224 setFeature(NORMALIZE_DATA, true); 225 setFeature(SCHEMA_AUGMENT_PSVI, true); 226 setFeature(GENERATE_SYNTHETIC_ANNOTATIONS, false); 227 setFeature(VALIDATE_ANNOTATIONS, false); 228 setFeature(HONOUR_ALL_SCHEMALOCATIONS, false); 229 230 setFeature(IGNORE_XSI_TYPE, false); 231 setFeature(ID_IDREF_CHECKING, true); 232 setFeature(IDENTITY_CONSTRAINT_CHECKING, true); 233 setFeature(UNPARSED_ENTITY_CHECKING, true); 234 235 // add default recognized properties 236 237 final String[] recognizedProperties = { 238 // NOTE: These shouldn't really be here but since the XML Schema 239 // validator is constructed dynamically, its recognized 240 // properties might not have been set and it would cause a 241 // not-recognized exception to be thrown. -Ac 242 SCHEMA_LOCATION, 243 SCHEMA_NONS_LOCATION, 244 ROOT_TYPE_DEF, 245 }; 246 247 addRecognizedProperties(recognizedProperties); 248 } // <init>(SymbolTable,XMLGrammarPool) 249 250 // 251 // Public methods 252 // 253 254 /** Configures the pipeline. */ 255 protected void configurePipeline() { 256 super.configurePipeline(); 257 if ( getFeature(XMLSCHEMA_VALIDATION )) { 258 // If schema validator was not in the pipeline insert it. 259 if (fSchemaValidator == null) { 260 fSchemaValidator = new XMLSchemaValidator(); 261 262 // add schema component 263 fProperties.put(SCHEMA_VALIDATOR, fSchemaValidator); 264 addComponent(fSchemaValidator); 265 // add schema message formatter 266 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 267 XSMessageFormatter xmft = new XSMessageFormatter(); 268 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft); 269 } 270 271 } 272 fLastComponent = fSchemaValidator; 273 fNamespaceBinder.setDocumentHandler(fSchemaValidator); 274 275 fSchemaValidator.setDocumentHandler(fDocumentHandler); 276 fSchemaValidator.setDocumentSource(fNamespaceBinder); 277 } 278 279 280 } // configurePipeline() 281 282 // features and properties 283 284 /** 285 * Check a feature. If feature is know and supported, this method simply 286 * returns. Otherwise, the appropriate exception is thrown. 287 * 288 * @param featureId The unique identifier (URI) of the feature. 289 * 290 * @throws XMLConfigurationException Thrown for configuration error. 291 * In general, components should 292 * only throw this exception if 293 * it is <strong>really</strong> 294 * a critical error. 295 */ 296 protected void checkFeature(String featureId) 297 throws XMLConfigurationException { 298 299 // 300 // Xerces Features 301 // 302 303 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 304 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 305 306 // 307 // http://apache.org/xml/features/validation/schema 308 // Lets the user turn Schema validation support on/off. 309 // 310 if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE.length() && 311 featureId.endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) { 312 return; 313 } 314 // activate full schema checking 315 if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length() && 316 featureId.endsWith(Constants.SCHEMA_FULL_CHECKING)) { 317 return; 318 } 319 // Feature identifier: expose schema normalized value 320 // http://apache.org/xml/features/validation/schema/normalized-value 321 if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE.length() && 322 featureId.endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) { 323 return; 324 } 325 // Feature identifier: send element default value via characters() 326 // http://apache.org/xml/features/validation/schema/element-default 327 if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT.length() && 328 featureId.endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) { 329 return; 330 } 331 } 332 333 // 334 // Not recognized 335 // 336 337 super.checkFeature(featureId); 338 339 } // checkFeature(String) 340 341 /** 342 * Check a property. If the property is know and supported, this method 343 * simply returns. Otherwise, the appropriate exception is thrown. 344 * 345 * @param propertyId The unique identifier (URI) of the property 346 * being set. 347 * 348 * @throws XMLConfigurationException Thrown for configuration error. 349 * In general, components should 350 * only throw this exception if 351 * it is <strong>really</strong> 352 * a critical error. 353 */ 354 protected void checkProperty(String propertyId) 355 throws XMLConfigurationException { 356 357 // 358 // Xerces Properties 359 // 360 361 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 362 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 363 364 if (suffixLength == Constants.SCHEMA_LOCATION.length() && 365 propertyId.endsWith(Constants.SCHEMA_LOCATION)) { 366 return; 367 } 368 if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length() && 369 propertyId.endsWith(Constants.SCHEMA_NONS_LOCATION)) { 370 return; 371 } 372 } 373 374 if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) { 375 final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length(); 376 377 if (suffixLength == Constants.SCHEMA_SOURCE.length() && 378 propertyId.endsWith(Constants.SCHEMA_SOURCE)) { 379 return; 380 } 381 } 382 383 // 384 // Not recognized 385 // 386 387 super.checkProperty(propertyId); 388 389 } // checkProperty(String) 390 391 } // class StandardParserConfiguration