KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > email > Message


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.email;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27
28 import org.apache.tools.ant.ProjectComponent;
29
30 /**
31  * Class representing an email message.
32  *
33  * @since Ant 1.5
34  */

35 public class Message extends ProjectComponent {
36     private File JavaDoc messageSource = null;
37     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
38     private String JavaDoc mimeType = "text/plain";
39     private boolean specified = false;
40     private String JavaDoc charset = null;
41
42     /** Creates a new empty message */
43     public Message() {
44     }
45
46
47     /**
48      * Creates a new message based on the given string
49      *
50      * @param text the message
51      */

52     public Message(String JavaDoc text) {
53         addText(text);
54     }
55
56
57     /**
58      * Creates a new message using the contents of the given file.
59      *
60      * @param file the source of the message
61      */

62     public Message(File JavaDoc file) {
63         messageSource = file;
64     }
65
66
67     /**
68      * Adds a textual part of the message
69      *
70      * @param text some text to add
71      */

72     public void addText(String JavaDoc text) {
73         buffer.append(text);
74     }
75
76
77     /**
78      * Sets the source file of the message
79      *
80      * @param src the source of the message
81      */

82     public void setSrc(File JavaDoc src) {
83         this.messageSource = src;
84     }
85
86
87     /**
88      * Sets the content type for the message
89      *
90      * @param mimeType a mime type e.g. "text/plain"
91      */

92     public void setMimeType(String JavaDoc mimeType) {
93         this.mimeType = mimeType;
94         specified = true;
95     }
96
97
98     /**
99      * Returns the content type
100      *
101      * @return the mime type
102      */

103     public String JavaDoc getMimeType() {
104         return mimeType;
105     }
106
107
108     /**
109      * Prints the message onto an output stream
110      *
111      * @param ps The print stream to write to
112      * @throws IOException if an error occurs
113      */

114     public void print(PrintStream JavaDoc ps)
115          throws IOException JavaDoc {
116         // We need character encoding aware printing here.
117
// So, using PrintWriter over OutputStreamWriter instead of PrintStream
118
PrintWriter JavaDoc out
119             = charset != null ? new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(ps, charset))
120                               : new PrintWriter JavaDoc(ps);
121         if (messageSource != null) {
122             // Read message from a file
123
FileReader JavaDoc freader = new FileReader JavaDoc(messageSource);
124
125             try {
126                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(freader);
127                 String JavaDoc line = null;
128                 while ((line = in.readLine()) != null) {
129                     out.println(getProject().replaceProperties(line));
130                 }
131             } finally {
132                 freader.close();
133             }
134         } else {
135             out.println(getProject().replaceProperties(buffer.substring(0)));
136         }
137         out.flush();
138     }
139
140
141     /**
142      * Returns true if the mimeType has been set.
143      *
144      * @return false if the default value is in use
145      */

146     public boolean isMimeTypeSpecified() {
147         return specified;
148     }
149
150     /**
151      * Sets the character set of mail message.
152      * Will be ignored if mimeType contains ....; Charset=... substring.
153      * @param charset the character set name.
154      * @since Ant 1.6
155      */

156     public void setCharset(String JavaDoc charset) {
157       this.charset = charset;
158     }
159     /**
160      * Returns the charset of mail message.
161      *
162      * @return Charset of mail message.
163      * @since Ant 1.6
164      */

165     public String JavaDoc getCharset() {
166       return charset;
167     }
168 }
169
170
Popular Tags