< prev index next >

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

Print this page




   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl ;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.Constants;
  24 import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler;
  25 import com.sun.org.apache.xerces.internal.impl.io.ASCIIReader;
  26 import com.sun.org.apache.xerces.internal.impl.io.UCSReader;
  27 import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader;
  28 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  29 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  30 import com.sun.org.apache.xerces.internal.util.*;
  31 import com.sun.org.apache.xerces.internal.util.URI;
  32 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  33 import com.sun.org.apache.xerces.internal.utils.XMLLimitAnalyzer;
  34 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
  35 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
  36 import com.sun.org.apache.xerces.internal.xni.Augmentations;
  37 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  38 import com.sun.org.apache.xerces.internal.xni.XNIException;
  39 import com.sun.org.apache.xerces.internal.xni.parser.*;
  40 import com.sun.xml.internal.stream.Entity;
  41 import com.sun.xml.internal.stream.StaxEntityResolverWrapper;
  42 import com.sun.xml.internal.stream.StaxXMLInputSource;
  43 import com.sun.xml.internal.stream.XMLEntityStorage;
  44 import java.io.*;
  45 import java.lang.reflect.Method;
  46 import java.net.HttpURLConnection;
  47 import java.net.URISyntaxException;
  48 import java.net.URL;
  49 import java.net.URLConnection;
  50 import java.util.HashMap;
  51 import java.util.Iterator;
  52 import java.util.Locale;
  53 import java.util.Map;
  54 import java.util.Stack;
  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;
  63 import javax.xml.stream.XMLInputFactory;
  64 import javax.xml.transform.Source;
  65 import jdk.xml.internal.JdkXmlUtils;
  66 import org.xml.sax.InputSource;
  67 
  68 
  69 /**
  70  * Will keep track of current entity.
  71  *
  72  * The entity manager handles the registration of general and parameter
  73  * entities; resolves entities; and starts entities. The entity manager
  74  * is a central component in a standard parser configuration and this
  75  * class works directly with the entity scanner to manage the underlying
  76  * xni.
  77  * <p>
  78  * This component requires the following features and properties from the
  79  * component manager that uses it:
  80  * <ul>
  81  *  <li>http://xml.org/sax/features/validation</li>
  82  *  <li>http://xml.org/sax/features/external-general-entities</li>


 403     protected XMLEntityStorage fEntityStorage ;
 404 
 405     protected final Object [] defaultEncoding = new Object[]{"UTF-8", null};
 406 
 407 
 408     // temp vars
 409 
 410     /** Resource identifer. */
 411     private final XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl();
 412 
 413     /** Augmentations for entities. */
 414     private final Augmentations fEntityAugs = new AugmentationsImpl();
 415 
 416     /** Pool of character buffers. */
 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;
 429 
 430     //
 431     // Constructors
 432     //
 433 
 434     /**
 435      * If this constructor is used to create the object, reset() should be invoked on this object
 436      */
 437     public XMLEntityManager() {
 438         //for entity managers not created by parsers
 439         fSecurityManager = new XMLSecurityManager(true);
 440         fEntityStorage = new XMLEntityStorage(this) ;
 441         setScannerVersion(Constants.XML_VERSION_1_0);
 442     } // <init>()
 443 


1027         }
1028 
1029         if(fEntityResolver != null){
1030             xmlInputSource = fEntityResolver.resolveEntity(ri);
1031             if(xmlInputSource != null) {
1032                 fISCreatedByResolver = true;
1033             }
1034         }
1035 
1036         if(xmlInputSource != null){
1037             //wrap this XMLInputSource to StaxInputSource
1038             staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
1039         }
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 
1057         // do default resolution
1058         //this works for both stax & Xerces, if staxInputSource is null,
1059         //it means parser need to revert to default resolution
1060         if (staxInputSource == null) {
1061             // REVISIT: when systemId is null, I think we should return null.
1062             //          is this the right solution? -SG
1063             //if (systemId != null)
1064             staxInputSource = new StaxXMLInputSource(
1065                     new XMLInputSource(publicId, literalSystemId, baseSystemId, true), false);
1066         }else if(staxInputSource.hasXMLStreamOrXMLEventReader()){
1067             //Waiting for the clarification from EG. - nb
1068         }
1069 
1070         if (DEBUG_RESOLVER) {
1071             System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")");
1072             System.err.println(" = " + xmlInputSource);
1073         }


