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

DOMEntityResolverWrapper

public class DOMEntityResolverWrapper extends Object implements org.apache.xerces.xni.parser.XMLEntityResolver
This class wraps DOM entity resolver to XNI entity resolver.
see
LSResourceResolver
author
Gopal Sharma, SUN MicroSystems Inc.
author
Elena Litani, IBM
author
Ramesh Mandava, Sun Microsystems
version
$Id: DOMEntityResolverWrapper.java 447241 2006-09-18 05:12:57Z mrglavas $

Fields Summary
private static final String
XML_TYPE
XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107"
private static final String
XSD_TYPE
XML Schema constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107"
protected LSResourceResolver
fEntityResolver
The DOM entity resolver.
Constructors Summary
public DOMEntityResolverWrapper()
Default constructor.


    //
    // Constructors
    //

       
      
public DOMEntityResolverWrapper(LSResourceResolver entityResolver)
Wraps the specified DOM entity resolver.

        setEntityResolver(entityResolver);
    
Methods Summary
public org.w3c.dom.ls.LSResourceResolvergetEntityResolver()
Returns the DOM entity resolver.

        return fEntityResolver;
    
private java.lang.StringgetType(org.apache.xerces.xni.XMLResourceIdentifier resourceIdentifier)
Determines the type of resource being resolved

        if (resourceIdentifier instanceof XMLGrammarDescription) {
            XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
            if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
                return XSD_TYPE;
            }
        }
        return XML_TYPE;
    
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 description of the resource to be revsoved
throws
XNIException Thrown on general error.
throws
IOException Thrown if resolved entity stream cannot be opened or some other i/o error occurs.

        // resolve entity using DOM entity resolver
        if (fEntityResolver != null) {
            // For entity resolution the type of the resource would be  XML TYPE
            // DOM L3 LS spec mention only the XML 1.0 recommendation right now
            LSInput inputSource =
                resourceIdentifier == null
                    ? fEntityResolver.resolveResource(
                        null,
                        null,
                        null,
                        null,
                        null)
                    : fEntityResolver.resolveResource(
                        getType(resourceIdentifier),
                        resourceIdentifier.getNamespace(),
                        resourceIdentifier.getPublicId(),
                        resourceIdentifier.getLiteralSystemId(),
                        resourceIdentifier.getBaseSystemId());
            if (inputSource != null) {
                String publicId = inputSource.getPublicId();
                String systemId = inputSource.getSystemId();
                String baseSystemId = inputSource.getBaseURI();
                InputStream byteStream = inputSource.getByteStream();
                Reader charStream = inputSource.getCharacterStream();
                String encoding = inputSource.getEncoding();
                String data = inputSource.getStringData();

                /**
                 * An LSParser looks at inputs specified in LSInput in
                 * the following order: characterStream, byteStream,
                 * stringData, systemId, publicId.
                 */          
                XMLInputSource xmlInputSource =
                    new XMLInputSource(publicId, systemId, baseSystemId);
                
                if (charStream != null) {
                    xmlInputSource.setCharacterStream(charStream);
                }
                else if (byteStream != null) {
                    xmlInputSource.setByteStream((InputStream) byteStream);
                }
                else if (data != null && data.length() != 0) {
                    xmlInputSource.setCharacterStream(new StringReader(data));
                }
                xmlInputSource.setEncoding(encoding);
                return xmlInputSource;
            }
        }

        // unable to resolve entity
        return null;

    
public voidsetEntityResolver(org.w3c.dom.ls.LSResourceResolver entityResolver)
Sets the DOM entity resolver.

        fEntityResolver = entityResolver;