jaxp/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java
changeset 39907 db51759e3695
parent 39799 2847de5336f2
child 40582 1dddef49982c
equal deleted inserted replaced
39906:230d872f56ea 39907:db51759e3695
    51 import java.util.Iterator;
    51 import java.util.Iterator;
    52 import java.util.Locale;
    52 import java.util.Locale;
    53 import java.util.Map;
    53 import java.util.Map;
    54 import java.util.Stack;
    54 import java.util.Stack;
    55 import java.util.StringTokenizer;
    55 import java.util.StringTokenizer;
       
    56 import javax.xml.XMLConstants;
       
    57 import javax.xml.catalog.CatalogException;
       
    58 import javax.xml.catalog.CatalogFeatures;
       
    59 import javax.xml.catalog.CatalogFeatures.Feature;
       
    60 import javax.xml.catalog.CatalogManager;
       
    61 import javax.xml.catalog.CatalogResolver;
       
    62 import javax.xml.catalog.CatalogUriResolver;
    56 import javax.xml.stream.XMLInputFactory;
    63 import javax.xml.stream.XMLInputFactory;
       
    64 import javax.xml.transform.Source;
       
    65 import jdk.xml.internal.JdkXmlUtils;
       
    66 import org.xml.sax.InputSource;
    57 
    67 
    58 
    68 
    59 /**
    69 /**
    60  * Will keep track of current entity.
    70  * Will keep track of current entity.
    61  *
    71  *
   182                 VALIDATION,
   192                 VALIDATION,
   183                 EXTERNAL_GENERAL_ENTITIES,
   193                 EXTERNAL_GENERAL_ENTITIES,
   184                 EXTERNAL_PARAMETER_ENTITIES,
   194                 EXTERNAL_PARAMETER_ENTITIES,
   185                 ALLOW_JAVA_ENCODINGS,
   195                 ALLOW_JAVA_ENCODINGS,
   186                 WARN_ON_DUPLICATE_ENTITYDEF,
   196                 WARN_ON_DUPLICATE_ENTITYDEF,
   187                 STANDARD_URI_CONFORMANT
   197                 STANDARD_URI_CONFORMANT,
       
   198                 XMLConstants.USE_CATALOG
   188     };
   199     };
   189 
   200 
   190     /** Feature defaults. */
   201     /** Feature defaults. */
   191     private static final Boolean[] FEATURE_DEFAULTS = {
   202     private static final Boolean[] FEATURE_DEFAULTS = {
   192                 null,
   203                 null,
   193                 Boolean.TRUE,
   204                 Boolean.TRUE,
   194                 Boolean.TRUE,
   205                 Boolean.TRUE,
   195                 Boolean.TRUE,
   206                 Boolean.TRUE,
   196                 Boolean.FALSE,
   207                 Boolean.FALSE,
   197                 Boolean.FALSE
   208                 Boolean.FALSE,
       
   209                 JdkXmlUtils.USE_CATALOG_DEFAULT
   198     };
   210     };
   199 
   211 
   200     /** Recognized properties. */
   212     /** Recognized properties. */
   201     private static final String[] RECOGNIZED_PROPERTIES = {
   213     private static final String[] RECOGNIZED_PROPERTIES = {
   202                 SYMBOL_TABLE,
   214                 SYMBOL_TABLE,
   203                 ERROR_REPORTER,
   215                 ERROR_REPORTER,
   204                 ENTITY_RESOLVER,
   216                 ENTITY_RESOLVER,
   205                 VALIDATION_MANAGER,
   217                 VALIDATION_MANAGER,
   206                 BUFFER_SIZE,
   218                 BUFFER_SIZE,
   207                 SECURITY_MANAGER,
   219                 SECURITY_MANAGER,
   208                 XML_SECURITY_PROPERTY_MANAGER
   220                 XML_SECURITY_PROPERTY_MANAGER,
       
   221                 JdkXmlUtils.CATALOG_DEFER,
       
   222                 JdkXmlUtils.CATALOG_FILES,
       
   223                 JdkXmlUtils.CATALOG_PREFER,
       
   224                 JdkXmlUtils.CATALOG_RESOLVE
   209     };
   225     };
   210 
   226 
   211     /** Property defaults. */
   227     /** Property defaults. */
   212     private static final Object[] PROPERTY_DEFAULTS = {
   228     private static final Object[] PROPERTY_DEFAULTS = {
   213                 null,
   229                 null,
   214                 null,
   230                 null,
   215                 null,
   231                 null,
   216                 null,
   232                 null,
   217                 new Integer(DEFAULT_BUFFER_SIZE),
   233                 DEFAULT_BUFFER_SIZE,
       
   234                 null,
       
   235                 null,
       
   236                 null,
       
   237                 null,
   218                 null,
   238                 null,
   219                 null
   239                 null
   220     };
   240     };
   221 
   241 
   222     private static final String XMLEntity = "[xml]".intern();
   242     private static final String XMLEntity = "[xml]".intern();
   393     /** Augmentations for entities. */
   413     /** Augmentations for entities. */
   394     private final Augmentations fEntityAugs = new AugmentationsImpl();
   414     private final Augmentations fEntityAugs = new AugmentationsImpl();
   395 
   415 
   396     /** Pool of character buffers. */
   416     /** Pool of character buffers. */
   397     private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE);
   417     private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE);
       
   418 
       
   419     /** indicate whether Catalog should be used for resolving external resources */
       
   420     private boolean fUseCatalog = true;
       
   421     CatalogFeatures fCatalogFeatures;
       
   422     CatalogResolver fCatalogResolver;
       
   423     CatalogUriResolver fCatalogUriResolver;
       
   424 
       
   425     private String fCatalogFile;
       
   426     private String fDefer;
       
   427     private String fPrefer;
       
   428     private String fResolve;
   398 
   429 
   399     //
   430     //
   400     // Constructors
   431     // Constructors
   401     //
   432     //
   402 
   433 
  1005         if(xmlInputSource != null){
  1036         if(xmlInputSource != null){
  1006             //wrap this XMLInputSource to StaxInputSource
  1037             //wrap this XMLInputSource to StaxInputSource
  1007             staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
  1038             staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
  1008         }
  1039         }
  1009 
  1040 
       
  1041         if (staxInputSource == null) {
       
  1042             if (fCatalogFeatures == null) {
       
  1043                 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
       
  1044             }
       
  1045             fCatalogFile = fCatalogFeatures.get(Feature.FILES);
       
  1046             if (fUseCatalog && fCatalogFile != null) {
       
  1047                 if (fCatalogResolver == null) {
       
  1048                     fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
       
  1049                 }
       
  1050                 InputSource is = fCatalogResolver.resolveEntity(publicId, literalSystemId);
       
  1051                 if (is != null && !is.isEmpty()) {
       
  1052                     staxInputSource = new StaxXMLInputSource(new XMLInputSource(is, true), true);
       
  1053                 }
       
  1054             }
       
  1055         }
       
  1056 
  1010         // do default resolution
  1057         // do default resolution
  1011         //this works for both stax & Xerces, if staxInputSource is null,
  1058         //this works for both stax & Xerces, if staxInputSource is null,
  1012         //it means parser need to revert to default resolution
  1059         //it means parser need to revert to default resolution
  1013         if (staxInputSource == null) {
  1060         if (staxInputSource == null) {
  1014             // REVISIT: when systemId is null, I think we should return null.
  1061             // REVISIT: when systemId is null, I think we should return null.
  1081 
  1128 
  1082         if (fEntityResolver != null) {
  1129         if (fEntityResolver != null) {
  1083             resourceIdentifier.setBaseSystemId(baseSystemId);
  1130             resourceIdentifier.setBaseSystemId(baseSystemId);
  1084             resourceIdentifier.setExpandedSystemId(expandedSystemId);
  1131             resourceIdentifier.setExpandedSystemId(expandedSystemId);
  1085             xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier);
  1132             xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier);
       
  1133         }
       
  1134 
       
  1135         if (xmlInputSource == null) {
       
  1136             if (fCatalogFeatures == null) {
       
  1137                 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
       
  1138             }
       
  1139             fCatalogFile = fCatalogFeatures.get(Feature.FILES);
       
  1140             if (fUseCatalog && fCatalogFile != null) {
       
  1141                 /*
       
  1142                  since the method can be called from various processors, both
       
  1143                  CatalogResolver and CatalogUriResolver are used to attempt to find
       
  1144                  a match
       
  1145                 */
       
  1146                 InputSource is = null;
       
  1147                 try {
       
  1148                     if (fCatalogResolver == null) {
       
  1149                         fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
       
  1150                     }
       
  1151                     String pid = (publicId != null? publicId : resourceIdentifier.getNamespace());
       
  1152                     if (pid != null || literalSystemId != null) {
       
  1153                         is = fCatalogResolver.resolveEntity(pid, literalSystemId);
       
  1154                     }
       
  1155                 } catch (CatalogException e) {}
       
  1156                 if (is != null && !is.isEmpty()) {
       
  1157                     xmlInputSource = new XMLInputSource(is, true);
       
  1158                 } else if (literalSystemId != null) {
       
  1159                     if (fCatalogUriResolver == null) {
       
  1160                         fCatalogUriResolver = CatalogManager.catalogUriResolver(fCatalogFeatures);
       
  1161                     }
       
  1162                     Source source = fCatalogUriResolver.resolve(literalSystemId, baseSystemId);
       
  1163                     if (source != null && !source.isEmpty()) {
       
  1164                         xmlInputSource = new XMLInputSource(publicId, source.getSystemId(), baseSystemId, true);
       
  1165                     }
       
  1166                 }
       
  1167             }
  1086         }
  1168         }
  1087 
  1169 
  1088         // do default resolution
  1170         // do default resolution
  1089         // REVISIT: what's the correct behavior if the user provided an entity
  1171         // REVISIT: what's the correct behavior if the user provided an entity
  1090         // resolver (fEntityResolver != null), but resolveEntity doesn't return
  1172         // resolver (fEntityResolver != null), but resolveEntity doesn't return
  1440             fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER);
  1522             fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER);
  1441         } catch (XMLConfigurationException e) {
  1523         } catch (XMLConfigurationException e) {
  1442             fStaxEntityResolver = null;
  1524             fStaxEntityResolver = null;
  1443         }
  1525         }
  1444 
  1526 
  1445         fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD)).booleanValue();
  1527         fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD));
  1446         fReplaceEntityReferences = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)).booleanValue();
  1528         fReplaceEntityReferences = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES));
  1447         fSupportExternalEntities = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)).booleanValue();
  1529         fSupportExternalEntities = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES));
  1448 
  1530 
  1449         // Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd
  1531         // Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd
  1450         fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue();
  1532         fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD));
       
  1533 
       
  1534         //Use Catalog
       
  1535         fUseCatalog = (Boolean)propertyManager.getProperty(XMLConstants.USE_CATALOG);
       
  1536         fCatalogFile = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_FILES);
       
  1537         fDefer = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_DEFER);
       
  1538         fPrefer = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_PREFER);
       
  1539         fResolve = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_RESOLVE);
  1451 
  1540 
  1452         // JAXP 1.5 feature
  1541         // JAXP 1.5 feature
  1453         XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER);
  1542         XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER);
  1454         fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
  1543         fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
  1455 
  1544 
  1532         XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER, null);
  1621         XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER, null);
  1533         if (spm == null) {
  1622         if (spm == null) {
  1534             spm = new XMLSecurityPropertyManager();
  1623             spm = new XMLSecurityPropertyManager();
  1535         }
  1624         }
  1536         fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
  1625         fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
       
  1626 
       
  1627         //Use Catalog
       
  1628         fUseCatalog = componentManager.getFeature(XMLConstants.USE_CATALOG, true);
       
  1629         fCatalogFile = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_FILES);
       
  1630         fDefer = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_DEFER);
       
  1631         fPrefer = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_PREFER);
       
  1632         fResolve = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_RESOLVE);
  1537 
  1633 
  1538         //reset general state
  1634         //reset general state
  1539         reset();
  1635         reset();
  1540 
  1636 
  1541         fEntityScanner.reset(componentManager);
  1637         fEntityScanner.reset(componentManager);
  1629             if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
  1725             if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
  1630                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
  1726                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
  1631                 fLoadExternalDTD = state;
  1727                 fLoadExternalDTD = state;
  1632                 return;
  1728                 return;
  1633             }
  1729             }
       
  1730         } else if (featureId.equals(XMLConstants.USE_CATALOG)) {
       
  1731             fUseCatalog = state;
  1634         }
  1732         }
  1635 
  1733 
  1636     } // setFeature(String,boolean)
  1734     } // setFeature(String,boolean)
  1637 
  1735 
  1638     /**
  1736     /**
  1689         //JAXP 1.5 properties
  1787         //JAXP 1.5 properties
  1690         if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER))
  1788         if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER))
  1691         {
  1789         {
  1692             XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value;
  1790             XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value;
  1693             fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
  1791             fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
       
  1792             return;
       
  1793         }
       
  1794 
       
  1795         //Catalog properties
       
  1796         if (propertyId.equals(JdkXmlUtils.CATALOG_FILES)) {
       
  1797             fCatalogFile = (String)value;
       
  1798         } else if (propertyId.equals(JdkXmlUtils.CATALOG_DEFER)) {
       
  1799             fDefer = (String)value;
       
  1800         } else if (propertyId.equals(JdkXmlUtils.CATALOG_PREFER)) {
       
  1801             fPrefer = (String)value;
       
  1802         } else if (propertyId.equals(JdkXmlUtils.CATALOG_RESOLVE)) {
       
  1803             fResolve = (String)value;
  1694         }
  1804         }
  1695     }
  1805     }
  1696 
  1806 
  1697     public void setLimitAnalyzer(XMLLimitAnalyzer fLimitAnalyzer) {
  1807     public void setLimitAnalyzer(XMLLimitAnalyzer fLimitAnalyzer) {
  1698         this.fLimitAnalyzer = fLimitAnalyzer;
  1808         this.fLimitAnalyzer = fLimitAnalyzer;
  2064             return null;
  2174             return null;
  2065         }
  2175         }
  2066 
  2176 
  2067         // system id has to be a valid URI
  2177         // system id has to be a valid URI
  2068         if (strict) {
  2178         if (strict) {
  2069 
       
  2070             try {
  2179             try {
  2071                 // if it's already an absolute one, return it
  2180                 // if it's already an absolute one, return it
  2072                 new URI(systemId);
  2181                 new URI(systemId);
  2073                 return systemId;
  2182                 return systemId;
  2074             }
  2183             }