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.io.IOException; 24 import java.util.Hashtable; 25 import java.util.Iterator; 26 import java.util.Map; 27 28 import javax.xml.parsers.DocumentBuilder; 29 import javax.xml.validation.Schema; 30 31 import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl; 32 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 33 import com.sun.org.apache.xerces.internal.impl.Constants; 34 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 35 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator; 36 import com.sun.org.apache.xerces.internal.jaxp.validation.XSGrammarPoolContainer; 37 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 38 import com.sun.org.apache.xerces.internal.util.SecurityManager; 39 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 40 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 41 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 42 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 43 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 44 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 45 import org.w3c.dom.DOMImplementation; 46 import org.w3c.dom.Document; 47 import org.xml.sax.EntityResolver; 48 import org.xml.sax.ErrorHandler; 49 import org.xml.sax.InputSource; 50 import org.xml.sax.SAXException; 51 import org.xml.sax.SAXNotRecognizedException; 52 import org.xml.sax.SAXNotSupportedException; 53 54 /** 55 * @author Rajiv Mordani 56 * @author Edwin Goei 57 * @version $Id: DocumentBuilderImpl.java,v 1.8 2010-11-01 04:40:06 joehw Exp $ 58 */ 59 public class DocumentBuilderImpl extends DocumentBuilder 60 implements JAXPConstants 61 { 62 /** Feature identifier: namespaces. */ 63 private static final String NAMESPACES_FEATURE = 64 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 78 /** Feature identifier: create cdata nodes feature. */ 79 private static final String CREATE_CDATA_NODES_FEATURE = 80 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_CDATA_NODES_FEATURE; 81 82 /** Feature identifier: XInclude processing */ 83 private static final String XINCLUDE_FEATURE = 84 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE; 85 86 /** feature identifier: XML Schema validation */ 87 private static final String XMLSCHEMA_VALIDATION_FEATURE = 88 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 89 90 /** Feature identifier: validation */ 91 private static final String VALIDATION_FEATURE = 92 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 93 94 /** Property identifier: security manager. */ 95 private static final String SECURITY_MANAGER = 96 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 97 98 private final DOMParser domParser; 99 private final Schema grammar; 100 101 private final XMLComponent fSchemaValidator; 102 private final XMLComponentManager fSchemaValidatorComponentManager; 103 private final ValidationManager fSchemaValidationManager; 104 private final UnparsedEntityHandler fUnparsedEntityHandler; 105 106 /** Initial ErrorHandler */ 107 private final ErrorHandler fInitErrorHandler; 108 109 /** Initial EntityResolver */ 110 private final EntityResolver fInitEntityResolver; 111 112 DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features) 113 throws SAXNotRecognizedException, SAXNotSupportedException { 114 this(dbf, dbfAttrs, features, false); 115 } 116 117 DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features, boolean secureProcessing) 138 // Set various parameters obtained from DocumentBuilderFactory 139 domParser.setFeature(INCLUDE_IGNORABLE_WHITESPACE, 140 !dbf.isIgnoringElementContentWhitespace()); 141 domParser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE, 142 !dbf.isExpandEntityReferences()); 143 domParser.setFeature(INCLUDE_COMMENTS_FEATURE, 144 !dbf.isIgnoringComments()); 145 domParser.setFeature(CREATE_CDATA_NODES_FEATURE, 146 !dbf.isCoalescing()); 147 148 // Avoid setting the XInclude processing feature if the value is false. 149 // This will keep the configuration from throwing an exception if it 150 // does not support XInclude. 151 if (dbf.isXIncludeAware()) { 152 domParser.setFeature(XINCLUDE_FEATURE, true); 153 } 154 155 // If the secure processing feature is on set a security manager. 156 if (secureProcessing) { 157 domParser.setProperty(SECURITY_MANAGER, new SecurityManager()); 158 } 159 160 this.grammar = dbf.getSchema(); 161 if (grammar != null) { 162 XMLParserConfiguration config = domParser.getXMLParserConfiguration(); 163 XMLComponent validatorComponent = null; 164 /** For Xerces grammars, use built-in schema validator. **/ 165 if (grammar instanceof XSGrammarPoolContainer) { 166 validatorComponent = new XMLSchemaValidator(); 167 fSchemaValidationManager = new ValidationManager(); 168 fUnparsedEntityHandler = new UnparsedEntityHandler(fSchemaValidationManager); 169 config.setDTDHandler(fUnparsedEntityHandler); 170 fUnparsedEntityHandler.setDTDHandler(domParser); 171 domParser.setDTDSource(fUnparsedEntityHandler); 172 fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config, 173 (XSGrammarPoolContainer) grammar, fSchemaValidationManager); 174 } 175 /** For third party grammars, use the JAXP validator component. **/ 176 else { 177 validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler()); 178 fSchemaValidationManager = null; 194 fSchemaValidator = null; 195 setFeatures(features); 196 } 197 198 // Set attributes 199 setDocumentBuilderFactoryAttributes(dbfAttrs); 200 201 // Initial EntityResolver 202 fInitEntityResolver = domParser.getEntityResolver(); 203 } 204 205 private void setFeatures(Hashtable features) 206 throws SAXNotSupportedException, SAXNotRecognizedException { 207 if (features != null) { 208 Iterator entries = features.entrySet().iterator(); 209 while (entries.hasNext()) { 210 Map.Entry entry = (Map.Entry) entries.next(); 211 String feature = (String) entry.getKey(); 212 boolean value = ((Boolean) entry.getValue()).booleanValue(); 213 domParser.setFeature(feature, value); 214 } 215 } 216 } 217 218 /** 219 * Set any DocumentBuilderFactory attributes of our underlying DOMParser 220 * 221 * Note: code does not handle possible conflicts between DOMParser 222 * attribute names and JAXP specific attribute names, 223 * eg. DocumentBuilderFactory.setValidating() 224 */ 225 private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs) 226 throws SAXNotSupportedException, SAXNotRecognizedException 227 { 228 if (dbfAttrs == null) { 229 // Nothing to do 230 return; 231 } 232 233 Iterator entries = dbfAttrs.entrySet().iterator(); 234 while (entries.hasNext()) { 235 Map.Entry entry = (Map.Entry) entries.next(); | 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.io.IOException; 24 import java.util.Hashtable; 25 import java.util.Iterator; 26 import java.util.Map; 27 28 import javax.xml.parsers.DocumentBuilder; 29 import javax.xml.validation.Schema; 30 import javax.xml.XMLConstants; 31 32 import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl; 33 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 34 import com.sun.org.apache.xerces.internal.impl.Constants; 35 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 36 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator; 37 import com.sun.org.apache.xerces.internal.jaxp.validation.XSGrammarPoolContainer; 38 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 39 import com.sun.org.apache.xerces.internal.util.SecurityManager; 40 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 41 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 42 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 43 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 44 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 45 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 46 import javax.xml.XMLConstants; 47 import org.w3c.dom.DOMImplementation; 48 import org.w3c.dom.Document; 49 import org.xml.sax.EntityResolver; 50 import org.xml.sax.ErrorHandler; 51 import org.xml.sax.InputSource; 52 import org.xml.sax.SAXException; 53 import org.xml.sax.SAXNotRecognizedException; 54 import org.xml.sax.SAXNotSupportedException; 55 56 /** 57 * @author Rajiv Mordani 58 * @author Edwin Goei 59 * @version $Id: DocumentBuilderImpl.java,v 1.8 2010-11-01 04:40:06 joehw Exp $ 60 */ 61 public class DocumentBuilderImpl extends DocumentBuilder 62 implements JAXPConstants 63 { 64 /** Feature identifier: namespaces. */ 65 private static final String NAMESPACES_FEATURE = 66 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 80 /** Feature identifier: create cdata nodes feature. */ 81 private static final String CREATE_CDATA_NODES_FEATURE = 82 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_CDATA_NODES_FEATURE; 83 84 /** Feature identifier: XInclude processing */ 85 private static final String XINCLUDE_FEATURE = 86 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE; 87 88 /** feature identifier: XML Schema validation */ 89 private static final String XMLSCHEMA_VALIDATION_FEATURE = 90 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 91 92 /** Feature identifier: validation */ 93 private static final String VALIDATION_FEATURE = 94 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 95 96 /** Property identifier: security manager. */ 97 private static final String SECURITY_MANAGER = 98 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 99 100 /** property identifier: access external dtd. */ 101 public static final String ACCESS_EXTERNAL_DTD = XMLConstants.ACCESS_EXTERNAL_DTD; 102 103 /** Property identifier: access to external schema */ 104 public static final String ACCESS_EXTERNAL_SCHEMA = XMLConstants.ACCESS_EXTERNAL_SCHEMA; 105 106 private final DOMParser domParser; 107 private final Schema grammar; 108 109 private final XMLComponent fSchemaValidator; 110 private final XMLComponentManager fSchemaValidatorComponentManager; 111 private final ValidationManager fSchemaValidationManager; 112 private final UnparsedEntityHandler fUnparsedEntityHandler; 113 114 /** Initial ErrorHandler */ 115 private final ErrorHandler fInitErrorHandler; 116 117 /** Initial EntityResolver */ 118 private final EntityResolver fInitEntityResolver; 119 120 DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features) 121 throws SAXNotRecognizedException, SAXNotSupportedException { 122 this(dbf, dbfAttrs, features, false); 123 } 124 125 DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features, boolean secureProcessing) 146 // Set various parameters obtained from DocumentBuilderFactory 147 domParser.setFeature(INCLUDE_IGNORABLE_WHITESPACE, 148 !dbf.isIgnoringElementContentWhitespace()); 149 domParser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE, 150 !dbf.isExpandEntityReferences()); 151 domParser.setFeature(INCLUDE_COMMENTS_FEATURE, 152 !dbf.isIgnoringComments()); 153 domParser.setFeature(CREATE_CDATA_NODES_FEATURE, 154 !dbf.isCoalescing()); 155 156 // Avoid setting the XInclude processing feature if the value is false. 157 // This will keep the configuration from throwing an exception if it 158 // does not support XInclude. 159 if (dbf.isXIncludeAware()) { 160 domParser.setFeature(XINCLUDE_FEATURE, true); 161 } 162 163 // If the secure processing feature is on set a security manager. 164 if (secureProcessing) { 165 domParser.setProperty(SECURITY_MANAGER, new SecurityManager()); 166 167 /** 168 * By default, secure processing is set, no external access is allowed. 169 * However, we need to check if it is actively set on the factory since we 170 * allow the use of the System Property or jaxp.properties to override 171 * the default value 172 */ 173 if (features != null) { 174 Object temp = features.get(XMLConstants.FEATURE_SECURE_PROCESSING); 175 if (temp != null) { 176 boolean value = ((Boolean) temp).booleanValue(); 177 if (value) { 178 domParser.setProperty(ACCESS_EXTERNAL_DTD, Constants.EXTERNAL_ACCESS_DEFAULT_FSP); 179 domParser.setProperty(ACCESS_EXTERNAL_SCHEMA, Constants.EXTERNAL_ACCESS_DEFAULT_FSP); 180 } 181 } 182 } 183 } 184 185 this.grammar = dbf.getSchema(); 186 if (grammar != null) { 187 XMLParserConfiguration config = domParser.getXMLParserConfiguration(); 188 XMLComponent validatorComponent = null; 189 /** For Xerces grammars, use built-in schema validator. **/ 190 if (grammar instanceof XSGrammarPoolContainer) { 191 validatorComponent = new XMLSchemaValidator(); 192 fSchemaValidationManager = new ValidationManager(); 193 fUnparsedEntityHandler = new UnparsedEntityHandler(fSchemaValidationManager); 194 config.setDTDHandler(fUnparsedEntityHandler); 195 fUnparsedEntityHandler.setDTDHandler(domParser); 196 domParser.setDTDSource(fUnparsedEntityHandler); 197 fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config, 198 (XSGrammarPoolContainer) grammar, fSchemaValidationManager); 199 } 200 /** For third party grammars, use the JAXP validator component. **/ 201 else { 202 validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler()); 203 fSchemaValidationManager = null; 219 fSchemaValidator = null; 220 setFeatures(features); 221 } 222 223 // Set attributes 224 setDocumentBuilderFactoryAttributes(dbfAttrs); 225 226 // Initial EntityResolver 227 fInitEntityResolver = domParser.getEntityResolver(); 228 } 229 230 private void setFeatures(Hashtable features) 231 throws SAXNotSupportedException, SAXNotRecognizedException { 232 if (features != null) { 233 Iterator entries = features.entrySet().iterator(); 234 while (entries.hasNext()) { 235 Map.Entry entry = (Map.Entry) entries.next(); 236 String feature = (String) entry.getKey(); 237 boolean value = ((Boolean) entry.getValue()).booleanValue(); 238 domParser.setFeature(feature, value); 239 if (feature.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 240 domParser.setProperty(ACCESS_EXTERNAL_DTD, ""); 241 domParser.setProperty(ACCESS_EXTERNAL_SCHEMA, ""); 242 } 243 } 244 } 245 } 246 247 /** 248 * Set any DocumentBuilderFactory attributes of our underlying DOMParser 249 * 250 * Note: code does not handle possible conflicts between DOMParser 251 * attribute names and JAXP specific attribute names, 252 * eg. DocumentBuilderFactory.setValidating() 253 */ 254 private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs) 255 throws SAXNotSupportedException, SAXNotRecognizedException 256 { 257 if (dbfAttrs == null) { 258 // Nothing to do 259 return; 260 } 261 262 Iterator entries = dbfAttrs.entrySet().iterator(); 263 while (entries.hasNext()) { 264 Map.Entry entry = (Map.Entry) entries.next(); |