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