1. // $Id: StreamSource.java,v 1.6.12.3 2004/07/13 22:27:51 jsuttor Exp $
  2. /*
  3. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  4. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  5. */
  6. /*
  7. * @(#)StreamSource.java 1.16 04/07/13
  8. */
  9. package javax.xml.transform.stream;
  10. import java.io.File;
  11. import java.io.InputStream;
  12. import java.io.Reader;
  13. import javax.xml.transform.Source;
  14. /**
  15. * <p>Acts as an holder for a transformation Source in the form
  16. * of a stream of XML markup.</p>
  17. *
  18. * <p><em>Note:</em> Due to their internal use of either a {@link Reader} or {@link InputStream} instance,
  19. * <code>StreamSource</code> instances may only be used once.</p>
  20. *
  21. * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
  22. * @version $Revision: 1.6.12.3 $, $Date: 2004/07/13 22:27:51 $
  23. */
  24. public class StreamSource implements Source {
  25. /** If {@link javax.xml.transform.TransformerFactory#getFeature}
  26. * returns true when passed this value as an argument,
  27. * the Transformer supports Source input of this type.
  28. */
  29. public static final String FEATURE =
  30. "http://javax.xml.transform.stream.StreamSource/feature";
  31. /**
  32. * <p>Zero-argument default constructor. If this constructor is used, and
  33. * no Stream source is set using
  34. * {@link #setInputStream(java.io.InputStream inputStream)} or
  35. * {@link #setReader(java.io.Reader reader)}, then the
  36. * <code>Transformer</code> will
  37. * create an empty source {@link java.io.InputStream} using
  38. * {@link java.io.InputStream#InputStream() new InputStream()}.</p>
  39. *
  40. * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
  41. */
  42. public StreamSource() { }
  43. /**
  44. * Construct a StreamSource from a byte stream. Normally,
  45. * a stream should be used rather than a reader, so
  46. * the XML parser can resolve character encoding specified
  47. * by the XML declaration.
  48. *
  49. * <p>If this constructor is used to process a stylesheet, normally
  50. * setSystemId should also be called, so that relative URI references
  51. * can be resolved.</p>
  52. *
  53. * @param inputStream A valid InputStream reference to an XML stream.
  54. */
  55. public StreamSource(InputStream inputStream) {
  56. setInputStream(inputStream);
  57. }
  58. /**
  59. * Construct a StreamSource from a byte stream. Normally,
  60. * a stream should be used rather than a reader, so that
  61. * the XML parser can resolve character encoding specified
  62. * by the XML declaration.
  63. *
  64. * <p>This constructor allows the systemID to be set in addition
  65. * to the input stream, which allows relative URIs
  66. * to be processed.</p>
  67. *
  68. * @param inputStream A valid InputStream reference to an XML stream.
  69. * @param systemId Must be a String that conforms to the URI syntax.
  70. */
  71. public StreamSource(InputStream inputStream, String systemId) {
  72. setInputStream(inputStream);
  73. setSystemId(systemId);
  74. }
  75. /**
  76. * Construct a StreamSource from a character reader. Normally,
  77. * a stream should be used rather than a reader, so that
  78. * the XML parser can resolve character encoding specified
  79. * by the XML declaration. However, in many cases the encoding
  80. * of the input stream is already resolved, as in the case of
  81. * reading XML from a StringReader.
  82. *
  83. * @param reader A valid Reader reference to an XML character stream.
  84. */
  85. public StreamSource(Reader reader) {
  86. setReader(reader);
  87. }
  88. /**
  89. * Construct a StreamSource from a character reader. Normally,
  90. * a stream should be used rather than a reader, so that
  91. * the XML parser may resolve character encoding specified
  92. * by the XML declaration. However, in many cases the encoding
  93. * of the input stream is already resolved, as in the case of
  94. * reading XML from a StringReader.
  95. *
  96. * @param reader A valid Reader reference to an XML character stream.
  97. * @param systemId Must be a String that conforms to the URI syntax.
  98. */
  99. public StreamSource(Reader reader, String systemId) {
  100. setReader(reader);
  101. setSystemId(systemId);
  102. }
  103. /**
  104. * Construct a StreamSource from a URL.
  105. *
  106. * @param systemId Must be a String that conforms to the URI syntax.
  107. */
  108. public StreamSource(String systemId) {
  109. this.systemId = systemId;
  110. }
  111. /**
  112. * Construct a StreamSource from a File.
  113. *
  114. * @param f Must a non-null File reference.
  115. */
  116. public StreamSource(File f) {
  117. setSystemId(f);
  118. }
  119. /**
  120. * Set the byte stream to be used as input. Normally,
  121. * a stream should be used rather than a reader, so that
  122. * the XML parser can resolve character encoding specified
  123. * by the XML declaration.
  124. *
  125. * <p>If this Source object is used to process a stylesheet, normally
  126. * setSystemId should also be called, so that relative URL references
  127. * can be resolved.</p>
  128. *
  129. * @param inputStream A valid InputStream reference to an XML stream.
  130. */
  131. public void setInputStream(InputStream inputStream) {
  132. this.inputStream = inputStream;
  133. }
  134. /**
  135. * Get the byte stream that was set with setByteStream.
  136. *
  137. * @return The byte stream that was set with setByteStream, or null
  138. * if setByteStream or the ByteStream constructor was not called.
  139. */
  140. public InputStream getInputStream() {
  141. return inputStream;
  142. }
  143. /**
  144. * Set the input to be a character reader. Normally,
  145. * a stream should be used rather than a reader, so that
  146. * the XML parser can resolve character encoding specified
  147. * by the XML declaration. However, in many cases the encoding
  148. * of the input stream is already resolved, as in the case of
  149. * reading XML from a StringReader.
  150. *
  151. * @param reader A valid Reader reference to an XML CharacterStream.
  152. */
  153. public void setReader(Reader reader) {
  154. this.reader = reader;
  155. }
  156. /**
  157. * Get the character stream that was set with setReader.
  158. *
  159. * @return The character stream that was set with setReader, or null
  160. * if setReader or the Reader constructor was not called.
  161. */
  162. public Reader getReader() {
  163. return reader;
  164. }
  165. /**
  166. * Set the public identifier for this Source.
  167. *
  168. * <p>The public identifier is always optional: if the application
  169. * writer includes one, it will be provided as part of the
  170. * location information.</p>
  171. *
  172. * @param publicId The public identifier as a string.
  173. */
  174. public void setPublicId(String publicId) {
  175. this.publicId = publicId;
  176. }
  177. /**
  178. * Get the public identifier that was set with setPublicId.
  179. *
  180. * @return The public identifier that was set with setPublicId, or null
  181. * if setPublicId was not called.
  182. */
  183. public String getPublicId() {
  184. return publicId;
  185. }
  186. /**
  187. * Set the system identifier for this Source.
  188. *
  189. * <p>The system identifier is optional if there is a byte stream
  190. * or a character stream, but it is still useful to provide one,
  191. * since the application can use it to resolve relative URIs
  192. * and can include it in error messages and warnings (the parser
  193. * will attempt to open a connection to the URI only if
  194. * there is no byte stream or character stream specified).</p>
  195. *
  196. * @param systemId The system identifier as a URL string.
  197. */
  198. public void setSystemId(String systemId) {
  199. this.systemId = systemId;
  200. }
  201. /**
  202. * Get the system identifier that was set with setSystemId.
  203. *
  204. * @return The system identifier that was set with setSystemId, or null
  205. * if setSystemId was not called.
  206. */
  207. public String getSystemId() {
  208. return systemId;
  209. }
  210. /**
  211. * Set the system ID from a File reference.
  212. *
  213. * @param f Must a non-null File reference.
  214. */
  215. public void setSystemId(File f) {
  216. String fpath=f.getAbsolutePath();
  217. if (File.separatorChar != '/') {
  218. fpath = fpath.replace(File.separatorChar, '/');
  219. }
  220. if( fpath.startsWith("/"))
  221. this.systemId= "file://" + fpath;
  222. else
  223. this.systemId = "file:///" + fpath;
  224. }
  225. //////////////////////////////////////////////////////////////////////
  226. // Internal state.
  227. //////////////////////////////////////////////////////////////////////
  228. /**
  229. * The public identifier for this input source, or null.
  230. */
  231. private String publicId;
  232. /**
  233. * The system identifier as a URL string, or null.
  234. */
  235. private String systemId;
  236. /**
  237. * The byte stream for this Source, or null.
  238. */
  239. private InputStream inputStream;
  240. /**
  241. * The character stream for this Source, or null.
  242. */
  243. private Reader reader;
  244. }