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