12005
|
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.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;
|
17534
|
30 |
import javax.xml.XMLConstants;
|
12005
|
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;
|
17534
|
46 |
import javax.xml.XMLConstants;
|
12005
|
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;
|
|
67 |
|
|
68 |
/** Feature identifier: include ignorable white space. */
|
|
69 |
private static final String INCLUDE_IGNORABLE_WHITESPACE =
|
|
70 |
Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_IGNORABLE_WHITESPACE;
|
|
71 |
|
|
72 |
/** Feature identifier: create entiry ref nodes feature. */
|
|
73 |
private static final String CREATE_ENTITY_REF_NODES_FEATURE =
|
|
74 |
Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_ENTITY_REF_NODES_FEATURE;
|
|
75 |
|
|
76 |
/** Feature identifier: include comments feature. */
|
|
77 |
private static final String INCLUDE_COMMENTS_FEATURE =
|
|
78 |
Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_COMMENTS_FEATURE;
|
|
79 |
|
|
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 |
|
17534
|
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 |
|
12005
|
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)
|
|
126 |
throws SAXNotRecognizedException, SAXNotSupportedException
|
|
127 |
{
|
|
128 |
domParser = new DOMParser();
|
|
129 |
|
|
130 |
// If validating, provide a default ErrorHandler that prints
|
|
131 |
// validation errors with a warning telling the user to set an
|
|
132 |
// ErrorHandler
|
|
133 |
if (dbf.isValidating()) {
|
|
134 |
fInitErrorHandler = new DefaultValidationErrorHandler();
|
|
135 |
setErrorHandler(fInitErrorHandler);
|
|
136 |
}
|
|
137 |
else {
|
|
138 |
fInitErrorHandler = domParser.getErrorHandler();
|
|
139 |
}
|
|
140 |
|
|
141 |
domParser.setFeature(VALIDATION_FEATURE, dbf.isValidating());
|
|
142 |
|
|
143 |
// "namespaceAware" == SAX Namespaces feature
|
|
144 |
domParser.setFeature(NAMESPACES_FEATURE, dbf.isNamespaceAware());
|
|
145 |
|
|
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());
|
17534
|
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 |
}
|
12005
|
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;
|
|
204 |
fUnparsedEntityHandler = null;
|
|
205 |
fSchemaValidatorComponentManager = config;
|
|
206 |
}
|
|
207 |
config.addRecognizedFeatures(validatorComponent.getRecognizedFeatures());
|
|
208 |
config.addRecognizedProperties(validatorComponent.getRecognizedProperties());
|
|
209 |
setFeatures(features); // Must set before calling setDocumentHandler()
|
|
210 |
config.setDocumentHandler((XMLDocumentHandler) validatorComponent);
|
|
211 |
((XMLDocumentSource)validatorComponent).setDocumentHandler(domParser);
|
|
212 |
domParser.setDocumentSource((XMLDocumentSource) validatorComponent);
|
|
213 |
fSchemaValidator = validatorComponent;
|
|
214 |
}
|
|
215 |
else {
|
|
216 |
fSchemaValidationManager = null;
|
|
217 |
fUnparsedEntityHandler = null;
|
|
218 |
fSchemaValidatorComponentManager = 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 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* Set any DocumentBuilderFactory attributes of our underlying DOMParser
|
|
245 |
*
|
|
246 |
* Note: code does not handle possible conflicts between DOMParser
|
|
247 |
* attribute names and JAXP specific attribute names,
|
|
248 |
* eg. DocumentBuilderFactory.setValidating()
|
|
249 |
*/
|
|
250 |
private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs)
|
|
251 |
throws SAXNotSupportedException, SAXNotRecognizedException
|
|
252 |
{
|
|
253 |
if (dbfAttrs == null) {
|
|
254 |
// Nothing to do
|
|
255 |
return;
|
|
256 |
}
|
|
257 |
|
|
258 |
Iterator entries = dbfAttrs.entrySet().iterator();
|
|
259 |
while (entries.hasNext()) {
|
|
260 |
Map.Entry entry = (Map.Entry) entries.next();
|
|
261 |
String name = (String) entry.getKey();
|
|
262 |
Object val = entry.getValue();
|
|
263 |
if (val instanceof Boolean) {
|
|
264 |
// Assume feature
|
|
265 |
domParser.setFeature(name, ((Boolean)val).booleanValue());
|
|
266 |
} else {
|
|
267 |
// Assume property
|
|
268 |
if (JAXP_SCHEMA_LANGUAGE.equals(name)) {
|
|
269 |
// JAXP 1.2 support
|
|
270 |
//None of the properties will take effect till the setValidating(true) has been called
|
|
271 |
if ( W3C_XML_SCHEMA.equals(val) ) {
|
|
272 |
if( isValidating() ) {
|
|
273 |
domParser.setFeature(XMLSCHEMA_VALIDATION_FEATURE, true);
|
|
274 |
// this should allow us not to emit DTD errors, as expected by the
|
|
275 |
// spec when schema validation is enabled
|
|
276 |
domParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
|
|
277 |
}
|
|
278 |
}
|
|
279 |
} else if(JAXP_SCHEMA_SOURCE.equals(name)){
|
|
280 |
if( isValidating() ) {
|
|
281 |
String value=(String)dbfAttrs.get(JAXP_SCHEMA_LANGUAGE);
|
|
282 |
if(value !=null && W3C_XML_SCHEMA.equals(value)){
|
|
283 |
domParser.setProperty(name, val);
|
|
284 |
}else{
|
|
285 |
throw new IllegalArgumentException(
|
|
286 |
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
|
|
287 |
"jaxp-order-not-supported",
|
|
288 |
new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
|
|
289 |
}
|
|
290 |
}
|
|
291 |
} else {
|
|
292 |
// Let Xerces code handle the property
|
|
293 |
domParser.setProperty(name, val);
|
|
294 |
}
|
|
295 |
}
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
/**
|
|
300 |
* Non-preferred: use the getDOMImplementation() method instead of this
|
|
301 |
* one to get a DOM Level 2 DOMImplementation object and then use DOM
|
|
302 |
* Level 2 methods to create a DOM Document object.
|
|
303 |
*/
|
|
304 |
public Document newDocument() {
|
|
305 |
return new com.sun.org.apache.xerces.internal.dom.DocumentImpl();
|
|
306 |
}
|
|
307 |
|
|
308 |
public DOMImplementation getDOMImplementation() {
|
|
309 |
return DOMImplementationImpl.getDOMImplementation();
|
|
310 |
}
|
|
311 |
|
|
312 |
public Document parse(InputSource is) throws SAXException, IOException {
|
|
313 |
if (is == null) {
|
|
314 |
throw new IllegalArgumentException(
|
|
315 |
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
|
|
316 |
"jaxp-null-input-source", null));
|
|
317 |
}
|
|
318 |
if (fSchemaValidator != null) {
|
|
319 |
if (fSchemaValidationManager != null) {
|
|
320 |
fSchemaValidationManager.reset();
|
|
321 |
fUnparsedEntityHandler.reset();
|
|
322 |
}
|
|
323 |
resetSchemaValidator();
|
|
324 |
}
|
|
325 |
domParser.parse(is);
|
|
326 |
Document doc = domParser.getDocument();
|
|
327 |
domParser.dropDocumentReferences();
|
|
328 |
return doc;
|
|
329 |
}
|
|
330 |
|
|
331 |
public boolean isNamespaceAware() {
|
|
332 |
try {
|
|
333 |
return domParser.getFeature(NAMESPACES_FEATURE);
|
|
334 |
}
|
|
335 |
catch (SAXException x) {
|
|
336 |
throw new IllegalStateException(x.getMessage());
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
|
340 |
public boolean isValidating() {
|
|
341 |
try {
|
|
342 |
return domParser.getFeature(VALIDATION_FEATURE);
|
|
343 |
}
|
|
344 |
catch (SAXException x) {
|
|
345 |
throw new IllegalStateException(x.getMessage());
|
|
346 |
}
|
|
347 |
}
|
|
348 |
|
|
349 |
/**
|
|
350 |
* Gets the XInclude processing mode for this parser
|
|
351 |
* @return the state of XInclude processing mode
|
|
352 |
*/
|
|
353 |
public boolean isXIncludeAware() {
|
|
354 |
try {
|
|
355 |
return domParser.getFeature(XINCLUDE_FEATURE);
|
|
356 |
}
|
|
357 |
catch (SAXException exc) {
|
|
358 |
return false;
|
|
359 |
}
|
|
360 |
}
|
|
361 |
|
|
362 |
public void setEntityResolver(EntityResolver er) {
|
|
363 |
domParser.setEntityResolver(er);
|
|
364 |
}
|
|
365 |
|
|
366 |
public void setErrorHandler(ErrorHandler eh) {
|
|
367 |
domParser.setErrorHandler(eh);
|
|
368 |
}
|
|
369 |
|
|
370 |
public Schema getSchema() {
|
|
371 |
return grammar;
|
|
372 |
}
|
|
373 |
|
|
374 |
public void reset() {
|
|
375 |
/** Restore the initial error handler. **/
|
|
376 |
if (domParser.getErrorHandler() != fInitErrorHandler) {
|
|
377 |
domParser.setErrorHandler(fInitErrorHandler);
|
|
378 |
}
|
|
379 |
/** Restore the initial entity resolver. **/
|
|
380 |
if (domParser.getEntityResolver() != fInitEntityResolver) {
|
|
381 |
domParser.setEntityResolver(fInitEntityResolver);
|
|
382 |
}
|
|
383 |
}
|
|
384 |
|
|
385 |
// package private
|
|
386 |
DOMParser getDOMParser() {
|
|
387 |
return domParser;
|
|
388 |
}
|
|
389 |
|
|
390 |
private void resetSchemaValidator() throws SAXException {
|
|
391 |
try {
|
|
392 |
fSchemaValidator.reset(fSchemaValidatorComponentManager);
|
|
393 |
}
|
|
394 |
// This should never be thrown from the schema validator.
|
|
395 |
catch (XMLConfigurationException e) {
|
|
396 |
throw new SAXException(e);
|
|
397 |
}
|
|
398 |
}
|
|
399 |
}
|