1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.util;
  23 
  24 
  25 import com.sun.org.apache.xerces.internal.xni.XNIException;
  26 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  27 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  28 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
  29 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  30 
  31 import org.w3c.dom.ls.LSResourceResolver;
  32 import org.w3c.dom.ls.LSInput;
  33 
  34 import java.io.InputStream;
  35 import java.io.IOException;
  36 import java.io.Reader;
  37 import java.io.StringReader;
  38 
  39 
  40 /**
  41  * This class wraps DOM entity resolver to XNI entity resolver.
  42  *
  43  * @see LSResourceResolver
  44  *
  45  * @author Gopal Sharma, SUN MicroSystems Inc.
  46  * @author Elena Litani, IBM
  47  * @author Ramesh Mandava, Sun Microsystems
  48  */
  49 public class DOMEntityResolverWrapper
  50     implements XMLEntityResolver {
  51 
  52     //
  53     // Data
  54     //
  55 
  56     /** XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
  57     private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml";
  58 
  59     /** XML Schema constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
  60     private static final String XSD_TYPE = "http://www.w3.org/2001/XMLSchema";
  61 
  62     /** The DOM entity resolver. */
  63     protected LSResourceResolver fEntityResolver;
  64 
  65     //
  66     // Constructors
  67     //
  68 
  69     /** Default constructor. */
  70     public DOMEntityResolverWrapper() {}
  71 
  72     /** Wraps the specified DOM entity resolver. */
  73     public DOMEntityResolverWrapper(LSResourceResolver entityResolver) {
  74         setEntityResolver(entityResolver);
  75     } // LSResourceResolver
  76 
  77     //
  78     // Public methods
  79     //
  80 
  81     /** Sets the DOM entity resolver. */
  82     public void setEntityResolver(LSResourceResolver entityResolver) {
  83         fEntityResolver = entityResolver;
  84     } // setEntityResolver(LSResourceResolver)
  85 
  86     /** Returns the DOM entity resolver. */
  87     public LSResourceResolver getEntityResolver() {
  88         return fEntityResolver;
  89     } // getEntityResolver():LSResourceResolver
  90 
  91     //
  92     // XMLEntityResolver methods
  93     //
  94 
  95     /**
  96      * Resolves an external parsed entity. If the entity cannot be
  97      * resolved, this method should return null.
  98      *
  99      * @param resourceIdentifier        description of the resource to be revsoved
 100      * @throws XNIException Thrown on general error.
 101      * @throws IOException  Thrown if resolved entity stream cannot be
 102      *                      opened or some other i/o error occurs.
 103      */
 104     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
 105         throws XNIException, IOException {
 106         // resolve entity using DOM entity resolver
 107         if (fEntityResolver != null) {
 108             // For entity resolution the type of the resource would be  XML TYPE
 109             // DOM L3 LS spec mention only the XML 1.0 recommendation right now
 110             LSInput inputSource =
 111                 resourceIdentifier == null
 112                     ? fEntityResolver.resolveResource(
 113                         null,
 114                         null,
 115                         null,
 116                         null,
 117                         null)
 118                     : fEntityResolver.resolveResource(
 119                         getType(resourceIdentifier),
 120                         resourceIdentifier.getNamespace(),
 121                         resourceIdentifier.getPublicId(),
 122                         resourceIdentifier.getLiteralSystemId(),
 123                         resourceIdentifier.getBaseSystemId());
 124             if (inputSource != null) {
 125                 String publicId = inputSource.getPublicId();
 126                 String systemId = inputSource.getSystemId();
 127                 String baseSystemId = inputSource.getBaseURI();
 128                 InputStream byteStream = inputSource.getByteStream();
 129                 Reader charStream = inputSource.getCharacterStream();
 130                 String encoding = inputSource.getEncoding();
 131                 String data = inputSource.getStringData();
 132 
 133                 /**
 134                  * An LSParser looks at inputs specified in LSInput in
 135                  * the following order: characterStream, byteStream,
 136                  * stringData, systemId, publicId.
 137                  */
 138                 XMLInputSource xmlInputSource =
 139                     new XMLInputSource(publicId, systemId, baseSystemId, true);
 140 
 141                 if (charStream != null) {
 142                     xmlInputSource.setCharacterStream(charStream);
 143                 }
 144                 else if (byteStream != null) {
 145                     xmlInputSource.setByteStream((InputStream) byteStream);
 146                 }
 147                 else if (data != null && data.length() != 0) {
 148                     xmlInputSource.setCharacterStream(new StringReader(data));
 149                 }
 150                 xmlInputSource.setEncoding(encoding);
 151                 return xmlInputSource;
 152             }
 153         }
 154 
 155         // unable to resolve entity
 156         return null;
 157 
 158     } // resolveEntity(String,String,String):XMLInputSource
 159 
 160     /** Determines the type of resource being resolved **/
 161     private String getType(XMLResourceIdentifier resourceIdentifier) {
 162         if (resourceIdentifier instanceof XMLGrammarDescription) {
 163             XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
 164             if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
 165                 return XSD_TYPE;
 166             }
 167         }
 168         return XML_TYPE;
 169     } // getType(XMLResourceIdentifier):String
 170 
 171 } // DOMEntityResolverWrapper