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

Save This Page
Home » Xerces-J-src.2.9.1 » org.apache.xerces » util » [javadoc | source]