1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  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 import javax.xml.catalog.CatalogException;
  32 
  33 import org.xml.sax.EntityResolver;
  34 import org.xml.sax.InputSource;
  35 import org.xml.sax.SAXException;
  36 
  37 /**
  38  * This class wraps a SAX entity resolver in an XNI entity resolver.
  39  *
  40  * @see EntityResolver
  41  *
  42  * @author Andy Clark, IBM
  43  *
  44  */
  45 public class EntityResolverWrapper
  46     implements XMLEntityResolver {
  47 
  48     //
  49     // Data
  50     //
  51 
  52     /** The SAX entity resolver. */
  53     protected EntityResolver fEntityResolver;
  54 
  55     //
  56     // Constructors
  57     //
  58 
  59     /** Default constructor. */
  60     public EntityResolverWrapper() {}
  61 
  62     /** Wraps the specified SAX entity resolver. */
  63     public EntityResolverWrapper(EntityResolver entityResolver) {
  64         setEntityResolver(entityResolver);
  65     } // <init>(EntityResolver)
  66 
  67     //
  68     // Public methods
  69     //
  70 
  71     /** Sets the SAX entity resolver. */
  72     public void setEntityResolver(EntityResolver entityResolver) {
  73         fEntityResolver = entityResolver;
  74     } // setEntityResolver(EntityResolver)
  75 
  76     /** Returns the SAX entity resolver. */
  77     public EntityResolver getEntityResolver() {
  78         return fEntityResolver;
  79     } // getEntityResolver():EntityResolver
  80 
  81     //
  82     // XMLEntityResolver methods
  83     //
  84 
  85     /**
  86      * Resolves an external parsed entity. If the entity cannot be
  87      * resolved, this method should return null.
  88      *
  89      * @param resourceIdentifier        contains the physical co-ordinates of the resource to be resolved
  90      *
  91      * @throws XNIException Thrown on general error.
  92      * @throws IOException  Thrown if resolved entity stream cannot be
  93      *                      opened or some other i/o error occurs.
  94      */
  95     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
  96         throws XNIException, IOException {
  97 
  98         // When both pubId and sysId are null, the user's entity resolver
  99         // can do nothing about it. We'd better not bother calling it.
 100         // This happens when the resourceIdentifier is a GrammarDescription,
 101         // which describes a schema grammar of some namespace, but without
 102         // any schema location hint. -Sg
 103         String pubId = resourceIdentifier.getPublicId();
 104         String sysId = resourceIdentifier.getExpandedSystemId();
 105         if (pubId == null && sysId == null)
 106             return null;
 107 
 108         // resolve entity using SAX entity resolver
 109         if (fEntityResolver != null && resourceIdentifier != null) {
 110             try {
 111                 InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId);
 112                 if (inputSource != null) {
 113                     String publicId = inputSource.getPublicId();
 114                     String systemId = inputSource.getSystemId();
 115                     String baseSystemId = resourceIdentifier.getBaseSystemId();
 116                     InputStream byteStream = inputSource.getByteStream();
 117                     Reader charStream = inputSource.getCharacterStream();
 118                     String encoding = inputSource.getEncoding();
 119                     XMLInputSource xmlInputSource =
 120                         new XMLInputSource(publicId, systemId, baseSystemId, true);
 121                     xmlInputSource.setByteStream(byteStream);
 122                     xmlInputSource.setCharacterStream(charStream);
 123                     xmlInputSource.setEncoding(encoding);
 124                     return xmlInputSource;
 125                 }
 126             }
 127 
 128             // error resolving entity
 129             catch (SAXException e) {
 130                 Exception ex = e.getException();
 131                 if (ex == null) {
 132                     ex = e;
 133                 }
 134                 throw new XNIException(ex);
 135             }
 136 
 137             catch (CatalogException e) {
 138                 throw new XNIException(e);
 139             }
 140         }
 141 
 142         // unable to resolve entity
 143         return null;
 144 
 145     } // resolveEntity(String,String,String):XMLInputSource
 146 }