1 16 package org.apache.xerces.xinclude; 17 18 import java.io.CharConversionException ; 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Enumeration ; 22 import java.util.Locale ; 23 import java.util.Stack ; 24 import java.util.StringTokenizer ; 25 26 import org.apache.xerces.impl.Constants; 27 import org.apache.xerces.impl.XMLEntityManager; 28 import org.apache.xerces.impl.XMLErrorReporter; 29 import org.apache.xerces.impl.io.MalformedByteSequenceException; 30 import org.apache.xerces.impl.msg.XMLMessageFormatter; 31 import org.apache.xerces.util.AugmentationsImpl; 32 import org.apache.xerces.util.HTTPInputSource; 33 import org.apache.xerces.util.IntStack; 34 import org.apache.xerces.util.ParserConfigurationSettings; 35 import org.apache.xerces.util.SecurityManager; 36 import org.apache.xerces.util.SymbolTable; 37 import org.apache.xerces.util.URI; 38 import org.apache.xerces.util.XMLAttributesImpl; 39 import org.apache.xerces.util.XMLResourceIdentifierImpl; 40 import org.apache.xerces.util.XMLChar; 41 import org.apache.xerces.util.XMLSymbols; 42 import org.apache.xerces.util.URI.MalformedURIException; 43 import org.apache.xerces.xni.Augmentations; 44 import org.apache.xerces.xni.NamespaceContext; 45 import org.apache.xerces.xni.QName; 46 import org.apache.xerces.xni.XMLAttributes; 47 import org.apache.xerces.xni.XMLDTDHandler; 48 import org.apache.xerces.xni.XMLDocumentHandler; 49 import org.apache.xerces.xni.XMLLocator; 50 import org.apache.xerces.xni.XMLResourceIdentifier; 51 import org.apache.xerces.xni.XMLString; 52 import org.apache.xerces.xni.XNIException; 53 import org.apache.xerces.xni.parser.XMLComponent; 54 import org.apache.xerces.xni.parser.XMLComponentManager; 55 import org.apache.xerces.xni.parser.XMLConfigurationException; 56 import org.apache.xerces.xni.parser.XMLDTDFilter; 57 import org.apache.xerces.xni.parser.XMLDTDSource; 58 import org.apache.xerces.xni.parser.XMLDocumentFilter; 59 import org.apache.xerces.xni.parser.XMLDocumentSource; 60 import org.apache.xerces.xni.parser.XMLEntityResolver; 61 import org.apache.xerces.xni.parser.XMLInputSource; 62 import org.apache.xerces.xni.parser.XMLParserConfiguration; 63 import org.apache.xerces.xpointer.XPointerHandler; 64 import org.apache.xerces.xpointer.XPointerProcessor; 65 66 114 public class XIncludeHandler 115 implements XMLComponent, XMLDocumentFilter, XMLDTDFilter { 116 117 public final static String XINCLUDE_DEFAULT_CONFIGURATION = 118 "org.apache.xerces.parsers.XIncludeParserConfiguration"; 119 public final static String HTTP_ACCEPT = "Accept"; 120 public final static String HTTP_ACCEPT_LANGUAGE = "Accept-Language"; 121 public final static String XPOINTER = "xpointer"; 122 123 public final static String XINCLUDE_NS_URI = 124 "http://www.w3.org/2001/XInclude".intern(); 125 public final static String XINCLUDE_INCLUDE = "include".intern(); 126 public final static String XINCLUDE_FALLBACK = "fallback".intern(); 127 128 public final static String XINCLUDE_PARSE_XML = "xml".intern(); 129 public final static String XINCLUDE_PARSE_TEXT = "text".intern(); 130 131 public final static String XINCLUDE_ATTR_HREF = "href".intern(); 132 public final static String XINCLUDE_ATTR_PARSE = "parse".intern(); 133 public final static String XINCLUDE_ATTR_ENCODING = "encoding".intern(); 134 public final static String XINCLUDE_ATTR_ACCEPT = "accept".intern(); 135 public final static String XINCLUDE_ATTR_ACCEPT_LANGUAGE = "accept-language".intern(); 136 137 public final static String XINCLUDE_INCLUDED = "[included]".intern(); 139 140 141 public final static String CURRENT_BASE_URI = "currentBaseURI"; 142 143 public final static String XINCLUDE_BASE = "base".intern(); 145 public final static QName XML_BASE_QNAME = 146 new QName( 147 XMLSymbols.PREFIX_XML, 148 XINCLUDE_BASE, 149 (XMLSymbols.PREFIX_XML + ":" + XINCLUDE_BASE).intern(), 150 NamespaceContext.XML_URI); 151 152 public final static String XINCLUDE_LANG = "lang".intern(); 154 public final static QName XML_LANG_QNAME = 155 new QName( 156 XMLSymbols.PREFIX_XML, 157 XINCLUDE_LANG, 158 (XMLSymbols.PREFIX_XML + ":" + XINCLUDE_LANG).intern(), 159 NamespaceContext.XML_URI); 160 161 public final static QName NEW_NS_ATTR_QNAME = 162 new QName( 163 XMLSymbols.PREFIX_XMLNS, 164 "", 165 XMLSymbols.PREFIX_XMLNS + ":", 166 NamespaceContext.XMLNS_URI); 167 168 private final static int STATE_NORMAL_PROCESSING = 1; 170 private final static int STATE_IGNORE = 2; 173 private final static int STATE_EXPECT_FALLBACK = 3; 176 177 179 180 protected static final String VALIDATION = 181 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 182 183 184 protected static final String SCHEMA_VALIDATION = 185 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 186 187 188 protected static final String DYNAMIC_VALIDATION = 189 Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE; 190 191 192 protected static final String ALLOW_UE_AND_NOTATION_EVENTS = 193 Constants.SAX_FEATURE_PREFIX 194 + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE; 195 196 197 protected static final String XINCLUDE_FIXUP_BASE_URIS = 198 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FIXUP_BASE_URIS_FEATURE; 199 200 201 protected static final String XINCLUDE_FIXUP_LANGUAGE = 202 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FIXUP_LANGUAGE_FEATURE; 203 204 205 protected static final String SYMBOL_TABLE = 206 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 207 208 209 protected static final String ERROR_REPORTER = 210 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 211 212 213 protected static final String ENTITY_RESOLVER = 214 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 215 216 217 protected static final String SECURITY_MANAGER = 218 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 219 220 221 public static final String BUFFER_SIZE = 222 Constants.XERCES_PROPERTY_PREFIX + Constants.BUFFER_SIZE_PROPERTY; 223 224 protected static final String PARSER_SETTINGS = 225 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 226 227 228 private static final String [] RECOGNIZED_FEATURES = 229 { ALLOW_UE_AND_NOTATION_EVENTS, XINCLUDE_FIXUP_BASE_URIS, XINCLUDE_FIXUP_LANGUAGE }; 230 231 232 private static final Boolean [] FEATURE_DEFAULTS = { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE }; 233 234 235 private static final String [] RECOGNIZED_PROPERTIES = 236 { ERROR_REPORTER, ENTITY_RESOLVER, SECURITY_MANAGER, BUFFER_SIZE }; 237 238 239 private static final Object [] PROPERTY_DEFAULTS = { null, null, null, new Integer (XMLEntityManager.DEFAULT_BUFFER_SIZE) }; 240 241 243 protected XMLDocumentHandler fDocumentHandler; 245 protected XMLDocumentSource fDocumentSource; 246 247 protected XMLDTDHandler fDTDHandler; 249 protected XMLDTDSource fDTDSource; 250 251 protected XIncludeHandler fParentXIncludeHandler; 253 254 protected int fBufferSize = XMLEntityManager.DEFAULT_BUFFER_SIZE; 256 257 protected String fParentRelativeURI; 261 262 protected XMLParserConfiguration fChildConfig; 265 266 protected XMLParserConfiguration fXIncludeChildConfig; 269 protected XMLParserConfiguration fXPointerChildConfig; 270 271 protected XPointerProcessor fXPtrProcessor = null; 273 274 protected XMLLocator fDocLocation; 275 protected XIncludeMessageFormatter fXIncludeMessageFormatter = new XIncludeMessageFormatter(); 276 protected XIncludeNamespaceSupport fNamespaceContext; 277 protected SymbolTable fSymbolTable; 278 protected XMLErrorReporter fErrorReporter; 279 protected XMLEntityResolver fEntityResolver; 280 protected SecurityManager fSecurityManager; 281 282 protected XIncludeTextReader fXInclude10TextReader; 284 protected XIncludeTextReader fXInclude11TextReader; 285 286 protected XMLResourceIdentifier fCurrentBaseURI; 288 protected IntStack fBaseURIScope; 289 protected Stack fBaseURI; 290 protected Stack fLiteralSystemID; 291 protected Stack fExpandedSystemID; 292 293 protected IntStack fLanguageScope; 295 protected Stack fLanguageStack; 296 protected String fCurrentLanguage; 297 298 protected ParserConfigurationSettings fSettings; 300 301 private int fDepth; 304 305 private int fResultDepth; 307 308 private static final int INITIAL_SIZE = 8; 310 311 private boolean[] fSawInclude = new boolean[INITIAL_SIZE]; 316 317 private boolean[] fSawFallback = new boolean[INITIAL_SIZE]; 321 322 private int[] fState = new int[INITIAL_SIZE]; 324 325 private ArrayList fNotations; 327 private ArrayList fUnparsedEntities; 328 329 private boolean fFixupBaseURIs = true; 331 private boolean fFixupLanguage = true; 332 333 private boolean fSendUEAndNotationEvents; 336 337 private boolean fIsXML11; 339 340 private boolean fInDTD; 342 343 private boolean fSeenRootElement; 345 346 private boolean fNeedCopyFeatures = true; 348 349 351 public XIncludeHandler() { 352 fDepth = 0; 353 354 fSawFallback[fDepth] = false; 355 fSawInclude[fDepth] = false; 356 fState[fDepth] = STATE_NORMAL_PROCESSING; 357 fNotations = new ArrayList (); 358 fUnparsedEntities = new ArrayList (); 359 360 fBaseURIScope = new IntStack(); 361 fBaseURI = new Stack (); 362 fLiteralSystemID = new Stack (); 363 fExpandedSystemID = new Stack (); 364 fCurrentBaseURI = new XMLResourceIdentifierImpl(); 365 366 fLanguageScope = new IntStack(); 367 fLanguageStack = new Stack (); 368 fCurrentLanguage = null; 369 } 370 371 373 public void reset(XMLComponentManager componentManager) 374 throws XNIException { 375 fNamespaceContext = null; 376 fDepth = 0; 377 fResultDepth = isRootDocument() ? 0 : fParentXIncludeHandler.getResultDepth(); 378 fNotations.clear(); 379 fUnparsedEntities.clear(); 380 fParentRelativeURI = null; 381 fIsXML11 = false; 382 fInDTD = false; 383 fSeenRootElement = false; 384 385 fBaseURIScope.clear(); 386 fBaseURI.clear(); 387 fLiteralSystemID.clear(); 388 fExpandedSystemID.clear(); 389 fLanguageScope.clear(); 390 fLanguageStack.clear(); 391 392 for (int i = 0; i < fState.length; ++i) { 399 fState[i] = STATE_NORMAL_PROCESSING; 400 } 401 for (int i = 0; i < fSawFallback.length; ++i) { 402 fSawFallback[i] = false; 403 } 404 for (int i = 0; i < fSawInclude.length; ++i) { 405 fSawInclude[i] = false; 406 } 407 408 try { 409 if (!componentManager.getFeature(PARSER_SETTINGS)) { 410 return; 412 } 413 } 414 catch (XMLConfigurationException e) {} 415 416 fNeedCopyFeatures = true; 418 419 try { 420 fSendUEAndNotationEvents = 421 componentManager.getFeature(ALLOW_UE_AND_NOTATION_EVENTS); 422 if (fChildConfig != null) { 423 fChildConfig.setFeature( 424 ALLOW_UE_AND_NOTATION_EVENTS, 425 fSendUEAndNotationEvents); 426 } 427 } 428 catch (XMLConfigurationException e) { 429 } 430 431 try { 432 fFixupBaseURIs = 433 componentManager.getFeature(XINCLUDE_FIXUP_BASE_URIS); 434 if (fChildConfig != null) { 435 fChildConfig.setFeature( 436 XINCLUDE_FIXUP_BASE_URIS, 437 fFixupBaseURIs); 438 } 439 } 440 catch (XMLConfigurationException e) { 441 fFixupBaseURIs = true; 442 } 443 444 try { 445 fFixupLanguage = 446 componentManager.getFeature(XINCLUDE_FIXUP_LANGUAGE); 447 if (fChildConfig != null) { 448 fChildConfig.setFeature( 449 XINCLUDE_FIXUP_LANGUAGE, 450 fFixupLanguage); 451 } 452 } 453 catch (XMLConfigurationException e) { 454 fFixupLanguage = true; 455 } 456 457 try { 459 SymbolTable value = 460 (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); 461 if (value != null) { 462 fSymbolTable = value; 463 if (fChildConfig != null) { 464 fChildConfig.setProperty(SYMBOL_TABLE, value); 465 } 466 } 467 } 468 catch (XMLConfigurationException e) { 469 fSymbolTable = null; 470 } 471 472 try { 474 XMLErrorReporter value = 475 (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 476 if (value != null) { 477 setErrorReporter(value); 478 if (fChildConfig != null) { 479 fChildConfig.setProperty(ERROR_REPORTER, value); 480 } 481 } 482 } 483 catch (XMLConfigurationException e) { 484 fErrorReporter = null; 485 } 486 487 try { 489 XMLEntityResolver value = 490 (XMLEntityResolver)componentManager.getProperty( 491 ENTITY_RESOLVER); 492 493 if (value != null) { 494 fEntityResolver = value; 495 if (fChildConfig != null) { 496 fChildConfig.setProperty(ENTITY_RESOLVER, value); 497 } 498 } 499 } 500 catch (XMLConfigurationException e) { 501 fEntityResolver = null; 502 } 503 504 try { 506 SecurityManager value = 507 (SecurityManager )componentManager.getProperty( 508 SECURITY_MANAGER); 509 510 if (value != null) { 511 fSecurityManager = value; 512 if (fChildConfig != null) { 513 fChildConfig.setProperty(SECURITY_MANAGER, value); 514 } 515 } 516 } 517 catch (XMLConfigurationException e) { 518 fSecurityManager = null; 519 } 520 521 try { 523 Integer value = 524 (Integer )componentManager.getProperty( 525 BUFFER_SIZE); 526 527 if (value != null && value.intValue() > 0) { 528 fBufferSize = value.intValue(); 529 if (fChildConfig != null) { 530 fChildConfig.setProperty(BUFFER_SIZE, value); 531 } 532 } 533 else { 534 fBufferSize = ((Integer )getPropertyDefault(BUFFER_SIZE)).intValue(); 535 } 536 } 537 catch (XMLConfigurationException e) { 538 fBufferSize = ((Integer )getPropertyDefault(BUFFER_SIZE)).intValue(); 539 } 540 541 if (fXInclude10TextReader != null) { 543 fXInclude10TextReader.setBufferSize(fBufferSize); 544 } 545 if (fXInclude11TextReader != null) { 547 fXInclude11TextReader.setBufferSize(fBufferSize); 548 } 549 550 fSettings = new ParserConfigurationSettings(); 551 copyFeatures(componentManager, fSettings); 552 553 try { 560 if (componentManager.getFeature(SCHEMA_VALIDATION)) { 561 fSettings.setFeature(SCHEMA_VALIDATION, false); 562 if (componentManager.getFeature(VALIDATION)) { 563 fSettings.setFeature(DYNAMIC_VALIDATION, true); 564 } 565 } 566 } 567 catch (XMLConfigurationException e) {} 568 569 } 573 578 public String [] getRecognizedFeatures() { 579 return (String [])(RECOGNIZED_FEATURES.clone()); 580 } 582 597 public void setFeature(String featureId, boolean state) 598 throws XMLConfigurationException { 599 if (featureId.equals(ALLOW_UE_AND_NOTATION_EVENTS)) { 600 fSendUEAndNotationEvents = state; 601 } 602 if (fSettings != null) { 603 fNeedCopyFeatures = true; 604 fSettings.setFeature(featureId, state); 605 } 606 } 608 613 public String [] getRecognizedProperties() { 614 return (String [])(RECOGNIZED_PROPERTIES.clone()); 615 } 617 632 public void setProperty(String propertyId, Object value) 633 throws XMLConfigurationException { 634 if (propertyId.equals(SYMBOL_TABLE)) { 635 fSymbolTable = (SymbolTable)value; 636 if (fChildConfig != null) { 637 fChildConfig.setProperty(propertyId, value); 638 } 639 return; 640 } 641 if (propertyId.equals(ERROR_REPORTER)) { 642 setErrorReporter((XMLErrorReporter)value); 643 if (fChildConfig != null) { 644 fChildConfig.setProperty(propertyId, value); 645 } 646 return; 647 } 648 if (propertyId.equals(ENTITY_RESOLVER)) { 649 fEntityResolver = (XMLEntityResolver)value; 650 if (fChildConfig != null) { 651 fChildConfig.setProperty(propertyId, value); 652 } 653 return; 654 } 655 if (propertyId.equals(SECURITY_MANAGER)) { 656 fSecurityManager = (SecurityManager )value; 657 if (fChildConfig != null) { 658 fChildConfig.setProperty(propertyId, value); 659 } 660 return; 661 } 662 if (propertyId.equals(BUFFER_SIZE)) { 663 Integer bufferSize = (Integer ) value; 664 if (fChildConfig != null) { 665 fChildConfig.setProperty(propertyId, value); 666 } 667 if (bufferSize != null && bufferSize.intValue() > 0) { 668 fBufferSize = bufferSize.intValue(); 669 if (fXInclude10TextReader != null) { 671 fXInclude10TextReader.setBufferSize(fBufferSize); 672 } 673 if (fXInclude11TextReader != null) { 675 fXInclude11TextReader.setBufferSize(fBufferSize); 676 } 677 } 678 return; 679 } 680 681 } 683 692 public Boolean getFeatureDefault(String featureId) { 693 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 694 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 695 return FEATURE_DEFAULTS[i]; 696 } 697 } 698 return null; 699 } 701 710 public Object getPropertyDefault(String propertyId) { 711 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 712 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 713 return PROPERTY_DEFAULTS[i]; 714 } 715 } 716 return null; 717 } 719 public void setDocumentHandler(XMLDocumentHandler handler) { 720 fDocumentHandler = handler; 721 } 722 723 public XMLDocumentHandler getDocumentHandler() { 724 return fDocumentHandler; 725 } 726 727 729 737 public void startDocument( 738 XMLLocator locator, 739 String encoding, 740 NamespaceContext namespaceContext, 741 Augmentations augs) 742 throws XNIException { 743 744 fErrorReporter.setDocumentLocator(locator); 747 748 if (!isRootDocument() 749 && fParentXIncludeHandler.searchForRecursiveIncludes(locator)) { 750 reportFatalError( 751 "RecursiveInclude", 752 new Object [] { locator.getExpandedSystemId()}); 753 } 754 755 if (!(namespaceContext instanceof XIncludeNamespaceSupport)) { 756 reportFatalError("IncompatibleNamespaceContext"); 757 } 758 fNamespaceContext = (XIncludeNamespaceSupport)namespaceContext; 759 fDocLocation = locator; 760 761 fCurrentBaseURI.setBaseSystemId(locator.getBaseSystemId()); 763 fCurrentBaseURI.setExpandedSystemId(locator.getExpandedSystemId()); 764 fCurrentBaseURI.setLiteralSystemId(locator.getLiteralSystemId()); 765 saveBaseURI(); 766 if (augs == null) { 767 augs = new AugmentationsImpl(); 768 } 769 augs.putItem(CURRENT_BASE_URI, fCurrentBaseURI); 770 771 fCurrentLanguage = XMLSymbols.EMPTY_STRING; 773 saveLanguage(fCurrentLanguage); 774 775 if (isRootDocument() && fDocumentHandler != null) { 776 fDocumentHandler.startDocument( 777 locator, 778 encoding, 779 namespaceContext, 780 augs); 781 } 782 } 783 784 public void xmlDecl( 785 String version, 786 String encoding, 787 String standalone, 788 Augmentations augs) 789 throws XNIException { 790 fIsXML11 = "1.1".equals(version); 791 if (isRootDocument() && fDocumentHandler != null) { 792 fDocumentHandler.xmlDecl(version, encoding, standalone, augs); 793 } 794 } 795 796 public void doctypeDecl( 797 String rootElement, 798 String publicId, 799 String systemId, 800 Augmentations augs) 801 throws XNIException { 802 if (isRootDocument() && fDocumentHandler != null) { 803 fDocumentHandler.doctypeDecl(rootElement, publicId, systemId, augs); 804 } 805 } 806 807 public void comment(XMLString text, Augmentations augs) 808 throws XNIException { 809 if (!fInDTD) { 810 if (fDocumentHandler != null 811 && getState() == STATE_NORMAL_PROCESSING) { 812 fDepth++; 813 augs = modifyAugmentations(augs); 814 fDocumentHandler.comment(text, augs); 815 fDepth--; 816 } 817 } 818 else if (fDTDHandler != null) { 819 fDTDHandler.comment(text, augs); 820 } 821 } 822 823 public void processingInstruction( 824 String target, 825 XMLString data, 826 Augmentations augs) 827 throws XNIException { 828 if (!fInDTD) { 829 if (fDocumentHandler != null 830 && getState() == STATE_NORMAL_PROCESSING) { 831 fDepth++; 833 augs = modifyAugmentations(augs); 834 fDocumentHandler.processingInstruction(target, data, augs); 835 fDepth--; 836 } 837 } 838 else if (fDTDHandler != null) { 839 fDTDHandler.processingInstruction(target, data, augs); 840 } 841 } 842 843 public void startElement( 844 QName element, 845 XMLAttributes attributes, 846 Augmentations augs) 847 throws XNIException { 848 fDepth++; 849 int lastState = getState(fDepth - 1); 850 if (lastState == STATE_EXPECT_FALLBACK && getState(fDepth - 2) == STATE_EXPECT_FALLBACK) { 854 setState(STATE_IGNORE); 855 } 856 else { 857 setState(lastState); 858 } 859 860 processXMLBaseAttributes(attributes); 863 if (fFixupLanguage) { 864 processXMLLangAttributes(attributes); 865 } 866 867 if (isIncludeElement(element)) { 868 boolean success = this.handleIncludeElement(attributes); 869 if (success) { 870 setState(STATE_IGNORE); 871 } 872 else { 873 setState(STATE_EXPECT_FALLBACK); 874 } 875 } 876 else if (isFallbackElement(element)) { 877 this.handleFallbackElement(); 878 } 879 else if (hasXIncludeNamespace(element)) { 880 if (getSawInclude(fDepth - 1)) { 881 reportFatalError( 882 "IncludeChild", 883 new Object [] { element.rawname }); 884 } 885 if (getSawFallback(fDepth - 1)) { 886 reportFatalError( 887 "FallbackChild", 888 new Object [] { element.rawname }); 889 } 890 if (getState() == STATE_NORMAL_PROCESSING) { 891 if (fResultDepth++ == 0) { 892 checkMultipleRootElements(); 893 } 894 if (fDocumentHandler != null) { 895 augs = modifyAugmentations(augs); 896 attributes = processAttributes(attributes); 897 fDocumentHandler.startElement(element, attributes, augs); 898 } 899 } 900 } 901 else if (getState() == STATE_NORMAL_PROCESSING) { 902 if (fResultDepth++ == 0) { 903 checkMultipleRootElements(); 904 } 905 if (fDocumentHandler != null) { 906 augs = modifyAugmentations(augs); 907 attributes = processAttributes(attributes); 908 fDocumentHandler.startElement(element, attributes, augs); 909 } 910 } 911 } 912 913 public void emptyElement( 914 QName element, 915 XMLAttributes attributes, 916 Augmentations augs) 917 throws XNIException { 918 fDepth++; 919 int lastState = getState(fDepth - 1); 920 if (lastState == STATE_EXPECT_FALLBACK && getState(fDepth - 2) == STATE_EXPECT_FALLBACK) { 924 setState(STATE_IGNORE); 925 } 926 else { 927 setState(lastState); 928 } 929 930 processXMLBaseAttributes(attributes); 933 if (fFixupLanguage) { 934 processXMLLangAttributes(attributes); 935 } 936 937 if (isIncludeElement(element)) { 938 boolean success = this.handleIncludeElement(attributes); 939 if (success) { 940 setState(STATE_IGNORE); 941 } 942 else { 943 reportFatalError("NoFallback"); 944 } 945 } 946 else if (isFallbackElement(element)) { 947 this.handleFallbackElement(); 948 } 949 else if (hasXIncludeNamespace(element)) { 950 if (getSawInclude(fDepth - 1)) { 951 reportFatalError( 952 "IncludeChild", 953 new Object [] { element.rawname }); 954 } 955 if (getSawFallback(fDepth - 1)) { 956 reportFatalError( 957 "FallbackChild", 958 new Object [] { element.rawname }); 959 } 960 if (getState() == STATE_NORMAL_PROCESSING) { 961 if (fResultDepth == 0) { 962 checkMultipleRootElements(); 963 } 964 if (fDocumentHandler != null) { 965 augs = modifyAugmentations(augs); 966 attributes = processAttributes(attributes); 967 fDocumentHandler.emptyElement(element, attributes, augs); 968 } 969 } 970 } 971 else if (getState() == STATE_NORMAL_PROCESSING) { 972 if (fResultDepth == 0) { 973 checkMultipleRootElements(); 974 } 975 if (fDocumentHandler != null) { 976 augs = modifyAugmentations(augs); 977 attributes = processAttributes(attributes); 978 fDocumentHandler.emptyElement(element, attributes, augs); 979 } 980 } 981 setSawFallback(fDepth + 1, false); 983 setSawInclude(fDepth, false); 984 985 if (fBaseURIScope.size() > 0 && fDepth == fBaseURIScope.peek()) { 987 restoreBaseURI(); 989 } 990 fDepth--; 991 } 992 993 public void endElement(QName element, Augmentations augs) 994 throws XNIException { 995 996 if (isIncludeElement(element)) { 997 if (getState() == STATE_EXPECT_FALLBACK 1000 && !getSawFallback(fDepth + 1)) { 1001 reportFatalError("NoFallback"); 1002 } 1003 } 1004 if (isFallbackElement(element)) { 1005 if (getState() == STATE_NORMAL_PROCESSING) { 1008 setState(STATE_IGNORE); 1009 } 1010 } 1011 else if (getState() == STATE_NORMAL_PROCESSING) { 1012 --fResultDepth; 1013 if (fDocumentHandler != null) { 1014 fDocumentHandler.endElement(element, augs); 1015 } 1016 } 1017 1018 setSawFallback(fDepth + 1, false); 1020 setSawInclude(fDepth, false); 1021 1022 if (fBaseURIScope.size() > 0 && fDepth == fBaseURIScope.peek()) { 1024 restoreBaseURI(); 1026 } 1027 1028 if (fLanguageScope.size() > 0 && fDepth == fLanguageScope.peek()) { 1030 fCurrentLanguage = restoreLanguage(); 1032 } 1033 1034 fDepth--; 1035 } 1036 1037 public void startGeneralEntity( 1038 String name, 1039 XMLResourceIdentifier resId, 1040 String encoding, 1041 Augmentations augs) 1042 throws XNIException { 1043 if (getState() == STATE_NORMAL_PROCESSING) { 1044 if (fResultDepth == 0) { 1045 if (augs != null && Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 1046 reportFatalError("UnexpandedEntityReferenceIllegal"); 1047 } 1048 } 1049 else if (fDocumentHandler != null) { 1050 fDocumentHandler.startGeneralEntity(name, resId, encoding, augs); 1051 } 1052 } 1053 } 1054 1055 public void textDecl(String version, String encoding, Augmentations augs) 1056 throws XNIException { 1057 if (fDocumentHandler != null 1058 && getState() == STATE_NORMAL_PROCESSING) { 1059 fDocumentHandler.textDecl(version, encoding, augs); 1060 } 1061 } 1062 1063 public void endGeneralEntity(String name, Augmentations augs) 1064 throws XNIException { 1065 if (fDocumentHandler != null 1066 && getState() == STATE_NORMAL_PROCESSING 1067 && fResultDepth != 0) { 1068 fDocumentHandler.endGeneralEntity(name, augs); 1069 } 1070 } 1071 1072 public void characters(XMLString text, Augmentations augs) 1073 throws XNIException { 1074 if (getState() == STATE_NORMAL_PROCESSING) { 1075 if (fResultDepth == 0) { 1076 checkWhitespace(text); 1077 } 1078 else if (fDocumentHandler != null) { 1079 fDepth++; 1081 augs = modifyAugmentations(augs); 1082 fDocumentHandler.characters(text, augs); 1083 fDepth--; 1084 } 1085 } 1086 } 1087 1088 public void ignorableWhitespace(XMLString text, Augmentations augs) 1089 throws XNIException { 1090 if (fDocumentHandler != null 1091 && getState() == STATE_NORMAL_PROCESSING 1092 && fResultDepth != 0) { 1093 fDocumentHandler.ignorableWhitespace(text, augs); 1094 } 1095 } 1096 1097 public void startCDATA(Augmentations augs) throws XNIException { 1098 if (fDocumentHandler != null 1099 && getState() == STATE_NORMAL_PROCESSING 1100 && fResultDepth != 0) { 1101 fDocumentHandler.startCDATA(augs); 1102 } 1103 } 1104 1105 public void endCDATA(Augmentations augs) throws XNIException { 1106 if (fDocumentHandler != null 1107 && getState() == STATE_NORMAL_PROCESSING 1108 && fResultDepth != 0) { 1109 fDocumentHandler.endCDATA(augs); 1110 } 1111 } 1112 1113 public void endDocument(Augmentations augs) throws XNIException { 1114 if (isRootDocument()) { 1115 if (!fSeenRootElement) { 1116 reportFatalError("RootElementRequired"); 1117 } 1118 if (fDocumentHandler != null) { 1119 fDocumentHandler.endDocument(augs); 1120 } 1121 } 1122 } 1123 1124 public void setDocumentSource(XMLDocumentSource source) { 1125 fDocumentSource = source; 1126 } 1127 1128 public XMLDocumentSource getDocumentSource() { 1129 return fDocumentSource; 1130 } 1131 1132 1136 1139 public void attributeDecl( 1140 String elementName, 1141 String attributeName, 1142 String type, 1143 String [] enumeration, 1144 String defaultType, 1145 XMLString defaultValue, 1146 XMLString nonNormalizedDefaultValue, 1147 Augmentations augmentations) 1148 throws XNIException { 1149 if (fDTDHandler != null) { 1150 fDTDHandler.attributeDecl( 1151 elementName, 1152 attributeName, 1153 type, 1154 enumeration, 1155 defaultType, 1156 defaultValue, 1157 nonNormalizedDefaultValue, 1158 augmentations); 1159 } 1160 } 1161 1162 1165 public void elementDecl( 1166 String name, 1167 String contentModel, 1168 Augmentations augmentations) 1169 throws XNIException { 1170 if (fDTDHandler != null) { 1171 fDTDHandler.elementDecl(name, contentModel, augmentations); 1172 } 1173 } 1174 1175 1178 public void endAttlist(Augmentations augmentations) throws XNIException { 1179 if (fDTDHandler != null) { 1180 fDTDHandler.endAttlist(augmentations); 1181 } 1182 } 1183 1184 1187 public void endConditional(Augmentations augmentations) 1188 throws XNIException { 1189 if (fDTDHandler != null) { 1190 fDTDHandler.endConditional(augmentations); 1191 } 1192 } 1193 1194 1197 public void endDTD(Augmentations augmentations) throws XNIException { 1198 if (fDTDHandler != null) { 1199 fDTDHandler.endDTD(augmentations); 1200 } 1201 fInDTD = false; 1202 } 1203 1204 1207 public void endExternalSubset(Augmentations augmentations) 1208 throws XNIException { 1209 if (fDTDHandler != null) { 1210 fDTDHandler.endExternalSubset(augmentations); 1211 } 1212 } 1213 1214 1217 public void endParameterEntity(String name, Augmentations augmentations) 1218 throws XNIException { 1219 if (fDTDHandler != null) { 1220 fDTDHandler.endParameterEntity(name, augmentations); 1221 } 1222 } 1223 1224 1227 public void externalEntityDecl( 1228 String name, 1229 XMLResourceIdentifier identifier, 1230 Augmentations augmentations) 1231 throws XNIException { 1232 if (fDTDHandler != null) { 1233 fDTDHandler.externalEntityDecl(name, identifier, augmentations); 1234 } 1235 } 1236 1237 1240 public XMLDTDSource getDTDSource() { 1241 return fDTDSource; 1242 } 1243 1244 1247 public void ignoredCharacters(XMLString text, Augmentations augmentations) 1248 throws XNIException { 1249 if (fDTDHandler != null) { 1250 fDTDHandler.ignoredCharacters(text, augmentations); 1251 } 1252 } 1253 1254 1257 public void internalEntityDecl( 1258 String name, 1259 XMLString text, 1260 XMLString nonNormalizedText, 1261 Augmentations augmentations) 1262 throws XNIException { 1263 if (fDTDHandler != null) { 1264 fDTDHandler.internalEntityDecl( 1265 name, 1266 text, 1267 nonNormalizedText, 1268 augmentations); 1269 } 1270 } 1271 1272 1275 public void notationDecl( 1276 String name, 1277 XMLResourceIdentifier identifier, 1278 Augmentations augmentations) 1279 throws XNIException { 1280 this.addNotation(name, identifier, augmentations); 1281 if (fDTDHandler != null) { 1282 fDTDHandler.notationDecl(name, identifier, augmentations); 1283 } 1284 } 1285 1286 1289 public void setDTDSource(XMLDTDSource source) { 1290 fDTDSource = source; 1291 } 1292 1293 1296 public void startAttlist(String elementName, Augmentations augmentations) 1297 throws XNIException { 1298 if (fDTDHandler != null) { 1299 fDTDHandler.startAttlist(elementName, augmentations); 1300 } 1301 } 1302 1303 1306 public void startConditional(short type, Augmentations augmentations) 1307 throws XNIException { 1308 if (fDTDHandler != null) { 1309 fDTDHandler.startConditional(type, augmentations); 1310 } 1311 } 1312 1313 1316 public void startDTD(XMLLocator locator, Augmentations augmentations) 1317 throws XNIException { 1318 fInDTD = true; 1319 if (fDTDHandler != null) { 1320 fDTDHandler.startDTD(locator, augmentations); 1321 } 1322 } 1323 1324 1327 public void startExternalSubset( 1328 XMLResourceIdentifier identifier, 1329 Augmentations augmentations) 1330 throws XNIException { 1331 if (fDTDHandler != null) { 1332 fDTDHandler.startExternalSubset(identifier, augmentations); 1333 } 1334 } 1335 1336 1339 public void startParameterEntity( 1340 String name, 1341 XMLResourceIdentifier identifier, 1342 String encoding, 1343 Augmentations augmentations) 1344 throws XNIException { 1345 if (fDTDHandler != null) { 1346 fDTDHandler.startParameterEntity( 1347 name, 1348 identifier, 1349 encoding, 1350 augmentations); 1351 } 1352 } 1353 1354 1357 public void unparsedEntityDecl( 1358 String name, 1359 XMLResourceIdentifier identifier, 1360 String notation, 1361 Augmentations augmentations) 1362 throws XNIException { 1363 this.addUnparsedEntity(name, identifier, notation, augmentations); 1364 if (fDTDHandler != null) { 1365 fDTDHandler.unparsedEntityDecl( 1366 name, 1367 identifier, 1368 notation, 1369 augmentations); 1370 } 1371 } 1372 1373 1376 public XMLDTDHandler getDTDHandler() { 1377 return fDTDHandler; 1378 } 1379 1380 1383 public void setDTDHandler(XMLDTDHandler handler) { 1384 fDTDHandler = handler; 1385 } 1386 1387 1389 private void setErrorReporter(XMLErrorReporter reporter) { 1390 fErrorReporter = reporter; 1391 if (fErrorReporter != null) { 1392 fErrorReporter.putMessageFormatter( 1393 XIncludeMessageFormatter.XINCLUDE_DOMAIN, fXIncludeMessageFormatter); 1394 if (fDocLocation != null) { 1396 fErrorReporter.setDocumentLocator(fDocLocation); 1397 } 1398 } 1399 } 1400 1401 protected void handleFallbackElement() { 1402 if (!getSawInclude(fDepth - 1)) { 1403 if (getState() == STATE_IGNORE) { 1404 return; 1405 } 1406 reportFatalError("FallbackParent"); 1407 } 1408 1409 setSawInclude(fDepth, false); 1410 fNamespaceContext.setContextInvalid(); 1411 1412 if (getSawFallback(fDepth)) { 1413 reportFatalError("MultipleFallbacks"); 1414 } 1415 else { 1416 setSawFallback(fDepth, true); 1417 } 1418 1419 if (getState() == STATE_EXPECT_FALLBACK) { 1423 setState(STATE_NORMAL_PROCESSING); 1424 } 1425 } 1426 1427 protected boolean handleIncludeElement(XMLAttributes attributes) 1428 throws XNIException { 1429 if (getSawInclude(fDepth - 1)) { 1430 reportFatalError("IncludeChild", new Object [] { XINCLUDE_INCLUDE }); 1431 } 1432 if (getState() == STATE_IGNORE) { 1433 return true; 1434 } 1435 setSawInclude(fDepth, true); 1436 fNamespaceContext.setContextInvalid(); 1437 1438 1444 String href = attributes.getValue(XINCLUDE_ATTR_HREF); 1445 String parse = attributes.getValue(XINCLUDE_ATTR_PARSE); 1446 String xpointer = attributes.getValue(XPOINTER); 1447 String accept = attributes.getValue(XINCLUDE_ATTR_ACCEPT); 1448 String acceptLanguage = attributes.getValue(XINCLUDE_ATTR_ACCEPT_LANGUAGE); 1449 1450 if (parse == null) { 1451 parse = XINCLUDE_PARSE_XML; 1452 } 1453 if (href == null) { 1454 href = XMLSymbols.EMPTY_STRING; 1455 } 1456 if (href.length() == 0 && XINCLUDE_PARSE_XML.equals(parse)) { 1457 if (xpointer == null) { 1458 reportFatalError("XpointerMissing"); 1459 } 1460 else { 1461 Locale locale = (fErrorReporter != null) ? fErrorReporter.getLocale() : null; 1464 String reason = fXIncludeMessageFormatter.formatMessage(locale, "XPointerStreamability", null); 1465 reportResourceError("XMLResourceError", new Object [] { href, reason }); 1466 return false; 1467 } 1468 } 1469 1470 URI hrefURI = null; 1471 1472 try { 1476 hrefURI = new URI(href, true); 1477 if (hrefURI.getFragment() != null) { 1478 reportFatalError("HrefFragmentIdentifierIllegal", new Object [] {href}); 1479 } 1480 } 1481 catch (URI.MalformedURIException exc) { 1482 String newHref = escapeHref(href); 1483 if (href != newHref) { 1484 href = newHref; 1485 try { 1486 hrefURI = new URI(href, true); 1487 if (hrefURI.getFragment() != null) { 1488 reportFatalError("HrefFragmentIdentifierIllegal", new Object [] {href}); 1489 } 1490 } 1491 catch (URI.MalformedURIException exc2) { 1492 reportFatalError("HrefSyntacticallyInvalid", new Object [] {href}); 1493 } 1494 } 1495 else { 1496 reportFatalError("HrefSyntacticallyInvalid", new Object [] {href}); 1497 } 1498 } 1499 1500 if (accept != null && !isValidInHTTPHeader(accept)) { 1503 reportFatalError("AcceptMalformed", null); 1504 accept = null; 1505 } 1506 if (acceptLanguage != null && !isValidInHTTPHeader(acceptLanguage)) { 1507 reportFatalError("AcceptLanguageMalformed", null); 1508 acceptLanguage = null; 1509 } 1510 1511 XMLInputSource includedSource = null; 1512 if (fEntityResolver != null) { 1513 try { 1514 XMLResourceIdentifier resourceIdentifier = 1515 new XMLResourceIdentifierImpl( 1516 null, 1517 href, 1518 fCurrentBaseURI.getExpandedSystemId(), 1519 XMLEntityManager.expandSystemId( 1520 href, 1521 fCurrentBaseURI.getExpandedSystemId(), 1522 false)); 1523 1524 includedSource = 1525 fEntityResolver.resolveEntity(resourceIdentifier); 1526 1527 if (includedSource != null && 1528 !(includedSource instanceof HTTPInputSource) && 1529 (accept != null || acceptLanguage != null) && 1530 includedSource.getCharacterStream() == null && 1531 includedSource.getByteStream() == null) { 1532 1533 includedSource = createInputSource(includedSource.getPublicId(), includedSource.getSystemId(), 1534 includedSource.getBaseSystemId(), accept, acceptLanguage); 1535 } 1536 } 1537 catch (IOException e) { 1538 reportResourceError( 1539 "XMLResourceError", 1540 new Object [] { href, e.getMessage()}); 1541 return false; 1542 } 1543 } 1544 1545 if (includedSource == null) { 1546 if (accept != null || acceptLanguage != null) { 1548 includedSource = createInputSource(null, href, fCurrentBaseURI.getExpandedSystemId(), accept, acceptLanguage); 1549 } 1550 else { 1551 includedSource = new XMLInputSource(null, href, fCurrentBaseURI.getExpandedSystemId()); 1552 } 1553 } 1554 1555 if (parse.equals(XINCLUDE_PARSE_XML)) { 1556 if ((xpointer != null && fXPointerChildConfig == null) 1558 || (xpointer == null && fXIncludeChildConfig == null) ) { 1559 1560 String parserName = XINCLUDE_DEFAULT_CONFIGURATION; 1561 if (xpointer != null) 1562 parserName = "org.apache.xerces.parsers.XPointerParserConfiguration"; 1563 1564 fChildConfig = 1565 (XMLParserConfiguration)ObjectFactory.newInstance( 1566 parserName, 1567 ObjectFactory.findClassLoader(), 1568 true); 1569 1570 if (fSymbolTable != null) fChildConfig.setProperty(SYMBOL_TABLE, fSymbolTable); 1572 if (fErrorReporter != null) fChildConfig.setProperty(ERROR_REPORTER, fErrorReporter); 1573 if (fEntityResolver != null) fChildConfig.setProperty(ENTITY_RESOLVER, fEntityResolver); 1574 fChildConfig.setProperty(SECURITY_MANAGER, fSecurityManager); 1575 fChildConfig.setProperty(BUFFER_SIZE, new Integer (fBufferSize)); 1576 1577 fNeedCopyFeatures = true; 1579 1580 fChildConfig.setProperty( 1582 Constants.XERCES_PROPERTY_PREFIX 1583 + Constants.NAMESPACE_CONTEXT_PROPERTY, 1584 fNamespaceContext); 1585 1586 fChildConfig.setFeature( 1587 XINCLUDE_FIXUP_BASE_URIS, 1588 fFixupBaseURIs); 1589 1590 fChildConfig.setFeature( 1591 XINCLUDE_FIXUP_LANGUAGE, 1592 fFixupLanguage); 1593 1594 1595 if (xpointer != null ) { 1597 1598 XPointerHandler newHandler = 1599 (XPointerHandler)fChildConfig.getProperty( 1600 Constants.XERCES_PROPERTY_PREFIX 1601 + Constants.XPOINTER_HANDLER_PROPERTY); 1602 1603 fXPtrProcessor = newHandler; 1604 1605 ((XPointerHandler)fXPtrProcessor).setProperty( 1607 Constants.XERCES_PROPERTY_PREFIX 1608 + Constants.NAMESPACE_CONTEXT_PROPERTY, 1609 fNamespaceContext); 1610 1611 ((XPointerHandler)fXPtrProcessor).setProperty(XINCLUDE_FIXUP_BASE_URIS, 1612 new Boolean (fFixupBaseURIs)); 1613 1614 ((XPointerHandler)fXPtrProcessor).setProperty( 1615 XINCLUDE_FIXUP_LANGUAGE, 1616 new Boolean (fFixupLanguage)); 1617 1618 if (fErrorReporter != null) 1619 ((XPointerHandler)fXPtrProcessor).setProperty(ERROR_REPORTER, fErrorReporter); 1620 1622 newHandler.setParent(this); 1623 newHandler.setDocumentHandler(this.getDocumentHandler()); 1624 fXPointerChildConfig = fChildConfig; 1625 } else { 1626 XIncludeHandler newHandler = 1627 (XIncludeHandler)fChildConfig.getProperty( 1628 Constants.XERCES_PROPERTY_PREFIX 1629 + Constants.XINCLUDE_HANDLER_PROPERTY); 1630 1631 newHandler.setParent(this); 1632 newHandler.setDocumentHandler(this.getDocumentHandler()); 1633 fXIncludeChildConfig = fChildConfig; 1634 } 1635 } 1636 1637 if (xpointer != null ) { 1639 fChildConfig = fXPointerChildConfig ; 1640 1641 try { 1643 ((XPointerProcessor)fXPtrProcessor).parseXPointer(xpointer); 1644 1645 } catch (XNIException ex) { 1646 reportResourceError( 1648 "XMLResourceError", 1649 new Object [] { href, ex.getMessage()}); 1650 return false; 1651 } 1652 } else { 1653 fChildConfig = fXIncludeChildConfig; 1654 } 1655 1656 if (fNeedCopyFeatures) { 1658 copyFeatures(fSettings, fChildConfig); 1659 } 1660 fNeedCopyFeatures = false; 1661 1662 try { 1663 fNamespaceContext.pushScope(); 1664 1665 fChildConfig.parse(includedSource); 1666 if (fErrorReporter != null) { 1668 fErrorReporter.setDocumentLocator(fDocLocation); 1669 } 1670 1671 if (xpointer != null ) { 1673 if (!((XPointerProcessor)fXPtrProcessor).isXPointerResolved()) { 1675 Locale locale = (fErrorReporter != null) ? fErrorReporter.getLocale() : null; 1676 String reason = fXIncludeMessageFormatter.formatMessage(locale, "XPointerResolutionUnsuccessful", null); 1677 reportResourceError("XMLResourceError", new Object [] {href, reason}); 1678 return false; 1680 } 1681 } 1682 } 1683 catch (XNIException e) { 1684 if (fErrorReporter != null) { 1686 fErrorReporter.setDocumentLocator(fDocLocation); 1687 } 1688 reportFatalError("XMLParseError", new Object [] { href }); 1689 } 1690 catch (IOException e) { 1691 if (fErrorReporter != null) { 1693 fErrorReporter.setDocumentLocator(fDocLocation); 1694 } 1695 reportResourceError( 1699 "XMLResourceError", 1700 new Object [] { href, e.getMessage()}); 1701 return false; 1702 } 1703 finally { 1704 fNamespaceContext.popScope(); 1705 } 1706 } 1707 else if (parse.equals(XINCLUDE_PARSE_TEXT)) { 1708 String encoding = attributes.getValue(XINCLUDE_ATTR_ENCODING); 1710 includedSource.setEncoding(encoding); 1711 XIncludeTextReader textReader = null; 1712 1713 try { 1714 if (!fIsXML11) { 1716 if (fXInclude10TextReader == null) { 1717 fXInclude10TextReader = new XIncludeTextReader(includedSource, this, fBufferSize); 1718 } 1719 else { 1720 fXInclude10TextReader.setInputSource(includedSource); 1721 } 1722 textReader = fXInclude10TextReader; 1723 } 1724 else { 1725 if (fXInclude11TextReader == null) { 1726 fXInclude11TextReader = new XInclude11TextReader(includedSource, this, fBufferSize); 1727 } 1728 else { 1729 fXInclude11TextReader.setInputSource(includedSource); 1730 } 1731 textReader = fXInclude11TextReader; 1732 } 1733 textReader.setErrorReporter(fErrorReporter); 1734 textReader.parse(); 1735 } 1736 catch (MalformedByteSequenceException ex) { 1738 fErrorReporter.reportError(ex.getDomain(), ex.getKey(), 1739 ex.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); 1740 } 1741 catch (CharConversionException e) { 1742 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 1743 "CharConversionFailure", null, XMLErrorReporter.SEVERITY_FATAL_ERROR); 1744 } 1745 catch (IOException e) { 1746 reportResourceError( 1747 "TextResourceError", 1748 new Object [] { href, e.getMessage()}); 1749 return false; 1750 } 1751 finally { 1752 if (textReader != null) { 1753 try { 1754 textReader.close(); 1755 } 1756 catch (IOException e) { 1757 reportResourceError( 1758 "TextResourceError", 1759 new Object [] { href, e.getMessage()}); 1760 return false; 1761 } 1762 } 1763 } 1764 } 1765 else { 1766 reportFatalError("InvalidParseValue", new Object [] { parse }); 1767 } 1768 return true; 1769 } 1770 1771 1776 protected boolean hasXIncludeNamespace(QName element) { 1777 return element.uri == XINCLUDE_NS_URI 1781 || fNamespaceContext.getURI(element.prefix) == XINCLUDE_NS_URI; 1782 } 1783 1784 1792 protected boolean isIncludeElement(QName element) { 1793 return element.localpart.equals(XINCLUDE_INCLUDE) && 1794 hasXIncludeNamespace(element); 1795 } 1796 1797 1805 protected boolean isFallbackElement(QName element) { 1806 return element.localpart.equals(XINCLUDE_FALLBACK) && 1807 hasXIncludeNamespace(element); 1808 } 1809 1810 1819 protected boolean sameBaseURIAsIncludeParent() { 1820 String parentBaseURI = getIncludeParentBaseURI(); 1821 String baseURI = fCurrentBaseURI.getExpandedSystemId(); 1822 return parentBaseURI != null && parentBaseURI.equals(baseURI); 1831 } 1832 1833 1845 protected boolean sameLanguageAsIncludeParent() { 1846 String parentLanguage = getIncludeParentLanguage(); 1847 return parentLanguage != null && parentLanguage.equalsIgnoreCase(fCurrentLanguage); 1848 } 1849 1850 1856 protected boolean searchForRecursiveIncludes(XMLLocator includedSource) { 1857 String includedSystemId = includedSource.getExpandedSystemId(); 1858 1859 if (includedSystemId == null) { 1860 try { 1861 includedSystemId = 1862 XMLEntityManager.expandSystemId( 1863 includedSource.getLiteralSystemId(), 1864 includedSource.getBaseSystemId(), 1865 false); 1866 } 1867 catch (MalformedURIException e) { 1868 reportFatalError("ExpandedSystemId"); 1869 } 1870 } 1871 1872 if (includedSystemId.equals(fCurrentBaseURI.getExpandedSystemId())) { 1873 return true; 1874 } 1875 1876 if (fParentXIncludeHandler == null) { 1877 return false; 1878 } 1879 return fParentXIncludeHandler.searchForRecursiveIncludes( 1880 includedSource); 1881 } 1882 1883 1889 protected boolean isTopLevelIncludedItem() { 1890 return isTopLevelIncludedItemViaInclude() 1891 || isTopLevelIncludedItemViaFallback(); 1892 } 1893 1894 protected boolean isTopLevelIncludedItemViaInclude() { 1895 return fDepth == 1 && !isRootDocument(); 1896 } 1897 1898 protected boolean isTopLevelIncludedItemViaFallback() { 1899 return getSawFallback(fDepth - 1); 1904 } 1905 1906 1920 protected XMLAttributes processAttributes(XMLAttributes attributes) { 1921 if (isTopLevelIncludedItem()) { 1922 if (fFixupBaseURIs && !sameBaseURIAsIncludeParent()) { 1926 if (attributes == null) { 1927 attributes = new XMLAttributesImpl(); 1928 } 1929 1930 String uri = null; 1933 try { 1934 uri = this.getRelativeBaseURI(); 1935 } 1936 catch (MalformedURIException e) { 1937 uri = fCurrentBaseURI.getExpandedSystemId(); 1940 } 1941 int index = 1942 attributes.addAttribute( 1943 XML_BASE_QNAME, 1944 XMLSymbols.fCDATASymbol, 1945 uri); 1946 attributes.setSpecified(index, true); 1947 } 1948 1949 if (fFixupLanguage && !sameLanguageAsIncludeParent()) { 1953 if (attributes == null) { 1954 attributes = new XMLAttributesImpl(); 1955 } 1956 int index = 1957 attributes.addAttribute( 1958 XML_LANG_QNAME, 1959 XMLSymbols.fCDATASymbol, 1960 fCurrentLanguage); 1961 attributes.setSpecified(index, true); 1962 } 1963 1964 Enumeration inscopeNS = fNamespaceContext.getAllPrefixes(); 1966 while (inscopeNS.hasMoreElements()) { 1967 String prefix = (String )inscopeNS.nextElement(); 1968 String parentURI = 1969 fNamespaceContext.getURIFromIncludeParent(prefix); 1970 String uri = fNamespaceContext.getURI(prefix); 1971 if (parentURI != uri && attributes != null) { 1972 if (prefix == XMLSymbols.EMPTY_STRING) { 1973 if (attributes 1974 .getValue( 1975 NamespaceContext.XMLNS_URI, 1976 XMLSymbols.PREFIX_XMLNS) 1977 == null) { 1978 if (attributes == null) { 1979 attributes = new XMLAttributesImpl(); 1980 } 1981 1982 QName ns = (QName)NEW_NS_ATTR_QNAME.clone(); 1983 ns.prefix = null; 1984 ns.localpart = XMLSymbols.PREFIX_XMLNS; 1985 ns.rawname = XMLSymbols.PREFIX_XMLNS; 1986 int index = 1987 attributes.addAttribute( 1988 ns, 1989 XMLSymbols.fCDATASymbol, 1990 uri != null ? uri : XMLSymbols.EMPTY_STRING); 1991 attributes.setSpecified(index, true); 1992 fNamespaceContext.declarePrefix(prefix, uri); 1996 } 1997 } 1998 else if ( 1999 attributes.getValue(NamespaceContext.XMLNS_URI, prefix) 2000 == null) { 2001 if (attributes == null) { 2002 attributes = new XMLAttributesImpl(); 2003 } 2004 2005 QName ns = (QName)NEW_NS_ATTR_QNAME.clone(); 2006 ns.localpart = prefix; 2007 ns.rawname += prefix; 2008 ns.rawname = (fSymbolTable != null) ? 2009 fSymbolTable.addSymbol(ns.rawname) : 2010 ns.rawname.intern(); 2011 int index = 2012 attributes.addAttribute( 2013 ns, 2014 XMLSymbols.fCDATASymbol, 2015 uri != null ? uri : XMLSymbols.EMPTY_STRING); 2016 attributes.setSpecified(index, true); 2017 fNamespaceContext.declarePrefix(prefix, uri); 2021 } 2022 } 2023 } 2024 } 2025 2026 if (attributes != null) { 2027 int length = attributes.getLength(); 2028 for (int i = 0; i < length; i++) { 2029 String type = attributes.getType(i); 2030 String value = attributes.getValue(i); 2031 if (type == XMLSymbols.fENTITYSymbol) { 2032 this.checkUnparsedEntity(value); 2033 } 2034 if (type == XMLSymbols.fENTITIESSymbol) { 2035 StringTokenizer st = new StringTokenizer (value); 2037 while (st.hasMoreTokens()) { 2038 String entName = st.nextToken(); 2039 this.checkUnparsedEntity(entName); 2040 } 2041 } 2042 else if (type == XMLSymbols.fNOTATIONSymbol) { 2043 this.checkNotation(value); 2045 } 2046 2052 } 2053 } 2054 2055 return attributes; 2056 } 2057 2058 2064 protected String getRelativeBaseURI() throws MalformedURIException { 2065 int includeParentDepth = getIncludeParentDepth(); 2066 String relativeURI = this.getRelativeURI(includeParentDepth); 2067 if (isRootDocument()) { 2068 return relativeURI; 2069 } 2070 else { 2071 if (relativeURI.equals("")) { 2072 relativeURI = fCurrentBaseURI.getLiteralSystemId(); 2073 } 2074 2075 if (includeParentDepth == 0) { 2076 if (fParentRelativeURI == null) { 2077 fParentRelativeURI = 2078 fParentXIncludeHandler.getRelativeBaseURI(); 2079 } 2080 if (fParentRelativeURI.equals("")) { 2081 return relativeURI; 2082 } 2083 2084 URI base = new URI(fParentRelativeURI, true); 2085 URI uri = new URI(base, relativeURI); 2086 2087 2088 final String baseScheme = base.getScheme(); 2089 final String literalScheme = uri.getScheme(); 2090 if (!isEqual(baseScheme, literalScheme)) { 2091 return relativeURI; 2092 } 2093 2094 2095 final String baseAuthority = base.getAuthority(); 2096 final String literalAuthority = uri.getAuthority(); 2097 if (!isEqual(baseAuthority, literalAuthority)) { 2098 return uri.getSchemeSpecificPart(); 2099 } 2100 2101 2106 final String literalPath = uri.getPath(); 2107 final String literalQuery = uri.getQueryString(); 2108 final String literalFragment = uri.getFragment(); 2109 if (literalQuery != null || literalFragment != null) { 2110 StringBuffer buffer = new StringBuffer (); 2111 if (literalPath != null) { 2112 buffer.append(literalPath); 2113 } 2114 if (literalQuery != null) { 2115 buffer.append('?'); 2116 buffer.append(literalQuery); 2117 } 2118 if (literalFragment != null) { 2119 buffer.append('#'); 2120 buffer.append(literalFragment); 2121 } 2122 return buffer.toString(); 2123 } 2124 return literalPath; 2125 } 2126 else { 2127 return relativeURI; 2128 } 2129 } 2130 } 2131 2132 2136 private String getIncludeParentBaseURI() { 2137 int depth = getIncludeParentDepth(); 2138 if (!isRootDocument() && depth == 0) { 2139 return fParentXIncludeHandler.getIncludeParentBaseURI(); 2140 } 2141 else { 2142 return this.getBaseURI(depth); 2143 } 2144 } 2145 2146 2151 private String getIncludeParentLanguage() { 2152 int depth = getIncludeParentDepth(); 2153 if (!isRootDocument() && depth == 0) { 2154 return fParentXIncludeHandler.getIncludeParentLanguage(); 2155 } 2156 else { 2157 return getLanguage(depth); 2158 } 2159 } 2160 2161 2169 private int getIncludeParentDepth() { 2170 for (int i = fDepth - 1; i >= 0; i--) { 2173 if (!getSawInclude(i) && !getSawFallback(i)) { 2180 return i; 2181 } 2182 } 2183 return 0; 2186 } 2187 2188 2191 private int getResultDepth() { 2192 return fResultDepth; 2193 } 2194 2195 2201 protected Augmentations modifyAugmentations(Augmentations augs) { 2202 return modifyAugmentations(augs, false); 2203 } 2204 2205 2212 protected Augmentations modifyAugmentations( 2213 Augmentations augs, 2214 boolean force) { 2215 if (force || isTopLevelIncludedItem()) { 2216 if (augs == null) { 2217 augs = new AugmentationsImpl(); 2218 } 2219 augs.putItem(XINCLUDE_INCLUDED, Boolean.TRUE); 2220 } 2221 return augs; 2222 } 2223 2224 protected int getState(int depth) { 2225 return fState[depth]; 2226 } 2227 2228 protected int getState() { 2229 return fState[fDepth]; 2230 } 2231 2232 protected void setState(int state) { 2233 if (fDepth >= fState.length) { 2234 int[] newarray = new int[fDepth * 2]; 2235 System.arraycopy(fState, 0, newarray, 0, fState.length); 2236 fState = newarray; 2237 } 2238 fState[fDepth] = state; 2239 } 2240 2241 2249 protected void setSawFallback(int depth, boolean val) { 2250 if (depth >= fSawFallback.length) { 2251 boolean[] newarray = new boolean[depth * 2]; 2252 System.arraycopy(fSawFallback, 0, newarray, 0, fSawFallback.length); 2253 fSawFallback = newarray; 2254 } 2255 fSawFallback[depth] = val; 2256 } 2257 2258 2265 protected boolean getSawFallback(int depth) { 2266 if (depth >= fSawFallback.length) { 2267 return false; 2268 } 2269 return fSawFallback[depth]; 2270 } 2271 2272 2279 protected void setSawInclude(int depth, boolean val) { 2280 if (depth >= fSawInclude.length) { 2281 boolean[] newarray = new boolean[depth * 2]; 2282 System.arraycopy(fSawInclude, 0, newarray, 0, fSawInclude.length); 2283 fSawInclude = newarray; 2284 } 2285 fSawInclude[depth] = val; 2286 } 2287 2288 2295 protected boolean getSawInclude(int depth) { 2296 if (depth >= fSawInclude.length) { 2297 return false; 2298 } 2299 return fSawInclude[depth]; 2300 } 2301 2302 protected void reportResourceError(String key) { 2303 this.reportFatalError(key, null); 2304 } 2305 2306 protected void reportResourceError(String key, Object [] args) { 2307 this.reportError(key, args, XMLErrorReporter.SEVERITY_WARNING); 2308 } 2309 2310 protected void reportFatalError(String key) { 2311 this.reportFatalError(key, null); 2312 } 2313 2314 protected void reportFatalError(String key, Object [] args) { 2315 this.reportError(key, args, XMLErrorReporter.SEVERITY_FATAL_ERROR); 2316 } 2317 2318 private void reportError(String key, Object [] args, short severity) { 2319 if (fErrorReporter != null) { 2320 fErrorReporter.reportError( 2321 XIncludeMessageFormatter.XINCLUDE_DOMAIN, 2322 key, 2323 args, 2324 severity); 2325 } 2326 } 2329 2330 2334 protected void setParent(XIncludeHandler parent) { 2335 fParentXIncludeHandler = parent; 2336 } 2337 2338 protected boolean isRootDocument() { 2340 return fParentXIncludeHandler == null; 2341 } 2342 2343 2349 protected void addUnparsedEntity( 2350 String name, 2351 XMLResourceIdentifier identifier, 2352 String notation, 2353 Augmentations augmentations) { 2354 UnparsedEntity ent = new UnparsedEntity(); 2355 ent.name = name; 2356 ent.systemId = identifier.getLiteralSystemId(); 2357 ent.publicId = identifier.getPublicId(); 2358 ent.baseURI = identifier.getBaseSystemId(); 2359 ent.expandedSystemId = identifier.getExpandedSystemId(); 2360 ent.notation = notation; 2361 ent.augmentations = augmentations; 2362 fUnparsedEntities.add(ent); 2363 } 2364 2365 2371 protected void addNotation( 2372 String name, 2373 XMLResourceIdentifier identifier, 2374 Augmentations augmentations) { 2375 Notation not = new Notation(); 2376 not.name = name; 2377 not.systemId = identifier.getLiteralSystemId(); 2378 not.publicId = identifier.getPublicId(); 2379 not.baseURI = identifier.getBaseSystemId(); 2380 not.expandedSystemId = identifier.getExpandedSystemId(); 2381 not.augmentations = augmentations; 2382 fNotations.add(not); 2383 } 2384 2385 2393 protected void checkUnparsedEntity(String entName) { 2394 UnparsedEntity ent = new UnparsedEntity(); 2395 ent.name = entName; 2396 int index = fUnparsedEntities.indexOf(ent); 2397 if (index != -1) { 2398 ent = (UnparsedEntity)fUnparsedEntities.get(index); 2399 checkNotation(ent.notation); 2401 checkAndSendUnparsedEntity(ent); 2402 } 2403 } 2404 2405 2412 protected void checkNotation(String notName) { 2413 Notation not = new Notation(); 2414 not.name = notName; 2415 int index = fNotations.indexOf(not); 2416 if (index != -1) { 2417 not = (Notation)fNotations.get(index); 2418 checkAndSendNotation(not); 2419 } 2420 } 2421 2422 2429 protected void checkAndSendUnparsedEntity(UnparsedEntity ent) { 2430 if (isRootDocument()) { 2431 int index = fUnparsedEntities.indexOf(ent); 2432 if (index == -1) { 2433 XMLResourceIdentifier id = 2437 new XMLResourceIdentifierImpl( 2438 ent.publicId, 2439 ent.systemId, 2440 ent.baseURI, 2441 ent.expandedSystemId); 2442 addUnparsedEntity( 2443 ent.name, 2444 id, 2445 ent.notation, 2446 ent.augmentations); 2447 if (fSendUEAndNotationEvents && fDTDHandler != null) { 2448 fDTDHandler.unparsedEntityDecl( 2449 ent.name, 2450 id, 2451 ent.notation, 2452 ent.augmentations); 2453 } 2454 } 2455 else { 2456 UnparsedEntity localEntity = 2457 (UnparsedEntity)fUnparsedEntities.get(index); 2458 if (!ent.isDuplicate(localEntity)) { 2459 reportFatalError( 2460 "NonDuplicateUnparsedEntity", 2461 new Object [] { ent.name }); 2462 } 2463 } 2464 } 2465 else { 2466 fParentXIncludeHandler.checkAndSendUnparsedEntity(ent); 2467 } 2468 } 2469 2470 2477 protected void checkAndSendNotation(Notation not) { 2478 if (isRootDocument()) { 2479 int index = fNotations.indexOf(not); 2480 if (index == -1) { 2481 XMLResourceIdentifier id = 2483 new XMLResourceIdentifierImpl( 2484 not.publicId, 2485 not.systemId, 2486 not.baseURI, 2487 not.expandedSystemId); 2488 addNotation(not.name, id, not.augmentations); 2489 if (fSendUEAndNotationEvents && fDTDHandler != null) { 2490 fDTDHandler.notationDecl(not.name, id, not.augmentations); 2491 } 2492 } 2493 else { 2494 Notation localNotation = (Notation)fNotations.get(index); 2495 if (!not.isDuplicate(localNotation)) { 2496 reportFatalError( 2497 "NonDuplicateNotation", 2498 new Object [] { not.name }); 2499 } 2500 } 2501 } 2502 else { 2503 fParentXIncludeHandler.checkAndSendNotation(not); 2504 } 2505 } 2506 2507 2512 private void checkWhitespace(XMLString value) { 2513 int end = value.offset + value.length; 2514 for (int i = value.offset; i < end; ++i) { 2515 if (!XMLChar.isSpace(value.ch[i])) { 2516 reportFatalError("ContentIllegalAtTopLevel"); 2517 return; 2518 } 2519 } 2520 } 2521 2522 2525 private void checkMultipleRootElements() { 2526 if (getRootElementProcessed()) { 2527 reportFatalError("MultipleRootElements"); 2528 } 2529 setRootElementProcessed(true); 2530 } 2531 2532 2535 private void setRootElementProcessed(boolean seenRoot) { 2536 if (isRootDocument()) { 2537 fSeenRootElement = seenRoot; 2538 return; 2539 } 2540 fParentXIncludeHandler.setRootElementProcessed(seenRoot); 2541 } 2542 2543 2546 private boolean getRootElementProcessed() { 2547 return isRootDocument() ? fSeenRootElement : fParentXIncludeHandler.getRootElementProcessed(); 2548 } 2549 2550 protected void copyFeatures( 2553 XMLComponentManager from, 2554 ParserConfigurationSettings to) { 2555 Enumeration features = Constants.getXercesFeatures(); 2556 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); 2557 features = Constants.getSAXFeatures(); 2558 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); 2559 } 2560 2561 protected void copyFeatures( 2562 XMLComponentManager from, 2563 XMLParserConfiguration to) { 2564 Enumeration features = Constants.getXercesFeatures(); 2565 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); 2566 features = Constants.getSAXFeatures(); 2567 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); 2568 } 2569 2570 private void copyFeatures1( 2571 Enumeration features, 2572 String featurePrefix, 2573 XMLComponentManager from, 2574 ParserConfigurationSettings to) { 2575 while (features.hasMoreElements()) { 2576 String featureId = featurePrefix + (String )features.nextElement(); 2577 2578 to.addRecognizedFeatures(new String [] { featureId }); 2579 2580 try { 2581 to.setFeature(featureId, from.getFeature(featureId)); 2582 } 2583 catch (XMLConfigurationException e) { 2584 } 2587 } 2588 } 2589 2590 private void copyFeatures1( 2591 Enumeration features, 2592 String featurePrefix, 2593 XMLComponentManager from, 2594 XMLParserConfiguration to) { 2595 while (features.hasMoreElements()) { 2596 String featureId = featurePrefix + (String )features.nextElement(); 2597 boolean value = from.getFeature(featureId); 2598 2599 try { 2600 to.setFeature(featureId, value); 2601 } 2602 catch (XMLConfigurationException e) { 2603 } 2606 } 2607 } 2608 2609 protected static class Notation { 2612 public String name; 2613 public String systemId; 2614 public String baseURI; 2615 public String publicId; 2616 public String expandedSystemId; 2617 public Augmentations augmentations; 2618 2619 public boolean equals(Object obj) { 2622 if (obj == null) { 2623 return false; 2624 } 2625 if (obj instanceof Notation) { 2626 Notation other = (Notation)obj; 2627 return name.equals(other.name); 2628 } 2629 return false; 2630 } 2631 2632 public boolean isDuplicate(Object obj) { 2640 if (obj != null && obj instanceof Notation) { 2641 Notation other = (Notation)obj; 2642 return name.equals(other.name) 2643 && isEqual(publicId, other.publicId) 2644 && isEqual(expandedSystemId, other.expandedSystemId); 2645 } 2646 return false; 2647 } 2648 2649 private boolean isEqual(String one, String two) { 2650 return (one == two || (one != null && one.equals(two))); 2651 } 2652 } 2653 2654 protected static class UnparsedEntity { 2657 public String name; 2658 public String systemId; 2659 public String baseURI; 2660 public String publicId; 2661 public String expandedSystemId; 2662 public String notation; 2663 public Augmentations augmentations; 2664 2665 public boolean equals(Object obj) { 2668 if (obj == null) { 2669 return false; 2670 } 2671 if (obj instanceof UnparsedEntity) { 2672 UnparsedEntity other = (UnparsedEntity)obj; 2673 return name.equals(other.name); 2674 } 2675 return false; 2676 } 2677 2678 public boolean isDuplicate(Object obj) { 2686 if (obj != null && obj instanceof UnparsedEntity) { 2687 UnparsedEntity other = (UnparsedEntity)obj; 2688 return name.equals(other.name) 2689 && isEqual(publicId, other.publicId) 2690 && isEqual(expandedSystemId, other.expandedSystemId) 2691 && isEqual(notation, other.notation); 2692 } 2693 return false; 2694 } 2695 2696 private boolean isEqual(String one, String two) { 2697 return (one == two || (one != null && one.equals(two))); 2698 } 2699 } 2700 2701 2703 2706 protected void saveBaseURI() { 2707 fBaseURIScope.push(fDepth); 2708 fBaseURI.push(fCurrentBaseURI.getBaseSystemId()); 2709 fLiteralSystemID.push(fCurrentBaseURI.getLiteralSystemId()); 2710 fExpandedSystemID.push(fCurrentBaseURI.getExpandedSystemId()); 2711 } 2712 2713 2716 protected void restoreBaseURI() { 2717 fBaseURI.pop(); 2718 fLiteralSystemID.pop(); 2719 fExpandedSystemID.pop(); 2720 fBaseURIScope.pop(); 2721 fCurrentBaseURI.setBaseSystemId((String )fBaseURI.peek()); 2722 fCurrentBaseURI.setLiteralSystemId((String )fLiteralSystemID.peek()); 2723 fCurrentBaseURI.setExpandedSystemId((String )fExpandedSystemID.peek()); 2724 } 2725 2726 2728 2733 protected void saveLanguage(String language) { 2734 fLanguageScope.push(fDepth); 2735 fLanguageStack.push(language); 2736 } 2737 2738 2741 public String restoreLanguage() { 2742 fLanguageStack.pop(); 2743 fLanguageScope.pop(); 2744 return (String ) fLanguageStack.peek(); 2745 } 2746 2747 2752 public String getBaseURI(int depth) { 2753 int scope = scopeOfBaseURI(depth); 2754 return (String )fExpandedSystemID.elementAt(scope); 2755 } 2756 2757 2762 public String getLanguage(int depth) { 2763 int scope = scopeOfLanguage(depth); 2764 return (String )fLanguageStack.elementAt(scope); 2765 } 2766 2767 2775 public String getRelativeURI(int depth) throws MalformedURIException { 2776 int start = scopeOfBaseURI(depth) + 1; 2780 if (start == fBaseURIScope.size()) { 2781 return ""; 2783 } 2784 URI uri = new URI("file", (String )fLiteralSystemID.elementAt(start)); 2785 for (int i = start + 1; i < fBaseURIScope.size(); i++) { 2786 uri = new URI(uri, (String )fLiteralSystemID.elementAt(i)); 2787 } 2788 return uri.getPath(); 2789 } 2790 2791 private int scopeOfBaseURI(int depth) { 2795 for (int i = fBaseURIScope.size() - 1; i >= 0; i--) { 2796 if (fBaseURIScope.elementAt(i) <= depth) 2797 return i; 2798 } 2799 return -1; 2801 } 2802 2803 private int scopeOfLanguage(int depth) { 2804 for (int i = fLanguageScope.size() - 1; i >= 0; i--) { 2805 if (fLanguageScope.elementAt(i) <= depth) 2806 return i; 2807 } 2808 return -1; 2810 } 2811 2812 2816 protected void processXMLBaseAttributes(XMLAttributes attributes) { 2817 String baseURIValue = 2818 attributes.getValue(NamespaceContext.XML_URI, "base"); 2819 if (baseURIValue != null) { 2820 try { 2821 String expandedValue = 2822 XMLEntityManager.expandSystemId( 2823 baseURIValue, 2824 fCurrentBaseURI.getExpandedSystemId(), 2825 false); 2826 fCurrentBaseURI.setLiteralSystemId(baseURIValue); 2827 fCurrentBaseURI.setBaseSystemId( 2828 fCurrentBaseURI.getExpandedSystemId()); 2829 fCurrentBaseURI.setExpandedSystemId(expandedValue); 2830 2831 saveBaseURI(); 2833 } 2834 catch (MalformedURIException e) { 2835 } 2837 } 2838 } 2839 2840 2844 protected void processXMLLangAttributes(XMLAttributes attributes) { 2845 String language = attributes.getValue(NamespaceContext.XML_URI, "lang"); 2846 if (language != null) { 2847 fCurrentLanguage = language; 2848 saveLanguage(fCurrentLanguage); 2849 } 2850 } 2851 2852 2860 private boolean isValidInHTTPHeader (String value) { 2861 char ch; 2862 for (int i = value.length() - 1; i >= 0; --i) { 2863 ch = value.charAt(i); 2864 if (ch < 0x20 || ch > 0x7E) { 2865 return false; 2866 } 2867 } 2868 return true; 2869 } 2870 2871 2874 private XMLInputSource createInputSource(String publicId, 2875 String systemId, String baseSystemId, 2876 String accept, String acceptLanguage) { 2877 2878 HTTPInputSource httpSource = new HTTPInputSource(publicId, systemId, baseSystemId); 2879 if (accept != null && accept.length() > 0) { 2880 httpSource.setHTTPRequestProperty(XIncludeHandler.HTTP_ACCEPT, accept); 2881 } 2882 if (acceptLanguage != null && acceptLanguage.length() > 0) { 2883 httpSource.setHTTPRequestProperty(XIncludeHandler.HTTP_ACCEPT_LANGUAGE, acceptLanguage); 2884 } 2885 return httpSource; 2886 } 2887 2888 private boolean isEqual(String one, String two) { 2889 return (one == two || (one != null && one.equals(two))); 2890 } 2891 2892 private static boolean gNeedEscaping[] = new boolean[128]; 2894 private static char gAfterEscaping1[] = new char[128]; 2896 private static char gAfterEscaping2[] = new char[128]; 2898 private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7', 2899 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 2900 static { 2902 char[] escChs = {' ', '<', '>', '"', '{', '}', '|', '\\', '^', '`'}; 2903 int len = escChs.length; 2904 char ch; 2905 for (int i = 0; i < len; i++) { 2906 ch = escChs[i]; 2907 gNeedEscaping[ch] = true; 2908 gAfterEscaping1[ch] = gHexChs[ch >> 4]; 2909 gAfterEscaping2[ch] = gHexChs[ch & 0xf]; 2910 } 2911 } 2912 2913 private String escapeHref(String href) { 2926 int len = href.length(); 2927 int ch; 2928 StringBuffer buffer = new StringBuffer (len*3); 2929 2930 int i = 0; 2932 for (; i < len; i++) { 2933 ch = href.charAt(i); 2934 if (ch > 0x7E) { 2936 break; 2937 } 2938 if (ch < 0x20) { 2940 return href; 2941 } 2942 if (gNeedEscaping[ch]) { 2943 buffer.append('%'); 2944 buffer.append(gAfterEscaping1[ch]); 2945 buffer.append(gAfterEscaping2[ch]); 2946 } 2947 else { 2948 buffer.append((char)ch); 2949 } 2950 } 2951 2952 if (i < len) { 2954 for (int j = i; j < len; ++j) { 2956 ch = href.charAt(j); 2957 if ((ch >= 0x20 && ch <= 0x7E) || 2958 (ch >= 0xA0 && ch <= 0xD7FF) || 2959 (ch >= 0xF900 && ch <= 0xFDCF) || 2960 (ch >= 0xFDF0 && ch <= 0xFFEF)) { 2961 continue; 2962 } 2963 if (XMLChar.isHighSurrogate(ch) && ++j < len) { 2964 int ch2 = href.charAt(j); 2965 if (XMLChar.isLowSurrogate(ch2)) { 2966 ch2 = XMLChar.supplemental((char)ch, (char)ch2); 2967 if (ch2 < 0xF0000 && (ch2 & 0xFFFF) <= 0xFFFD) { 2968 continue; 2969 } 2970 } 2971 } 2972 return href; 2974 } 2975 2976 byte[] bytes = null; 2978 byte b; 2979 try { 2980 bytes = href.substring(i).getBytes("UTF-8"); 2981 } catch (java.io.UnsupportedEncodingException e) { 2982 return href; 2984 } 2985 len = bytes.length; 2986 2987 for (i = 0; i < len; i++) { 2989 b = bytes[i]; 2990 if (b < 0) { 2992 ch = b + 256; 2993 buffer.append('%'); 2994 buffer.append(gHexChs[ch >> 4]); 2995 buffer.append(gHexChs[ch & 0xf]); 2996 } 2997 else if (gNeedEscaping[b]) { 2998 buffer.append('%'); 2999 buffer.append(gAfterEscaping1[b]); 3000 buffer.append(gAfterEscaping2[b]); 3001 } 3002 else { 3003 buffer.append((char)b); 3004 } 3005 } 3006 } 3007 3008 if (buffer.length() != len) { 3011 return buffer.toString(); 3012 } 3013 else { 3014 return href; 3015 } 3016 } 3017} 3018 | Popular Tags |