1. /*
  2. * Copyright 2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.sun.org.apache.xerces.internal.util;
  17. import java.io.InputStream;
  18. import java.io.IOException;
  19. import java.io.Reader;
  20. import com.sun.org.apache.xerces.internal.impl.ExternalSubsetResolver;
  21. import com.sun.org.apache.xerces.internal.impl.XMLEntityDescription;
  22. import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  23. import com.sun.org.apache.xerces.internal.xni.XNIException;
  24. import com.sun.org.apache.xerces.internal.xni.grammars.XMLDTDDescription;
  25. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  26. import org.xml.sax.EntityResolver;
  27. import org.xml.sax.ext.EntityResolver2;
  28. import org.xml.sax.InputSource;
  29. import org.xml.sax.SAXException;
  30. /**
  31. * <p>This class wraps a SAX entity resolver (EntityResolver2) in an XNI entity resolver.</p>
  32. *
  33. * @author Michael Glavassevich, IBM
  34. *
  35. * @version $Id: EntityResolver2Wrapper.java,v 1.2 2004/05/07 09:00:32 vk112360 Exp $
  36. */
  37. public class EntityResolver2Wrapper
  38. implements ExternalSubsetResolver {
  39. //
  40. // Data
  41. //
  42. /** An instance of SAX2 Extensions 1.1's EntityResolver2. */
  43. protected EntityResolver2 fEntityResolver;
  44. //
  45. // Constructors
  46. //
  47. /** Default constructor. */
  48. public EntityResolver2Wrapper() {}
  49. /**
  50. * <p>Creates a new instance wrapping the given SAX entity resolver.</p>
  51. *
  52. * @param entityResolver the SAX entity resolver to wrap
  53. */
  54. public EntityResolver2Wrapper(EntityResolver2 entityResolver) {
  55. setEntityResolver(entityResolver);
  56. } // <init>(EntityResolver2)
  57. //
  58. // Public methods
  59. //
  60. /**
  61. * <p>Sets the SAX entity resolver wrapped by this object.</p>
  62. *
  63. * @param entityResolver the SAX entity resolver to wrap
  64. */
  65. public void setEntityResolver(EntityResolver2 entityResolver) {
  66. fEntityResolver = entityResolver;
  67. } // setEntityResolver(EntityResolver2)
  68. /**
  69. * <p>Returns the SAX entity resolver wrapped by this object.</p>
  70. *
  71. * @return the SAX entity resolver wrapped by this object
  72. */
  73. public EntityResolver2 getEntityResolver() {
  74. return fEntityResolver;
  75. } // getEntityResolver():EntityResolver2
  76. //
  77. // ExternalSubsetResolver methods
  78. //
  79. /**
  80. * <p>Locates an external subset for documents which do not explicitly
  81. * provide one. If no external subset is provided, this method should
  82. * return <code>null</code>.</p>
  83. *
  84. * @param grammarDescription a description of the DTD
  85. *
  86. * @throws XNIException Thrown on general error.
  87. * @throws IOException Thrown if resolved entity stream cannot be
  88. * opened or some other i/o error occurs.
  89. */
  90. public XMLInputSource getExternalSubset(XMLDTDDescription grammarDescription)
  91. throws XNIException, IOException {
  92. if (fEntityResolver != null) {
  93. String name = grammarDescription.getRootName();
  94. String baseURI = grammarDescription.getBaseSystemId();
  95. // Resolve using EntityResolver2
  96. try {
  97. InputSource inputSource = fEntityResolver.getExternalSubset(name, baseURI);
  98. return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null;
  99. }
  100. // error resolving external subset
  101. catch (SAXException e) {
  102. Exception ex = e.getException();
  103. if (ex == null) {
  104. ex = e;
  105. }
  106. throw new XNIException(ex);
  107. }
  108. }
  109. // unable to resolve external subset
  110. return null;
  111. } // getExternalSubset(XMLDTDDescription):XMLInputSource
  112. //
  113. // XMLEntityResolver methods
  114. //
  115. /**
  116. * Resolves an external parsed entity. If the entity cannot be
  117. * resolved, this method should return null.
  118. *
  119. * @param resourceIdentifier contains the physical co-ordinates of the resource to be resolved
  120. *
  121. * @throws XNIException Thrown on general error.
  122. * @throws IOException Thrown if resolved entity stream cannot be
  123. * opened or some other i/o error occurs.
  124. */
  125. public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
  126. throws XNIException, IOException {
  127. if (fEntityResolver != null) {
  128. String pubId = resourceIdentifier.getPublicId();
  129. String sysId = resourceIdentifier.getExpandedSystemId();
  130. String baseURI = resourceIdentifier.getBaseSystemId();
  131. String name = null;
  132. if (resourceIdentifier instanceof XMLDTDDescription) {
  133. name = "[dtd]";
  134. }
  135. else if (resourceIdentifier instanceof XMLEntityDescription) {
  136. name = ((XMLEntityDescription) resourceIdentifier).getEntityName();
  137. }
  138. // When all of the parameters are null, the user's entity resolver
  139. // can do nothing about it. We'd better not bother calling it.
  140. // This happens when the resourceIdentifier is a GrammarDescription,
  141. // which describes a schema grammar of some namespace, but without
  142. // any schema location hint. -Sg
  143. if (pubId == null && sysId == null && baseURI == null && name == null) {
  144. return null;
  145. }
  146. // Resolve using EntityResolver2
  147. try {
  148. InputSource inputSource =
  149. fEntityResolver.resolveEntity(name, pubId, baseURI, sysId);
  150. return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null;
  151. }
  152. // error resolving entity
  153. catch (SAXException e) {
  154. Exception ex = e.getException();
  155. if (ex == null) {
  156. ex = e;
  157. }
  158. throw new XNIException(ex);
  159. }
  160. }
  161. // unable to resolve entity
  162. return null;
  163. } // resolveEntity(XMLResourceIdentifier):XMLInputSource
  164. /**
  165. * Creates an XMLInputSource from a SAX InputSource.
  166. */
  167. private XMLInputSource createXMLInputSource(InputSource source, String baseURI) {
  168. String publicId = source.getPublicId();
  169. String systemId = source.getSystemId();
  170. String baseSystemId = baseURI;
  171. InputStream byteStream = source.getByteStream();
  172. Reader charStream = source.getCharacterStream();
  173. String encoding = source.getEncoding();
  174. XMLInputSource xmlInputSource =
  175. new XMLInputSource(publicId, systemId, baseSystemId);
  176. xmlInputSource.setByteStream(byteStream);
  177. xmlInputSource.setCharacterStream(charStream);
  178. xmlInputSource.setEncoding(encoding);
  179. return xmlInputSource;
  180. } // createXMLInputSource(InputSource,String):XMLInputSource
  181. } // class EntityResolver2Wrapper