1. // $Id: StreamResult.java,v 1.3.22.4 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. * @(#)StreamResult.java 1.15 04/07/13
  8. */
  9. package javax.xml.transform.stream;
  10. import javax.xml.transform.Result;
  11. import java.io.File;
  12. import java.io.OutputStream;
  13. import java.io.Writer;
  14. import java.net.MalformedURLException;
  15. /**
  16. * <p>Acts as an holder for a transformation result,
  17. * which may be XML, plain Text, HTML, or some other form of markup.</p>
  18. *
  19. * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
  20. */
  21. public class StreamResult implements Result {
  22. /** If {@link javax.xml.transform.TransformerFactory#getFeature}
  23. * returns true when passed this value as an argument,
  24. * the Transformer supports Result output of this type.
  25. */
  26. public static final String FEATURE =
  27. "http://javax.xml.transform.stream.StreamResult/feature";
  28. /**
  29. * Zero-argument default constructor.
  30. */
  31. public StreamResult() {
  32. }
  33. /**
  34. * Construct a StreamResult from a byte stream. Normally,
  35. * a stream should be used rather than a reader, so that
  36. * the transformer may use instructions contained in the
  37. * transformation instructions to control the encoding.
  38. *
  39. * @param outputStream A valid OutputStream reference.
  40. */
  41. public StreamResult(OutputStream outputStream) {
  42. setOutputStream(outputStream);
  43. }
  44. /**
  45. * Construct a StreamResult from a character stream. Normally,
  46. * a stream should be used rather than a reader, so that
  47. * the transformer may use instructions contained in the
  48. * transformation instructions to control the encoding. However,
  49. * there are times when it is useful to write to a character
  50. * stream, such as when using a StringWriter.
  51. *
  52. * @param writer A valid Writer reference.
  53. */
  54. public StreamResult(Writer writer) {
  55. setWriter(writer);
  56. }
  57. /**
  58. * Construct a StreamResult from a URL.
  59. *
  60. * @param systemId Must be a String that conforms to the URI syntax.
  61. */
  62. public StreamResult(String systemId) {
  63. this.systemId = systemId;
  64. }
  65. /**
  66. * Construct a StreamResult from a File.
  67. *
  68. * @param f Must a non-null File reference.
  69. */
  70. public StreamResult(File f) {
  71. setSystemId(f);
  72. }
  73. /**
  74. * Set the ByteStream that is to be written to. Normally,
  75. * a stream should be used rather than a reader, so that
  76. * the transformer may use instructions contained in the
  77. * transformation instructions to control the encoding.
  78. *
  79. * @param outputStream A valid OutputStream reference.
  80. */
  81. public void setOutputStream(OutputStream outputStream) {
  82. this.outputStream = outputStream;
  83. }
  84. /**
  85. * Get the byte stream that was set with setOutputStream.
  86. *
  87. * @return The byte stream that was set with setOutputStream, or null
  88. * if setOutputStream or the ByteStream constructor was not called.
  89. */
  90. public OutputStream getOutputStream() {
  91. return outputStream;
  92. }
  93. /**
  94. * Set the writer that is to receive the result. Normally,
  95. * a stream should be used rather than a writer, so that
  96. * the transformer may use instructions contained in the
  97. * transformation instructions to control the encoding. However,
  98. * there are times when it is useful to write to a writer,
  99. * such as when using a StringWriter.
  100. *
  101. * @param writer A valid Writer reference.
  102. */
  103. public void setWriter(Writer writer) {
  104. this.writer = writer;
  105. }
  106. /**
  107. * Get the character stream that was set with setWriter.
  108. *
  109. * @return The character stream that was set with setWriter, or null
  110. * if setWriter or the Writer constructor was not called.
  111. */
  112. public Writer getWriter() {
  113. return writer;
  114. }
  115. /**
  116. * Set the systemID that may be used in association
  117. * with the byte or character stream, or, if neither is set, use
  118. * this value as a writeable URI (probably a file name).
  119. *
  120. * @param systemId The system identifier as a URI string.
  121. */
  122. public void setSystemId(String systemId) {
  123. this.systemId = systemId;
  124. }
  125. /**
  126. * <p>Set the system ID from a <code>File</code> reference.</p>
  127. *
  128. * <p>Note the use of {@link File#toURI()} and {@link File#toURL()}.
  129. * <code>toURI()</code> is prefered and used if possible.
  130. * To allow JAXP 1.3 to run on J2SE 1.3, <code>toURL()</code>
  131. * is used if a {@link NoSuchMethodException} is thrown by the attempt
  132. * to use <code>toURI()</code>.</p>
  133. *
  134. * @param f Must a non-null File reference.
  135. */
  136. public void setSystemId(File f) {
  137. try {
  138. // assume >= 1.4
  139. this.systemId = f.toURI().toString();
  140. } catch (java.lang.NoSuchMethodError nme) {
  141. // running on J2SE 1.3?
  142. try {
  143. this.systemId = f.toURL().toString();
  144. } catch (MalformedURLException malformedURLException) {
  145. this.systemId = null;
  146. throw new RuntimeException(
  147. "javax.xml.transform.stream.StreamResult#setSystemId(File f) with MalformedURLException: "
  148. + malformedURLException.toString()
  149. );
  150. }
  151. } catch (Exception exception) {
  152. throw new RuntimeException(
  153. "javax.xml.transform.stream.StreamResult#setSystemId(File f):"
  154. + " unexpected Exception: " + exception.toString()
  155. );
  156. }
  157. }
  158. /**
  159. * Get the system identifier that was set with setSystemId.
  160. *
  161. * @return The system identifier that was set with setSystemId, or null
  162. * if setSystemId was not called.
  163. */
  164. public String getSystemId() {
  165. return systemId;
  166. }
  167. //////////////////////////////////////////////////////////////////////
  168. // Internal state.
  169. //////////////////////////////////////////////////////////////////////
  170. /**
  171. * The systemID that may be used in association
  172. * with the byte or character stream, or, if neither is set, use
  173. * this value as a writeable URI (probably a file name).
  174. */
  175. private String systemId;
  176. /**
  177. * The byte stream that is to be written to.
  178. */
  179. private OutputStream outputStream;
  180. /**
  181. * The character stream that is to be written to.
  182. */
  183. private Writer writer;
  184. }