1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2000-2005 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.util.SymbolTable;
  25 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
  26 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
  27 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  28 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
  29 import org.xml.sax.SAXNotRecognizedException;
  30 import org.xml.sax.SAXNotSupportedException;
  31 
  32 /**
  33  * This is the main Xerces SAX parser class. It uses the abstract SAX
  34  * parser with a document scanner, a dtd scanner, and a validator, as
  35  * well as a grammar pool.
  36  *
  37  * @author Arnaud  Le Hors, IBM
  38  * @author Andy Clark, IBM
  39  *
  40  */
  41 public class SAXParser
  42     extends AbstractSAXParser {
  43 
  44     //
  45     // Constants
  46     //
  47 
  48     // features
  49 
  50     /** Feature identifier: notify built-in refereces. */
  51     protected static final String NOTIFY_BUILTIN_REFS =
  52         Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
  53 
  54     protected static final String REPORT_WHITESPACE =
  55             Constants.SUN_SCHEMA_FEATURE_PREFIX + Constants.SUN_REPORT_IGNORED_ELEMENT_CONTENT_WHITESPACE;
  56 
  57     /** Recognized features. */
  58     private static final String[] RECOGNIZED_FEATURES = {
  59         NOTIFY_BUILTIN_REFS,
  60         REPORT_WHITESPACE
  61     };
  62 
  63     // properties
  64 
  65     /** Property identifier: symbol table. */
  66     protected static final String SYMBOL_TABLE =
  67         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
  68 
  69     /** Property identifier: XML grammar pool. */
  70     protected static final String XMLGRAMMAR_POOL =
  71         Constants.XERCES_PROPERTY_PREFIX+Constants.XMLGRAMMAR_POOL_PROPERTY;
  72 
  73     /** Recognized properties. */
  74     private static final String[] RECOGNIZED_PROPERTIES = {
  75         SYMBOL_TABLE,
  76         XMLGRAMMAR_POOL,
  77     };
  78 
  79 
  80     //
  81     // Constructors
  82     //
  83 
  84     /**
  85      * Constructs a SAX parser using the specified parser configuration.
  86      */
  87     public SAXParser(XMLParserConfiguration config) {
  88         super(config);
  89     } // <init>(XMLParserConfiguration)
  90 
  91     /**
  92      * Constructs a SAX parser using the dtd/xml schema parser configuration.
  93      */
  94     public SAXParser() {
  95         this(null, null);
  96     } // <init>()
  97 
  98     /**
  99      * Constructs a SAX parser using the specified symbol table.
 100      */
 101     public SAXParser(SymbolTable symbolTable) {
 102         this(symbolTable, null);
 103     } // <init>(SymbolTable)
 104 
 105     /**
 106      * Constructs a SAX parser using the specified symbol table and
 107      * grammar pool.
 108      */
 109     public SAXParser(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
 110         super(new XIncludeAwareParserConfiguration());
 111 
 112         // set features
 113         fConfiguration.addRecognizedFeatures(RECOGNIZED_FEATURES);
 114         fConfiguration.setFeature(NOTIFY_BUILTIN_REFS, true);
 115 
 116         // set properties
 117         fConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES);
 118         if (symbolTable != null) {
 119             fConfiguration.setProperty(SYMBOL_TABLE, symbolTable);
 120         }
 121         if (grammarPool != null) {
 122             fConfiguration.setProperty(XMLGRAMMAR_POOL, grammarPool);
 123         }
 124 
 125     } // <init>(SymbolTable,XMLGrammarPool)
 126 
 127     /**
 128      * Sets the particular property in the underlying implementation of
 129      * org.xml.sax.XMLReader.
 130      */
 131     public void setProperty(String name, Object value)
 132         throws SAXNotRecognizedException, SAXNotSupportedException {
 133         /**
 134          * It's possible for users to set a security manager through the interface.
 135          * If it's the old SecurityManager, convert it to the new XMLSecurityManager
 136          */
 137         if (name.equals(Constants.SECURITY_MANAGER)) {
 138             securityManager = XMLSecurityManager.convert(value, securityManager);
 139             super.setProperty(Constants.SECURITY_MANAGER, securityManager);
 140             return;
 141         }
 142         if (name.equals(Constants.XML_SECURITY_PROPERTY_MANAGER)) {
 143             if (value == null) {
 144                 securityPropertyManager = new XMLSecurityPropertyManager();
 145             } else {
 146                 securityPropertyManager = (XMLSecurityPropertyManager)value;
 147             }
 148             super.setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER, securityPropertyManager);
 149             return;
 150         }
 151 
 152         if (securityManager == null) {
 153             securityManager = new XMLSecurityManager(true);
 154             super.setProperty(Constants.SECURITY_MANAGER, securityManager);
 155         }
 156 
 157         if (securityPropertyManager == null) {
 158             securityPropertyManager = new XMLSecurityPropertyManager();
 159             super.setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER, securityPropertyManager);
 160         }
 161 
 162         int index = securityPropertyManager.getIndex(name);
 163         if (index > -1) {
 164             /**
 165              * this is a direct call to this parser, not a subclass since
 166              * internally the support of this property is done through
 167              * XMLSecurityPropertyManager
 168              */
 169             securityPropertyManager.setValue(index, XMLSecurityPropertyManager.State.APIPROPERTY, (String)value);
 170         } else {
 171             //check if the property is managed by security manager
 172             if (!securityManager.setLimit(name, XMLSecurityManager.State.APIPROPERTY, value)) {
 173                 //fall back to the default configuration to handle the property
 174                 super.setProperty(name, value);
 175             }
 176         }
 177     }
 178 } // class SAXParser