author | joehw |
Tue, 09 Jul 2013 16:34:52 -0700 | |
changeset 18890 | 25bdeca3173b |
parent 17786 | 4471252c40b4 |
child 19112 | 3bf2022fbd9a |
child 20967 | d3f1925927e5 |
permissions | -rw-r--r-- |
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.HashMap; |
|
25 |
import java.util.Hashtable; |
|
26 |
import java.util.Iterator; |
|
27 |
import java.util.Map; |
|
28 |
||
29 |
import javax.xml.XMLConstants; |
|
30 |
import javax.xml.validation.Schema; |
|
31 |
||
32 |
import com.sun.org.apache.xerces.internal.impl.Constants; |
|
33 |
import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; |
|
34 |
import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator; |
|
35 |
import com.sun.org.apache.xerces.internal.jaxp.validation.XSGrammarPoolContainer; |
|
36 |
import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter; |
|
37 |
import com.sun.org.apache.xerces.internal.util.SecurityManager; |
|
38 |
import com.sun.org.apache.xerces.internal.util.Status; |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
39 |
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; |
12005 | 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 com.sun.org.apache.xerces.internal.xs.AttributePSVI; |
|
47 |
import com.sun.org.apache.xerces.internal.xs.ElementPSVI; |
|
48 |
import com.sun.org.apache.xerces.internal.xs.PSVIProvider; |
|
49 |
import org.xml.sax.EntityResolver; |
|
50 |
import org.xml.sax.ErrorHandler; |
|
51 |
import org.xml.sax.HandlerBase; |
|
52 |
import org.xml.sax.InputSource; |
|
53 |
import org.xml.sax.Parser; |
|
54 |
import org.xml.sax.SAXException; |
|
55 |
import org.xml.sax.SAXNotRecognizedException; |
|
56 |
import org.xml.sax.SAXNotSupportedException; |
|
57 |
import org.xml.sax.XMLReader; |
|
58 |
import org.xml.sax.helpers.DefaultHandler; |
|
59 |
||
60 |
/** |
|
61 |
* This is the implementation specific class for the |
|
62 |
* <code>javax.xml.parsers.SAXParser</code>. |
|
63 |
* |
|
64 |
* @author Rajiv Mordani |
|
65 |
* @author Edwin Goei |
|
66 |
* |
|
67 |
* @version $Id: SAXParserImpl.java,v 1.7 2010-11-01 04:40:06 joehw Exp $ |
|
68 |
*/ |
|
69 |
public class SAXParserImpl extends javax.xml.parsers.SAXParser |
|
70 |
implements JAXPConstants, PSVIProvider { |
|
71 |
||
72 |
/** Feature identifier: namespaces. */ |
|
73 |
private static final String NAMESPACES_FEATURE = |
|
74 |
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; |
|
75 |
||
76 |
/** Feature identifier: namespace prefixes. */ |
|
77 |
private static final String NAMESPACE_PREFIXES_FEATURE = |
|
78 |
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE; |
|
79 |
||
80 |
/** Feature identifier: validation. */ |
|
81 |
private static final String VALIDATION_FEATURE = |
|
82 |
Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; |
|
83 |
||
84 |
/** Feature identifier: XML Schema validation */ |
|
85 |
private static final String XMLSCHEMA_VALIDATION_FEATURE = |
|
86 |
Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; |
|
87 |
||
88 |
/** Feature identifier: XInclude processing */ |
|
89 |
private static final String XINCLUDE_FEATURE = |
|
90 |
Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE; |
|
91 |
||
92 |
/** Property identifier: security manager. */ |
|
93 |
private static final String SECURITY_MANAGER = |
|
94 |
Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; |
|
95 |
||
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
96 |
/** Property identifier: Security property manager. */ |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
97 |
private static final String XML_SECURITY_PROPERTY_MANAGER = |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
98 |
Constants.XML_SECURITY_PROPERTY_MANAGER; |
17534 | 99 |
|
12005 | 100 |
private final JAXPSAXParser xmlReader; |
101 |
private String schemaLanguage = null; // null means DTD |
|
102 |
private final Schema grammar; |
|
103 |
||
104 |
private final XMLComponent fSchemaValidator; |
|
105 |
private final XMLComponentManager fSchemaValidatorComponentManager; |
|
106 |
private final ValidationManager fSchemaValidationManager; |
|
107 |
private final UnparsedEntityHandler fUnparsedEntityHandler; |
|
108 |
||
109 |
/** Initial ErrorHandler */ |
|
110 |
private final ErrorHandler fInitErrorHandler; |
|
111 |
||
112 |
/** Initial EntityResolver */ |
|
113 |
private final EntityResolver fInitEntityResolver; |
|
114 |
||
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
115 |
private XMLSecurityPropertyManager fSecurityPropertyMgr; |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
116 |
|
12005 | 117 |
/** |
118 |
* Create a SAX parser with the associated features |
|
119 |
* @param features Hashtable of SAX features, may be null |
|
120 |
*/ |
|
121 |
SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features) |
|
122 |
throws SAXException { |
|
123 |
this(spf, features, false); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Create a SAX parser with the associated features |
|
128 |
* @param features Hashtable of SAX features, may be null |
|
129 |
*/ |
|
130 |
SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features, boolean secureProcessing) |
|
131 |
throws SAXException |
|
132 |
{ |
|
133 |
// Instantiate a SAXParser directly and not through SAX so that we use the right ClassLoader |
|
134 |
xmlReader = new JAXPSAXParser(this); |
|
135 |
||
136 |
// JAXP "namespaceAware" == SAX Namespaces feature |
|
137 |
// Note: there is a compatibility problem here with default values: |
|
138 |
// JAXP default is false while SAX 2 default is true! |
|
139 |
xmlReader.setFeature0(NAMESPACES_FEATURE, spf.isNamespaceAware()); |
|
140 |
||
141 |
// SAX "namespaces" and "namespace-prefixes" features should not |
|
142 |
// both be false. We make them opposite for backward compatibility |
|
143 |
// since JAXP 1.0 apps may want to receive xmlns* attributes. |
|
144 |
xmlReader.setFeature0(NAMESPACE_PREFIXES_FEATURE, !spf.isNamespaceAware()); |
|
145 |
||
146 |
// Avoid setting the XInclude processing feature if the value is false. |
|
147 |
// This will keep the configuration from throwing an exception if it |
|
148 |
// does not support XInclude. |
|
149 |
if (spf.isXIncludeAware()) { |
|
150 |
xmlReader.setFeature0(XINCLUDE_FEATURE, true); |
|
151 |
} |
|
152 |
||
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
153 |
fSecurityPropertyMgr = new XMLSecurityPropertyManager(); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
154 |
xmlReader.setProperty0(XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
155 |
|
12005 | 156 |
// If the secure processing feature is on set a security manager. |
157 |
if (secureProcessing) { |
|
158 |
xmlReader.setProperty0(SECURITY_MANAGER, new SecurityManager()); |
|
17534 | 159 |
/** |
160 |
* By default, secure processing is set, no external access is allowed. |
|
161 |
* However, we need to check if it is actively set on the factory since we |
|
162 |
* allow the use of the System Property or jaxp.properties to override |
|
163 |
* the default value |
|
164 |
*/ |
|
165 |
if (features != null) { |
|
166 |
Object temp = features.get(XMLConstants.FEATURE_SECURE_PROCESSING); |
|
167 |
if (temp != null) { |
|
168 |
boolean value = ((Boolean) temp).booleanValue(); |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
169 |
if (value && Constants.IS_JDK8_OR_ABOVE) { |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
170 |
fSecurityPropertyMgr.setValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD, |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
171 |
XMLSecurityPropertyManager.State.FSP, Constants.EXTERNAL_ACCESS_DEFAULT_FSP); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
172 |
fSecurityPropertyMgr.setValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_SCHEMA, |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
173 |
XMLSecurityPropertyManager.State.FSP, Constants.EXTERNAL_ACCESS_DEFAULT_FSP); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
174 |
|
17534 | 175 |
} |
176 |
} |
|
177 |
} |
|
12005 | 178 |
} |
179 |
||
180 |
// Set application's features, followed by validation features. |
|
181 |
setFeatures(features); |
|
182 |
||
183 |
// If validating, provide a default ErrorHandler that prints |
|
184 |
// validation errors with a warning telling the user to set an |
|
185 |
// ErrorHandler. |
|
186 |
if (spf.isValidating()) { |
|
187 |
fInitErrorHandler = new DefaultValidationErrorHandler(); |
|
188 |
xmlReader.setErrorHandler(fInitErrorHandler); |
|
189 |
} |
|
190 |
else { |
|
191 |
fInitErrorHandler = xmlReader.getErrorHandler(); |
|
192 |
} |
|
193 |
xmlReader.setFeature0(VALIDATION_FEATURE, spf.isValidating()); |
|
194 |
||
195 |
// Get the Schema object from the factory |
|
196 |
this.grammar = spf.getSchema(); |
|
197 |
if (grammar != null) { |
|
198 |
XMLParserConfiguration config = xmlReader.getXMLParserConfiguration(); |
|
199 |
XMLComponent validatorComponent = null; |
|
200 |
/** For Xerces grammars, use built-in schema validator. **/ |
|
201 |
if (grammar instanceof XSGrammarPoolContainer) { |
|
202 |
validatorComponent = new XMLSchemaValidator(); |
|
203 |
fSchemaValidationManager = new ValidationManager(); |
|
204 |
fUnparsedEntityHandler = new UnparsedEntityHandler(fSchemaValidationManager); |
|
205 |
config.setDTDHandler(fUnparsedEntityHandler); |
|
206 |
fUnparsedEntityHandler.setDTDHandler(xmlReader); |
|
207 |
xmlReader.setDTDSource(fUnparsedEntityHandler); |
|
208 |
fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config, |
|
209 |
(XSGrammarPoolContainer) grammar, fSchemaValidationManager); |
|
210 |
} |
|
211 |
/** For third party grammars, use the JAXP validator component. **/ |
|
212 |
else { |
|
213 |
validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler()); |
|
214 |
fSchemaValidationManager = null; |
|
215 |
fUnparsedEntityHandler = null; |
|
216 |
fSchemaValidatorComponentManager = config; |
|
217 |
} |
|
218 |
config.addRecognizedFeatures(validatorComponent.getRecognizedFeatures()); |
|
219 |
config.addRecognizedProperties(validatorComponent.getRecognizedProperties()); |
|
220 |
config.setDocumentHandler((XMLDocumentHandler) validatorComponent); |
|
221 |
((XMLDocumentSource)validatorComponent).setDocumentHandler(xmlReader); |
|
222 |
xmlReader.setDocumentSource((XMLDocumentSource) validatorComponent); |
|
223 |
fSchemaValidator = validatorComponent; |
|
224 |
} |
|
225 |
else { |
|
226 |
fSchemaValidationManager = null; |
|
227 |
fUnparsedEntityHandler = null; |
|
228 |
fSchemaValidatorComponentManager = null; |
|
229 |
fSchemaValidator = null; |
|
230 |
} |
|
231 |
||
232 |
// Initial EntityResolver |
|
233 |
fInitEntityResolver = xmlReader.getEntityResolver(); |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Set any features of our XMLReader based on any features set on the |
|
238 |
* SAXParserFactory. |
|
239 |
* |
|
240 |
* XXX Does not handle possible conflicts between SAX feature names and |
|
241 |
* JAXP specific feature names, eg. SAXParserFactory.isValidating() |
|
242 |
*/ |
|
243 |
private void setFeatures(Hashtable features) |
|
244 |
throws SAXNotSupportedException, SAXNotRecognizedException { |
|
245 |
if (features != null) { |
|
246 |
Iterator entries = features.entrySet().iterator(); |
|
247 |
while (entries.hasNext()) { |
|
248 |
Map.Entry entry = (Map.Entry) entries.next(); |
|
249 |
String feature = (String) entry.getKey(); |
|
250 |
boolean value = ((Boolean) entry.getValue()).booleanValue(); |
|
251 |
xmlReader.setFeature0(feature, value); |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
public Parser getParser() throws SAXException { |
|
257 |
// Xerces2 AbstractSAXParser implements SAX1 Parser |
|
258 |
// assert(xmlReader instanceof Parser); |
|
259 |
return (Parser) xmlReader; |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Returns the XMLReader that is encapsulated by the implementation of |
|
264 |
* this class. |
|
265 |
*/ |
|
266 |
public XMLReader getXMLReader() { |
|
267 |
return xmlReader; |
|
268 |
} |
|
269 |
||
270 |
public boolean isNamespaceAware() { |
|
271 |
try { |
|
272 |
return xmlReader.getFeature(NAMESPACES_FEATURE); |
|
273 |
} |
|
274 |
catch (SAXException x) { |
|
275 |
throw new IllegalStateException(x.getMessage()); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
public boolean isValidating() { |
|
280 |
try { |
|
281 |
return xmlReader.getFeature(VALIDATION_FEATURE); |
|
282 |
} |
|
283 |
catch (SAXException x) { |
|
284 |
throw new IllegalStateException(x.getMessage()); |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* Gets the XInclude processing mode for this parser |
|
290 |
* @return the state of XInclude processing mode |
|
291 |
*/ |
|
292 |
public boolean isXIncludeAware() { |
|
293 |
try { |
|
294 |
return xmlReader.getFeature(XINCLUDE_FEATURE); |
|
295 |
} |
|
296 |
catch (SAXException exc) { |
|
297 |
return false; |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Sets the particular property in the underlying implementation of |
|
303 |
* org.xml.sax.XMLReader. |
|
304 |
*/ |
|
305 |
public void setProperty(String name, Object value) |
|
306 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
307 |
xmlReader.setProperty(name, value); |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* returns the particular property requested for in the underlying |
|
312 |
* implementation of org.xml.sax.XMLReader. |
|
313 |
*/ |
|
314 |
public Object getProperty(String name) |
|
315 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
316 |
return xmlReader.getProperty(name); |
|
317 |
} |
|
318 |
||
319 |
public void parse(InputSource is, DefaultHandler dh) |
|
320 |
throws SAXException, IOException { |
|
321 |
if (is == null) { |
|
322 |
throw new IllegalArgumentException(); |
|
323 |
} |
|
324 |
if (dh != null) { |
|
325 |
xmlReader.setContentHandler(dh); |
|
326 |
xmlReader.setEntityResolver(dh); |
|
327 |
xmlReader.setErrorHandler(dh); |
|
328 |
xmlReader.setDTDHandler(dh); |
|
329 |
xmlReader.setDocumentHandler(null); |
|
330 |
} |
|
331 |
xmlReader.parse(is); |
|
332 |
} |
|
333 |
||
334 |
public void parse(InputSource is, HandlerBase hb) |
|
335 |
throws SAXException, IOException { |
|
336 |
if (is == null) { |
|
337 |
throw new IllegalArgumentException(); |
|
338 |
} |
|
339 |
if (hb != null) { |
|
340 |
xmlReader.setDocumentHandler(hb); |
|
341 |
xmlReader.setEntityResolver(hb); |
|
342 |
xmlReader.setErrorHandler(hb); |
|
343 |
xmlReader.setDTDHandler(hb); |
|
344 |
xmlReader.setContentHandler(null); |
|
345 |
} |
|
346 |
xmlReader.parse(is); |
|
347 |
} |
|
348 |
||
349 |
public Schema getSchema() { |
|
350 |
return grammar; |
|
351 |
} |
|
352 |
||
353 |
public void reset() { |
|
354 |
try { |
|
355 |
/** Restore initial values of features and properties. **/ |
|
356 |
xmlReader.restoreInitState(); |
|
357 |
} |
|
358 |
catch (SAXException exc) { |
|
359 |
// This should never happen. We only store recognized |
|
360 |
// features and properties in the hash maps. For now |
|
361 |
// just ignore it. |
|
362 |
} |
|
363 |
/** Restore various handlers. **/ |
|
364 |
xmlReader.setContentHandler(null); |
|
365 |
xmlReader.setDTDHandler(null); |
|
366 |
if (xmlReader.getErrorHandler() != fInitErrorHandler) { |
|
367 |
xmlReader.setErrorHandler(fInitErrorHandler); |
|
368 |
} |
|
369 |
if (xmlReader.getEntityResolver() != fInitEntityResolver) { |
|
370 |
xmlReader.setEntityResolver(fInitEntityResolver); |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
/* |
|
375 |
* PSVIProvider methods |
|
376 |
*/ |
|
377 |
||
378 |
public ElementPSVI getElementPSVI() { |
|
379 |
return ((PSVIProvider)xmlReader).getElementPSVI(); |
|
380 |
} |
|
381 |
||
382 |
public AttributePSVI getAttributePSVI(int index) { |
|
383 |
return ((PSVIProvider)xmlReader).getAttributePSVI(index); |
|
384 |
} |
|
385 |
||
386 |
public AttributePSVI getAttributePSVIByName(String uri, String localname) { |
|
387 |
return ((PSVIProvider)xmlReader).getAttributePSVIByName(uri, localname); |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* Extension of SAXParser. This class tracks changes to |
|
392 |
* features and properties to allow the parser to be reset to |
|
393 |
* its initial state. |
|
394 |
*/ |
|
395 |
public static class JAXPSAXParser extends com.sun.org.apache.xerces.internal.parsers.SAXParser { |
|
396 |
||
397 |
private final HashMap fInitFeatures = new HashMap(); |
|
398 |
private final HashMap fInitProperties = new HashMap(); |
|
399 |
private final SAXParserImpl fSAXParser; |
|
400 |
||
401 |
public JAXPSAXParser() { |
|
402 |
this(null); |
|
403 |
} |
|
404 |
||
405 |
JAXPSAXParser(SAXParserImpl saxParser) { |
|
406 |
super(); |
|
407 |
fSAXParser = saxParser; |
|
408 |
} |
|
409 |
||
410 |
/** |
|
411 |
* Override SAXParser's setFeature method to track the initial state |
|
412 |
* of features. This keeps us from affecting the performance of the |
|
413 |
* SAXParser when it is created with XMLReaderFactory. |
|
414 |
*/ |
|
415 |
public synchronized void setFeature(String name, boolean value) |
|
416 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
417 |
if (name == null) { |
|
418 |
// TODO: Add localized error message. |
|
419 |
throw new NullPointerException(); |
|
420 |
} |
|
421 |
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { |
|
422 |
try { |
|
423 |
setProperty(SECURITY_MANAGER, value ? new SecurityManager() : null); |
|
424 |
} |
|
425 |
catch (SAXNotRecognizedException exc) { |
|
426 |
// If the property is not supported |
|
427 |
// re-throw the exception if the value is true. |
|
428 |
if (value) { |
|
429 |
throw exc; |
|
430 |
} |
|
431 |
} |
|
432 |
catch (SAXNotSupportedException exc) { |
|
433 |
// If the property is not supported |
|
434 |
// re-throw the exception if the value is true. |
|
435 |
if (value) { |
|
436 |
throw exc; |
|
437 |
} |
|
438 |
} |
|
439 |
return; |
|
440 |
} |
|
441 |
if (!fInitFeatures.containsKey(name)) { |
|
442 |
boolean current = super.getFeature(name); |
|
443 |
fInitFeatures.put(name, current ? Boolean.TRUE : Boolean.FALSE); |
|
444 |
} |
|
445 |
/** Forward feature to the schema validator if there is one. **/ |
|
446 |
if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { |
|
447 |
setSchemaValidatorFeature(name, value); |
|
448 |
} |
|
449 |
super.setFeature(name, value); |
|
450 |
} |
|
451 |
||
452 |
public synchronized boolean getFeature(String name) |
|
453 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
454 |
if (name == null) { |
|
455 |
// TODO: Add localized error message. |
|
456 |
throw new NullPointerException(); |
|
457 |
} |
|
458 |
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { |
|
459 |
try { |
|
460 |
return (super.getProperty(SECURITY_MANAGER) != null); |
|
461 |
} |
|
462 |
// If the property is not supported the value must be false. |
|
463 |
catch (SAXException exc) { |
|
464 |
return false; |
|
465 |
} |
|
466 |
} |
|
467 |
return super.getFeature(name); |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Override SAXParser's setProperty method to track the initial state |
|
472 |
* of properties. This keeps us from affecting the performance of the |
|
473 |
* SAXParser when it is created with XMLReaderFactory. |
|
474 |
*/ |
|
475 |
public synchronized void setProperty(String name, Object value) |
|
476 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
477 |
if (name == null) { |
|
478 |
// TODO: Add localized error message. |
|
479 |
throw new NullPointerException(); |
|
480 |
} |
|
481 |
if (fSAXParser != null) { |
|
482 |
// JAXP 1.2 support |
|
483 |
if (JAXP_SCHEMA_LANGUAGE.equals(name)) { |
|
484 |
// The spec says if a schema is given via SAXParserFactory |
|
485 |
// the JAXP 1.2 properties shouldn't be allowed. |
|
486 |
if (fSAXParser.grammar != null) { |
|
487 |
throw new SAXNotSupportedException( |
|
488 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-already-specified", new Object[] {name})); |
|
489 |
} |
|
490 |
if ( W3C_XML_SCHEMA.equals(value) ) { |
|
491 |
//None of the properties will take effect till the setValidating(true) has been called |
|
492 |
if( fSAXParser.isValidating() ) { |
|
493 |
fSAXParser.schemaLanguage = W3C_XML_SCHEMA; |
|
494 |
setFeature(XMLSCHEMA_VALIDATION_FEATURE, true); |
|
495 |
// this will allow the parser not to emit DTD-related |
|
496 |
// errors, as the spec demands |
|
497 |
if (!fInitProperties.containsKey(JAXP_SCHEMA_LANGUAGE)) { |
|
498 |
fInitProperties.put(JAXP_SCHEMA_LANGUAGE, super.getProperty(JAXP_SCHEMA_LANGUAGE)); |
|
499 |
} |
|
500 |
super.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); |
|
501 |
} |
|
502 |
||
503 |
} |
|
504 |
else if (value == null) { |
|
505 |
fSAXParser.schemaLanguage = null; |
|
506 |
setFeature(XMLSCHEMA_VALIDATION_FEATURE, false); |
|
507 |
} |
|
508 |
else { |
|
509 |
// REVISIT: It would be nice if we could format this message |
|
510 |
// using a user specified locale as we do in the underlying |
|
511 |
// XMLReader -- mrglavas |
|
512 |
throw new SAXNotSupportedException( |
|
513 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-not-supported", null)); |
|
514 |
} |
|
515 |
return; |
|
516 |
} |
|
517 |
else if (JAXP_SCHEMA_SOURCE.equals(name)) { |
|
518 |
// The spec says if a schema is given via SAXParserFactory |
|
519 |
// the JAXP 1.2 properties shouldn't be allowed. |
|
520 |
if (fSAXParser.grammar != null) { |
|
521 |
throw new SAXNotSupportedException( |
|
522 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-already-specified", new Object[] {name})); |
|
523 |
} |
|
524 |
String val = (String)getProperty(JAXP_SCHEMA_LANGUAGE); |
|
525 |
if ( val != null && W3C_XML_SCHEMA.equals(val) ) { |
|
526 |
if (!fInitProperties.containsKey(JAXP_SCHEMA_SOURCE)) { |
|
527 |
fInitProperties.put(JAXP_SCHEMA_SOURCE, super.getProperty(JAXP_SCHEMA_SOURCE)); |
|
528 |
} |
|
529 |
super.setProperty(name, value); |
|
530 |
} |
|
531 |
else { |
|
532 |
throw new SAXNotSupportedException( |
|
533 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), |
|
534 |
"jaxp-order-not-supported", |
|
535 |
new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE})); |
|
536 |
} |
|
537 |
return; |
|
538 |
} |
|
539 |
} |
|
540 |
/** Forward property to the schema validator if there is one. **/ |
|
541 |
if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { |
|
542 |
setSchemaValidatorProperty(name, value); |
|
543 |
} |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
544 |
/** Check to see if the property is managed by the property manager **/ |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
545 |
int index = fSAXParser.fSecurityPropertyMgr.getIndex(name); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
546 |
if (index > -1) { |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
547 |
fSAXParser.fSecurityPropertyMgr.setValue(index, |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
548 |
XMLSecurityPropertyManager.State.APIPROPERTY, (String)value); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
549 |
} else { |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
550 |
if (!fInitProperties.containsKey(name)) { |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
551 |
fInitProperties.put(name, super.getProperty(name)); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
552 |
} |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
553 |
super.setProperty(name, value); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
554 |
} |
12005 | 555 |
} |
556 |
||
557 |
public synchronized Object getProperty(String name) |
|
558 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
559 |
if (name == null) { |
|
560 |
// TODO: Add localized error message. |
|
561 |
throw new NullPointerException(); |
|
562 |
} |
|
563 |
if (fSAXParser != null && JAXP_SCHEMA_LANGUAGE.equals(name)) { |
|
564 |
// JAXP 1.2 support |
|
565 |
return fSAXParser.schemaLanguage; |
|
566 |
} |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
567 |
int index = fSAXParser.fSecurityPropertyMgr.getIndex(name); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
568 |
if (index > -1) { |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
569 |
return fSAXParser.fSecurityPropertyMgr.getValueByIndex(index); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
570 |
} |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17786
diff
changeset
|
571 |
|
12005 | 572 |
return super.getProperty(name); |
573 |
} |
|
574 |
||
575 |
synchronized void restoreInitState() |
|
576 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
577 |
Iterator iter; |
|
578 |
if (!fInitFeatures.isEmpty()) { |
|
579 |
iter = fInitFeatures.entrySet().iterator(); |
|
580 |
while (iter.hasNext()) { |
|
581 |
Map.Entry entry = (Map.Entry) iter.next(); |
|
582 |
String name = (String) entry.getKey(); |
|
583 |
boolean value = ((Boolean) entry.getValue()).booleanValue(); |
|
584 |
super.setFeature(name, value); |
|
585 |
} |
|
586 |
fInitFeatures.clear(); |
|
587 |
} |
|
588 |
if (!fInitProperties.isEmpty()) { |
|
589 |
iter = fInitProperties.entrySet().iterator(); |
|
590 |
while (iter.hasNext()) { |
|
591 |
Map.Entry entry = (Map.Entry) iter.next(); |
|
592 |
String name = (String) entry.getKey(); |
|
593 |
Object value = entry.getValue(); |
|
594 |
super.setProperty(name, value); |
|
595 |
} |
|
596 |
fInitProperties.clear(); |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
public void parse(InputSource inputSource) |
|
601 |
throws SAXException, IOException { |
|
602 |
if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { |
|
603 |
if (fSAXParser.fSchemaValidationManager != null) { |
|
604 |
fSAXParser.fSchemaValidationManager.reset(); |
|
605 |
fSAXParser.fUnparsedEntityHandler.reset(); |
|
606 |
} |
|
607 |
resetSchemaValidator(); |
|
608 |
} |
|
609 |
super.parse(inputSource); |
|
610 |
} |
|
611 |
||
612 |
public void parse(String systemId) |
|
613 |
throws SAXException, IOException { |
|
614 |
if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { |
|
615 |
if (fSAXParser.fSchemaValidationManager != null) { |
|
616 |
fSAXParser.fSchemaValidationManager.reset(); |
|
617 |
fSAXParser.fUnparsedEntityHandler.reset(); |
|
618 |
} |
|
619 |
resetSchemaValidator(); |
|
620 |
} |
|
621 |
super.parse(systemId); |
|
622 |
} |
|
623 |
||
624 |
XMLParserConfiguration getXMLParserConfiguration() { |
|
625 |
return fConfiguration; |
|
626 |
} |
|
627 |
||
628 |
void setFeature0(String name, boolean value) |
|
629 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
630 |
super.setFeature(name, value); |
|
631 |
} |
|
632 |
||
633 |
boolean getFeature0(String name) |
|
634 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
635 |
return super.getFeature(name); |
|
636 |
} |
|
637 |
||
638 |
void setProperty0(String name, Object value) |
|
639 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
640 |
super.setProperty(name, value); |
|
641 |
} |
|
642 |
||
643 |
Object getProperty0(String name) |
|
644 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
645 |
return super.getProperty(name); |
|
646 |
} |
|
647 |
||
648 |
private void setSchemaValidatorFeature(String name, boolean value) |
|
649 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
650 |
try { |
|
651 |
fSAXParser.fSchemaValidator.setFeature(name, value); |
|
652 |
} |
|
653 |
// This should never be thrown from the schema validator. |
|
654 |
catch (XMLConfigurationException e) { |
|
655 |
String identifier = e.getIdentifier(); |
|
656 |
if (e.getType() == Status.NOT_RECOGNIZED) { |
|
657 |
throw new SAXNotRecognizedException( |
|
658 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), |
|
659 |
"feature-not-recognized", new Object [] {identifier})); |
|
660 |
} |
|
661 |
else { |
|
662 |
throw new SAXNotSupportedException( |
|
663 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), |
|
664 |
"feature-not-supported", new Object [] {identifier})); |
|
665 |
} |
|
666 |
} |
|
667 |
} |
|
668 |
||
669 |
private void setSchemaValidatorProperty(String name, Object value) |
|
670 |
throws SAXNotRecognizedException, SAXNotSupportedException { |
|
671 |
try { |
|
672 |
fSAXParser.fSchemaValidator.setProperty(name, value); |
|
673 |
} |
|
674 |
// This should never be thrown from the schema validator. |
|
675 |
catch (XMLConfigurationException e) { |
|
676 |
String identifier = e.getIdentifier(); |
|
677 |
if (e.getType() == Status.NOT_RECOGNIZED) { |
|
678 |
throw new SAXNotRecognizedException( |
|
679 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), |
|
680 |
"property-not-recognized", new Object [] {identifier})); |
|
681 |
} |
|
682 |
else { |
|
683 |
throw new SAXNotSupportedException( |
|
684 |
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), |
|
685 |
"property-not-supported", new Object [] {identifier})); |
|
686 |
} |
|
687 |
} |
|
688 |
} |
|
689 |
||
690 |
private void resetSchemaValidator() throws SAXException { |
|
691 |
try { |
|
692 |
fSAXParser.fSchemaValidator.reset(fSAXParser.fSchemaValidatorComponentManager); |
|
693 |
} |
|
694 |
// This should never be thrown from the schema validator. |
|
695 |
catch (XMLConfigurationException e) { |
|
696 |
throw new SAXException(e); |
|
697 |
} |
|
698 |
} |
|
699 |
} |
|
700 |
} |