1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.xni.parser;
  58. import com.sun.org.apache.xerces.internal.util.XMLInputSourceAdaptor;
  59. import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  60. import java.io.InputStream;
  61. import java.io.Reader;
  62. import javax.xml.transform.stream.StreamSource;
  63. /**
  64. * This class represents an input source for an XML document. The
  65. * basic properties of an input source are the following:
  66. * <ul>
  67. * <li>public identifier</li>
  68. * <li>system identifier</li>
  69. * <li>byte stream or character stream</li>
  70. * <li>
  71. * </ul>
  72. *
  73. * @author Andy Clark, IBM
  74. *
  75. * @version $Id: XMLInputSource.java,v 1.4 2002/01/29 01:15:19 lehors Exp $
  76. */
  77. public class XMLInputSource {
  78. //
  79. // Data
  80. //
  81. /** Public identifier. */
  82. protected String fPublicId;
  83. /** System identifier. */
  84. protected String fSystemId;
  85. /** Base system identifier. */
  86. protected String fBaseSystemId;
  87. /** Byte stream. */
  88. protected InputStream fByteStream;
  89. /** Character stream. */
  90. protected Reader fCharStream;
  91. /** Encoding. */
  92. protected String fEncoding;
  93. //
  94. // Constructors
  95. //
  96. /**
  97. * Constructs an input source from just the public and system
  98. * identifiers, leaving resolution of the entity and opening of
  99. * the input stream up to the caller.
  100. *
  101. * @param publicId The public identifier, if known.
  102. * @param systemId The system identifier. This value should
  103. * always be set, if possible, and can be
  104. * relative or absolute. If the system identifier
  105. * is relative, then the base system identifier
  106. * should be set.
  107. * @param baseSystemId The base system identifier. This value should
  108. * always be set to the fully expanded URI of the
  109. * base system identifier, if possible.
  110. */
  111. public XMLInputSource(String publicId, String systemId,
  112. String baseSystemId) {
  113. fPublicId = publicId;
  114. fSystemId = systemId;
  115. fBaseSystemId = baseSystemId;
  116. } // <init>(String,String,String)
  117. /**
  118. * Constructs an input source from a XMLResourceIdentifier
  119. * object, leaving resolution of the entity and opening of
  120. * the input stream up to the caller.
  121. *
  122. * @param resourceIdentifier the XMLResourceIdentifier containing the information
  123. */
  124. public XMLInputSource(XMLResourceIdentifier resourceIdentifier) {
  125. fPublicId = resourceIdentifier.getPublicId();
  126. fSystemId = resourceIdentifier.getLiteralSystemId();
  127. fBaseSystemId = resourceIdentifier.getBaseSystemId();
  128. } // <init>(XMLResourceIdentifier)
  129. /**
  130. * Constructs an input source from a byte stream.
  131. *
  132. * @param publicId The public identifier, if known.
  133. * @param systemId The system identifier. This value should
  134. * always be set, if possible, and can be
  135. * relative or absolute. If the system identifier
  136. * is relative, then the base system identifier
  137. * should be set.
  138. * @param baseSystemId The base system identifier. This value should
  139. * always be set to the fully expanded URI of the
  140. * base system identifier, if possible.
  141. * @param byteStream The byte stream.
  142. * @param encoding The encoding of the byte stream, if known.
  143. */
  144. public XMLInputSource(String publicId, String systemId,
  145. String baseSystemId, InputStream byteStream,
  146. String encoding) {
  147. fPublicId = publicId;
  148. fSystemId = systemId;
  149. fBaseSystemId = baseSystemId;
  150. fByteStream = byteStream;
  151. fEncoding = encoding;
  152. } // <init>(String,String,String,InputStream,String)
  153. /**
  154. * Constructs an input source from a character stream.
  155. *
  156. * @param publicId The public identifier, if known.
  157. * @param systemId The system identifier. This value should
  158. * always be set, if possible, and can be
  159. * relative or absolute. If the system identifier
  160. * is relative, then the base system identifier
  161. * should be set.
  162. * @param baseSystemId The base system identifier. This value should
  163. * always be set to the fully expanded URI of the
  164. * base system identifier, if possible.
  165. * @param charStream The character stream.
  166. * @param encoding The original encoding of the byte stream
  167. * used by the reader, if known.
  168. */
  169. public XMLInputSource(String publicId, String systemId,
  170. String baseSystemId, Reader charStream,
  171. String encoding) {
  172. fPublicId = publicId;
  173. fSystemId = systemId;
  174. fBaseSystemId = baseSystemId;
  175. fCharStream = charStream;
  176. fEncoding = encoding;
  177. } // <init>(String,String,String,Reader,String)
  178. /**
  179. * Constructs an input source from {@link StreamSource}.
  180. */
  181. public XMLInputSource( StreamSource source ) {
  182. fPublicId = source.getPublicId();
  183. fSystemId = source.getSystemId();
  184. fCharStream = source.getReader();
  185. fByteStream = source.getInputStream();
  186. }
  187. //
  188. // Public methods
  189. //
  190. /**
  191. * Sets the public identifier.
  192. *
  193. * @param publicId The new public identifier.
  194. */
  195. public void setPublicId(String publicId) {
  196. fPublicId = publicId;
  197. } // setPublicId(String)
  198. /** Returns the public identifier. */
  199. public String getPublicId() {
  200. return fPublicId;
  201. } // getPublicId():String
  202. /**
  203. * Sets the system identifier.
  204. *
  205. * @param systemId The new system identifier.
  206. */
  207. public void setSystemId(String systemId) {
  208. fSystemId = systemId;
  209. } // setSystemId(String)
  210. /** Returns the system identifier. */
  211. public String getSystemId() {
  212. return fSystemId;
  213. } // getSystemId():String
  214. /**
  215. * Sets the base system identifier.
  216. *
  217. * @param baseSystemId The new base system identifier.
  218. */
  219. public void setBaseSystemId(String baseSystemId) {
  220. fBaseSystemId = baseSystemId;
  221. } // setBaseSystemId(String)
  222. /** Returns the base system identifier. */
  223. public String getBaseSystemId() {
  224. return fBaseSystemId;
  225. } // getBaseSystemId():String
  226. /**
  227. * Sets the byte stream. If the byte stream is not already opened
  228. * when this object is instantiated, then the code that opens the
  229. * stream should also set the byte stream on this object. Also, if
  230. * the encoding is auto-detected, then the encoding should also be
  231. * set on this object.
  232. *
  233. * @param byteStream The new byte stream.
  234. */
  235. public void setByteStream(InputStream byteStream) {
  236. fByteStream = byteStream;
  237. } // setByteStream(InputSource)
  238. /** Returns the byte stream. */
  239. public InputStream getByteStream() {
  240. return fByteStream;
  241. } // getByteStream():InputStream
  242. /**
  243. * Sets the character stream. If the character stream is not already
  244. * opened when this object is instantiated, then the code that opens
  245. * the stream should also set the character stream on this object.
  246. * Also, the encoding of the byte stream used by the reader should
  247. * also be set on this object, if known.
  248. *
  249. * @param charStream The new character stream.
  250. *
  251. * @see #setEncoding
  252. */
  253. public void setCharacterStream(Reader charStream) {
  254. fCharStream = charStream;
  255. } // setCharacterStream(Reader)
  256. /** Returns the character stream. */
  257. public Reader getCharacterStream() {
  258. return fCharStream;
  259. } // getCharacterStream():Reader
  260. /**
  261. * Sets the encoding of the stream.
  262. *
  263. * @param encoding The new encoding.
  264. */
  265. public void setEncoding(String encoding) {
  266. fEncoding = encoding;
  267. } // setEncoding(String)
  268. /** Returns the encoding of the stream, or null if not known. */
  269. public String getEncoding() {
  270. return fEncoding;
  271. } // getEncoding():String
  272. /**
  273. * Wraps this object into a {@link Source} object.
  274. */
  275. public final XMLInputSourceAdaptor toSource() {
  276. return new XMLInputSourceAdaptor(this);
  277. }
  278. } // class XMLInputSource