javax.xml.stream.XMLResolver Java Examples

The following examples show how to use javax.xml.stream.XMLResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CatalogSupportBase.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an XMLStreamReader.
 *
 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
 * through the factory
 * @param useCatalog the value of USE_CATALOG
 * @param catalog the path to a catalog
 * @param xml the xml to be parsed
 * @param resolver a resolver to be set on the reader
 * @return an instance of the XMLStreamReader
 * @throws FileNotFoundException
 * @throws XMLStreamException
 */
XMLStreamReader getStreamReader(boolean setUseCatalog, boolean useCatalog,
        String catalog, String xml, XMLResolver resolver)
        throws FileNotFoundException, XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (catalog != null) {
        factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
    }

    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);

    if (resolver != null) {
        factory.setProperty(XMLInputFactory.RESOLVER, resolver);
    }

    if (setUseCatalog) {
        factory.setProperty(XMLConstants.USE_CATALOG, useCatalog);
    }

    InputStream entityxml = new FileInputStream(xml);
    XMLStreamReader streamReader = factory.createXMLStreamReader(xml, entityxml);
    return streamReader;
}
 
Example #2
Source File: DefaultInputResolver.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * A very simple utility expansion method used generally when the
 * only way to resolve an entity is via passed resolver; and where
 * failing to resolve it is not fatal.
 */
public static WstxInputSource resolveEntityUsing
    (WstxInputSource refCtxt, String entityName,
     String publicId, String systemId,
     XMLResolver resolver, ReaderConfig cfg, int xmlVersion)
    throws IOException, XMLStreamException
{
    URL ctxt = (refCtxt == null) ? null : refCtxt.getSource();
    if (ctxt == null) {
        ctxt = URLUtil.urlFromCurrentDir();
    }
    Object source = resolver.resolveEntity(publicId, systemId, ctxt.toExternalForm(), entityName);
    return (source == null) ? null : sourceFrom(refCtxt, cfg, entityName, xmlVersion, source);
}
 
Example #3
Source File: DefaultInputResolver.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * Basic external resource resolver implementation; usable both with
 * DTD and entity resolution.
 *
 * @param parent Input source that contains reference to be expanded.
 * @param pathCtxt Reference context to use for resolving path, if
 *   known. If null, reference context of the parent will
 *   be used; and if that is null (which is possible), the
 *   current working directory will be assumed.
 * @param entityName Name/id of the entity being expanded, if this is an
 *   entity expansion; null otherwise (for example, when resolving external
 *   subset).
 * @param publicId Public identifier of the resource, if known; null/empty
 *   otherwise. Default implementation just ignores the identifier.
 * @param systemId System identifier of the resource. Although interface
 *   allows null/empty, default implementation considers this an error.
 * @param xmlVersion Xml version as declared by the main parsed
 *   document. Currently only relevant for checking that XML 1.0 document
 *   does not include XML 1.1 external parsed entities.
 *   If XML_V_UNKNOWN, no checks will be done.
 * @param customResolver Custom resolver to use first for resolution,
 *   if any (may be null).
 * @param cfg Reader configuration object used by the parser that is
 *   resolving the entity
 *
 * @return Input source, if entity could be resolved; null if it could
 *   not be resolved. In latter case processor may use its own default
 *   resolution mechanism.
 */
public static WstxInputSource resolveEntity
    (WstxInputSource parent, URL pathCtxt, String entityName,
     String publicId, String systemId,
     XMLResolver customResolver, ReaderConfig cfg, int xmlVersion)
    throws IOException, XMLStreamException
{
    if (pathCtxt == null) {
        pathCtxt = parent.getSource();
        if (pathCtxt == null) {
            pathCtxt = URLUtil.urlFromCurrentDir();
        }
    }

    // Do we have a custom resolver that may be able to resolve it?
    if (customResolver != null) {
        Object source = customResolver.resolveEntity(publicId, systemId, pathCtxt.toExternalForm(), entityName);
        if (source != null) {
            return sourceFrom(parent, cfg, entityName, xmlVersion, source);
        }
    }
        
    // Have to have a system id, then...
    if (systemId == null) {
        throw new XMLStreamException("Can not resolve "
                                     +((entityName == null) ? "[External DTD subset]" : ("entity '"+entityName+"'"))+" without a system id (public id '"
                                     +publicId+"')");
    }
    URL url = URLUtil.urlFromSystemId(systemId, pathCtxt);
    return sourceFromURL(parent, cfg, entityName, xmlVersion, url, publicId);
}
 
