jaxp/src/com/sun/org/apache/xerces/internal/impl/XMLDocumentFragmentScannerImpl.java
changeset 17534 21dc0b2762da
parent 14939 2e992d2acc8b
child 18890 25bdeca3173b
child 20964 a5cd729f176d
equal deleted inserted replaced
17533:93a2cadbbd33 17534:21dc0b2762da
    50 import com.sun.org.apache.xerces.internal.xni.Augmentations;
    50 import com.sun.org.apache.xerces.internal.xni.Augmentations;
    51 import com.sun.org.apache.xerces.internal.impl.Constants;
    51 import com.sun.org.apache.xerces.internal.impl.Constants;
    52 import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler;
    52 import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler;
    53 import com.sun.org.apache.xerces.internal.util.SecurityManager;
    53 import com.sun.org.apache.xerces.internal.util.SecurityManager;
    54 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
    54 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
       
    55 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
    55 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
    56 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
       
    57 import com.sun.xml.internal.stream.Entity;
       
    58 import javax.xml.XMLConstants;
    56 import javax.xml.stream.XMLStreamConstants;
    59 import javax.xml.stream.XMLStreamConstants;
    57 import javax.xml.stream.events.XMLEvent;
    60 import javax.xml.stream.events.XMLEvent;
    58 
    61 
    59 /**
    62 /**
    60  *
    63  *
   157 
   160 
   158     /** Property identifier: entity resolver. */
   161     /** Property identifier: entity resolver. */
   159     protected static final String ENTITY_RESOLVER =
   162     protected static final String ENTITY_RESOLVER =
   160             Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
   163             Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
   161 
   164 
       
   165     /** Feature identifier: standard uri conformant */
       
   166     protected static final String STANDARD_URI_CONFORMANT =
       
   167             Constants.XERCES_FEATURE_PREFIX +Constants.STANDARD_URI_CONFORMANT_FEATURE;
       
   168 
       
   169     /** property identifier: access external dtd. */
       
   170     protected static final String ACCESS_EXTERNAL_DTD = XMLConstants.ACCESS_EXTERNAL_DTD;
       
   171 
       
   172     /** access external dtd: file protocol
       
   173      *  For DOM/SAX, the secure feature is set to true by default
       
   174      */
       
   175     final static String EXTERNAL_ACCESS_DEFAULT = Constants.EXTERNAL_ACCESS_DEFAULT;
       
   176 
   162     // recognized features and properties
   177     // recognized features and properties
   163 
   178 
   164     /** Recognized features. */
   179     /** Recognized features. */
   165     private static final String[] RECOGNIZED_FEATURES = {
   180     private static final String[] RECOGNIZED_FEATURES = {
   166                 NAMESPACES,
   181                 NAMESPACES,
   182     /** Recognized properties. */
   197     /** Recognized properties. */
   183     private static final String[] RECOGNIZED_PROPERTIES = {
   198     private static final String[] RECOGNIZED_PROPERTIES = {
   184         SYMBOL_TABLE,
   199         SYMBOL_TABLE,
   185                 ERROR_REPORTER,
   200                 ERROR_REPORTER,
   186                 ENTITY_MANAGER,
   201                 ENTITY_MANAGER,
       
   202                 ACCESS_EXTERNAL_DTD
   187     };
   203     };
   188 
   204 
   189     /** Property defaults. */
   205     /** Property defaults. */
   190     private static final Object[] PROPERTY_DEFAULTS = {
   206     private static final Object[] PROPERTY_DEFAULTS = {
   191                 null,
   207                 null,
   192                 null,
   208                 null,
   193                 null,
   209                 null,
       
   210                 EXTERNAL_ACCESS_DEFAULT
   194     };
   211     };
   195 
   212 
   196     private static final char [] cdata = {'[','C','D','A','T','A','['};
   213     private static final char [] cdata = {'[','C','D','A','T','A','['};
   197     private static final char [] endTag = {'<','/'};
   214     private static final char [] endTag = {'<','/'};
   198 
   215 
   295     protected boolean fReportCdataEvent = false ;
   312     protected boolean fReportCdataEvent = false ;
   296     protected boolean fIsCoalesce = false ;
   313     protected boolean fIsCoalesce = false ;
   297     protected String fDeclaredEncoding =  null;
   314     protected String fDeclaredEncoding =  null;
   298     /** Xerces Feature: Disallow doctype declaration. */
   315     /** Xerces Feature: Disallow doctype declaration. */
   299     protected boolean fDisallowDoctype = false;
   316     protected boolean fDisallowDoctype = false;
       
   317     /**
       
   318      * comma-delimited list of protocols that are allowed for the purpose
       
   319      * of accessing external dtd or entity references
       
   320      */
       
   321     protected String fAccessExternalDTD = EXTERNAL_ACCESS_DEFAULT;
       
   322 
       
   323     /**
       
   324      * standard uri conformant (strict uri).
       
   325      * http://apache.org/xml/features/standard-uri-conformant
       
   326      */
       
   327     protected boolean fStrictURI;
   300 
   328 
   301     // drivers
   329     // drivers
   302 
   330 
   303     /** Active driver. */
   331     /** Active driver. */
   304     protected Driver fDriver;
   332     protected Driver fDriver;
   411      *                 permitted to completely scan a document if it does
   439      *                 permitted to completely scan a document if it does
   412      *                 not support this "pull" scanning model.
   440      *                 not support this "pull" scanning model.
   413      *
   441      *
   414      * @return True if there is more to scan, false otherwise.
   442      * @return True if there is more to scan, false otherwise.
   415      */
   443      */
   416    /* public boolean scanDocument(boolean complete)
       
   417     throws IOException, XNIException {
       
   418 
       
   419         // keep dispatching "events"
       
   420         fEntityManager.setEntityHandler(this);
       
   421 
       
   422         return true;
       
   423 
       
   424     } // scanDocument(boolean):boolean
       
   425     */
       
   426 
       
   427     public boolean scanDocument(boolean complete)
   444     public boolean scanDocument(boolean complete)
   428     throws IOException, XNIException {
   445     throws IOException, XNIException {
   429 
   446 
   430         // keep dispatching "events"
   447         // keep dispatching "events"
   431         fEntityManager.setEntityHandler(this);
   448         fEntityManager.setEntityHandler(this);
   577         //attribute
   594         //attribute
   578         fReadingAttributes = false;
   595         fReadingAttributes = false;
   579         //xxx: external entities are supported in Xerces
   596         //xxx: external entities are supported in Xerces
   580         // it would be good to define feature for this case
   597         // it would be good to define feature for this case
   581         fSupportExternalEntities = true;
   598         fSupportExternalEntities = true;
       
   599         fSupportExternalEntities = true;
       
   600         fSupportExternalEntities = true;
       
   601         fSupportExternalEntities = true;
   582         fReplaceEntityReferences = true;
   602         fReplaceEntityReferences = true;
   583         fIsCoalesce = false;
   603         fIsCoalesce = false;
   584 
   604 
   585         // setup Driver
   605         // setup Driver
   586         setScannerState(SCANNER_STATE_CONTENT);
   606         setScannerState(SCANNER_STATE_CONTENT);
   587         setDriver(fContentDriver);
   607         setDriver(fContentDriver);
   588         fEntityStore = fEntityManager.getEntityStore();
   608         fEntityStore = fEntityManager.getEntityStore();
   589 
   609 
   590         dtdGrammarUtil = null;
   610         dtdGrammarUtil = null;
   591 
   611 
       
   612         // JAXP 1.5 features and properties
       
   613         fAccessExternalDTD = (String) componentManager.getProperty(ACCESS_EXTERNAL_DTD, EXTERNAL_ACCESS_DEFAULT);
       
   614         fStrictURI = componentManager.getFeature(STANDARD_URI_CONFORMANT, false);
   592 
   615 
   593         //fEntityManager.test();
   616         //fEntityManager.test();
   594     } // reset(XMLComponentManager)
   617     } // reset(XMLComponentManager)
   595 
   618 
   596 
   619 
   637         fEntityStore = fEntityManager.getEntityStore();
   660         fEntityStore = fEntityManager.getEntityStore();
   638         //fEntityManager.test();
   661         //fEntityManager.test();
   639 
   662 
   640         dtdGrammarUtil = null;
   663         dtdGrammarUtil = null;
   641 
   664 
       
   665         // Oracle jdk feature
       
   666         fAccessExternalDTD = (String) propertyManager.getProperty(ACCESS_EXTERNAL_DTD);
       
   667 
   642     } // reset(XMLComponentManager)
   668     } // reset(XMLComponentManager)
   643 
   669 
   644     /**
   670     /**
   645      * Returns a list of feature identifiers that are recognized by
   671      * Returns a list of feature identifiers that are recognized by
   646      * this component. This method may return null if no features
   672      * this component. This method may return null if no features
   731             String property = propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length());
   757             String property = propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length());
   732             if (property.equals(Constants.ENTITY_MANAGER_PROPERTY)) {
   758             if (property.equals(Constants.ENTITY_MANAGER_PROPERTY)) {
   733                 fEntityManager = (XMLEntityManager)value;
   759                 fEntityManager = (XMLEntityManager)value;
   734             }
   760             }
   735             return;
   761             return;
       
   762         }
       
   763 
       
   764         //JAXP 1.5 properties
       
   765         if (propertyId.startsWith(Constants.JAXPAPI_PROPERTY_PREFIX)) {
       
   766             if (propertyId.equals(ACCESS_EXTERNAL_DTD))
       
   767             {
       
   768                 fAccessExternalDTD = (String)value;
       
   769             }
   736         }
   770         }
   737 
   771 
   738     } // setProperty(String,Object)
   772     } // setProperty(String,Object)
   739 
   773 
   740     /**
   774     /**
  1844         }
  1878         }
  1845 
  1879 
  1846         //1. if the entity is external and support to external entities is not required
  1880         //1. if the entity is external and support to external entities is not required
  1847         // 2. or entities should not be replaced
  1881         // 2. or entities should not be replaced
  1848         //3. or if it is built in entity reference.
  1882         //3. or if it is built in entity reference.
  1849         if((fEntityStore.isExternalEntity(name) && !fSupportExternalEntities) || (!fEntityStore.isExternalEntity(name) && !fReplaceEntityReferences) || foundBuiltInRefs){
  1883         boolean isEE = fEntityStore.isExternalEntity(name);
       
  1884         if((isEE && !fSupportExternalEntities) || (!isEE && !fReplaceEntityReferences) || foundBuiltInRefs){
  1850             fScannerState = SCANNER_STATE_REFERENCE;
  1885             fScannerState = SCANNER_STATE_REFERENCE;
  1851             return ;
  1886             return ;
  1852         }
  1887         }
  1853         // start general entity
  1888         // start general entity
  1854         if (!fEntityStore.isDeclaredEntity(name)) {
  1889         if (!fEntityStore.isDeclaredEntity(name)) {
  1993             }
  2028             }
  1994         }
  2029         }
  1995         return "null";
  2030         return "null";
  1996 
  2031 
  1997     } // getDriverName():String
  2032     } // getDriverName():String
       
  2033 
       
  2034     String checkAccess(String systemId, String allowedProtocols) throws IOException {
       
  2035         String baseSystemId = fEntityScanner.getBaseSystemId();
       
  2036         String expandedSystemId = fEntityManager.expandSystemId(systemId, baseSystemId,fStrictURI);
       
  2037         return SecuritySupport.checkAccess(expandedSystemId, allowedProtocols, Constants.ACCESS_EXTERNAL_ALL);
       
  2038     }
  1998 
  2039 
  1999     //
  2040     //
  2000     // Classes
  2041     // Classes
  2001     //
  2042     //
  2002 
  2043