1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.email;
  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.FileReader;
  21. import java.io.IOException;
  22. import java.io.OutputStreamWriter;
  23. import java.io.PrintStream;
  24. import java.io.PrintWriter;
  25. import org.apache.tools.ant.ProjectComponent;
  26. /**
  27. * Class representing an email message.
  28. *
  29. * @since Ant 1.5
  30. */
  31. public class Message extends ProjectComponent {
  32. private File messageSource = null;
  33. private StringBuffer buffer = new StringBuffer();
  34. private String mimeType = "text/plain";
  35. private boolean specified = false;
  36. private String charset = null;
  37. /** Creates a new empty message */
  38. public Message() {
  39. }
  40. /**
  41. * Creates a new message based on the given string
  42. *
  43. * @param text the message
  44. */
  45. public Message(String text) {
  46. addText(text);
  47. }
  48. /**
  49. * Creates a new message using the contents of the given file.
  50. *
  51. * @param file the source of the message
  52. */
  53. public Message(File file) {
  54. messageSource = file;
  55. }
  56. /**
  57. * Adds a textual part of the message
  58. *
  59. * @param text some text to add
  60. */
  61. public void addText(String text) {
  62. buffer.append(text);
  63. }
  64. /**
  65. * Sets the source file of the message
  66. *
  67. * @param src the source of the message
  68. */
  69. public void setSrc(File src) {
  70. this.messageSource = src;
  71. }
  72. /**
  73. * Sets the content type for the message
  74. *
  75. * @param mimeType a mime type e.g. "text/plain"
  76. */
  77. public void setMimeType(String mimeType) {
  78. this.mimeType = mimeType;
  79. specified = true;
  80. }
  81. /**
  82. * Returns the content type
  83. *
  84. * @return the mime type
  85. */
  86. public String getMimeType() {
  87. return mimeType;
  88. }
  89. /**
  90. * Prints the message onto an output stream
  91. *
  92. * @param ps The print stream to write to
  93. * @throws IOException if an error occurs
  94. */
  95. public void print(PrintStream ps)
  96. throws IOException {
  97. // We need character encoding aware printing here.
  98. // So, using PrintWriter over OutputStreamWriter instead of PrintStream
  99. PrintWriter out
  100. = charset != null ? new PrintWriter(new OutputStreamWriter(ps, charset))
  101. : new PrintWriter(ps);
  102. if (messageSource != null) {
  103. // Read message from a file
  104. FileReader freader = new FileReader(messageSource);
  105. try {
  106. BufferedReader in = new BufferedReader(freader);
  107. String line = null;
  108. while ((line = in.readLine()) != null) {
  109. out.println(getProject().replaceProperties(line));
  110. }
  111. } finally {
  112. freader.close();
  113. }
  114. } else {
  115. out.println(getProject().replaceProperties(buffer.substring(0)));
  116. }
  117. out.flush();
  118. }
  119. /**
  120. * Returns true if the mimeType has been set.
  121. *
  122. * @return false if the default value is in use
  123. */
  124. public boolean isMimeTypeSpecified() {
  125. return specified;
  126. }
  127. /**
  128. * Sets the character set of mail message.
  129. * Will be ignored if mimeType contains ....; Charset=... substring.
  130. * @since Ant 1.6
  131. */
  132. public void setCharset(String charset) {
  133. this.charset = charset;
  134. }
  135. /**
  136. * Returns the charset of mail message.
  137. *
  138. * @return Charset of mail message.
  139. * @since Ant 1.6
  140. */
  141. public String getCharset() {
  142. return charset;
  143. }
  144. }