1123         if (needExpand)
1124             expandedSystemId = expandSystemId(literalSystemId, baseSystemId,false);
1125 
1126         // give the entity resolver a chance
1127         XMLInputSource xmlInputSource = null;
1128 
1129         if (fEntityResolver != null) {
1130             resourceIdentifier.setBaseSystemId(baseSystemId);
1131             resourceIdentifier.setExpandedSystemId(expandedSystemId);
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             }
1168         }
1169 
1170         // do default resolution
1171         // REVISIT: what's the correct behavior if the user provided an entity
1172         // resolver (fEntityResolver != null), but resolveEntity doesn't return
1173         // an input source (xmlInputSource == null)?
1174         // do we do default resolution, or do we just return null? -SG
1175         if (xmlInputSource == null) {
1176             // REVISIT: when systemId is null, I think we should return null.
1177             //          is this the right solution? -SG
1178             //if (systemId != null)
1179             xmlInputSource = new XMLInputSource(publicId, literalSystemId, baseSystemId, false);
1180         }
1181 
1182         if (DEBUG_RESOLVER) {




   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl ;
  22 


  23 import com.sun.org.apache.xerces.internal.impl.io.ASCIIReader;
  24 import com.sun.org.apache.xerces.internal.impl.io.UCSReader;
  25 import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader;
  26 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  27 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  28 import com.sun.org.apache.xerces.internal.util.*;
  29 import com.sun.org.apache.xerces.internal.util.URI;
  30 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  31 import com.sun.org.apache.xerces.internal.utils.XMLLimitAnalyzer;
  32 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
  33 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
  34 import com.sun.org.apache.xerces.internal.xni.Augmentations;
  35 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  36 import com.sun.org.apache.xerces.internal.xni.XNIException;
  37 import com.sun.org.apache.xerces.internal.xni.parser.*;
  38 import com.sun.xml.internal.stream.Entity;
  39 import com.sun.xml.internal.stream.StaxEntityResolverWrapper;
  40 import com.sun.xml.internal.stream.StaxXMLInputSource;
  41 import com.sun.xml.internal.stream.XMLEntityStorage;
  42 import java.io.*;

  43 import java.net.HttpURLConnection;
  44 import java.net.URISyntaxException;
  45 import java.net.URL;
  46 import java.net.URLConnection;
  47 import java.util.HashMap;
  48 import java.util.Iterator;
  49 import java.util.Locale;
  50 import java.util.Map;
  51 import java.util.Stack;
  52 import java.util.StringTokenizer;
  53 import javax.xml.XMLConstants;
  54 import javax.xml.catalog.CatalogException;
  55 import javax.xml.catalog.CatalogFeatures;
  56 import javax.xml.catalog.CatalogFeatures.Feature;
  57 import javax.xml.catalog.CatalogManager;
  58 import javax.xml.catalog.CatalogResolver;

  59 import javax.xml.stream.XMLInputFactory;
  60 import javax.xml.transform.Source;
  61 import jdk.xml.internal.JdkXmlUtils;
  62 import org.xml.sax.InputSource;
  63 
  64 
  65 /**
  66  * Will keep track of current entity.
  67  *
  68  * The entity manager handles the registration of general and parameter
  69  * entities; resolves entities; and starts entities. The entity manager
  70  * is a central component in a standard parser configuration and this
  71  * class works directly with the entity scanner to manage the underlying
  72  * xni.
  73  * <p>
  74  * This component requires the following features and properties from the
  75  * component manager that uses it:
  76  * <ul>
  77  *  <li>http://xml.org/sax/features/validation</li>
  78  *  <li>http://xml.org/sax/features/external-general-entities</li>


 399     protected XMLEntityStorage fEntityStorage ;
 400 
 401     protected final Object [] defaultEncoding = new Object[]{"UTF-8", null};
 402 
 403 
 404     // temp vars
 405 
 406     /** Resource identifer. */
 407     private final XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl();
 408 
 409     /** Augmentations for entities. */
 410     private final Augmentations fEntityAugs = new AugmentationsImpl();
 411 
 412     /** Pool of character buffers. */
 413     private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE);
 414 
 415     /** indicate whether Catalog should be used for resolving external resources */
 416     private boolean fUseCatalog = true;
 417     CatalogFeatures fCatalogFeatures;
 418     CatalogResolver fCatalogResolver;

 419 
 420     private String fCatalogFile;
 421     private String fDefer;
 422     private String fPrefer;
 423     private String fResolve;
 424 
 425     //
 426     // Constructors
 427     //
 428 
 429     /**
 430      * If this constructor is used to create the object, reset() should be invoked on this object
 431      */
 432     public XMLEntityManager() {
 433         //for entity managers not created by parsers
 434         fSecurityManager = new XMLSecurityManager(true);
 435         fEntityStorage = new XMLEntityStorage(this) ;
 436         setScannerVersion(Constants.XML_VERSION_1_0);
 437     } // <init>()
 438 


