1 57 58 package com.sun.org.apache.xerces.internal.jaxp; 59 60 import java.util.Enumeration ; 61 import java.util.HashMap ; 62 import java.util.Hashtable ; 63 64 import javax.xml.parsers.SAXParserFactory ; 65 import javax.xml.validation.Schema ; 66 67 import com.sun.org.apache.xerces.internal.impl.Constants; 68 import com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser; 69 import com.sun.org.apache.xerces.internal.parsers.JAXPConfiguration; 70 import org.xml.sax.SAXException ; 71 import org.xml.sax.SAXNotRecognizedException ; 72 import org.xml.sax.SAXNotSupportedException ; 73 import org.xml.sax.XMLReader ; 74 import com.sun.org.apache.xerces.internal.util.SecurityManager; 75 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter; 76 import com.sun.org.apache.xerces.internal.xni.XNIException; 77 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 78 79 88 public class SAXParserImpl extends javax.xml.parsers.SAXParser 89 implements JAXPConstants { 90 91 private final JAXPConfiguration parserConfiguration; 93 private final AbstractSAXParser xmlReader; 94 private String schemaLanguage = null; private final Schema grammar; 96 private final SecurityManager secureProcessing; 97 private boolean isXIncludeAware; 98 private boolean enableSecureProcessing = true; 99 100 private Hashtable features; 104 private SAXParserFactory spf; 105 106 private Hashtable parserFeatures = new Hashtable (); 107 108 public boolean isXIncludeAware() { 109 return isXIncludeAware; 110 } 111 112 113 117 SAXParserImpl(SAXParserFactory spfactory, Hashtable factoryfeatures) 118 throws SAXException 119 { 120 this.spf = spfactory ; 121 this.features = factoryfeatures; 122 secureProcessing = new SecurityManager (); 124 this.grammar = spf.getSchema(); 125 parserConfiguration = new JAXPConfiguration(grammar); 126 xmlReader = new com.sun.org.apache.xerces.internal.parsers.SAXParser(parserConfiguration); 127 128 init(); 130 } 131 132 133 void resetSettings() throws SAXNotSupportedException , SAXNotRecognizedException { 134 Enumeration keys = parserFeatures.keys(); 135 136 while(keys.hasMoreElements()){ 137 String propertyId = (String )keys.nextElement(); 138 Object value = parserFeatures.get(propertyId); 139 if(value instanceof Boolean ){ 140 xmlReader.setFeature(propertyId, parserConfiguration.getFeatureDefaultValue(propertyId)); 143 } 144 else{ xmlReader.setProperty(propertyId, null); 148 } 149 } 150 parserFeatures.clear(); 152 153 } 154 155 void init() throws SAXNotSupportedException , SAXNotRecognizedException { 157 158 schemaLanguage = null ; 159 this.isXIncludeAware = spf.isXIncludeAware(); 160 if(features != null ){ 161 Object tmpValue = features.get(Constants.FEATURE_SECURE_PROCESSING); 162 if( tmpValue != null ) 163 enableSecureProcessing = ((Boolean )tmpValue).booleanValue(); 164 } 165 166 if(enableSecureProcessing){ 167 try { 168 setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, secureProcessing); 169 } catch (SAXNotRecognizedException sex) { 170 sex.printStackTrace(); 171 } catch (SAXNotSupportedException se) { 172 se.printStackTrace(); 173 } 174 } 175 176 xmlReader.setFeature( 177 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE, 178 isXIncludeAware); 179 180 if (spf.isValidating()) { 184 xmlReader.setErrorHandler(new DefaultValidationErrorHandler()); 185 } 186 187 xmlReader.setFeature(Constants.SAX_FEATURE_PREFIX + 188 Constants.VALIDATION_FEATURE, spf.isValidating()); 189 190 xmlReader.setFeature(Constants.SAX_FEATURE_PREFIX + 194 Constants.NAMESPACES_FEATURE, 195 spf.isNamespaceAware()); 196 197 xmlReader.setFeature(Constants.SAX_FEATURE_PREFIX + 201 Constants.NAMESPACE_PREFIXES_FEATURE, 202 !spf.isNamespaceAware()); 203 setFeatures(features); 204 205 } 207 214 private void setFeatures(Hashtable features) 215 throws SAXNotSupportedException , SAXNotRecognizedException 216 { 217 if (features != null) { 218 for (Enumeration e = features.keys(); e.hasMoreElements();) { 219 String feature = (String )e.nextElement(); 220 boolean value = ((Boolean )features.get(feature)).booleanValue(); 221 if(!feature.equals(Constants.FEATURE_SECURE_PROCESSING)) 222 xmlReader.setFeature(feature, value); 223 } 224 } 225 } 226 227 230 public org.xml.sax.Parser getParser() throws SAXException { 231 return xmlReader; 232 } 233 234 248 249 public void reset(){ 250 if(xmlReader != null){ 251 try{ 252 xmlReader.reset(); 253 resetSettings(); 255 256 } 257 catch(XNIException ex){ 261 } 263 catch(SAXException sax){} 264 } 265 } 266 267 268 272 public XMLReader getXMLReader() { 273 return xmlReader; 274 } 275 276 public boolean isNamespaceAware() { 277 try { 278 return xmlReader.getFeature(Constants.SAX_FEATURE_PREFIX + 279 Constants.NAMESPACES_FEATURE); 280 } catch (SAXException x) { 281 throw new IllegalStateException (x.getMessage()); 282 } 283 } 284 285 public boolean isValidating() { 286 try { 287 return xmlReader.getFeature(Constants.SAX_FEATURE_PREFIX + 288 Constants.VALIDATION_FEATURE); 289 } catch (SAXException x) { 290 throw new IllegalStateException (x.getMessage()); 291 } 292 } 293 294 298 public void setProperty(String name, Object value) 299 throws SAXNotRecognizedException , SAXNotSupportedException 300 { 301 if(grammar!=null) { 305 if (JAXP_SCHEMA_LANGUAGE.equals(name) 306 || JAXP_SCHEMA_SOURCE.equals(name)) { 307 throw new SAXNotSupportedException ( 308 SAXMessageFormatter.formatMessage(null, "schema-already-specified", null)); 309 } 310 } 311 312 if (JAXP_SCHEMA_LANGUAGE.equals(name)) { 313 if ( W3C_XML_SCHEMA.equals(value) ) { 315 if( isValidating() ) { 317 schemaLanguage = W3C_XML_SCHEMA; 318 xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX + 319 Constants.SCHEMA_VALIDATION_FEATURE, 320 true); 321 xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX + 323 Constants.SCHEMA_FULL_CHECKING, 324 true); 325 parserFeatures.put(Constants.XERCES_FEATURE_PREFIX + 329 Constants.SCHEMA_VALIDATION_FEATURE, new Boolean (true)); 330 parserFeatures.put(Constants.XERCES_FEATURE_PREFIX + 331 Constants.SCHEMA_FULL_CHECKING, new Boolean (true)); 332 333 xmlReader.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 336 parserFeatures.put(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 337 338 } 339 else{ 340 } 342 343 } else if (value == null) { 344 schemaLanguage = null; 345 xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX + 346 Constants.SCHEMA_VALIDATION_FEATURE, 347 false); 348 parserFeatures.put(Constants.XERCES_FEATURE_PREFIX + 349 Constants.SCHEMA_VALIDATION_FEATURE, 350 new Boolean (false)); 351 352 } else { 353 throw new SAXNotSupportedException ( 357 SAXMessageFormatter.formatMessage(null, "schema-not-supported", null)); 358 } 359 } 360 else if(JAXP_SCHEMA_SOURCE.equals(name)) { 361 if ( isValidating() ) { 363 String val = (String )getProperty(JAXP_SCHEMA_LANGUAGE); 364 if ( val != null && W3C_XML_SCHEMA.equals(val) ) { 365 xmlReader.setProperty(name, value); 366 parserFeatures.put(name, value); 367 } 368 else { 369 throw new SAXNotSupportedException ( 370 SAXMessageFormatter.formatMessage(null, 371 "jaxp-order-not-supported", 372 new Object [] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE})); 373 } 374 } 375 } 376 389 else if(value instanceof Boolean ){ 390 xmlReader.setFeature(name,((Boolean )value).booleanValue()); 392 parserFeatures.put(name, value); 393 } 394 else{ 395 xmlReader.setProperty(name, value); 396 if (value == null) { 398 parserFeatures.remove(name); 399 } else { 400 parserFeatures.put(name, value); 401 } 402 } 403 } 404 405 409 public Object getProperty(String name) 410 throws SAXNotRecognizedException , SAXNotSupportedException 411 { 412 if (JAXP_SCHEMA_LANGUAGE.equals(name)) { 414 return schemaLanguage; 416 } else { 417 return xmlReader.getProperty(name); 418 } 419 } 420 421 422 public Schema getSchema(){ 423 return grammar; 424 } 425 426 432 public boolean isSecureProcessing(){ 433 return secureProcessing!=null; 434 } 435 436 437 } 438 | Popular Tags |