1 57 58 package com.sun.org.apache.xerces.internal.xinclude; 59 60 import java.io.CharConversionException ; 61 import java.io.IOException ; 62 import java.io.InputStream ; 63 import java.io.Reader ; 64 import java.net.HttpURLConnection ; 65 import java.net.URL ; 66 import java.net.URLConnection ; 67 import java.util.Enumeration ; 68 import java.util.Stack ; 69 import java.util.StringTokenizer ; 70 import java.util.Vector ; 71 72 import com.sun.org.apache.xerces.internal.impl.Constants; 73 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 74 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 75 import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException; 76 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 77 import com.sun.org.apache.xerces.internal.util.AugmentationsImpl; 78 import com.sun.org.apache.xerces.internal.util.IntStack; 79 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; 80 import com.sun.org.apache.xerces.internal.util.SecurityManager; 81 import com.sun.org.apache.xerces.internal.util.URI; 82 import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl; 83 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; 84 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 85 import com.sun.org.apache.xerces.internal.util.URI.MalformedURIException; 86 import com.sun.org.apache.xerces.internal.xni.Augmentations; 87 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 88 import com.sun.org.apache.xerces.internal.xni.QName; 89 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 90 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; 91 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 92 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 93 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 94 import com.sun.org.apache.xerces.internal.xni.XMLString; 95 import com.sun.org.apache.xerces.internal.xni.XNIException; 96 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 97 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 98 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 99 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDFilter; 100 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDSource; 101 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter; 102 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 103 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 104 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 105 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 106 107 149 public class XIncludeHandler 150 implements XMLComponent, XMLDocumentFilter, XMLDTDFilter { 151 152 public final static String XPOINTER_DEFAULT_CONFIGURATION = 155 "com.sun.org.apache.xerces.internal.parsers.XPointerParserConfiguration"; 156 protected static final String XPOINTER_SCHEMA = 157 Constants.XERCES_PROPERTY_PREFIX + Constants.XPOINTER_SCHEMA_PROPERTY; 158 protected static final String XINCLUDE_AWARE = 159 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE ; 160 161 public final static String XINCLUDE_DEFAULT_CONFIGURATION = 163 "com.sun.org.apache.xerces.internal.parsers.XIncludeParserConfiguration"; 164 public final static String HTTP_ACCEPT = "Accept"; 165 public final static String HTTP_ACCEPT_LANGUAGE = "Accept-Language"; 166 public final static String HTTP_ACCEPT_CHARSET = "Accept-Charset"; 167 public final static String XPOINTER = "xpointer"; 168 169 public final static String XINCLUDE_NS_URI = 170 "http://www.w3.org/2001/XInclude".intern(); 171 public final static String XINCLUDE_INCLUDE = "include".intern(); 172 public final static String XINCLUDE_FALLBACK = "fallback".intern(); 173 public final static String XINCLUDE_PARSE_XML = "xml".intern(); 174 public final static String XINCLUDE_PARSE_TEXT = "text".intern(); 175 176 public final static String XINCLUDE_ATTR_HREF = "href".intern(); 177 public final static String XINCLUDE_ATTR_PARSE = "parse".intern(); 178 public final static String XINCLUDE_ATTR_ENCODING = "encoding".intern(); 179 public final static String XINCLUDE_ATTR_ACCEPT = "accept".intern(); 180 public final static String XINCLUDE_ATTR_ACCEPT_LANGUAGE = "accept-language".intern(); 181 public final static String XINCLUDE_ATTR_ACCEPT_CHARSET = "accept-charset".intern(); 182 183 public final static String XINCLUDE_INCLUDED = "[included]".intern(); 185 186 187 public final static String CURRENT_BASE_URI = "currentBaseURI"; 188 189 public final static String XINCLUDE_BASE = "base"; 191 public final static QName XML_BASE_QNAME = 192 new QName( 193 XMLSymbols.PREFIX_XML, 194 XINCLUDE_BASE, 195 XMLSymbols.PREFIX_XML + ":" + XINCLUDE_BASE, 196 NamespaceContext.XML_URI); 197 198 public final static QName NEW_NS_ATTR_QNAME = 199 new QName( 200 XMLSymbols.PREFIX_XMLNS, 201 "", 202 XMLSymbols.PREFIX_XMLNS + ":", 203 NamespaceContext.XMLNS_URI); 204 205 private final static int STATE_NORMAL_PROCESSING = 1; 207 private final static int STATE_IGNORE = 2; 210 private final static int STATE_EXPECT_FALLBACK = 3; 213 214 216 217 protected static final String ALLOW_UE_AND_NOTATION_EVENTS = 218 Constants.SAX_FEATURE_PREFIX 219 + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE; 220 221 222 protected static final String ERROR_REPORTER = 223 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 224 225 226 protected static final String ENTITY_RESOLVER = 227 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 228 229 230 protected static final String SECURITY_MANAGER = 231 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 232 233 234 private static final String [] RECOGNIZED_FEATURES = 235 { ALLOW_UE_AND_NOTATION_EVENTS }; 236 237 238 private static final Boolean [] FEATURE_DEFAULTS = { Boolean.TRUE }; 239 240 241 242 private static final String [] RECOGNIZED_PROPERTIES = 243 { ERROR_REPORTER, ENTITY_RESOLVER, XPOINTER_SCHEMA }; 244 245 private static final Object [] PROPERTY_DEFAULTS = { null, null, null }; 246 247 249 protected XMLDocumentHandler fDocumentHandler; 251 protected XMLDocumentSource fDocumentSource; 252 253 protected XPointerFramework fXPointerFramework = null; 254 protected XPointerSchema [] fXPointerSchema; 255 256 protected XMLDTDHandler fDTDHandler; 258 protected XMLDTDSource fDTDSource; 259 260 protected XIncludeHandler fParentXIncludeHandler; 262 263 protected String fParentRelativeURI; 267 268 protected XMLParserConfiguration fChildConfig; 271 272 protected XMLLocator fDocLocation; 273 protected XIncludeNamespaceSupport fNamespaceContext; 274 protected XMLErrorReporter fErrorReporter; 275 protected XMLEntityResolver fEntityResolver; 276 protected SecurityManager fSecurityManager; 277 278 protected XMLResourceIdentifier fCurrentBaseURI; 280 protected IntStack baseURIScope; 281 protected Stack baseURI; 282 protected Stack literalSystemID; 283 protected Stack expandedSystemID; 284 285 protected ParserConfigurationSettings fSettings; 287 288 private int fDepth; 291 292 private static final int INITIAL_SIZE = 8; 294 295 private boolean[] fSawInclude = new boolean[INITIAL_SIZE]; 300 301 private boolean[] fSawFallback = new boolean[INITIAL_SIZE]; 305 306 private int[] fState = new int[INITIAL_SIZE]; 308 309 private Vector fNotations; 311 private Vector fUnparsedEntities; 312 313 private boolean fSendUEAndNotationEvents; 316 317 private boolean fIsXML11; 319 320 private boolean fInDTD; 322 323 325 public XIncludeHandler() { 326 fDepth = 0; 327 328 fSawFallback[fDepth] = false; 329 fSawInclude[fDepth] = false; 330 fState[fDepth] = STATE_NORMAL_PROCESSING; 331 fNotations = new Vector (); 332 fUnparsedEntities = new Vector (); 333 334 baseURIScope = new IntStack(); 335 baseURI = new Stack (); 336 literalSystemID = new Stack (); 337 expandedSystemID = new Stack (); 338 fCurrentBaseURI = new XMLResourceIdentifierImpl(); 339 } 340 341 343 public void reset(XMLComponentManager componentManager) 344 throws XNIException { 345 fNamespaceContext = null; 346 fDepth = 0; 347 fNotations = new Vector (); 348 fUnparsedEntities = new Vector (); 349 fParentRelativeURI = null; 350 fIsXML11 = false; 351 fInDTD = false; 352 353 baseURIScope.clear(); 354 baseURI.clear(); 355 literalSystemID.clear(); 356 expandedSystemID.clear(); 357 358 for (int i = 0; i < fState.length; ++i) { 365 fState[i] = STATE_NORMAL_PROCESSING; 366 } 367 for (int i = 0; i < fSawFallback.length; ++i) { 368 fSawFallback[i] = false; 369 } 370 for (int i = 0; i < fSawInclude.length; ++i) { 371 fSawInclude[i] = false; 372 } 373 374 try { 375 fSendUEAndNotationEvents = 376 componentManager.getFeature(ALLOW_UE_AND_NOTATION_EVENTS); 377 if (fChildConfig != null) { 378 fChildConfig.setFeature( 379 ALLOW_UE_AND_NOTATION_EVENTS, 380 fSendUEAndNotationEvents); 381 } 382 } 383 catch (XMLConfigurationException e) { 384 } 385 386 try { 388 XMLErrorReporter value = 389 (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 390 if (value != null) { 391 setErrorReporter(value); 392 if (fChildConfig != null) { 393 fChildConfig.setProperty(ERROR_REPORTER, value); 394 } 395 } 396 397 } 398 catch (XMLConfigurationException e) { 399 fErrorReporter = null; 400 } 401 402 try { 404 XMLEntityResolver value = 405 (XMLEntityResolver)componentManager.getProperty( 406 ENTITY_RESOLVER); 407 408 if (value != null) { 409 fEntityResolver = value; 410 if (fChildConfig != null) { 411 fChildConfig.setProperty(ENTITY_RESOLVER, value); 412 } 413 } 414 } 415 catch (XMLConfigurationException e) { 416 fEntityResolver = null; 417 } 418 419 try { 420 fXPointerSchema = 421 (XPointerSchema [])componentManager.getProperty( 422 XPOINTER_SCHEMA); 423 } 424 catch (XMLConfigurationException e) { 425 fXPointerSchema = null; 426 } 427 428 try { 430 SecurityManager value = 431 (SecurityManager )componentManager.getProperty( 432 SECURITY_MANAGER); 433 434 if (value != null) { 435 fSecurityManager = value; 436 if (fChildConfig != null) { 437 fChildConfig.setProperty(SECURITY_MANAGER, value); 438 } 439 } 440 } 441 catch (XMLConfigurationException e) { 442 fSecurityManager = null; 443 } 444 445 fSettings = new ParserConfigurationSettings(); 446 copyFeatures(componentManager, fSettings); 447 } 451 456 public String [] getRecognizedFeatures() { 457 return RECOGNIZED_FEATURES; 458 } 460 475 public void setFeature(String featureId, boolean state) 476 throws XMLConfigurationException { 477 if (featureId.equals(ALLOW_UE_AND_NOTATION_EVENTS)) { 478 fSendUEAndNotationEvents = state; 479 } 480 if (fSettings != null) { 481 fSettings.setFeature(featureId, state); 482 } 483 } 485 490 public String [] getRecognizedProperties() { 491 return RECOGNIZED_PROPERTIES; 492 } 494 509 public void setProperty(String propertyId, Object value) 510 throws XMLConfigurationException { 511 if (propertyId.equals(ERROR_REPORTER)) { 512 setErrorReporter((XMLErrorReporter)value); 513 if (fChildConfig != null) { 514 fChildConfig.setProperty(propertyId, value); 515 } 516 } 517 if (propertyId.equals(ENTITY_RESOLVER)) { 518 fEntityResolver = (XMLEntityResolver)value; 519 if (fChildConfig != null) { 520 fChildConfig.setProperty(propertyId, value); 521 } 522 } 523 if (propertyId.equals(SECURITY_MANAGER)) { 524 fSecurityManager = (SecurityManager )value; 525 if (fChildConfig != null) { 526 fChildConfig.setProperty(propertyId, value); 527 } 528 } 529 530 } 532 541 public Boolean getFeatureDefault(String featureId) { 542 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 543 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 544 return FEATURE_DEFAULTS[i]; 545 } 546 } 547 return null; 548 } 550 559 public Object getPropertyDefault(String propertyId) { 560 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 561 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 562 return PROPERTY_DEFAULTS[i]; 563 } 564 } 565 return null; 566 } 568 public void setDocumentHandler(XMLDocumentHandler handler) { 569 fDocumentHandler = handler; 570 } 571 572 public XMLDocumentHandler getDocumentHandler() { 573 return fDocumentHandler; 574 } 575 576 578 586 public void startDocument( 587 XMLLocator locator, 588 String encoding, 589 NamespaceContext namespaceContext, 590 Augmentations augs) 591 throws XNIException { 592 593 if(fErrorReporter != null) 596 fErrorReporter.setDocumentLocator(locator); 597 if (!isRootDocument() 598 && fParentXIncludeHandler.searchForRecursiveIncludes(locator)) { 599 reportFatalError( 600 "RecursiveInclude", 601 new Object [] { locator.getExpandedSystemId()}); 602 } 603 604 if (!(namespaceContext instanceof XIncludeNamespaceSupport)) { 605 reportFatalError("IncompatibleNamespaceContext"); 606 } 607 fNamespaceContext = (XIncludeNamespaceSupport)namespaceContext; 608 fDocLocation = locator; 609 610 fCurrentBaseURI.setBaseSystemId(locator.getBaseSystemId()); 612 fCurrentBaseURI.setExpandedSystemId(locator.getExpandedSystemId()); 613 fCurrentBaseURI.setLiteralSystemId(locator.getLiteralSystemId()); 614 saveBaseURI(); 615 if (augs == null) { 616 augs = new AugmentationsImpl(); 617 } 618 augs.putItem(CURRENT_BASE_URI, fCurrentBaseURI); 619 620 if (isRootDocument() && fDocumentHandler != null) { 621 fDocumentHandler.startDocument( 622 locator, 623 encoding, 624 namespaceContext, 625 augs); 626 } 627 } 628 629 public void xmlDecl( 630 String version, 631 String encoding, 632 String standalone, 633 Augmentations augs) 634 throws XNIException { 635 fIsXML11 = "1.1".equals(version); 636 if (isRootDocument() && fDocumentHandler != null) { 637 fDocumentHandler.xmlDecl(version, encoding, standalone, augs); 638 } 639 } 640 641 public void doctypeDecl( 642 String rootElement, 643 String publicId, 644 String systemId, 645 Augmentations augs) 646 throws XNIException { 647 if (isRootDocument() && fDocumentHandler != null) { 648 fDocumentHandler.doctypeDecl(rootElement, publicId, systemId, augs); 649 } 650 } 651 652 public void comment(XMLString text, Augmentations augs) 653 throws XNIException { 654 if (!fInDTD) { 655 if (fDocumentHandler != null 656 && getState() == STATE_NORMAL_PROCESSING) { 657 fDepth++; 658 augs = modifyAugmentations(augs); 659 fDocumentHandler.comment(text, augs); 660 fDepth--; 661 } 662 } 663 else if (fDTDHandler != null) { 664 fDTDHandler.comment(text, augs); 665 } 666 } 667 668 public void processingInstruction( 669 String target, 670 XMLString data, 671 Augmentations augs) 672 throws XNIException { 673 if (!fInDTD) { 674 if (fDocumentHandler != null 675 && getState() == STATE_NORMAL_PROCESSING) { 676 fDepth++; 678 augs = modifyAugmentations(augs); 679 fDocumentHandler.processingInstruction(target, data, augs); 680 fDepth--; 681 } 682 } 683 else if (fDTDHandler != null) { 684 fDTDHandler.processingInstruction(target, data, augs); 685 } 686 } 687 688 public void startElement( 689 QName element, 690 XMLAttributes attributes, 691 Augmentations augs) 692 throws XNIException { 693 fDepth++; 694 setState(getState(fDepth - 1)); 695 processXMLBaseAttributes(attributes); 697 698 if (isIncludeElement(element)) { 699 boolean success = this.handleIncludeElement(attributes); 700 if (success) { 701 setState(STATE_IGNORE); 702 } 703 else { 704 setState(STATE_EXPECT_FALLBACK); 705 } 706 } 707 else if (isFallbackElement(element)) { 708 this.handleFallbackElement(); 709 } 710 else if ( 711 fDocumentHandler != null 712 && getState() == STATE_NORMAL_PROCESSING) { 713 augs = modifyAugmentations(augs); 714 attributes = processAttributes(attributes); 715 fDocumentHandler.startElement(element, attributes, augs); 716 } 717 } 718 719 public void emptyElement( 720 QName element, 721 XMLAttributes attributes, 722 Augmentations augs) 723 throws XNIException { 724 fDepth++; 725 setState(getState(fDepth - 1)); 726 727 processXMLBaseAttributes(attributes); 729 730 if (isIncludeElement(element)) { 731 boolean success = this.handleIncludeElement(attributes); 732 if (success) { 733 setState(STATE_IGNORE); 734 } 735 else { 736 reportFatalError("NoFallback"); 737 } 738 } 739 else if (isFallbackElement(element)) { 740 this.handleFallbackElement(); 741 } 742 else if (hasXIncludeNamespace(element)) { 743 if (getSawInclude(fDepth - 1)) { 744 reportFatalError( 745 "IncludeChild", 746 new Object [] { element.rawname }); 747 } 748 } 749 else if ( 750 fDocumentHandler != null 751 && getState() == STATE_NORMAL_PROCESSING) { 752 augs = modifyAugmentations(augs); 753 attributes = processAttributes(attributes); 754 fDocumentHandler.emptyElement(element, attributes, augs); 755 } 756 setSawFallback(fDepth + 1, false); 758 setSawInclude(fDepth + 1, false); 759 760 if (baseURIScope.size() > 0 && fDepth == baseURIScope.peek()) { 762 restoreBaseURI(); 764 } 765 fDepth--; 766 } 767 768 public void endElement(QName element, Augmentations augs) 769 throws XNIException { 770 771 if (isIncludeElement(element)) { 772 if (getState() == STATE_EXPECT_FALLBACK 775 && !getSawFallback(fDepth + 1)) { 776 reportFatalError("NoFallback"); 777 } 778 } 779 if (isFallbackElement(element)) { 780 if (getState() == STATE_NORMAL_PROCESSING) { 783 setState(STATE_IGNORE); 784 } 785 } 786 else if ( 787 fDocumentHandler != null 788 && getState() == STATE_NORMAL_PROCESSING) { 789 fDocumentHandler.endElement(element, augs); 790 } 791 792 setSawFallback(fDepth + 1, false); 794 setSawInclude(fDepth + 1, false); 795 796 if (baseURIScope.size() > 0 && fDepth == baseURIScope.peek()) { 798 restoreBaseURI(); 800 } 801 fDepth--; 802 } 803 804 public void startGeneralEntity( 805 String name, 806 XMLResourceIdentifier resId, 807 String encoding, 808 Augmentations augs) 809 throws XNIException { 810 if (fDocumentHandler != null 811 && getState() == STATE_NORMAL_PROCESSING) { 812 fDocumentHandler.startGeneralEntity(name, resId, encoding, augs); 813 } 814 } 815 816 public void textDecl(String version, String encoding, Augmentations augs) 817 throws XNIException { 818 if (fDocumentHandler != null 819 && getState() == STATE_NORMAL_PROCESSING) { 820 fDocumentHandler.textDecl(version, encoding, augs); 821 } 822 } 823 824 public void endGeneralEntity(String name, Augmentations augs) 825 throws XNIException { 826 if (fDocumentHandler != null 827 && getState() == STATE_NORMAL_PROCESSING) { 828 fDocumentHandler.endGeneralEntity(name, augs); 829 } 830 } 831 832 public void characters(XMLString text, Augmentations augs) 833 throws XNIException { 834 if (fDocumentHandler != null 835 && getState() == STATE_NORMAL_PROCESSING) { 836 fDepth++; 838 augs = modifyAugmentations(augs); 839 fDocumentHandler.characters(text, augs); 840 fDepth--; 841 } 842 } 843 844 public void ignorableWhitespace(XMLString text, Augmentations augs) 845 throws XNIException { 846 if (fDocumentHandler != null 847 && getState() == STATE_NORMAL_PROCESSING) { 848 fDocumentHandler.ignorableWhitespace(text, augs); 849 } 850 } 851 852 public void startCDATA(Augmentations augs) throws XNIException { 853 if (fDocumentHandler != null 854 && getState() == STATE_NORMAL_PROCESSING) { 855 fDocumentHandler.startCDATA(augs); 856 } 857 } 858 859 public void endCDATA(Augmentations augs) throws XNIException { 860 if (fDocumentHandler != null 861 && getState() == STATE_NORMAL_PROCESSING) { 862 fDocumentHandler.endCDATA(augs); 863 } 864 } 865 866 public void endDocument(Augmentations augs) throws XNIException { 867 if (isRootDocument() && fDocumentHandler != null) { 868 fDocumentHandler.endDocument(augs); 869 } 870 } 871 872 public void setDocumentSource(XMLDocumentSource source) { 873 fDocumentSource = source; 874 } 875 876 public XMLDocumentSource getDocumentSource() { 877 return fDocumentSource; 878 } 879 880 884 887 public void attributeDecl( 888 String elementName, 889 String attributeName, 890 String type, 891 String [] enumeration, 892 String defaultType, 893 XMLString defaultValue, 894 XMLString nonNormalizedDefaultValue, 895 Augmentations augmentations) 896 throws XNIException { 897 if (fDTDHandler != null) { 898 fDTDHandler.attributeDecl( 899 elementName, 900 attributeName, 901 type, 902 enumeration, 903 defaultType, 904 defaultValue, 905 nonNormalizedDefaultValue, 906 augmentations); 907 } 908 } 909 910 913 public void elementDecl( 914 String name, 915 String contentModel, 916 Augmentations augmentations) 917 throws XNIException { 918 if (fDTDHandler != null) { 919 fDTDHandler.elementDecl(name, contentModel, augmentations); 920 } 921 } 922 923 926 public void endAttlist(Augmentations augmentations) throws XNIException { 927 if (fDTDHandler != null) { 928 fDTDHandler.endAttlist(augmentations); 929 } 930 } 931 932 935 public void endConditional(Augmentations augmentations) 936 throws XNIException { 937 if (fDTDHandler != null) { 938 fDTDHandler.endConditional(augmentations); 939 } 940 } 941 942 945 public void endDTD(Augmentations augmentations) throws XNIException { 946 if (fDTDHandler != null) { 947 fDTDHandler.endDTD(augmentations); 948 } 949 fInDTD = false; 950 } 951 952 955 public void endExternalSubset(Augmentations augmentations) 956 throws XNIException { 957 if (fDTDHandler != null) { 958 fDTDHandler.endExternalSubset(augmentations); 959 } 960 } 961 962 965 public void endParameterEntity(String name, Augmentations augmentations) 966 throws XNIException { 967 if (fDTDHandler != null) { 968 fDTDHandler.endParameterEntity(name, augmentations); 969 } 970 } 971 972 975 public void externalEntityDecl( 976 String name, 977 XMLResourceIdentifier identifier, 978 Augmentations augmentations) 979 throws XNIException { 980 if (fDTDHandler != null) { 981 fDTDHandler.externalEntityDecl(name, identifier, augmentations); 982 } 983 } 984 985 988 public XMLDTDSource getDTDSource() { 989 return fDTDSource; 990 } 991 992 995 public void ignoredCharacters(XMLString text, Augmentations augmentations) 996 throws XNIException { 997 if (fDTDHandler != null) { 998 fDTDHandler.ignoredCharacters(text, augmentations); 999 } 1000 } 1001 1002 1005 public void internalEntityDecl( 1006 String name, 1007 XMLString text, 1008 XMLString nonNormalizedText, 1009 Augmentations augmentations) 1010 throws XNIException { 1011 if (fDTDHandler != null) { 1012 fDTDHandler.internalEntityDecl( 1013 name, 1014 text, 1015 nonNormalizedText, 1016 augmentations); 1017 } 1018 } 1019 1020 1023 public void notationDecl( 1024 String name, 1025 XMLResourceIdentifier identifier, 1026 Augmentations augmentations) 1027 throws XNIException { 1028 this.addNotation(name, identifier, augmentations); 1029 if (fDTDHandler != null) { 1030 fDTDHandler.notationDecl(name, identifier, augmentations); 1031 } 1032 } 1033 1034 1037 public void setDTDSource(XMLDTDSource source) { 1038 fDTDSource = source; 1039 } 1040 1041 1044 public void startAttlist(String elementName, Augmentations augmentations) 1045 throws XNIException { 1046 if (fDTDHandler != null) { 1047 fDTDHandler.startAttlist(elementName, augmentations); 1048 } 1049 } 1050 1051 1054 public void startConditional(short type, Augmentations augmentations) 1055 throws XNIException { 1056 if (fDTDHandler != null) { 1057 fDTDHandler.startConditional(type, augmentations); 1058 } 1059 } 1060 1061 1064 public void startDTD(XMLLocator locator, Augmentations augmentations) 1065 throws XNIException { 1066 fInDTD = true; 1067 if (fDTDHandler != null) { 1068 fDTDHandler.startDTD(locator, augmentations); 1069 } 1070 } 1071 1072 1075 public void startExternalSubset( 1076 XMLResourceIdentifier identifier, 1077 Augmentations augmentations) 1078 throws XNIException { 1079 if (fDTDHandler != null) { 1080 fDTDHandler.startExternalSubset(identifier, augmentations); 1081 } 1082 } 1083 1084 1087 public void startParameterEntity( 1088 String name, 1089 XMLResourceIdentifier identifier, 1090 String encoding, 1091 Augmentations augmentations) 1092 throws XNIException { 1093 if (fDTDHandler != null) { 1094 fDTDHandler.startParameterEntity( 1095 name, 1096 identifier, 1097 encoding, 1098 augmentations); 1099 } 1100 } 1101 1102 1105 public void unparsedEntityDecl( 1106 String name, 1107 XMLResourceIdentifier identifier, 1108 String notation, 1109 Augmentations augmentations) 1110 throws XNIException { 1111 this.addUnparsedEntity(name, identifier, notation, augmentations); 1112 if (fDTDHandler != null) { 1113 fDTDHandler.unparsedEntityDecl( 1114 name, 1115 identifier, 1116 notation, 1117 augmentations); 1118 } 1119 } 1120 1121 1124 public XMLDTDHandler getDTDHandler() { 1125 return fDTDHandler; 1126 } 1127 1128 1131 public void setDTDHandler(XMLDTDHandler handler) { 1132 fDTDHandler = handler; 1133 } 1134 1135 1137 private void setErrorReporter(XMLErrorReporter reporter) { 1138 fErrorReporter = reporter; 1139 if (fErrorReporter != null) { 1140 fErrorReporter.putMessageFormatter( 1141 XIncludeMessageFormatter.XINCLUDE_DOMAIN, 1142 new XIncludeMessageFormatter()); 1143 if (fDocLocation != null) { 1145 fErrorReporter.setDocumentLocator(fDocLocation); 1146 } 1147 } 1148 } 1149 1150 protected void handleFallbackElement() { 1151 setSawInclude(fDepth, false); 1152 fNamespaceContext.setContextInvalid(); 1153 if (!getSawInclude(fDepth - 1)) { 1154 reportFatalError("FallbackParent"); 1155 } 1156 1157 if (getSawFallback(fDepth)) { 1158 reportFatalError("MultipleFallbacks"); 1159 } 1160 else { 1161 setSawFallback(fDepth, true); 1162 } 1163 1164 if (getState() == STATE_EXPECT_FALLBACK) { 1168 setState(STATE_NORMAL_PROCESSING); 1169 } 1170 } 1171 1172 protected boolean handleIncludeElement(XMLAttributes attributes) 1173 throws XNIException { 1174 setSawInclude(fDepth, true); 1175 fNamespaceContext.setContextInvalid(); 1176 if (getSawInclude(fDepth - 1)) { 1177 reportFatalError("IncludeChild", new Object [] { XINCLUDE_INCLUDE }); 1178 } 1179 if (getState() == STATE_IGNORE) 1180 return true; 1181 1182 1188 String href = attributes.getValue(XINCLUDE_ATTR_HREF); 1189 String parse = attributes.getValue(XINCLUDE_ATTR_PARSE); 1190 String xpointerPart = attributes.getValue(XPOINTER); 1191 String accept = attributes.getValue(XINCLUDE_ATTR_ACCEPT); 1192 String acceptLanguage = attributes.getValue(XINCLUDE_ATTR_ACCEPT_LANGUAGE); 1193 1194 if (href == null && xpointerPart == null) { 1195 reportFatalError("XpointerMissing"); 1196 } 1197 if (parse == null) { 1198 parse = XINCLUDE_PARSE_XML; 1199 } 1200 1201 boolean xpointer = false; 1202 String parserName = null; 1203 1204 1206 if( href.indexOf("#")!=-1 ) 1207 href = href.substring(0,href.indexOf("#")); 1208 1209 if ( xpointerPart != null && parse.equals(XINCLUDE_PARSE_XML)) 1210 xpointer = true; 1211 1212 if (accept != null && !isValidInHTTPHeader(accept)) { 1215 reportFatalError("AcceptMalformed", null); 1216 } 1217 if (acceptLanguage != null && !isValidInHTTPHeader(acceptLanguage)) { 1218 reportFatalError("AcceptLanguageMalformed", null); 1219 } 1220 1221 XMLInputSource includedSource = null; 1222 if (fEntityResolver != null) { 1223 try { 1224 XMLResourceIdentifier resourceIdentifier = 1225 new XMLResourceIdentifierImpl( 1226 null, 1227 href, 1228 fCurrentBaseURI.getExpandedSystemId(), 1229 XMLEntityManager.expandSystemId( 1230 href, 1231 fCurrentBaseURI.getExpandedSystemId(), 1232 false)); 1233 1234 includedSource = 1235 fEntityResolver.resolveEntity(resourceIdentifier); 1236 } catch (IOException e) { 1237 reportResourceError( 1238 "XMLResourceError", 1239 new Object [] { href, e.getMessage()}); 1240 return false; 1241 } 1242 } 1243 1244 if (includedSource == null) { 1245 includedSource = 1246 new XIncludeInputSource( 1247 null, 1248 href, 1249 fCurrentBaseURI.getExpandedSystemId()); 1250 } 1251 1252 if(parse.equals(XINCLUDE_PARSE_XML) && xpointer ){ 1253 if(fXPointerFramework == null){ 1254 fXPointerFramework = new XPointerFramework(); 1255 fXPointerFramework.setXPointerSchema(fXPointerSchema); 1256 }else{ 1257 fXPointerFramework.reset(); 1258 fXPointerFramework.setXPointerSchema(fXPointerSchema); 1259 } 1260 parserName = XPOINTER_DEFAULT_CONFIGURATION; 1261 fChildConfig = createXPointerParser(); 1262 fXPointerFramework.setSchemaPointer(xpointerPart); 1263 1269 XPointerSchema fXPointerSchemaS; 1271 1272 if((fXPointerSchemaS = fXPointerFramework.getNextXPointerSchema()) == null && fXPointerFramework.getSchemaCount() == 0){ 1273 fNamespaceContext.pushScope(); 1274 fXPointerSchemaS = fXPointerFramework.getDefaultSchema(); 1275 fXPointerSchemaS.setXPointerSchemaPointer(xpointerPart); 1276 processSchema(fXPointerSchemaS,includedSource); 1277 1283 }else { 1284 try { 1285 fNamespaceContext.pushScope(); 1286 if(fXPointerSchemaS != null && processSchema(fXPointerSchemaS,includedSource)){ 1287 ; 1288 }else{ 1289 for (int i=fXPointerFramework.getCurrentPointer(); i <=fXPointerFramework.getSchemaCount();i++){ 1290 fXPointerSchemaS = fXPointerFramework.getNextXPointerSchema(); 1291 if(fXPointerSchemaS != null && processSchema(fXPointerSchemaS,includedSource)){ 1292 break; 1293 } 1294 } 1295 } 1296 } 1297 catch (XNIException e) { 1298 reportFatalError("XMLParseError"); 1299 } 1300 finally { 1301 fNamespaceContext.popScope(); 1302 } 1303 } 1304 1305 } 1306 else if (parse.equals(XINCLUDE_PARSE_XML)) { 1307 parserName = XINCLUDE_DEFAULT_CONFIGURATION; 1310 1311 fChildConfig = 1312 (XMLParserConfiguration)ObjectFactory.newInstance( 1313 parserName, 1314 ObjectFactory.findClassLoader(), 1315 true); 1316 if (fErrorReporter != null) fChildConfig.setProperty(ERROR_REPORTER, fErrorReporter); 1318 if (fEntityResolver != null) fChildConfig.setProperty(ENTITY_RESOLVER, fEntityResolver); 1319 if (fSecurityManager != null) fChildConfig.setProperty(SECURITY_MANAGER, fSecurityManager); 1320 1321 fChildConfig.setProperty( 1323 Constants.XERCES_PROPERTY_PREFIX 1324 + Constants.NAMESPACE_CONTEXT_PROPERTY, 1325 fNamespaceContext); 1326 1327 XIncludeHandler newHandler = 1328 (XIncludeHandler)fChildConfig.getProperty( 1329 Constants.XERCES_PROPERTY_PREFIX 1330 + Constants.XINCLUDE_HANDLER_PROPERTY); 1331 newHandler.setParent(this); 1332 newHandler.setDocumentHandler(this.getDocumentHandler()); 1333 1335 copyFeatures(fSettings, fChildConfig); 1337 fChildConfig.setFeature( 1340 Constants.XERCES_FEATURE_PREFIX 1341 + Constants.SCHEMA_VALIDATION_FEATURE, 1342 false); 1343 fChildConfig.setFeature( 1344 Constants.XERCES_FEATURE_PREFIX 1345 + Constants.XINCLUDE_AWARE, 1346 true); 1347 1348 try { 1349 fNamespaceContext.pushScope(); 1350 includedSource = setHttpProperties(includedSource,attributes); 1351 fChildConfig.parse(includedSource); 1352 if (fErrorReporter != null) { 1354 fErrorReporter.setDocumentLocator(fDocLocation); 1355 } 1356 } 1357 catch (XNIException e) { 1358 if (fErrorReporter != null) { 1360 fErrorReporter.setDocumentLocator(fDocLocation); 1361 } 1362 reportFatalError("XMLParseError", new Object [] { href }); 1363 } 1364 catch (IOException e) { 1365 if (fErrorReporter != null) { 1367 fErrorReporter.setDocumentLocator(fDocLocation); 1368 } 1369 reportResourceError( 1373 "XMLResourceError", 1374 new Object [] { href, e.getMessage()}); 1375 return false; 1376 } 1377 finally { 1378 fNamespaceContext.popScope(); 1379 } 1380 } 1381 else if (parse.equals(XINCLUDE_PARSE_TEXT)) { 1382 String encoding = attributes.getValue(XINCLUDE_ATTR_ENCODING); 1384 includedSource.setEncoding(encoding); 1385 1386 XIncludeTextReader reader = null; 1387 try { 1388 if (fIsXML11) { 1389 reader = new XInclude11TextReader(includedSource, this); 1390 } 1391 else { 1392 reader = new XIncludeTextReader(includedSource, this); 1393 } 1394 if (includedSource.getCharacterStream() == null 1395 && includedSource.getByteStream() == null) { 1396 reader.setHttpProperties(accept, acceptLanguage); 1397 } 1398 reader.setErrorReporter(fErrorReporter); 1399 reader.parse(); 1400 } 1401 catch (MalformedByteSequenceException ex) { 1403 fErrorReporter.reportError(ex.getDomain(), ex.getKey(), 1404 ex.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR); 1405 } 1406 catch (CharConversionException e) { 1407 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 1408 "CharConversionFailure", null, XMLErrorReporter.SEVERITY_FATAL_ERROR); 1409 } 1410 catch (IOException e) { 1411 reportResourceError( 1412 "TextResourceError", 1413 new Object [] { href, e.getMessage()}); 1414 return false; 1415 } 1416 finally { 1417 if (reader != null) { 1418 try { 1419 reader.close(); 1420 } 1421 catch (IOException e) { 1422 reportResourceError( 1423 "TextResourceError", 1424 new Object [] { href, e.getMessage()}); 1425 return false; 1426 } 1427 } 1428 } 1429 } 1430 else { 1431 reportFatalError("InvalidParseValue", new Object [] { parse }); 1432 } 1433 return true; 1434 } 1435 1436 1441 protected boolean hasXIncludeNamespace(QName element) { 1442 return element.uri == XINCLUDE_NS_URI 1446 || fNamespaceContext.getURI(element.prefix) == XINCLUDE_NS_URI; 1447 } 1448 1449 1457 protected boolean isIncludeElement(QName element) { 1458 return element.localpart.equals(XINCLUDE_INCLUDE) && 1459 hasXIncludeNamespace(element); 1460 } 1461 1462 1470 protected boolean isFallbackElement(QName element) { 1471 return element.localpart.equals(XINCLUDE_FALLBACK) && 1472 hasXIncludeNamespace(element); 1473 } 1474 1475 1484 protected boolean sameBaseURIAsIncludeParent() { 1485 String parentBaseURI = getIncludeParentBaseURI(); 1486 String baseURI = fCurrentBaseURI.getExpandedSystemId(); 1487 return parentBaseURI != null && parentBaseURI.equals(baseURI); 1496 } 1497 1498 1504 protected boolean searchForRecursiveIncludes(XMLLocator includedSource) { 1505 String includedSystemId = includedSource.getExpandedSystemId(); 1506 1507 if (includedSystemId == null) { 1508 try { 1509 includedSystemId = 1510 XMLEntityManager.expandSystemId( 1511 includedSource.getLiteralSystemId(), 1512 includedSource.getBaseSystemId(), 1513 false); 1514 } 1515 catch (MalformedURIException e) { 1516 reportFatalError("ExpandedSystemId"); 1517 } 1518 } 1519 1520 if (includedSystemId.equals(fCurrentBaseURI.getExpandedSystemId())) { 1521 return true; 1522 } 1523 1524 if (fParentXIncludeHandler == null) { 1525 return false; 1526 } 1527 return fParentXIncludeHandler.searchForRecursiveIncludes( 1528 includedSource); 1529 } 1530 1531 1537 protected boolean isTopLevelIncludedItem() { 1538 return isTopLevelIncludedItemViaInclude() 1539 || isTopLevelIncludedItemViaFallback(); 1540 } 1541 1542 protected boolean isTopLevelIncludedItemViaInclude() { 1543 return fDepth == 1 && !isRootDocument(); 1544 } 1545 1546 protected boolean isTopLevelIncludedItemViaFallback() { 1547 return getSawFallback(fDepth - 1); 1552 } 1553 1554 1568 protected XMLAttributes processAttributes(XMLAttributes attributes) { 1569 if (isTopLevelIncludedItem()) { 1570 if (!sameBaseURIAsIncludeParent()) { 1574 if (attributes == null) { 1575 attributes = new XMLAttributesImpl(); 1576 } 1577 1578 String uri = null; 1582 try { 1583 uri = this.getRelativeBaseURI(); 1584 } 1585 catch (MalformedURIException e) { 1586 uri = fCurrentBaseURI.getExpandedSystemId(); 1589 } 1590 int index = 1591 attributes.addAttribute( 1592 XML_BASE_QNAME, 1593 XMLSymbols.fCDATASymbol, 1594 uri); 1595 attributes.setSpecified(index, true); 1596 } 1597 1598 Enumeration inscopeNS = fNamespaceContext.getAllPrefixes(); 1600 while (inscopeNS.hasMoreElements()) { 1601 String prefix = (String )inscopeNS.nextElement(); 1602 String parentURI = 1603 fNamespaceContext.getURIFromIncludeParent(prefix); 1604 String uri = fNamespaceContext.getURI(prefix); 1605 if (parentURI != uri && attributes != null) { 1606 if (prefix == XMLSymbols.EMPTY_STRING) { 1607 if (attributes 1608 .getValue( 1609 NamespaceContext.XMLNS_URI, 1610 XMLSymbols.PREFIX_XMLNS) 1611 == null) { 1612 if (attributes == null) { 1613 attributes = new XMLAttributesImpl(); 1614 } 1615 1616 QName ns = (QName)NEW_NS_ATTR_QNAME.clone(); 1617 ns.localpart = XMLSymbols.PREFIX_XMLNS; 1618 ns.rawname = XMLSymbols.PREFIX_XMLNS; 1619 attributes.addAttribute( 1620 ns, 1621 XMLSymbols.fCDATASymbol, 1622 uri); 1623 } 1624 } 1625 else if ( 1626 attributes.getValue(NamespaceContext.XMLNS_URI, prefix) 1627 == null) { 1628 if (attributes == null) { 1629 attributes = new XMLAttributesImpl(); 1630 } 1631 1632 QName ns = (QName)NEW_NS_ATTR_QNAME.clone(); 1633 ns.localpart = prefix; 1634 ns.rawname += prefix; 1635 attributes.addAttribute( 1636 ns, 1637 XMLSymbols.fCDATASymbol, 1638 uri); 1639 } 1640 } 1641 } 1642 } 1643 1644 if (attributes != null) { 1645 int length = attributes.getLength(); 1646 for (int i = 0; i < length; i++) { 1647 String type = attributes.getType(i); 1648 String value = attributes.getValue(i); 1649 if (type == XMLSymbols.fENTITYSymbol) { 1650 this.checkUnparsedEntity(value); 1651 } 1652 if (type == XMLSymbols.fENTITIESSymbol) { 1653 StringTokenizer st = new StringTokenizer (value); 1655 while (st.hasMoreTokens()) { 1656 String entName = st.nextToken(); 1657 this.checkUnparsedEntity(entName); 1658 } 1659 } 1660 else if (type == XMLSymbols.fNOTATIONSymbol) { 1661 this.checkNotation(value); 1663 } 1664 1670 } 1671 } 1672 1673 return attributes; 1674 } 1675 1676 1682 protected String getRelativeBaseURI() throws MalformedURIException { 1683 int includeParentDepth = getIncludeParentDepth(); 1684 String relativeURI = this.getRelativeURI(includeParentDepth); 1685 if (isRootDocument()) { 1686 return relativeURI; 1687 } 1688 else { 1689 if (relativeURI.equals("")) { 1690 relativeURI = fCurrentBaseURI.getLiteralSystemId(); 1691 } 1692 1693 if (includeParentDepth == 0) { 1694 if (fParentRelativeURI == null) { 1695 fParentRelativeURI = 1696 fParentXIncludeHandler.getRelativeBaseURI(); 1697 } 1698 if (fParentRelativeURI.equals("")) { 1699 return relativeURI; 1700 } 1701 URI uri = new URI("file", fParentRelativeURI); 1702 uri = new URI(uri, relativeURI); 1703 return uri.getPath(); 1704 } 1705 else { 1706 return relativeURI; 1707 } 1708 } 1709 } 1710 1711 1715 private String getIncludeParentBaseURI() { 1716 int depth = getIncludeParentDepth(); 1717 if (!isRootDocument() && depth == 0) { 1718 return fParentXIncludeHandler.getIncludeParentBaseURI(); 1719 } 1720 else { 1721 return this.getBaseURI(depth); 1722 } 1723 } 1724 1725 1733 private int getIncludeParentDepth() { 1734 for (int i = fDepth - 1; i >= 0; i--) { 1737 if (!getSawInclude(i) && !getSawFallback(i)) { 1744 return i; 1745 } 1746 } 1747 return 0; 1750 } 1751 1752 1758 protected Augmentations modifyAugmentations(Augmentations augs) { 1759 return modifyAugmentations(augs, false); 1760 } 1761 1762 1769 protected Augmentations modifyAugmentations( 1770 Augmentations augs, 1771 boolean force) { 1772 if (force || isTopLevelIncludedItem()) { 1773 if (augs == null) { 1774 augs = new AugmentationsImpl(); 1775 } 1776 augs.putItem(XINCLUDE_INCLUDED, Boolean.TRUE); 1777 } 1778 return augs; 1779 } 1780 1781 protected int getState(int depth) { 1782 return fState[depth]; 1783 } 1784 1785 protected int getState() { 1786 return fState[fDepth]; 1787 } 1788 1789 protected void setState(int state) { 1790 if (fDepth >= fState.length) { 1791 int[] newarray = new int[fDepth * 2]; 1792 System.arraycopy(fState, 0, newarray, 0, fState.length); 1793 fState = newarray; 1794 } 1795 fState[fDepth] = state; 1796 } 1797 1798 1806 protected void setSawFallback(int depth, boolean val) { 1807 if (depth >= fSawFallback.length) { 1808 boolean[] newarray = new boolean[depth * 2]; 1809 System.arraycopy(fSawFallback, 0, newarray, 0, fSawFallback.length); 1810 fSawFallback = newarray; 1811 } 1812 fSawFallback[depth] = val; 1813 } 1814 1815 1822 protected boolean getSawFallback(int depth) { 1823 if (depth >= fSawFallback.length) { 1824 return false; 1825 } 1826 return fSawFallback[depth]; 1827 } 1828 1829 1836 protected void setSawInclude(int depth, boolean val) { 1837 if (depth >= fSawInclude.length) { 1838 boolean[] newarray = new boolean[depth * 2]; 1839 System.arraycopy(fSawInclude, 0, newarray, 0, fSawInclude.length); 1840 fSawInclude = newarray; 1841 } 1842 fSawInclude[depth] = val; 1843 } 1844 1845 1852 protected boolean getSawInclude(int depth) { 1853 if (depth >= fSawInclude.length) { 1854 return false; 1855 } 1856 return fSawInclude[depth]; 1857 } 1858 1859 protected void reportResourceError(String key) { 1860 this.reportFatalError(key, null); 1861 } 1862 1863 protected void reportResourceError(String key, Object [] args) { 1864 this.reportError(key, args, XMLErrorReporter.SEVERITY_WARNING); 1865 } 1866 1867 protected void reportFatalError(String key) { 1868 this.reportFatalError(key, null); 1869 } 1870 1871 protected void reportFatalError(String key, Object [] args) { 1872 this.reportError(key, args, XMLErrorReporter.SEVERITY_FATAL_ERROR); 1873 } 1874 1875 private void reportError(String key, Object [] args, short severity) { 1876 if (fErrorReporter != null) { 1877 fErrorReporter.reportError( 1878 XIncludeMessageFormatter.XINCLUDE_DOMAIN, 1879 key, 1880 args, 1881 severity); 1882 } 1883 } 1886 1887 1891 protected void setParent(XIncludeHandler parent) { 1892 fParentXIncludeHandler = parent; 1893 } 1894 1895 protected boolean isRootDocument() { 1897 return fParentXIncludeHandler == null; 1898 } 1899 1900 1906 protected void addUnparsedEntity( 1907 String name, 1908 XMLResourceIdentifier identifier, 1909 String notation, 1910 Augmentations augmentations) { 1911 UnparsedEntity ent = new UnparsedEntity(); 1912 ent.name = name; 1913 ent.systemId = identifier.getLiteralSystemId(); 1914 ent.publicId = identifier.getPublicId(); 1915 ent.baseURI = identifier.getBaseSystemId(); 1916 ent.notation = notation; 1917 ent.augmentations = augmentations; 1918 fUnparsedEntities.add(ent); 1919 } 1920 1921 1927 protected void addNotation( 1928 String name, 1929 XMLResourceIdentifier identifier, 1930 Augmentations augmentations) { 1931 Notation not = new Notation(); 1932 not.name = name; 1933 not.systemId = identifier.getLiteralSystemId(); 1934 not.publicId = identifier.getPublicId(); 1935 not.baseURI = identifier.getBaseSystemId(); 1936 not.augmentations = augmentations; 1937 fNotations.add(not); 1938 } 1939 1940 1948 protected void checkUnparsedEntity(String entName) { 1949 UnparsedEntity ent = new UnparsedEntity(); 1950 ent.name = entName; 1951 int index = fUnparsedEntities.indexOf(ent); 1952 if (index != -1) { 1953 ent = (UnparsedEntity)fUnparsedEntities.get(index); 1954 checkNotation(ent.notation); 1956 checkAndSendUnparsedEntity(ent); 1957 } 1958 } 1959 1960 1967 protected void checkNotation(String notName) { 1968 Notation not = new Notation(); 1969 not.name = notName; 1970 int index = fNotations.indexOf(not); 1971 if (index != -1) { 1972 not = (Notation)fNotations.get(index); 1973 checkAndSendNotation(not); 1974 } 1975 } 1976 1977 1984 protected void checkAndSendUnparsedEntity(UnparsedEntity ent) { 1985 if (isRootDocument()) { 1986 int index = fUnparsedEntities.indexOf(ent); 1987 if (index == -1) { 1988 XMLResourceIdentifier id = 1992 new XMLResourceIdentifierImpl( 1993 ent.publicId, 1994 ent.systemId, 1995 ent.baseURI, 1996 null); 1997 this.addUnparsedEntity( 1998 ent.name, 1999 id, 2000 ent.notation, 2001 ent.augmentations); 2002 if (fSendUEAndNotationEvents && fDTDHandler != null) { 2003 fDTDHandler.unparsedEntityDecl( 2004 ent.name, 2005 id, 2006 ent.notation, 2007 ent.augmentations); 2008 } 2009 } 2010 else { 2011 UnparsedEntity localEntity = 2012 (UnparsedEntity)fUnparsedEntities.get(index); 2013 if (!ent.isDuplicate(localEntity)) { 2014 reportFatalError( 2015 "NonDuplicateUnparsedEntity", 2016 new Object [] { ent.name }); 2017 } 2018 } 2019 } 2020 else { 2021 fParentXIncludeHandler.checkAndSendUnparsedEntity(ent); 2022 } 2023 } 2024 2025 2032 protected void checkAndSendNotation(Notation not) { 2033 if (isRootDocument()) { 2034 int index = fNotations.indexOf(not); 2035 if (index == -1) { 2036 XMLResourceIdentifier id = 2038 new XMLResourceIdentifierImpl( 2039 not.publicId, 2040 not.systemId, 2041 not.baseURI, 2042 null); 2043 this.addNotation(not.name, id, not.augmentations); 2044 if (fSendUEAndNotationEvents && fDTDHandler != null) { 2045 fDTDHandler.notationDecl(not.name, id, not.augmentations); 2046 } 2047 } 2048 else { 2049 Notation localNotation = (Notation)fNotations.get(index); 2050 if (!not.isDuplicate(localNotation)) { 2051 reportFatalError( 2052 "NonDuplicateNotation", 2053 new Object [] { not.name }); 2054 } 2055 } 2056 } 2057 else { 2058 fParentXIncludeHandler.checkAndSendNotation(not); 2059 } 2060 } 2061 2062 protected void copyFeatures( 2065 XMLComponentManager from, 2066 ParserConfigurationSettings to) { 2067 Enumeration features = Constants.getXercesFeatures(); 2068 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); 2069 features = Constants.getSAXFeatures(); 2070 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); 2071 } 2072 2073 protected void copyFeatures( 2074 XMLComponentManager from, 2075 XMLParserConfiguration to) { 2076 Enumeration features = Constants.getXercesFeatures(); 2077 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); 2078 features = Constants.getSAXFeatures(); 2079 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); 2080 } 2081 2082 private void copyFeatures1( 2083 Enumeration features, 2084 String featurePrefix, 2085 XMLComponentManager from, 2086 ParserConfigurationSettings to) { 2087 while (features.hasMoreElements()) { 2088 String featureId = featurePrefix + (String )features.nextElement(); 2089 2090 to.addRecognizedFeatures(new String [] { featureId }); 2091 2092 try { 2093 to.setFeature(featureId, from.getFeature(featureId)); 2094 } 2095 catch (XMLConfigurationException e) { 2096 } 2099 } 2100 } 2101 2102 private void copyFeatures1( 2103 Enumeration features, 2104 String featurePrefix, 2105 XMLComponentManager from, 2106 XMLParserConfiguration to) { 2107 while (features.hasMoreElements()) { 2108 String featureId = featurePrefix + (String )features.nextElement(); 2109 boolean value = from.getFeature(featureId); 2110 2111 try { 2112 to.setFeature(featureId, value); 2113 } 2114 catch (XMLConfigurationException e) { 2115 } 2118 } 2119 } 2120 2121 protected class Notation { 2124 public String name; 2125 public String systemId; 2126 public String baseURI; 2127 public String publicId; 2128 public Augmentations augmentations; 2129 2130 public boolean equals(Object obj) { 2133 if (obj == null) { 2134 return false; 2135 } 2136 if (obj instanceof Notation) { 2137 Notation other = (Notation)obj; 2138 return name.equals(other.name); 2139 } 2140 return false; 2141 } 2142 2143 public boolean isDuplicate(Object obj) { 2148 if (obj != null && obj instanceof Notation) { 2149 Notation other = (Notation)obj; 2150 return name.equals(other.name) 2151 && (systemId == other.systemId 2152 || (systemId != null && systemId.equals(other.systemId))) 2153 && (publicId == other.publicId 2154 || (publicId != null && publicId.equals(other.publicId))) 2155 && (baseURI == other.baseURI 2156 || (baseURI != null && baseURI.equals(other.baseURI))); 2157 } 2158 return false; 2159 } 2160 } 2161 2162 protected class UnparsedEntity { 2165 public String name; 2166 public String systemId; 2167 public String baseURI; 2168 public String publicId; 2169 public String notation; 2170 public Augmentations augmentations; 2171 2172 public boolean equals(Object obj) { 2175 if (obj == null) { 2176 return false; 2177 } 2178 if (obj instanceof UnparsedEntity) { 2179 UnparsedEntity other = (UnparsedEntity)obj; 2180 return name.equals(other.name); 2181 } 2182 return false; 2183 } 2184 2185 public boolean isDuplicate(Object obj) { 2190 if (obj != null && obj instanceof UnparsedEntity) { 2191 UnparsedEntity other = (UnparsedEntity)obj; 2192 return name.equals(other.name) 2193 && (systemId == other.systemId 2194 || (systemId != null && systemId.equals(other.systemId))) 2195 && (publicId == other.publicId 2196 || (publicId != null && publicId.equals(other.publicId))) 2197 && (baseURI == other.baseURI 2198 || (baseURI != null && baseURI.equals(other.baseURI))) 2199 && (notation == other.notation 2200 || (notation != null && notation.equals(other.notation))); 2201 } 2202 return false; 2203 } 2204 } 2205 2206 2208 2211 protected void saveBaseURI() { 2212 baseURIScope.push(fDepth); 2213 baseURI.push(fCurrentBaseURI.getBaseSystemId()); 2214 literalSystemID.push(fCurrentBaseURI.getLiteralSystemId()); 2215 expandedSystemID.push(fCurrentBaseURI.getExpandedSystemId()); 2216 } 2217 2218 2221 protected void restoreBaseURI() { 2222 baseURI.pop(); 2223 literalSystemID.pop(); 2224 expandedSystemID.pop(); 2225 baseURIScope.pop(); 2226 fCurrentBaseURI.setBaseSystemId((String )baseURI.peek()); 2227 fCurrentBaseURI.setLiteralSystemId((String )literalSystemID.peek()); 2228 fCurrentBaseURI.setExpandedSystemId((String )expandedSystemID.peek()); 2229 } 2230 2231 2236 public String getBaseURI(int depth) { 2237 int scope = scopeOf(depth); 2238 return (String )expandedSystemID.elementAt(scope); 2239 } 2240 2241 2249 public String getRelativeURI(int depth) throws MalformedURIException { 2250 int start = scopeOf(depth) + 1; 2254 if (start == baseURIScope.size()) { 2255 return ""; 2257 } 2258 URI uri = new URI("file", (String )literalSystemID.elementAt(start)); 2259 for (int i = start + 1; i < baseURIScope.size(); i++) { 2260 uri = new URI(uri, (String )literalSystemID.elementAt(i)); 2261 } 2262 return uri.getPath(); 2263 } 2264 2265 private int scopeOf(int depth) { 2269 for (int i = baseURIScope.size() - 1; i >= 0; i--) { 2270 if (baseURIScope.elementAt(i) <= depth) 2271 return i; 2272 } 2273 2274 return -1; 2276 } 2277 2278 2282 protected void processXMLBaseAttributes(XMLAttributes attributes) { 2283 String baseURIValue = 2284 attributes.getValue(NamespaceContext.XML_URI, "base"); 2285 if (baseURIValue != null) { 2286 try { 2287 String expandedValue = 2288 XMLEntityManager.expandSystemId( 2289 baseURIValue, 2290 fCurrentBaseURI.getExpandedSystemId(), 2291 false); 2292 fCurrentBaseURI.setLiteralSystemId(baseURIValue); 2293 fCurrentBaseURI.setBaseSystemId( 2294 fCurrentBaseURI.getExpandedSystemId()); 2295 fCurrentBaseURI.setExpandedSystemId(expandedValue); 2296 2297 saveBaseURI(); 2299 } 2300 catch (MalformedURIException e) { 2301 } 2303 } 2304 } 2305 2306 2309 protected XMLInputSource setHttpProperties(XMLInputSource source,XMLAttributes attributes) throws IOException { 2310 String httpAcceptLang = attributes.getValue(HTTP_ACCEPT_LANGUAGE); 2311 String httpAccept = attributes.getValue(HTTP_ACCEPT); 2312 String httpAcceptchar = attributes.getValue(HTTP_ACCEPT_CHARSET); 2313 if (source.getCharacterStream() == null && source.getByteStream() == null) { 2314 XIncludeInputSource includeSource = new XIncludeInputSource(source.getPublicId(),source.getSystemId(),source.getBaseSystemId(), source.getByteStream(),source.getEncoding()); 2315 includeSource.setProperty(XINCLUDE_ATTR_ACCEPT,attributes.getValue(XINCLUDE_ATTR_ACCEPT)); 2316 includeSource.setProperty(XINCLUDE_ATTR_ACCEPT_CHARSET,attributes.getValue(XINCLUDE_ATTR_ACCEPT_CHARSET)); 2317 includeSource.setProperty(XINCLUDE_ATTR_ACCEPT_LANGUAGE,attributes.getValue(XINCLUDE_ATTR_ACCEPT_LANGUAGE)); 2318 } 2319 return source; 2320 } 2321 2322 public boolean processSchema(XPointerSchema fXPointerSchemaS, 2323 XMLInputSource includedSource){ 2324 try{ 2325 fChildConfig = createXPointerParser(); fChildConfig.setProperty(Constants.XERCES_PROPERTY_PREFIX 2327 + Constants.XINCLUDE_HANDLER_PROPERTY, 2328 fXPointerSchemaS); 2329 fXPointerSchemaS.setParent(this); 2330 fXPointerSchemaS.setDocumentHandler(this.getDocumentHandler()); 2331 fChildConfig.parse(includedSource); 2332 } 2333 catch (Exception e) { 2334 reportResourceError( 2336 "XMLResourceError", 2337 new Object [] { null, e.getMessage()}); 2338 } 2339 return fXPointerSchemaS.isSubResourceIndentified(); 2340 } 2341 2342 2350 private boolean isValidInHTTPHeader(String value) { 2351 char ch; 2352 for (int i = value.length() - 1; i >= 0; --i) { 2353 ch = value.charAt(i); 2354 if (ch < 0x20 || ch > 0x7E) { 2355 return false; 2356 } 2357 } 2358 return true; 2359 } 2360 2361 protected XMLParserConfiguration createXPointerParser(){ 2362 XMLParserConfiguration childConfig = 2363 (XMLParserConfiguration)ObjectFactory.newInstance( 2364 XPOINTER_DEFAULT_CONFIGURATION, 2365 ObjectFactory.findClassLoader(), 2366 true); 2367 childConfig.setProperty(ERROR_REPORTER, fErrorReporter); 2368 childConfig.setProperty( 2369 Constants.XERCES_PROPERTY_PREFIX 2370 + Constants.NAMESPACE_CONTEXT_PROPERTY, 2371 fNamespaceContext); 2372 copyFeatures(fSettings, childConfig); 2373 return childConfig; 2374 } 2375} 2376 | Popular Tags |