1022         }
1023 
1024         if(fEntityResolver != null){
1025             xmlInputSource = fEntityResolver.resolveEntity(ri);
1026             if(xmlInputSource != null) {
1027                 fISCreatedByResolver = true;
1028             }
1029         }
1030 
1031         if(xmlInputSource != null){
1032             //wrap this XMLInputSource to StaxInputSource
1033             staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver);
1034         }
1035 
1036         if (staxInputSource == null) {
1037             if (fCatalogFeatures == null) {
1038                 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
1039             }
1040             fCatalogFile = fCatalogFeatures.get(Feature.FILES);
1041             if (fUseCatalog && fCatalogFile != null) {
1042                 try {
1043                     if (fCatalogResolver == null) {
1044                         fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1045                     }
1046                     InputSource is = fCatalogResolver.resolveEntity(publicId, literalSystemId);
1047                     if (is != null && !is.isEmpty()) {
1048                         staxInputSource = new StaxXMLInputSource(new XMLInputSource(is, true), true);
1049                     }
1050                 } catch (CatalogException e) {
1051                     fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,"CatalogException",
1052                     new Object[]{SecuritySupport.sanitizePath(fCatalogFile)},
1053                     XMLErrorReporter.SEVERITY_FATAL_ERROR, e );
1054                 }
1055             }
1056         }
1057 
1058         // do default resolution
1059         //this works for both stax & Xerces, if staxInputSource is null,
1060         //it means parser need to revert to default resolution
1061         if (staxInputSource == null) {
1062             // REVISIT: when systemId is null, I think we should return null.
1063             //          is this the right solution? -SG
1064             //if (systemId != null)
1065             staxInputSource = new StaxXMLInputSource(
1066                     new XMLInputSource(publicId, literalSystemId, baseSystemId, true), false);
1067         }else if(staxInputSource.hasXMLStreamOrXMLEventReader()){
1068             //Waiting for the clarification from EG. - nb
1069         }
1070 
1071         if (DEBUG_RESOLVER) {
1072             System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")");
1073             System.err.println(" = " + xmlInputSource);
1074         }


1124         if (needExpand)
1125             expandedSystemId = expandSystemId(literalSystemId, baseSystemId,false);
1126 
1127         // give the entity resolver a chance
1128         XMLInputSource xmlInputSource = null;
1129 
1130         if (fEntityResolver != null) {
1131             resourceIdentifier.setBaseSystemId(baseSystemId);
1132             resourceIdentifier.setExpandedSystemId(expandedSystemId);
1133             xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier);
1134         }
1135 
1136         if (xmlInputSource == null) {
1137             if (fCatalogFeatures == null) {
1138                 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
1139             }
1140             fCatalogFile = fCatalogFeatures.get(Feature.FILES);
1141             if (fUseCatalog && fCatalogFile != null) {
1142                 /*
1143                  since the method can be called from various processors, both
1144                  EntityResolver and URIResolver are used to attempt to find
1145                  a match
1146                 */
1147                 InputSource is = null;
1148                 try {
1149                     if (fCatalogResolver == null) {
1150                         fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1151                     }
1152                     String pid = (publicId != null? publicId : resourceIdentifier.getNamespace());
1153                     if (pid != null || literalSystemId != null) {
1154                         is = fCatalogResolver.resolveEntity(pid, literalSystemId);
1155                     }
1156                 } catch (CatalogException e) {}
1157 
1158                 if (is != null && !is.isEmpty()) {
1159                     xmlInputSource = new XMLInputSource(is, true);
1160                 } else if (literalSystemId != null) {
1161                     if (fCatalogResolver == null) {
1162                         fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1163                     }
1164 
1165                     Source source = null;
1166                     try {
1167                         source = fCatalogResolver.resolve(literalSystemId, baseSystemId);
1168                     } catch (CatalogException e) {
1169                         throw new XNIException(e);
1170                     }

1171                     if (source != null && !source.isEmpty()) {
1172                         xmlInputSource = new XMLInputSource(publicId, source.getSystemId(), baseSystemId, true);
1173                     }
1174                 }
1175             }
1176         }
1177 
1178         // do default resolution
1179         // REVISIT: what's the correct behavior if the user provided an entity
1180         // resolver (fEntityResolver != null), but resolveEntity doesn't return
1181         // an input source (xmlInputSource == null)?
1182         // do we do default resolution, or do we just return null? -SG
1183         if (xmlInputSource == null) {
1184             // REVISIT: when systemId is null, I think we should return null.
1185             //          is this the right solution? -SG
1186             //if (systemId != null)
1187             xmlInputSource = new XMLInputSource(publicId, literalSystemId, baseSystemId, false);
1188         }
1189 
1190         if (DEBUG_RESOLVER) {


< prev index next >