1. /*
  2. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. /*
  6. * @(#)StreamResult.java 1.13 03/01/23
  7. */
  8. package javax.xml.transform.stream;
  9. import javax.xml.transform.*;
  10. import java.lang.String;
  11. import java.io.OutputStream;
  12. import java.io.Writer;
  13. import java.io.File;
  14. /**
  15. * Acts as an holder for a transformation result,
  16. * which may be XML, plain Text, HTML, or some other form of markup.
  17. *
  18. */
  19. public class StreamResult implements Result {
  20. /** If {@link javax.xml.transform.TransformerFactory#getFeature}
  21. * returns true when passed this value as an argument,
  22. * the Transformer supports Result output of this type.
  23. */
  24. public static final String FEATURE =
  25. "http://javax.xml.transform.stream.StreamResult/feature";
  26. /**
  27. * Zero-argument default constructor.
  28. */
  29. public StreamResult() {}
  30. /**
  31. * Construct a StreamResult from a byte stream. Normally,
  32. * a stream should be used rather than a reader, so that
  33. * the transformer may use instructions contained in the
  34. * transformation instructions to control the encoding.
  35. *
  36. * @param outputStream A valid OutputStream reference.
  37. */
  38. public StreamResult(OutputStream outputStream) {
  39. setOutputStream(outputStream);
  40. }
  41. /**
  42. * Construct a StreamResult from a character stream. Normally,
  43. * a stream should be used rather than a reader, so that
  44. * the transformer may use instructions contained in the
  45. * transformation instructions to control the encoding. However,
  46. * there are times when it is useful to write to a character
  47. * stream, such as when using a StringWriter.
  48. *
  49. * @param writer A valid Writer reference.
  50. */
  51. public StreamResult(Writer writer) {
  52. setWriter(writer);
  53. }
  54. /**
  55. * Construct a StreamResult from a URL.
  56. *
  57. * @param systemId Must be a String that conforms to the URI syntax.
  58. */
  59. public StreamResult(String systemId) {
  60. this.systemId = systemId;
  61. }
  62. /**
  63. * Construct a StreamResult from a File.
  64. *
  65. * @param f Must a non-null File reference.
  66. */
  67. public StreamResult(File f) {
  68. setSystemId(f);
  69. }
  70. /**
  71. * Set the ByteStream that is to be written to. Normally,
  72. * a stream should be used rather than a reader, so that
  73. * the transformer may use instructions contained in the
  74. * transformation instructions to control the encoding.
  75. *
  76. * @param outputStream A valid OutputStream reference.
  77. */
  78. public void setOutputStream(OutputStream outputStream) {
  79. this.outputStream = outputStream;
  80. }
  81. /**
  82. * Get the byte stream that was set with setOutputStream.
  83. *
  84. * @return The byte stream that was set with setOutputStream, or null
  85. * if setOutputStream or the ByteStream constructor was not called.
  86. */
  87. public OutputStream getOutputStream() {
  88. return outputStream;
  89. }
  90. /**
  91. * Set the writer that is to receive the result. Normally,
  92. * a stream should be used rather than a writer, so that
  93. * the transformer may use instructions contained in the
  94. * transformation instructions to control the encoding. However,
  95. * there are times when it is useful to write to a writer,
  96. * such as when using a StringWriter.
  97. *
  98. * @param writer A valid Writer reference.
  99. */
  100. public void setWriter(Writer writer) {
  101. this.writer = writer;
  102. }
  103. /**
  104. * Get the character stream that was set with setWriter.
  105. *
  106. * @return The character stream that was set with setWriter, or null
  107. * if setWriter or the Writer constructor was not called.
  108. */
  109. public Writer getWriter() {
  110. return writer;
  111. }
  112. /**
  113. * Set the systemID that may be used in association
  114. * with the byte or character stream, or, if neither is set, use
  115. * this value as a writeable URI (probably a file name).
  116. *
  117. * @param systemId The system identifier as a URI string.
  118. */
  119. public void setSystemId(String systemId) {
  120. this.systemId = systemId;
  121. }
  122. /**
  123. * Set the system ID from a File reference.
  124. *
  125. * @param f Must a non-null File reference.
  126. */
  127. public void setSystemId(File f) {
  128. String fpath=f.getAbsolutePath();
  129. if (File.separatorChar != '/') {
  130. fpath = fpath.replace(File.separatorChar, '/');
  131. }
  132. if( fpath.startsWith("/"))
  133. this.systemId= "file://" + fpath;
  134. else
  135. this.systemId = "file:///" + fpath;
  136. }
  137. /**
  138. * Get the system identifier that was set with setSystemId.
  139. *
  140. * @return The system identifier that was set with setSystemId, or null
  141. * if setSystemId was not called.
  142. */
  143. public String getSystemId() {
  144. return systemId;
  145. }
  146. //////////////////////////////////////////////////////////////////////
  147. // Internal state.
  148. //////////////////////////////////////////////////////////////////////
  149. /**
  150. * The systemID that may be used in association
  151. * with the byte or character stream, or, if neither is set, use
  152. * this value as a writeable URI (probably a file name).
  153. */
  154. private String systemId;
  155. /**
  156. * The byte stream that is to be written to.
  157. */
  158. private OutputStream outputStream;
  159. /**
  160. * The character stream that is to be written to.
  161. */
  162. private Writer writer;
  163. }