1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2000-2002,2004,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.jaxp;
  22 
  23 import java.util.Hashtable;
  24 
  25 import javax.xml.XMLConstants;
  26 import javax.xml.parsers.ParserConfigurationException;
  27 import javax.xml.parsers.SAXParser;
  28 import javax.xml.parsers.SAXParserFactory;
  29 import javax.xml.validation.Schema;
  30 
  31 import org.xml.sax.SAXException;
  32 import org.xml.sax.SAXNotRecognizedException;
  33 import org.xml.sax.SAXNotSupportedException;
  34 
  35 import com.sun.org.apache.xerces.internal.impl.Constants;
  36 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
  37 
  38 /**
  39  * This is the implementation specific class for the
  40  * <code>javax.xml.parsers.SAXParserFactory</code>. This is the platform
  41  * default implementation for the platform.
  42  *
  43  * @author Rajiv Mordani
  44  * @author Edwin Goei
  45  *
  46  * @version $Id: SAXParserFactoryImpl.java,v 1.7 2009/07/28 23:48:32 joehw Exp $
  47  */
  48 public class SAXParserFactoryImpl extends SAXParserFactory {
  49 
  50     /** Feature identifier: validation. */
  51     private static final String VALIDATION_FEATURE =
  52         Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
  53 
  54     /** Feature identifier: namespaces. */
  55     private static final String NAMESPACES_FEATURE =
  56         Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
  57 
  58     /** Feature identifier: XInclude processing */
  59     private static final String XINCLUDE_FEATURE =
  60         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE;
  61 
  62     private Hashtable features;
  63     private Schema grammar;
  64     private boolean isXIncludeAware;
  65 
  66     /**
  67      * State of the secure processing feature, initially <code>false</code>
  68      */
  69     private boolean fSecureProcess = true;
  70 
  71     /**
  72      * Creates a new instance of <code>SAXParser</code> using the currently
  73      * configured factory parameters.
  74      * @return javax.xml.parsers.SAXParser
  75      */
  76     public SAXParser newSAXParser()
  77         throws ParserConfigurationException
  78     {
  79         SAXParser saxParserImpl;
  80         try {
  81             saxParserImpl = new SAXParserImpl(this, features, fSecureProcess);
  82         } catch (SAXException se) {
  83             // Translate to ParserConfigurationException
  84             throw new ParserConfigurationException(se.getMessage());
  85         }
  86         return saxParserImpl;
  87     }
  88 
  89     /**
  90      * Common code for translating exceptions
  91      */
  92     private SAXParserImpl newSAXParserImpl()
  93         throws ParserConfigurationException, SAXNotRecognizedException,
  94         SAXNotSupportedException
  95     {
  96         SAXParserImpl saxParserImpl;
  97         try {
  98             saxParserImpl = new SAXParserImpl(this, features);
  99         } catch (SAXNotSupportedException e) {
 100             throw e;
 101         } catch (SAXNotRecognizedException e) {
 102             throw e;
 103         } catch (SAXException se) {
 104             throw new ParserConfigurationException(se.getMessage());
 105         }
 106         return saxParserImpl;
 107     }
 108 
 109     /**
 110      * Sets the particular feature in the underlying implementation of
 111      * org.xml.sax.XMLReader.
 112      */
 113     public void setFeature(String name, boolean value)
 114         throws ParserConfigurationException, SAXNotRecognizedException,
 115                 SAXNotSupportedException {
 116         if (name == null) {
 117             throw new NullPointerException();
 118         }
 119         // If this is the secure processing feature, save it then return.
 120         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 121             if (System.getSecurityManager() != null && (!value)) {
 122                 throw new ParserConfigurationException(
 123                         SAXMessageFormatter.formatMessage(null,
 124                         "jaxp-secureprocessing-feature", null));
 125             }
 126             fSecureProcess = value;
 127             return;
 128         }
 129 
 130         // XXX This is ugly.  We have to collect the features and then
 131         // later create an XMLReader to verify the features.
 132         putInFeatures(name, value);
 133         // Test the feature by possibly throwing SAX exceptions
 134         try {
 135             newSAXParserImpl();
 136         } catch (SAXNotSupportedException e) {
 137             features.remove(name);
 138             throw e;
 139         } catch (SAXNotRecognizedException e) {
 140             features.remove(name);
 141             throw e;
 142         }
 143     }
 144 
 145     /**
 146      * returns the particular property requested for in the underlying
 147      * implementation of org.xml.sax.XMLReader.
 148      */
 149     public boolean getFeature(String name)
 150         throws ParserConfigurationException, SAXNotRecognizedException,
 151                 SAXNotSupportedException {
 152         if (name == null) {
 153             throw new NullPointerException();
 154         }
 155         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 156             return fSecureProcess;
 157         }
 158         // Check for valid name by creating a dummy XMLReader to get
 159         // feature value
 160         return newSAXParserImpl().getXMLReader().getFeature(name);
 161     }
 162 
 163     public Schema getSchema() {
 164         return grammar;
 165     }
 166 
 167     public void setSchema(Schema grammar) {
 168         this.grammar = grammar;
 169     }
 170 
 171     public boolean isXIncludeAware() {
 172         return getFromFeatures(XINCLUDE_FEATURE);
 173     }
 174 
 175     public void setXIncludeAware(boolean state) {
 176         putInFeatures(XINCLUDE_FEATURE, state);
 177     }
 178 
 179 
 180     public void setValidating(boolean validating) {
 181         putInFeatures(VALIDATION_FEATURE, validating);
 182     }
 183 
 184     public boolean isValidating() {
 185          return getFromFeatures(VALIDATION_FEATURE);
 186     }
 187 
 188     private void putInFeatures(String name, boolean value){
 189          if (features == null) {
 190             features = new Hashtable();
 191         }
 192         features.put(name, value ? Boolean.TRUE : Boolean.FALSE);
 193     }
 194 
 195     private boolean getFromFeatures(String name){
 196          if (features == null){
 197             return false;
 198          }
 199          else {
 200              Object value = features.get(name);
 201              return (value == null) ? false : Boolean.valueOf(value.toString()).booleanValue();
 202          }
 203     }
 204 
 205     public boolean isNamespaceAware() {
 206         return getFromFeatures(NAMESPACES_FEATURE);
 207     }
 208 
 209     public void setNamespaceAware(boolean awareness) {
 210        putInFeatures(NAMESPACES_FEATURE, awareness);
 211     }
 212 
 213  }