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