src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java

Print this page

        

@@ -51,11 +51,21 @@
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Stack;
 import java.util.StringTokenizer;
+import javax.xml.XMLConstants;
+import javax.xml.catalog.CatalogException;
+import javax.xml.catalog.CatalogFeatures;
+import javax.xml.catalog.CatalogFeatures.Feature;
+import javax.xml.catalog.CatalogManager;
+import javax.xml.catalog.CatalogResolver;
+import javax.xml.catalog.CatalogUriResolver;
 import javax.xml.stream.XMLInputFactory;
+import javax.xml.transform.Source;
+import jdk.xml.internal.JdkXmlUtils;
+import org.xml.sax.InputSource;
 
 
 /**
  * Will keep track of current entity.
  *

@@ -182,41 +192,51 @@
                 VALIDATION,
                 EXTERNAL_GENERAL_ENTITIES,
                 EXTERNAL_PARAMETER_ENTITIES,
                 ALLOW_JAVA_ENCODINGS,
                 WARN_ON_DUPLICATE_ENTITYDEF,
-                STANDARD_URI_CONFORMANT
+                STANDARD_URI_CONFORMANT,
+                XMLConstants.USE_CATALOG
     };
 
     /** Feature defaults. */
     private static final Boolean[] FEATURE_DEFAULTS = {
                 null,
                 Boolean.TRUE,
                 Boolean.TRUE,
                 Boolean.TRUE,
                 Boolean.FALSE,
-                Boolean.FALSE
+                Boolean.FALSE,
+                JdkXmlUtils.USE_CATALOG_DEFAULT
     };
 
     /** Recognized properties. */
     private static final String[] RECOGNIZED_PROPERTIES = {
                 SYMBOL_TABLE,
                 ERROR_REPORTER,
                 ENTITY_RESOLVER,
                 VALIDATION_MANAGER,
                 BUFFER_SIZE,
                 SECURITY_MANAGER,
-                XML_SECURITY_PROPERTY_MANAGER
+                XML_SECURITY_PROPERTY_MANAGER,
+                JdkXmlUtils.CATALOG_DEFER,
+                JdkXmlUtils.CATALOG_FILES,
+                JdkXmlUtils.CATALOG_PREFER,
+                JdkXmlUtils.CATALOG_RESOLVE
     };
 
     /** Property defaults. */
     private static final Object[] PROPERTY_DEFAULTS = {
                 null,
                 null,
                 null,
                 null,
-                new Integer(DEFAULT_BUFFER_SIZE),
+                DEFAULT_BUFFER_SIZE,
+                null,
+                null,
+                null,
+                null,
                 null,
                 null
     };
 
     private static final String XMLEntity = "[xml]".intern();

@@ -394,10 +414,21 @@
     private final Augmentations fEntityAugs = new AugmentationsImpl();
 
     /** Pool of character buffers. */
     private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE);
 
+    /** indicate whether Catalog should be used for resolving external resources */
+    private boolean fUseCatalog = true;
+    CatalogFeatures fCatalogFeatures;
+    CatalogResolver fCatalogResolver;
+    CatalogUriResolver fCatalogUriResolver;
+
+    private String fCatalogFile;
+    private String fDefer;
+    private String fPrefer;
+    private String fResolve;
+
     //
     // Constructors
     //
 
     /**

@@ -1005,10 +1036,26 @@
         if(xmlInputSource != null){
             //wrap this XMLInputSource to StaxInputSource
             staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
         }
 
+        if (staxInputSource == null) {
+            if (fCatalogFeatures == null) {
+                fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
+            }
+            fCatalogFile = fCatalogFeatures.get(Feature.FILES);
+            if (fUseCatalog && fCatalogFile != null) {
+                if (fCatalogResolver == null) {
+                    fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
+                }
+                InputSource is = fCatalogResolver.resolveEntity(publicId, literalSystemId);
+                if (is != null && !is.isEmpty()) {
+                    staxInputSource = new StaxXMLInputSource(new XMLInputSource(is, true), true);
+                }
+            }
+        }
+
         // do default resolution
         //this works for both stax & Xerces, if staxInputSource is null,
         //it means parser need to revert to default resolution
         if (staxInputSource == null) {
             // REVISIT: when systemId is null, I think we should return null.

@@ -1083,10 +1130,45 @@
             resourceIdentifier.setBaseSystemId(baseSystemId);
             resourceIdentifier.setExpandedSystemId(expandedSystemId);
             xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier);
         }
 
+        if (xmlInputSource == null) {
+            if (fCatalogFeatures == null) {
+                fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
+            }
+            fCatalogFile = fCatalogFeatures.get(Feature.FILES);
+            if (fUseCatalog && fCatalogFile != null) {
+                /*
+                 since the method can be called from various processors, both
+                 CatalogResolver and CatalogUriResolver are used to attempt to find
+                 a match
+                */
+                InputSource is = null;
+                try {
+                    if (fCatalogResolver == null) {
+                        fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
+                    }
+                    String pid = (publicId != null? publicId : resourceIdentifier.getNamespace());
+                    if (pid != null || literalSystemId != null) {
+                        is = fCatalogResolver.resolveEntity(pid, literalSystemId);
+                    }
+                } catch (CatalogException e) {}
+                if (is != null && !is.isEmpty()) {
+                    xmlInputSource = new XMLInputSource(is, true);
+                } else if (literalSystemId != null) {
+                    if (fCatalogUriResolver == null) {
+                        fCatalogUriResolver = CatalogManager.catalogUriResolver(fCatalogFeatures);
+                    }
+                    Source source = fCatalogUriResolver.resolve(literalSystemId, baseSystemId);
+                    if (source != null && !source.isEmpty()) {
+                        xmlInputSource = new XMLInputSource(publicId, source.getSystemId(), baseSystemId, true);
+                    }
+                }
+            }
+        }
+
         // do default resolution
         // REVISIT: what's the correct behavior if the user provided an entity
         // resolver (fEntityResolver != null), but resolveEntity doesn't return
         // an input source (xmlInputSource == null)?
         // do we do default resolution, or do we just return null? -SG