Example #4
Source File: UnparsedExtEntity.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
public WstxInputSource expand(WstxInputSource parent,
        XMLResolver res, ReaderConfig cfg, int xmlVersion)
{
    // Should never get called, actually...
    throw new IllegalStateException("Internal error: createInputSource() called for unparsed (external) entity.");
}
 
Example #5
Source File: ParsedExtEntity.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
public WstxInputSource expand(WstxInputSource parent,
                              XMLResolver res, ReaderConfig cfg,
                              int xmlVersion)
    throws IOException, XMLStreamException
{
    /* 05-Feb-2006, TSa: If xmlVersion not explicitly known, it defaults
     *    to 1.0
     */
    if (xmlVersion == XmlConsts.XML_V_UNKNOWN) {
        xmlVersion = XmlConsts.XML_V_10;
    }
    return DefaultInputResolver.resolveEntity
        (parent, mContext, mName, getPublicId(), getSystemId(), res, cfg, xmlVersion);
}
 
Example #6
Source File: IntEntity.java    From woodstox with Apache License 2.0 5 votes vote down vote up
@Override
public WstxInputSource expand(WstxInputSource parent,
                              XMLResolver res, ReaderConfig cfg,
                              int xmlVersion)
{
    /* 26-Dec-2006, TSa: Better leave source as null, since internal
     *   entity declaration context should never be used: when expanding,
     *   reference context is to be used.
     */
    return InputSourceFactory.constructCharArraySource
        //(parent, mName, mRepl, 0, mRepl.length, mContentLocation, getSource());
        (parent, mName, mRepl, 0, mRepl.length, mContentLocation, null);
}
 
Example #7
Source File: StreamScanner.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor used when creating a complete new (main-level) reader that
 * does not share its input buffers or state with another reader.
 */
protected StreamScanner(WstxInputSource input, ReaderConfig cfg,
                        XMLResolver res)
{
    super();
    mInput = input;
    // 17-Jun-2004, TSa: Need to know root-level input source
    mRootInput = input;

    mConfig = cfg;
    mSymbols = cfg.getSymbols();
    int cf = cfg.getConfigFlags();
    mCfgNsEnabled = (cf & CFG_NAMESPACE_AWARE) != 0;
    mCfgReplaceEntities = (cf & CFG_REPLACE_ENTITY_REFS) != 0;

    mAllowXml11EscapedCharsInXml10 = mConfig.willAllowXml11EscapedCharsInXml10();

    mNormalizeLFs = mConfig.willNormalizeLFs();
    mInputBuffer = null;
    mInputPtr = mInputEnd = 0;
    mEntityResolver = res;
    
    mCfgTreatCharRefsAsEntities = mConfig.willTreatCharRefsAsEnts();
    if (mCfgTreatCharRefsAsEntities) {
        mCachedEntities = new HashMap<String,IntEntity>();
    } else {
        mCachedEntities = Collections.emptyMap();
    }
}
 
Example #8
Source File: CatalogSupportBase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testStAXNegative(boolean setUseCatalog, boolean useCatalog, String catalog,
        String xml, XMLResolver resolver, String expected) throws Exception {

        XMLStreamReader streamReader = getStreamReader(
                setUseCatalog, useCatalog, catalog, xml, resolver);
        String text = getText(streamReader, XMLStreamConstants.ENTITY_REFERENCE);
        Assert.assertEquals(text.trim(), expected);
}
 
Example #9
Source File: CatalogSupportBase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testStAX(boolean setUseCatalog, boolean useCatalog, String catalog,
        String xml, XMLResolver resolver, String expected) throws Exception {

        XMLStreamReader streamReader = getStreamReader(
                setUseCatalog, useCatalog, catalog, xml, resolver);
        String text = getText(streamReader, XMLStreamConstants.CHARACTERS);
        Assert.assertEquals(text.trim(), expected);
}
 
