1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.io.IOException ; 61 import java.util.ArrayList ; 62 import java.util.HashMap ; 63 import java.util.Locale ; 64 65 import com.sun.org.apache.xerces.internal.impl.Constants; 66 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; 67 import com.sun.org.apache.xerces.internal.util.SymbolTable; 68 import com.sun.org.apache.xerces.internal.xni.XMLDTDContentModelHandler; 69 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; 70 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 71 import com.sun.org.apache.xerces.internal.xni.XNIException; 72 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 73 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 74 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 75 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 76 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 77 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 78 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 80 81 133 public abstract class BasicParserConfiguration 134 extends ParserConfigurationSettings 135 implements XMLParserConfiguration { 136 137 141 143 144 protected static final String VALIDATION = 145 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 146 147 148 protected static final String NAMESPACES = 149 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 150 151 152 protected static final String EXTERNAL_GENERAL_ENTITIES = 153 Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE; 154 155 156 protected static final String EXTERNAL_PARAMETER_ENTITIES = 157 Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE; 158 159 161 162 protected static final String XML_STRING = 163 Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY; 164 165 166 protected static final String SYMBOL_TABLE = 167 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 168 169 170 protected static final String ERROR_HANDLER = 171 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 172 173 174 protected static final String ENTITY_RESOLVER = 175 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 176 177 181 183 184 protected SymbolTable fSymbolTable; 185 186 187 189 190 protected Locale fLocale; 191 192 193 protected ArrayList fComponents; 194 195 197 198 protected XMLDocumentHandler fDocumentHandler; 199 200 201 protected XMLDTDHandler fDTDHandler; 202 203 204 protected XMLDTDContentModelHandler fDTDContentModelHandler; 205 206 207 protected XMLDocumentSource fLastComponent; 208 209 213 214 protected BasicParserConfiguration() { 215 this(null, null); 216 } 218 223 protected BasicParserConfiguration(SymbolTable symbolTable) { 224 this(symbolTable, null); 225 } 227 234 protected BasicParserConfiguration(SymbolTable symbolTable, 235 XMLComponentManager parentSettings) { 236 super(parentSettings); 237 238 fComponents = new ArrayList (); 240 241 fRecognizedFeatures = new ArrayList (); 243 fRecognizedProperties = new ArrayList (); 244 245 fFeatures = new HashMap (); 247 fProperties = new HashMap (); 248 249 final String [] recognizedFeatures = { 251 PARSER_SETTINGS, 252 VALIDATION, 253 NAMESPACES, 254 EXTERNAL_GENERAL_ENTITIES, 255 EXTERNAL_PARAMETER_ENTITIES, 256 }; 257 addRecognizedFeatures(recognizedFeatures); 258 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 259 fFeatures.put(VALIDATION, Boolean.FALSE); 261 fFeatures.put(NAMESPACES, Boolean.TRUE); 262 fFeatures.put(EXTERNAL_GENERAL_ENTITIES, Boolean.TRUE); 263 fFeatures.put(EXTERNAL_PARAMETER_ENTITIES, Boolean.TRUE); 264 265 final String [] recognizedProperties = { 267 XML_STRING, 268 SYMBOL_TABLE, 269 ERROR_HANDLER, 270 ENTITY_RESOLVER, 271 }; 272 addRecognizedProperties(recognizedProperties); 273 274 if (symbolTable == null) { 275 symbolTable = new SymbolTable(); 276 } 277 fSymbolTable = symbolTable; 278 fProperties.put(SYMBOL_TABLE, fSymbolTable); 279 280 } 282 289 protected void addComponent(XMLComponent component) { 290 291 if (fComponents.contains(component)) { 293 return; 294 } 295 fComponents.add(component); 296 297 String [] recognizedFeatures = component.getRecognizedFeatures(); 299 addRecognizedFeatures(recognizedFeatures); 300 301 String [] recognizedProperties = component.getRecognizedProperties(); 303 addRecognizedProperties(recognizedProperties); 304 305 if (recognizedFeatures != null) { 307 for (int i = 0; i < recognizedFeatures.length; i++) { 308 String featureId = recognizedFeatures[i]; 309 Boolean state = component.getFeatureDefault(featureId); 310 if (state != null) { 311 super.setFeature(featureId, state.booleanValue()); 312 } 313 } 314 } 315 if (recognizedProperties != null) { 316 for (int i = 0; i < recognizedProperties.length; i++) { 317 String propertyId = recognizedProperties[i]; 318 Object value = component.getPropertyDefault(propertyId); 319 if (value != null) { 320 super.setProperty(propertyId, value); 321 } 322 } 323 } 324 325 } 327 331 355 public abstract void parse(XMLInputSource inputSource) 356 throws XNIException, IOException ; 357 358 364 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 365 fDocumentHandler = documentHandler; 366 if (fLastComponent != null) { 367 fLastComponent.setDocumentHandler(fDocumentHandler); 368 if (fDocumentHandler !=null){ 369 fDocumentHandler.setDocumentSource(fLastComponent); 370 } 371 } 372 } 374 375 public XMLDocumentHandler getDocumentHandler() { 376 return fDocumentHandler; 377 } 379 384 public void setDTDHandler(XMLDTDHandler dtdHandler) { 385 fDTDHandler = dtdHandler; 386 } 388 389 public XMLDTDHandler getDTDHandler() { 390 return fDTDHandler; 391 } 393 398 public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) { 399 fDTDContentModelHandler = handler; 400 } 402 403 public XMLDTDContentModelHandler getDTDContentModelHandler() { 404 return fDTDContentModelHandler; 405 } 407 414 public void setEntityResolver(XMLEntityResolver resolver) { 415 fProperties.put(ENTITY_RESOLVER, resolver); 417 } 419 426 public XMLEntityResolver getEntityResolver() { 427 return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER); 429 } 431 449 public void setErrorHandler(XMLErrorHandler errorHandler) { 450 fProperties.put(ERROR_HANDLER, errorHandler); 452 } 454 461 public XMLErrorHandler getErrorHandler() { 462 return (XMLErrorHandler)fProperties.get(ERROR_HANDLER); 464 } 466 479 public void setFeature(String featureId, boolean state) 480 throws XMLConfigurationException { 481 482 int count = fComponents.size(); 484 for (int i = 0; i < count; i++) { 485 XMLComponent c = (XMLComponent) fComponents.get(i); 486 c.setFeature(featureId, state); 487 } 488 super.setFeature(featureId, state); 490 491 } 493 499 public void setProperty(String propertyId, Object value) 500 throws XMLConfigurationException { 501 502 int count = fComponents.size(); 504 for (int i = 0; i < count; i++) { 505 XMLComponent c = (XMLComponent) fComponents.get(i); 506 c.setProperty(propertyId, value); 507 } 508 509 super.setProperty(propertyId, value); 511 512 } 514 522 public void setLocale(Locale locale) throws XNIException { 523 fLocale = locale; 524 } 526 527 public Locale getLocale() { 528 return fLocale; 529 } 531 535 538 protected void reset() throws XNIException { 539 540 int count = fComponents.size(); 542 for (int i = 0; i < count; i++) { 543 XMLComponent c = (XMLComponent) fComponents.get(i); 544 c.reset(this); 545 } 546 547 } 549 558 protected void checkProperty(String propertyId) 559 throws XMLConfigurationException { 560 561 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 563 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 564 565 if (suffixLength == Constants.XML_STRING_PROPERTY.length() && 576 propertyId.endsWith(Constants.XML_STRING_PROPERTY)) { 577 short type = XMLConfigurationException.NOT_SUPPORTED; 581 throw new XMLConfigurationException(type, propertyId); 582 } 583 } 584 585 super.checkProperty(propertyId); 587 588 } 590 591 603 protected void checkFeature(String featureId) 604 throws XMLConfigurationException { 605 606 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 610 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 611 612 if (suffixLength == Constants.PARSER_SETTINGS.length() && 616 featureId.endsWith(Constants.PARSER_SETTINGS)) { 617 short type = XMLConfigurationException.NOT_SUPPORTED; 618 throw new XMLConfigurationException(type, featureId); 619 } 620 } 621 622 super.checkFeature(featureId); 623 624 } 626 627 } | Popular Tags |