286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001, 2002,2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.util;
286N/A
286N/Aimport java.io.InputStream;
286N/Aimport java.io.IOException;
286N/Aimport java.io.Reader;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.xni.XNIException;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
286N/A
286N/Aimport org.xml.sax.EntityResolver;
286N/Aimport org.xml.sax.InputSource;
286N/Aimport org.xml.sax.SAXException;
286N/A
286N/A/**
286N/A * This class wraps a SAX entity resolver in an XNI entity resolver.
286N/A *
286N/A * @see EntityResolver
286N/A *
286N/A * @author Andy Clark, IBM
286N/A *
286N/A */
286N/Apublic class EntityResolverWrapper
286N/A implements XMLEntityResolver {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** The SAX entity resolver. */
286N/A protected EntityResolver fEntityResolver;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A public EntityResolverWrapper() {}
286N/A
286N/A /** Wraps the specified SAX entity resolver. */
286N/A public EntityResolverWrapper(EntityResolver entityResolver) {
286N/A setEntityResolver(entityResolver);
286N/A } // <init>(EntityResolver)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /** Sets the SAX entity resolver. */
286N/A public void setEntityResolver(EntityResolver entityResolver) {
286N/A fEntityResolver = entityResolver;
286N/A } // setEntityResolver(EntityResolver)
286N/A
286N/A /** Returns the SAX entity resolver. */
286N/A public EntityResolver getEntityResolver() {
286N/A return fEntityResolver;
286N/A } // getEntityResolver():EntityResolver
286N/A
286N/A //
286N/A // XMLEntityResolver methods
286N/A //
286N/A
286N/A /**
286N/A * Resolves an external parsed entity. If the entity cannot be
286N/A * resolved, this method should return null.
286N/A *
286N/A * @param resourceIdentifier contains the physical co-ordinates of the resource to be resolved
286N/A *
286N/A * @throws XNIException Thrown on general error.
286N/A * @throws IOException Thrown if resolved entity stream cannot be
286N/A * opened or some other i/o error occurs.
286N/A */
286N/A public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
286N/A throws XNIException, IOException {
286N/A
286N/A // When both pubId and sysId are null, the user's entity resolver
286N/A // can do nothing about it. We'd better not bother calling it.
286N/A // This happens when the resourceIdentifier is a GrammarDescription,
286N/A // which describes a schema grammar of some namespace, but without
286N/A // any schema location hint. -Sg
286N/A String pubId = resourceIdentifier.getPublicId();
286N/A String sysId = resourceIdentifier.getExpandedSystemId();
286N/A if (pubId == null && sysId == null)
286N/A return null;
286N/A
286N/A // resolve entity using SAX entity resolver
286N/A if (fEntityResolver != null && resourceIdentifier != null) {
286N/A try {
286N/A InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId);
286N/A if (inputSource != null) {
286N/A String publicId = inputSource.getPublicId();
286N/A String systemId = inputSource.getSystemId();
286N/A String baseSystemId = resourceIdentifier.getBaseSystemId();
286N/A InputStream byteStream = inputSource.getByteStream();
286N/A Reader charStream = inputSource.getCharacterStream();
286N/A String encoding = inputSource.getEncoding();
286N/A XMLInputSource xmlInputSource =
286N/A new XMLInputSource(publicId, systemId, baseSystemId);
286N/A xmlInputSource.setByteStream(byteStream);
286N/A xmlInputSource.setCharacterStream(charStream);
286N/A xmlInputSource.setEncoding(encoding);
286N/A return xmlInputSource;
286N/A }
286N/A }
286N/A
286N/A // error resolving entity
286N/A catch (SAXException e) {
286N/A Exception ex = e.getException();
286N/A if (ex == null) {
286N/A ex = e;
286N/A }
286N/A throw new XNIException(ex);
286N/A }
286N/A }
286N/A
286N/A // unable to resolve entity
286N/A return null;
286N/A
286N/A } // resolveEntity(String,String,String):XMLInputSource
286N/A}