Example #10
Source File: SystemIdResolver.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public XMLResolver asXMLResolver() {
  return new XMLResolver() {
    @Override
    public Object resolveEntity(String publicId, String systemId, String baseURI, String namespace) throws XMLStreamException {
      try {
        final InputSource src = SystemIdResolver.this.resolveEntity(null, publicId, baseURI, systemId);
        return (src == null) ? null : src.getByteStream();
      } catch (IOException ioe) {
        throw new XMLStreamException("Cannot resolve entity", ioe);
      }
    }
  };
}
 
Example #11
Source File: StaxEntityResolverWrapper.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public XMLResolver getStaxEntityResolver(){
    return fStaxResolver ;
}
 
Example #12
Source File: StaxEntityResolverWrapper.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/** Creates a new instance of StaxEntityResolverWrapper */
public StaxEntityResolverWrapper(XMLResolver resolver) {
    fStaxResolver = resolver ;
}
 
Example #13
Source File: StaxEntityResolverWrapper.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void setStaxEntityResolver(XMLResolver resolver ){
    fStaxResolver = resolver ;
}
 
Example #14
Source File: StaxEntityResolverWrapper.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/** Creates a new instance of StaxEntityResolverWrapper */
public StaxEntityResolverWrapper(XMLResolver resolver) {
    fStaxResolver = resolver ;
}
 
