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