1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Locale ; 23 24 import org.apache.xerces.impl.Constants; 25 import org.apache.xerces.util.ParserConfigurationSettings; 26 import org.apache.xerces.util.SymbolTable; 27 import org.apache.xerces.xni.XMLDTDContentModelHandler; 28 import org.apache.xerces.xni.XMLDTDHandler; 29 import org.apache.xerces.xni.XMLDocumentHandler; 30 import org.apache.xerces.xni.XNIException; 31 import org.apache.xerces.xni.parser.XMLComponent; 32 import org.apache.xerces.xni.parser.XMLComponentManager; 33 import org.apache.xerces.xni.parser.XMLConfigurationException; 34 import org.apache.xerces.xni.parser.XMLDocumentSource; 35 import org.apache.xerces.xni.parser.XMLEntityResolver; 36 import org.apache.xerces.xni.parser.XMLErrorHandler; 37 import org.apache.xerces.xni.parser.XMLInputSource; 38 import org.apache.xerces.xni.parser.XMLParserConfiguration; 39 40 92 public abstract class BasicParserConfiguration 93 extends ParserConfigurationSettings 94 implements XMLParserConfiguration { 95 96 100 102 103 protected static final String VALIDATION = 104 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 105 106 107 protected static final String NAMESPACES = 108 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 109 110 111 protected static final String EXTERNAL_GENERAL_ENTITIES = 112 Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE; 113 114 115 protected static final String EXTERNAL_PARAMETER_ENTITIES = 116 Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE; 117 118 120 121 protected static final String XML_STRING = 122 Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY; 123 124 125 protected static final String SYMBOL_TABLE = 126 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 127 128 129 protected static final String ERROR_HANDLER = 130 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 131 132 133 protected static final String ENTITY_RESOLVER = 134 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 135 136 140 142 143 protected SymbolTable fSymbolTable; 144 145 146 148 149 protected Locale fLocale; 150 151 152 protected ArrayList fComponents; 153 154 156 157 protected XMLDocumentHandler fDocumentHandler; 158 159 160 protected XMLDTDHandler fDTDHandler; 161 162 163 protected XMLDTDContentModelHandler fDTDContentModelHandler; 164 165 166 protected XMLDocumentSource fLastComponent; 167 168 172 173 protected BasicParserConfiguration() { 174 this(null, null); 175 } 177 182 protected BasicParserConfiguration(SymbolTable symbolTable) { 183 this(symbolTable, null); 184 } 186 193 protected BasicParserConfiguration(SymbolTable symbolTable, 194 XMLComponentManager parentSettings) { 195 super(parentSettings); 196 197 fComponents = new ArrayList (); 199 200 fRecognizedFeatures = new ArrayList (); 202 fRecognizedProperties = new ArrayList (); 203 204 fFeatures = new HashMap (); 206 fProperties = new HashMap (); 207 208 final String [] recognizedFeatures = { 210 PARSER_SETTINGS, 211 VALIDATION, 212 NAMESPACES, 213 EXTERNAL_GENERAL_ENTITIES, 214 EXTERNAL_PARAMETER_ENTITIES, 215 }; 216 addRecognizedFeatures(recognizedFeatures); 217 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 218 fFeatures.put(VALIDATION, Boolean.FALSE); 220 fFeatures.put(NAMESPACES, Boolean.TRUE); 221 fFeatures.put(EXTERNAL_GENERAL_ENTITIES, Boolean.TRUE); 222 fFeatures.put(EXTERNAL_PARAMETER_ENTITIES, Boolean.TRUE); 223 224 final String [] recognizedProperties = { 226 XML_STRING, 227 SYMBOL_TABLE, 228 ERROR_HANDLER, 229 ENTITY_RESOLVER, 230 }; 231 addRecognizedProperties(recognizedProperties); 232 233 if (symbolTable == null) { 234 symbolTable = new SymbolTable(); 235 } 236 fSymbolTable = symbolTable; 237 fProperties.put(SYMBOL_TABLE, fSymbolTable); 238 239 } 241 248 protected void addComponent(XMLComponent component) { 249 250 if (fComponents.contains(component)) { 252 return; 253 } 254 fComponents.add(component); 255 256 String [] recognizedFeatures = component.getRecognizedFeatures(); 258 addRecognizedFeatures(recognizedFeatures); 259 260 String [] recognizedProperties = component.getRecognizedProperties(); 262 addRecognizedProperties(recognizedProperties); 263 264 if (recognizedFeatures != null) { 266 for (int i = 0; i < recognizedFeatures.length; i++) { 267 String featureId = recognizedFeatures[i]; 268 Boolean state = component.getFeatureDefault(featureId); 269 if (state != null) { 270 super.setFeature(featureId, state.booleanValue()); 271 } 272 } 273 } 274 if (recognizedProperties != null) { 275 for (int i = 0; i < recognizedProperties.length; i++) { 276 String propertyId = recognizedProperties[i]; 277 Object value = component.getPropertyDefault(propertyId); 278 if (value != null) { 279 super.setProperty(propertyId, value); 280 } 281 } 282 } 283 284 } 286 290 314 public abstract void parse(XMLInputSource inputSource) 315 throws XNIException, IOException ; 316 317 323 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 324 fDocumentHandler = documentHandler; 325 if (fLastComponent != null) { 326 fLastComponent.setDocumentHandler(fDocumentHandler); 327 if (fDocumentHandler !=null){ 328 fDocumentHandler.setDocumentSource(fLastComponent); 329 } 330 } 331 } 333 334 public XMLDocumentHandler getDocumentHandler() { 335 return fDocumentHandler; 336 } 338 343 public void setDTDHandler(XMLDTDHandler dtdHandler) { 344 fDTDHandler = dtdHandler; 345 } 347 348 public XMLDTDHandler getDTDHandler() { 349 return fDTDHandler; 350 } 352 357 public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) { 358 fDTDContentModelHandler = handler; 359 } 361 362 public XMLDTDContentModelHandler getDTDContentModelHandler() { 363 return fDTDContentModelHandler; 364 } 366 373 public void setEntityResolver(XMLEntityResolver resolver) { 374 fProperties.put(ENTITY_RESOLVER, resolver); 376 } 378 385 public XMLEntityResolver getEntityResolver() { 386 return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER); 388 } 390 408 public void setErrorHandler(XMLErrorHandler errorHandler) { 409 fProperties.put(ERROR_HANDLER, errorHandler); 411 } 413 420 public XMLErrorHandler getErrorHandler() { 421 return (XMLErrorHandler)fProperties.get(ERROR_HANDLER); 423 } 425 438 public void setFeature(String featureId, boolean state) 439 throws XMLConfigurationException { 440 441 int count = fComponents.size(); 443 for (int i = 0; i < count; i++) { 444 XMLComponent c = (XMLComponent) fComponents.get(i); 445 c.setFeature(featureId, state); 446 } 447 super.setFeature(featureId, state); 449 450 } 452 458 public void setProperty(String propertyId, Object value) 459 throws XMLConfigurationException { 460 461 int count = fComponents.size(); 463 for (int i = 0; i < count; i++) { 464 XMLComponent c = (XMLComponent) fComponents.get(i); 465 c.setProperty(propertyId, value); 466 } 467 468 super.setProperty(propertyId, value); 470 471 } 473 481 public void setLocale(Locale locale) throws XNIException { 482 fLocale = locale; 483 } 485 486 public Locale getLocale() { 487 return fLocale; 488 } 490 494 497 protected void reset() throws XNIException { 498 499 int count = fComponents.size(); 501 for (int i = 0; i < count; i++) { 502 XMLComponent c = (XMLComponent) fComponents.get(i); 503 c.reset(this); 504 } 505 506 } 508 517 protected void checkProperty(String propertyId) 518 throws XMLConfigurationException { 519 520 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 522 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 523 524 if (suffixLength == Constants.XML_STRING_PROPERTY.length() && 535 propertyId.endsWith(Constants.XML_STRING_PROPERTY)) { 536 short type = XMLConfigurationException.NOT_SUPPORTED; 540 throw new XMLConfigurationException(type, propertyId); 541 } 542 } 543 544 super.checkProperty(propertyId); 546 547 } 549 550 562 protected void checkFeature(String featureId) 563 throws XMLConfigurationException { 564 565 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 569 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 570 571 if (suffixLength == Constants.PARSER_SETTINGS.length() && 575 featureId.endsWith(Constants.PARSER_SETTINGS)) { 576 short type = XMLConfigurationException.NOT_SUPPORTED; 577 throw new XMLConfigurationException(type, featureId); 578 } 579 } 580 581 super.checkFeature(featureId); 582 583 } 585 586 } | Popular Tags |