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.9 2010-11-01 04:40:06 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 putInFeatures(name, value); 128 return; 129 } 130 131 // XXX This is ugly. We have to collect the features and then 132 // later create an XMLReader to verify the features. 133 putInFeatures(name, value); 134 // Test the feature by possibly throwing SAX exceptions 135 try { 136 newSAXParserImpl(); 137 } catch (SAXNotSupportedException e) { 138 features.remove(name); 139 throw e; 140 } catch (SAXNotRecognizedException e) { 141 features.remove(name); 142 throw e; 143 } 144 } 145 146 /** 147 * returns the particular property requested for in the underlying 148 * implementation of org.xml.sax.XMLReader. 149 */ 150 public boolean getFeature(String name) 151 throws ParserConfigurationException, SAXNotRecognizedException, 152 SAXNotSupportedException { 153 if (name == null) { 154 throw new NullPointerException(); 155 } 156 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 157 return fSecureProcess; 158 } 159 // Check for valid name by creating a dummy XMLReader to get 160 // feature value 161 return newSAXParserImpl().getXMLReader().getFeature(name); 162 } 163 164 public Schema getSchema() { 165 return grammar; 166 } 167 168 public void setSchema(Schema grammar) { 169 this.grammar = grammar; 170 } 171 172 public boolean isXIncludeAware() { 173 return getFromFeatures(XINCLUDE_FEATURE); 174 } 175 176 public void setXIncludeAware(boolean state) { 177 putInFeatures(XINCLUDE_FEATURE, state); 178 } 179 180 181 public void setValidating(boolean validating) { 182 putInFeatures(VALIDATION_FEATURE, validating); 183 } 184 185 public boolean isValidating() { 186 return getFromFeatures(VALIDATION_FEATURE); 187 } 188 189 private void putInFeatures(String name, boolean value){ 190 if (features == null) { 191 features = new Hashtable(); 192 } 193 features.put(name, value ? Boolean.TRUE : Boolean.FALSE); 194 } 195 196 private boolean getFromFeatures(String name){ 197 if (features == null){ 198 return false; 199 } 200 else { 201 Object value = features.get(name); 202 return (value == null) ? false : Boolean.valueOf(value.toString()).booleanValue(); 203 } 204 } 205 206 public boolean isNamespaceAware() { 207 return getFromFeatures(NAMESPACES_FEATURE); 208 } 209 210 public void setNamespaceAware(boolean awareness) { 211 putInFeatures(NAMESPACES_FEATURE, awareness); 212 } 213 214 }