| 1 57 58 package org.enhydra.apache.xerces.framework; 59 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.Reader ; 63 import java.util.Locale ; 64 65 import org.enhydra.apache.xerces.readers.DefaultEntityHandler; 66 import org.enhydra.apache.xerces.readers.XMLDeclRecognizer; 67 import org.enhydra.apache.xerces.readers.XMLEntityReaderFactory; 68 import org.enhydra.apache.xerces.utils.ChunkyCharArray; 69 import org.enhydra.apache.xerces.utils.ImplementationMessages; 70 import org.enhydra.apache.xerces.utils.StringPool; 71 import org.enhydra.apache.xerces.utils.XMLMessageProvider; 72 import org.enhydra.apache.xerces.utils.XMLMessages; 73 import org.enhydra.apache.xerces.validators.common.GrammarResolver; 74 import org.enhydra.apache.xerces.validators.common.GrammarResolverImpl; 75 import org.enhydra.apache.xerces.validators.common.XMLValidator; 76 import org.enhydra.apache.xerces.validators.datatype.DatatypeMessageProvider; 77 import org.enhydra.apache.xerces.validators.schema.SchemaMessageProvider; 78 import org.xml.sax.EntityResolver ; 79 import org.xml.sax.ErrorHandler ; 80 import org.xml.sax.InputSource ; 81 import org.xml.sax.Locator ; 82 import org.xml.sax.SAXException ; 83 import org.xml.sax.SAXNotRecognizedException ; 84 import org.xml.sax.SAXNotSupportedException ; 85 import org.xml.sax.SAXParseException ; 86 87 92 public abstract class XMLParser 93 implements XMLErrorReporter, XMLDocumentHandler.DTDHandler { 94 95 99 101 102 protected static final String SAX2_FEATURES_PREFIX = "http://xml.org/sax/features/"; 103 104 105 protected static final String SAX2_PROPERTIES_PREFIX = "http://xml.org/sax/properties/"; 106 107 108 protected static final String XERCES_FEATURES_PREFIX = "http://apache.org/xml/features/"; 109 110 111 protected static final String XERCES_PROPERTIES_PREFIX = "http://apache.org/xml/properties/"; 112 113 115 116 private static final String RECOGNIZED_FEATURES[] = { 117 "http://xml.org/sax/features/validation", 119 "http://xml.org/sax/features/external-general-entities", 120 "http://xml.org/sax/features/external-parameter-entities", 121 "http://xml.org/sax/features/namespaces", 122 "http://apache.org/xml/features/validation/schema", 124 "http://apache.org/xml/features/validation/schema-full-checking", 126 "http://apache.org/xml/features/validation/dynamic", 127 "http://apache.org/xml/features/validation/default-attribute-values", 128 129 "http://apache.org/xml/features/validation/validate-content-models", 130 "http://apache.org/xml/features/validation/validate-datatypes", 131 "http://apache.org/xml/features/validation/warn-on-duplicate-attdef", 132 "http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", 133 "http://apache.org/xml/features/allow-java-encodings", 134 "http://apache.org/xml/features/continue-after-fatal-error", 135 "http://apache.org/xml/features/nonvalidating/load-dtd-grammar", 136 "http://apache.org/xml/features/nonvalidating/load-external-dtd" 137 }; 138 139 140 private static final String RECOGNIZED_PROPERTIES[] = { 141 "http://xml.org/sax/properties/xml-string", 143 "http://apache.org/xml/properties/schema/external-schemaLocation", 145 "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" 146 }; 147 148 150 151 private static final boolean PRINT_EXCEPTION_STACK_TRACE = false; 152 153 157 protected GrammarResolver fGrammarResolver = null; 158 159 161 protected boolean fParseInProgress = false; 162 private boolean fNeedReset = false; 163 164 166 167 private boolean fContinueAfterFatalError = false; 168 169 171 172 private ErrorHandler fErrorHandler = null; 173 174 176 private Locale fLocale = null; 177 178 180 private static XMLMessageProvider fgXMLMessages = new XMLMessages(); 181 private static XMLMessageProvider fgImplementationMessages = new ImplementationMessages(); 182 private static XMLMessageProvider fgSchemaMessages = new SchemaMessageProvider(); 183 private static XMLMessageProvider fgDatatypeMessages= new DatatypeMessageProvider(); 184 185 protected StringPool fStringPool = null; 189 protected XMLErrorReporter fErrorReporter = null; 190 protected DefaultEntityHandler fEntityHandler = null; 191 protected XMLDocumentScanner fScanner = null; 192 protected XMLValidator fValidator = null; 193 194 198 201 protected XMLParser() { 202 this(new StringPool()); 203 } 204 205 protected XMLParser(StringPool stringPool) { 206 fStringPool = stringPool; 207 fErrorReporter = this; 208 fEntityHandler = new DefaultEntityHandler(fStringPool, fErrorReporter); 209 fScanner = new XMLDocumentScanner(fStringPool, fErrorReporter, fEntityHandler, new ChunkyCharArray(fStringPool)); 210 fValidator = new XMLValidator(fStringPool, fErrorReporter, fEntityHandler, fScanner); 211 fGrammarResolver = new GrammarResolverImpl(); 212 fScanner.setGrammarResolver(fGrammarResolver); 213 fValidator.setGrammarResolver(fGrammarResolver); 214 try { 215 setNamespaces(true); 217 } 218 catch (Exception e) { 219 } 221 } 222 223 226 protected void initHandlers(boolean sendCharDataAsCharArray, 227 XMLDocumentHandler docHandler, 228 XMLDocumentHandler.DTDHandler dtdHandler) 229 { 230 fValidator.initHandlers(sendCharDataAsCharArray, docHandler, dtdHandler); 231 fScanner.setDTDHandler(this); 232 } 233 234 238 240 249 public String [] getFeaturesRecognized() { 250 return RECOGNIZED_FEATURES; 251 } 252 253 260 public boolean isFeatureRecognized(String featureId) { 261 String [] recognizedFeatures = getFeaturesRecognized(); 262 for (int i = 0; i < recognizedFeatures.length; i++) { 263 if (featureId.equals(recognizedFeatures[i])) 264 return true; 265 } 266 return false; 267 } 268 269 278 public String [] getPropertiesRecognized() { 279 return RECOGNIZED_PROPERTIES; 280 } 281 282 289 public boolean isPropertyRecognized(String propertyId) { 290 String [] recognizedProperties = getPropertiesRecognized(); 291 for (int i = 0; i < recognizedProperties.length; i++) { 292 if (propertyId.equals(recognizedProperties[i])) 293 return true; 294 } 295 return false; 296 } 297 298 300 306 public boolean parseSomeSetup(InputSource source) throws Exception { 307 if (fNeedReset) 308 resetOrCopy(); 309 fParseInProgress = true; 310 fNeedReset = true; 311 return fEntityHandler.startReadingFromDocument(source); 312 } 313 314 319 public boolean parseSome() throws Exception { 320 if (!fScanner.parseSome(false)) { 321 fParseInProgress = false; 322 return false; 323 } 324 return true; 325 } 326 327 329 330 public void reset() throws Exception { 331 fGrammarResolver.clearGrammarResolver(); 332 fStringPool.reset(); 333 fEntityHandler.reset(fStringPool); 334 fScanner.reset(fStringPool, new ChunkyCharArray(fStringPool)); 335 fValidator.reset(fStringPool); 336 fNeedReset = false; 337 } 338 339 341 346 public final Locator getLocator() { 347 return fEntityHandler; 348 } 349 350 351 356 public final Locale getfLocale() { 357 return fLocale; 358 } 359 360 365 public final XMLMessageProvider getfgXMLMessages() { 366 return fgXMLMessages; 367 } 368 369 374 public final XMLMessageProvider getfgImplementationMessages() { 375 return fgImplementationMessages; 376 } 377 378 383 public final XMLMessageProvider getfgSchemaMessages() { 384 return fgSchemaMessages; 385 } 386 387 392 public final XMLMessageProvider getfgDatatypeMessages() { 393 return fgDatatypeMessages; 394 } 395 396 399 public void setReaderFactory(XMLEntityReaderFactory readerFactory) { 400 fEntityHandler.setReaderFactory(readerFactory); 401 } 402 403 408 public void addRecognizer(XMLDeclRecognizer recognizer) { 409 fEntityHandler.addRecognizer(recognizer); 410 } 411 412 416 418 431 protected void setValidation(boolean validate) 432 throws SAXNotRecognizedException , SAXNotSupportedException { 433 if (fParseInProgress) { 434 throw new SAXNotSupportedException ("PAR004 Cannot setFeature(http://xml.org/sax/features/validation): parse is in progress.\n"+ 435 "http://xml.org/sax/features/validation"); 436 } 437 try { 438 fScanner.setValidationEnabled(validate); 441 fValidator.setValidationEnabled(validate); 442 } 443 catch (Exception ex) { 444 throw new SAXNotSupportedException (ex.getMessage()); 445 } 446 } 447 448 453 protected boolean getValidation() 454 throws SAXNotRecognizedException , SAXNotSupportedException { 455 return fValidator.getValidationEnabled(); 456 } 457 458 476 protected void setExternalGeneralEntities(boolean expand) 477 throws SAXNotRecognizedException , SAXNotSupportedException { 478 if (fParseInProgress) { 479 throw new SAXNotSupportedException ("PAR004 Cannot setFeature(http://xml.org/sax/features/external-general-entities): parse is in progress.\n"+ 480 "http://xml.org/sax/features/external-general-entities"); 481 } 482 if (!expand) { 483 throw new SAXNotSupportedException ("http://xml.org/sax/features/external-general-entities"); 484 } 485 } 486 487 494 protected boolean getExternalGeneralEntities() 495 throws SAXNotRecognizedException , SAXNotSupportedException { 496 return true; 497 } 498 499 517 protected void setExternalParameterEntities(boolean expand) 518 throws SAXNotRecognizedException , SAXNotSupportedException { 519 if (fParseInProgress) { 520 throw new SAXNotSupportedException ("PAR004 Cannot setFeature(http://xml.org/sax/features/external-general-entities): parse is in progress.\n"+ 521 "http://xml.org/sax/features/external-general-entities"); 522 } 523 if (!expand) { 524 throw new SAXNotSupportedException ("http://xml.org/sax/features/external-parameter-entities"); 525 } 526 } 527 528 535 protected boolean getExternalParameterEntities() 536 throws SAXNotRecognizedException , SAXNotSupportedException { 537 return true; 538 } 539 540 553 protected void setNamespaces(boolean process) 554 throws SAXNotRecognizedException , SAXNotSupportedException { 555 if (fParseInProgress) { 556 throw new SAXNotSupportedException ("PAR004 Cannot setFeature(http://xml.org/sax/features/namespaces): parse is in progress.\n"+ 557 "http://xml.org/sax/features/namespaces"); 558 } 559 fScanner.setNamespacesEnabled(process); 560 fValidator.setNamespacesEnabled(process); 563 } 564 565 570 protected boolean getNamespaces() 571 throws SAXNotRecognizedException , SAXNotSupportedException { 572 return fValidator.getNamespacesEnabled(); 573 } 574 575 577 590 protected void setValidationSchema(boolean schema) 591 throws SAXNotRecognizedException , SAXNotSupportedException { 592 if (fParseInProgress) { 593 throw new SAXNotSupportedException ("http://apache.org/xml/features/validation/schema: parse is in progress"); 595 } 596 fValidator.setSchemaValidationEnabled(schema); 597 } 598 599 604 protected boolean getValidationSchema() 605 throws SAXNotRecognizedException , SAXNotSupportedException { 606 return fValidator.getSchemaValidationEnabled(); 607 } 608 609 624 protected void setValidationSchemaFullChecking(boolean schemaFullChecking) 625 throws SAXNotRecognizedException , SAXNotSupportedException { 626 if (fParseInProgress) { 627 throw new SAXNotSupportedException ("http://apache.org/xml/features/validation/schema-full-checking: parse is in progress"); 629 } 630 fValidator.setSchemaFullCheckingEnabled(schemaFullChecking); 631 } 632 633 634 642 protected void setNormalizeContents(boolean normalize) { 643 fValidator.setNormalizeContents(normalize); 644 } 645 protected boolean getNormalizeContents() { 646 return fValidator.getNormalizeConents(); 647 } 648 649 663 protected void setExternalSchemaLocation(Object value) 664 throws SAXNotRecognizedException , SAXNotSupportedException { 665 if (fParseInProgress) { 666 throw new SAXNotSupportedException ("http://apache.org/xml/properties/validation/schema/external-schemaLocation: parse is in progress"); 668 } 669 fValidator.setExternalSchemas(value); 670 } 671 672 684 protected void setExternalNoNamespaceSchemaLocation(Object value) 685 throws SAXNotRecognizedException , SAXNotSupportedException { 686 if (fParseInProgress) { 687 throw new SAXNotSupportedException ("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation: parse is in progress"); 689 } 690 fValidator.setExternalNoNamespaceSchema(value); 691 } 692 693 698 protected boolean getValidationSchemaFullChecking() 699 throws SAXNotRecognizedException , SAXNotSupportedException { 700 return fValidator.getSchemaFullCheckingEnabled(); 701 } 702 703 704 720 protected void setValidationDynamic(boolean dynamic) 721 throws SAXNotRecognizedException , SAXNotSupportedException { 722 if (fParseInProgress) { 723 throw new SAXNotSupportedException ("http://apache.org/xml/features/validation/dynamic: parse is in progress"); 725 } 726 try { 727 fValidator.setDynamicValidationEnabled(dynamic); 728 } 729 catch (Exception ex) { 730 throw new SAXNotSupportedException (ex.getMessage()); 731 } 732 } 733 734 740 protected boolean getValidationDynamic() 741 throws SAXNotRecognizedException , SAXNotSupportedException { 742 return fValidator.getDynamicValidationEnabled(); 743 } 744 745 748 protected void setNormalizeAttributeValues(boolean normalize) { 749 fValidator.setNormalizeAttributeValues(normalize); 750 } 751 752 767 protected void setLoadDTDGrammar(boolean loadDTDGrammar) 768 throws SAXNotRecognizedException , SAXNotSupportedException { 769 if (fParseInProgress) { 770 throw new SAXNotSupportedException ("http://apache.org/xml/features/nonvalidating/load-dtd-grammar: parse is in progress"); 772 } 773 try { 774 fValidator.setLoadDTDGrammar(loadDTDGrammar); 775 } 776 catch (Exception ex) { 777 throw new SAXNotSupportedException (ex.getMessage()); 778 } 779 } 780 781 786 protected boolean getLoadDTDGrammar() 787 throws SAXNotRecognizedException , SAXNotSupportedException { 788 return fValidator.getLoadDTDGrammar(); 789 } 790 791 806 protected void setLoadExternalDTD(boolean loadExternalDTD) 807 throws SAXNotRecognizedException , SAXNotSupportedException { 808 if (fParseInProgress) { 809 throw new SAXNotSupportedException ("http://apache.org/xml/features/nonvalidating/load-external-dtd: parse is in progress"); 811 } 812 try { 813 fScanner.setLoadExternalDTD(loadExternalDTD); 814 } 815 catch (Exception ex) { 816 throw new SAXNotSupportedException (ex.getMessage()); 817 } 818 } 819 820 825 protected boolean getLoadExternalDTD() 826 throws SAXNotRecognizedException , SAXNotSupportedException { 827 return fScanner.getLoadExternalDTD(); 828 } 829 830 844 protected void setValidationWarnOnDuplicateAttdef(boolean warn) 845 throws SAXNotRecognizedException , SAXNotSupportedException { 846 fValidator.setWarningOnDuplicateAttDef(warn); 847 } 848 849 855 protected boolean getValidationWarnOnDuplicateAttdef() 856 throws SAXNotRecognizedException , SAXNotSupportedException { 857 return fValidator.getWarningOnDuplicateAttDef(); 858 } 859 860 875 protected void setValidationWarnOnUndeclaredElemdef(boolean warn) 876 throws SAXNotRecognizedException , SAXNotSupportedException { 877 fValidator.setWarningOnUndeclaredElements(warn); 878 } 879 880 886 protected boolean getVa
|