@@ -1440,16 +1522,23 @@
             fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER);
         } catch (XMLConfigurationException e) {
             fStaxEntityResolver = null;
         }
 
-        fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD)).booleanValue();
-        fReplaceEntityReferences = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)).booleanValue();
-        fSupportExternalEntities = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)).booleanValue();
+        fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD));
+        fReplaceEntityReferences = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES));
+        fSupportExternalEntities = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES));
 
         // Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd
-        fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue();
+        fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD));
+
+        //Use Catalog
+        fUseCatalog = (Boolean)propertyManager.getProperty(XMLConstants.USE_CATALOG);
+        fCatalogFile = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_FILES);
+        fDefer = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_DEFER);
+        fPrefer = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_PREFER);
+        fResolve = (String)propertyManager.getProperty(JdkXmlUtils.CATALOG_RESOLVE);
 
         // JAXP 1.5 feature
         XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER);
         fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
 

@@ -1533,10 +1622,17 @@
         if (spm == null) {
             spm = new XMLSecurityPropertyManager();
         }
         fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
 
+        //Use Catalog
+        fUseCatalog = componentManager.getFeature(XMLConstants.USE_CATALOG, true);
+        fCatalogFile = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_FILES);
+        fDefer = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_DEFER);
+        fPrefer = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_PREFER);
+        fResolve = (String)componentManager.getProperty(JdkXmlUtils.CATALOG_RESOLVE);
+
         //reset general state
         reset();
 
         fEntityScanner.reset(componentManager);
         fEntityStorage.reset(componentManager);

@@ -1629,10 +1725,12 @@
             if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
                 fLoadExternalDTD = state;
                 return;
             }
+        } else if (featureId.equals(XMLConstants.USE_CATALOG)) {
+            fUseCatalog = state;
         }
 
     } // setFeature(String,boolean)
 
     /**

@@ -1689,10 +1787,22 @@
         //JAXP 1.5 properties
         if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER))
         {
             XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value;
             fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD);
+            return;
+        }
+
+        //Catalog properties
+        if (propertyId.equals(JdkXmlUtils.CATALOG_FILES)) {
+            fCatalogFile = (String)value;
+        } else if (propertyId.equals(JdkXmlUtils.CATALOG_DEFER)) {
+            fDefer = (String)value;
+        } else if (propertyId.equals(JdkXmlUtils.CATALOG_PREFER)) {
+            fPrefer = (String)value;
+        } else if (propertyId.equals(JdkXmlUtils.CATALOG_RESOLVE)) {
+            fResolve = (String)value;
         }
     }
 
     public void setLimitAnalyzer(XMLLimitAnalyzer fLimitAnalyzer) {
         this.fLimitAnalyzer = fLimitAnalyzer;

@@ -2064,11 +2174,10 @@
             return null;
         }
 
         // system id has to be a valid URI
         if (strict) {
-
             try {
                 // if it's already an absolute one, return it
                 new URI(systemId);
                 return systemId;
             }