FileDocCategorySizeDatePackage
EntityResolver2Wrapper.javaAPI DocJava SE 6 API7570Tue Jun 10 00:22:50 BST 2008com.sun.org.apache.xerces.internal.util

EntityResolver2Wrapper

public class EntityResolver2Wrapper extends Object implements ExternalSubsetResolver

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

author
Michael Glavassevich, IBM
version
$Id: EntityResolver2Wrapper.java,v 1.2.6.1 2005/09/05 07:52:14 neerajbj Exp $

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 com.sun.org.apache.xerces.internal.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 com.sun.org.apache.xerces.internal.xni.parser.XMLInputSourcegetExternalSubset(com.sun.org.apache.xerces.internal.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 com.sun.org.apache.xerces.internal.xni.parser.XMLInputSourceresolveEntity(com.sun.org.apache.xerces.internal.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;