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