Example #15
Source File: PropertyManager.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void setProperty(String property, Object value){
    String equivalentProperty = null ;
    if(property == XMLInputFactory.IS_NAMESPACE_AWARE || property.equals(XMLInputFactory.IS_NAMESPACE_AWARE)){
        equivalentProperty = Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE ;
    }
    else if(property == XMLInputFactory.IS_VALIDATING || property.equals(XMLInputFactory.IS_VALIDATING)){
        if( (value instanceof Boolean) && ((Boolean)value).booleanValue()){
            throw new java.lang.IllegalArgumentException("true value of isValidating not supported") ;
        }
    }
    else if(property == STRING_INTERNING || property.equals(STRING_INTERNING)){
        if( (value instanceof Boolean) && !((Boolean)value).booleanValue()){
            throw new java.lang.IllegalArgumentException("false value of " + STRING_INTERNING + "feature is not supported") ;
        }
    }
    else if(property == XMLInputFactory.RESOLVER || property.equals(XMLInputFactory.RESOLVER)){
        //add internal stax property
        supportedProps.put( Constants.XERCES_PROPERTY_PREFIX + Constants.STAX_ENTITY_RESOLVER_PROPERTY , new StaxEntityResolverWrapper((XMLResolver)value)) ;
    }

    /**
     * It's possible for users to set a security manager through the interface.
     * If it's the old SecurityManager, convert it to the new XMLSecurityManager
     */
    if (property.equals(Constants.SECURITY_MANAGER)) {
        fSecurityManager = XMLSecurityManager.convert(value, fSecurityManager);
        supportedProps.put(Constants.SECURITY_MANAGER, fSecurityManager);
        return;
    }
    if (property.equals(Constants.XML_SECURITY_PROPERTY_MANAGER)) {
        if (value == null) {
            fSecurityPropertyMgr = new XMLSecurityPropertyManager();
        } else {
            fSecurityPropertyMgr = (XMLSecurityPropertyManager)value;
        }
        supportedProps.put(Constants.XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
        return;
    }

    //check if the property is managed by security manager
    if (fSecurityManager == null ||
            !fSecurityManager.setLimit(property, XMLSecurityManager.State.APIPROPERTY, value)) {
        //check if the property is managed by security property manager
        if (fSecurityPropertyMgr == null ||
                !fSecurityPropertyMgr.setValue(property, XMLSecurityPropertyManager.State.APIPROPERTY, value)) {
            //fall back to the existing property manager
            supportedProps.put(property, value);
        }
    }

    if(equivalentProperty != null){
        supportedProps.put(equivalentProperty, value ) ;
    }
}
 
Example #16
Source File: XMLInputFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public XMLResolver getXMLResolver() {
    return null;
}
 
Example #17
Source File: StaxEntityResolverWrapper.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Creates a new instance of StaxEntityResolverWrapper */
public StaxEntityResolverWrapper(XMLResolver resolver) {
    fStaxResolver = resolver ;
}
 
Example #18
Source File: XMLInputFactoryWrapper.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public XMLResolver getXMLResolver() {
    return defaultImpl.getXMLResolver();
}
 
Example #19
Source File: XMLInputFactoryWrapper.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void setXMLResolver(XMLResolver resolver) {
    defaultImpl.setXMLResolver(resolver);
}
 
Example #20
Source File: PropertyManager.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void setProperty(String property, Object value){
    String equivalentProperty = null ;
    if(property == XMLInputFactory.IS_NAMESPACE_AWARE || property.equals(XMLInputFactory.IS_NAMESPACE_AWARE)){
        equivalentProperty = Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE ;
    }
    else if(property == XMLInputFactory.IS_VALIDATING || property.equals(XMLInputFactory.IS_VALIDATING)){
        if( (value instanceof Boolean) && ((Boolean)value).booleanValue()){
            throw new java.lang.IllegalArgumentException("true value of isValidating not supported") ;
        }
    }
    else if(property == STRING_INTERNING || property.equals(STRING_INTERNING)){
        if( (value instanceof Boolean) && !((Boolean)value).booleanValue()){
            throw new java.lang.IllegalArgumentException("false value of " + STRING_INTERNING + "feature is not supported") ;
        }
    }
    else if(property == XMLInputFactory.RESOLVER || property.equals(XMLInputFactory.RESOLVER)){
        //add internal stax property
        supportedProps.put( Constants.XERCES_PROPERTY_PREFIX + Constants.STAX_ENTITY_RESOLVER_PROPERTY , new StaxEntityResolverWrapper((XMLResolver)value)) ;
    }

    /**
     * It's possible for users to set a security manager through the interface.
     * If it's the old SecurityManager, convert it to the new XMLSecurityManager
     */
    if (property.equals(Constants.SECURITY_MANAGER)) {
        fSecurityManager = XMLSecurityManager.convert(value, fSecurityManager);
        supportedProps.put(Constants.SECURITY_MANAGER, fSecurityManager);
        return;
    }
    if (property.equals(Constants.XML_SECURITY_PROPERTY_MANAGER)) {
        if (value == null) {
            fSecurityPropertyMgr = new XMLSecurityPropertyManager();
        } else {
            fSecurityPropertyMgr = (XMLSecurityPropertyManager)value;
        }
        supportedProps.put(Constants.XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
        return;
    }

    //check if the property is managed by security manager
    if (fSecurityManager == null ||
            !fSecurityManager.setLimit(property, XMLSecurityManager.State.APIPROPERTY, value)) {
        //check if the property is managed by security property manager
        if (fSecurityPropertyMgr == null ||
                !fSecurityPropertyMgr.setValue(property, XMLSecurityPropertyManager.State.APIPROPERTY, value)) {
            //fall back to the existing property manager
            supportedProps.put(property, value);
        }
    }

    if(equivalentProperty != null){
        supportedProps.put(equivalentProperty, value ) ;
    }
}
 
Example #21
Source File: StaxEntityResolverWrapper.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/** Creates a new instance of StaxEntityResolverWrapper */
public StaxEntityResolverWrapper(XMLResolver resolver) {
    fStaxResolver = resolver ;
}
 
Example #22
Source File: StaxEntityResolverWrapper.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void setStaxEntityResolver(XMLResolver resolver ){
    fStaxResolver = resolver ;
}
 
Example #23
Source File: StaxEntityResolverWrapper.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public XMLResolver getStaxEntityResolver(){
    return fStaxResolver ;
}
 
Example #24
Source File: PropertyManager.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void setProperty(String property, Object value){
    String equivalentProperty = null ;
    if(property == XMLInputFactory.IS_NAMESPACE_AWARE || property.equals(XMLInputFactory.IS_NAMESPACE_AWARE)){
        equivalentProperty = Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE ;
    }
    else if(property == XMLInputFactory.IS_VALIDATING || property.equals(XMLInputFactory.IS_VALIDATING)){
        if( (value instanceof Boolean) && ((Boolean)value).booleanValue()){
            throw new java.lang.IllegalArgumentException("true value of isValidating not supported") ;
        }
    }
    else if(property == STRING_INTERNING || property.equals(STRING_INTERNING)){
        if( (value instanceof Boolean) && !((Boolean)value).booleanValue()){
            throw new java.lang.IllegalArgumentException("false value of " + STRING_INTERNING + "feature is not supported") ;
        }
    }
    else if(property == XMLInputFactory.RESOLVER || property.equals(XMLInputFactory.RESOLVER)){
        //add internal stax property
        supportedProps.put( Constants.XERCES_PROPERTY_PREFIX + Constants.STAX_ENTITY_RESOLVER_PROPERTY , new StaxEntityResolverWrapper((XMLResolver)value)) ;
    }

    /**
     * It's possible for users to set a security manager through the interface.
     * If it's the old SecurityManager, convert it to the new XMLSecurityManager
     */
    if (property.equals(Constants.SECURITY_MANAGER)) {
        fSecurityManager = XMLSecurityManager.convert(value, fSecurityManager);
        supportedProps.put(Constants.SECURITY_MANAGER, fSecurityManager);
        return;
    }
    if (property.equals(Constants.XML_SECURITY_PROPERTY_MANAGER)) {
        if (value == null) {
            fSecurityPropertyMgr = new XMLSecurityPropertyManager();
        } else {
            fSecurityPropertyMgr = (XMLSecurityPropertyManager)value;
        }
        supportedProps.put(Constants.XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
        return;
    }

    //check if the property is managed by security manager
    if (fSecurityManager == null ||
            !fSecurityManager.setLimit(property, XMLSecurityManager.State.APIPROPERTY, value)) {
        //check if the property is managed by security property manager
        if (fSecurityPropertyMgr == null ||
                !fSecurityPropertyMgr.setValue(property, XMLSecurityPropertyManager.State.APIPROPERTY, value)) {
            //fall back to the existing property manager
            supportedProps.put(property, value);
        }
    }

    if(equivalentProperty != null){
        supportedProps.put(equivalentProperty, value ) ;
    }
}
 
Example #25
Source File: StaxEntityResolverWrapper.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Creates a new instance of StaxEntityResolverWrapper */
public StaxEntityResolverWrapper(XMLResolver resolver) {
    fStaxResolver = resolver ;
}
 
Example #26
Source File: StaxEntityResolverWrapper.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void setStaxEntityResolver(XMLResolver resolver ){
    fStaxResolver = resolver ;
}
 
Example #27
Source File: StaxEntityResolverWrapper.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public XMLResolver getStaxEntityResolver(){
    return fStaxResolver ;
}
 
Example #28
Source File: AbstractXMLInputFactory.java    From jettison with Apache License 2.0 4 votes vote down vote up
public XMLResolver getXMLResolver() {
    return null;
}
 
Example #29
Source File: StreamScanner.java    From woodstox with Apache License 2.0 4 votes vote down vote up
/**
 *<p>
 * note: only called from the local expandEntity() method
 */
private EntityDecl expandUnresolvedEntity(String id)
    throws XMLStreamException
{
    XMLResolver resolver = mConfig.getUndeclaredEntityResolver();
    if (resolver != null) {
        /* Ok, we can check for recursion here; but let's only do that
         * if there is any chance that it might get resolved by
         * the special resolver (it must have been resolved this way
         * earlier, too...)
         */
        if (mInput.isOrIsExpandedFrom(id)) {
            throwRecursionError(id);
        }

        WstxInputSource oldInput = mInput;
        oldInput.saveContext(this);
        // null, null -> no public or system ids
        int xmlVersion = mDocXmlVersion;
        // 05-Feb-2006, TSa: If xmlVersion not explicitly known, defaults to 1.0
        if (xmlVersion == XmlConsts.XML_V_UNKNOWN) {
            xmlVersion = XmlConsts.XML_V_10;
        }
        WstxInputSource newInput;
        try {
            newInput = DefaultInputResolver.resolveEntityUsing
                (oldInput, id, null, null, resolver, mConfig, xmlVersion);
            if (mCfgTreatCharRefsAsEntities) {
                return new IntEntity(WstxInputLocation.getEmptyLocation(), newInput.getEntityId(),
                        newInput.getSource(), new char[]{}, WstxInputLocation.getEmptyLocation());
            }
        } catch (IOException ioe) {
            throw constructFromIOE(ioe);
        }
        if (newInput != null) {
            // true -> is external
            initInputSource(newInput, true, id);
            return null;
        }
    }
    handleUndeclaredEntity(id);
    return null;
}
 
Example #30
Source File: ExtEntity.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public abstract WstxInputSource expand(WstxInputSource parent,
                                       XMLResolver res, ReaderConfig cfg,
                                       int xmlVersion)
    throws IOException, XMLStreamException;