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