1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001, 2002,2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.util;
  22 
  23 import java.io.InputStream;
  24 import java.io.IOException;
  25 import java.io.Reader;
  26 
  27 import com.sun.org.apache.xerces.internal.xni.XNIException;
  28 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  29 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
  30 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  31 
  32 import org.xml.sax.EntityResolver;
  33 import org.xml.sax.InputSource;
  34 import org.xml.sax.SAXException;
  35 
  36 /**
  37  * This class wraps a SAX entity resolver in an XNI entity resolver.
  38  *
  39  * @see EntityResolver
  40  *
  41  * @author Andy Clark, IBM
  42  *
  43  */
  44 public class EntityResolverWrapper
  45     implements XMLEntityResolver {
  46 
  47     //
  48     // Data
  49     //
  50 
  51     /** The SAX entity resolver. */
  52     protected EntityResolver fEntityResolver;
  53 
  54     //
  55     // Constructors
  56     //
  57 
  58     /** Default constructor. */
  59     public EntityResolverWrapper() {}
  60 
  61     /** Wraps the specified SAX entity resolver. */
  62     public EntityResolverWrapper(EntityResolver entityResolver) {
  63         setEntityResolver(entityResolver);
  64     } // <init>(EntityResolver)
  65 
  66     //
  67     // Public methods
  68     //
  69 
  70     /** Sets the SAX entity resolver. */
  71     public void setEntityResolver(EntityResolver entityResolver) {
  72         fEntityResolver = entityResolver;
  73     } // setEntityResolver(EntityResolver)
  74 
  75     /** Returns the SAX entity resolver. */
  76     public EntityResolver getEntityResolver() {
  77         return fEntityResolver;
  78     } // getEntityResolver():EntityResolver
  79 
  80     //
  81     // XMLEntityResolver methods
  82     //
  83 
  84     /**
  85      * Resolves an external parsed entity. If the entity cannot be
  86      * resolved, this method should return null.
  87      *
  88      * @param resourceIdentifier        contains the physical co-ordinates of the resource to be resolved
  89      *
  90      * @throws XNIException Thrown on general error.
  91      * @throws IOException  Thrown if resolved entity stream cannot be
  92      *                      opened or some other i/o error occurs.
  93      */
  94     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
  95         throws XNIException, IOException {
  96 
  97         // When both pubId and sysId are null, the user's entity resolver
  98         // can do nothing about it. We'd better not bother calling it.
  99         // This happens when the resourceIdentifier is a GrammarDescription,
 100         // which describes a schema grammar of some namespace, but without
 101         // any schema location hint. -Sg
 102         String pubId = resourceIdentifier.getPublicId();
 103         String sysId = resourceIdentifier.getExpandedSystemId();
 104         if (pubId == null && sysId == null)
 105             return null;
 106 
 107         // resolve entity using SAX entity resolver
 108         if (fEntityResolver != null && resourceIdentifier != null) {
 109             try {
 110                 InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId);
 111                 if (inputSource != null) {
 112                     String publicId = inputSource.getPublicId();
 113                     String systemId = inputSource.getSystemId();
 114                     String baseSystemId = resourceIdentifier.getBaseSystemId();
 115                     InputStream byteStream = inputSource.getByteStream();
 116                     Reader charStream = inputSource.getCharacterStream();
 117                     String encoding = inputSource.getEncoding();
 118                     XMLInputSource xmlInputSource =
 119                         new XMLInputSource(publicId, systemId, baseSystemId, true);
 120                     xmlInputSource.setByteStream(byteStream);
 121                     xmlInputSource.setCharacterStream(charStream);
 122                     xmlInputSource.setEncoding(encoding);
 123                     return xmlInputSource;
 124                 }
 125             }
 126 
 127             // error resolving entity
 128             catch (SAXException e) {
 129                 Exception ex = e.getException();
 130                 if (ex == null) {
 131                     ex = e;
 132                 }
 133                 throw new XNIException(ex);
 134             }
 135         }
 136 
 137         // unable to resolve entity
 138         return null;
 139 
 140     } // resolveEntity(String,String,String):XMLInputSource
 141 }