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