FileDocCategorySizeDatePackage
EntityResolver2Wrapper.javaAPI DocApache Xerces 3.0.17839Fri Sep 14 20:33:52 BST 2007org.apache.xerces.util

EntityResolver2Wrapper

public class EntityResolver2Wrapper extends Object implements org.apache.xerces.impl.ExternalSubsetResolver

This class wraps a SAX entity resolver (EntityResolver2) in an XNI entity resolver.

author
Michael Glavassevich, IBM
version
$Id: EntityResolver2Wrapper.java 447241 2006-09-18 05:12:57Z mrglavas $

Fields Summary
protected EntityResolver2
fEntityResolver
An instance of SAX2 Extensions 1.1's EntityResolver2.
Constructors Summary
public EntityResolver2Wrapper()
Default constructor.

public EntityResolver2Wrapper(EntityResolver2 entityResolver)

Creates a new instance wrapping the given SAX entity resolver.

param
entityResolver the SAX entity resolver to wrap

        setEntityResolver(entityResolver);
    
Methods Summary
private org.apache.xerces.xni.parser.XMLInputSourcecreateXMLInputSource(org.xml.sax.InputSource source, java.lang.String baseURI)
Creates an XMLInputSource from a SAX InputSource.

 
        String publicId = source.getPublicId();
        String systemId = source.getSystemId();
        String baseSystemId = baseURI;
        InputStream byteStream = source.getByteStream();
        Reader charStream = source.getCharacterStream();
        String encoding = source.getEncoding();
        XMLInputSource xmlInputSource =
            new XMLInputSource(publicId, systemId, baseSystemId);
        xmlInputSource.setByteStream(byteStream);
        xmlInputSource.setCharacterStream(charStream);
        xmlInputSource.setEncoding(encoding);
        return xmlInputSource;
        
    
public org.xml.sax.ext.EntityResolver2getEntityResolver()

Returns the SAX entity resolver wrapped by this object.

return
the SAX entity resolver wrapped by this object

        return fEntityResolver;
    
public org.apache.xerces.xni.parser.XMLInputSourcegetExternalSubset(org.apache.xerces.xni.grammars.XMLDTDDescription grammarDescription)

Locates an external subset for documents which do not explicitly provide one. If no external subset is provided, this method should return null.

param
grammarDescription a description of the DTD
throws
XNIException Thrown on general error.
throws
IOException Thrown if resolved entity stream cannot be opened or some other i/o error occurs.

        
        if (fEntityResolver != null) {
            
            String name = grammarDescription.getRootName();
            String baseURI = grammarDescription.getBaseSystemId();
            
            // Resolve using EntityResolver2
            try {
                InputSource inputSource = fEntityResolver.getExternalSubset(name, baseURI);
                return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null;
            }
            // error resolving external subset
            catch (SAXException e) {
                Exception ex = e.getException();
                if (ex == null) {
                    ex = e;
                }
                throw new XNIException(ex);
            }
        }
        
        // unable to resolve external subset
        return null;
        
    
public org.apache.xerces.xni.parser.XMLInputSourceresolveEntity(org.apache.xerces.xni.XMLResourceIdentifier resourceIdentifier)
Resolves an external parsed entity. If the entity cannot be resolved, this method should return null.

param
resourceIdentifier contains the physical co-ordinates of the resource to be resolved
throws
XNIException Thrown on general error.
throws
IOException Thrown if resolved entity stream cannot be opened or some other i/o error occurs.

        
        if (fEntityResolver != null) {
            
            String pubId = resourceIdentifier.getPublicId();
            String sysId = resourceIdentifier.getLiteralSystemId();
            String baseURI = resourceIdentifier.getBaseSystemId();
            String name = null;
            if (resourceIdentifier instanceof XMLDTDDescription) {
                name = "[dtd]";
            }
            else if (resourceIdentifier instanceof XMLEntityDescription) {
                name = ((XMLEntityDescription) resourceIdentifier).getEntityName();
            }
            
            // When both pubId and sysId are null, the user's entity resolver
            // can do nothing about it. We'd better not bother calling it.
            // This happens when the resourceIdentifier is a GrammarDescription,
            // which describes a schema grammar of some namespace, but without
            // any schema location hint. -Sg
            if (pubId == null && sysId == null) {
                return null;
            }
            
            // Resolve using EntityResolver2
            try {
                InputSource inputSource = 
                    fEntityResolver.resolveEntity(name, pubId, baseURI, sysId);
                return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null;
            }
            // error resolving entity
            catch (SAXException e) {
                Exception ex = e.getException();
                if (ex == null) {
                    ex = e;
                }
                throw new XNIException(ex);
            }   
        }
        
        // unable to resolve entity
        return null;
        
    
public voidsetEntityResolver(org.xml.sax.ext.EntityResolver2 entityResolver)

Sets the SAX entity resolver wrapped by this object.

param
entityResolver the SAX entity resolver to wrap

        fEntityResolver = entityResolver;