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 |
|
---|
19 | import java.io.BufferedReader;
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.FileReader;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.io.OutputStreamWriter;
|
---|
24 | import java.io.PrintStream;
|
---|
25 | import java.io.PrintWriter;
|
---|
26 |
|
---|
27 | import org.apache.tools.ant.ProjectComponent;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Class representing an email message.
|
---|
31 | *
|
---|
32 | * @since Ant 1.5
|
---|
33 | */
|
---|
34 | public class Message extends ProjectComponent {
|
---|
35 | private File messageSource = null;
|
---|
36 | private StringBuffer buffer = new StringBuffer();
|
---|
37 | private String mimeType = "text/plain";
|
---|
38 | private boolean specified = false;
|
---|
39 | private String charset = null;
|
---|
40 |
|
---|
41 | /** Creates a new empty message */
|
---|
42 | public Message() {
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Creates a new message based on the given string
|
---|
48 | *
|
---|
49 | * @param text the message
|
---|
50 | */
|
---|
51 | public Message(String text) {
|
---|
52 | addText(text);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Creates a new message using the contents of the given file.
|
---|
58 | *
|
---|
59 | * @param file the source of the message
|
---|
60 | */
|
---|
61 | public Message(File file) {
|
---|
62 | messageSource = file;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Adds a textual part of the message
|
---|
68 | *
|
---|
69 | * @param text some text to add
|
---|
70 | */
|
---|
71 | public void addText(String text) {
|
---|
72 | buffer.append(text);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Sets the source file of the message
|
---|
78 | *
|
---|
79 | * @param src the source of the message
|
---|
80 | */
|
---|
81 | public void setSrc(File src) {
|
---|
82 | this.messageSource = src;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Sets the content type for the message
|
---|
88 | *
|
---|
89 | * @param mimeType a mime type e.g. "text/plain"
|
---|
90 | */
|
---|
91 | public void setMimeType(String mimeType) {
|
---|
92 | this.mimeType = mimeType;
|
---|
93 | specified = true;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Returns the content type
|
---|
99 | *
|
---|
100 | * @return the mime type
|
---|
101 | */
|
---|
102 | public String getMimeType() {
|
---|
103 | return mimeType;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Prints the message onto an output stream
|
---|
109 | *
|
---|
110 | * @param ps The print stream to write to
|
---|
111 | * @throws IOException if an error occurs
|
---|
112 | */
|
---|
113 | public void print(PrintStream ps)
|
---|
114 | throws IOException {
|
---|
115 | // We need character encoding aware printing here.
|
---|
116 | // So, using PrintWriter over OutputStreamWriter instead of PrintStream
|
---|
117 | PrintWriter out
|
---|
118 | = charset != null ? new PrintWriter(new OutputStreamWriter(ps, charset))
|
---|
119 | : new PrintWriter(ps);
|
---|
120 | if (messageSource != null) {
|
---|
121 | // Read message from a file
|
---|
122 | FileReader freader = new FileReader(messageSource);
|
---|
123 |
|
---|
124 | try {
|
---|
125 | BufferedReader in = new BufferedReader(freader);
|
---|
126 | String line = null;
|
---|
127 | while ((line = in.readLine()) != null) {
|
---|
128 | out.println(getProject().replaceProperties(line));
|
---|
129 | }
|
---|
130 | } finally {
|
---|
131 | freader.close();
|
---|
132 | }
|
---|
133 | } else {
|
---|
134 | out.println(getProject().replaceProperties(buffer.substring(0)));
|
---|
135 | }
|
---|
136 | out.flush();
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns true if the mimeType has been set.
|
---|
142 | *
|
---|
143 | * @return false if the default value is in use
|
---|
144 | */
|
---|
145 | public boolean isMimeTypeSpecified() {
|
---|
146 | return specified;
|
---|
147 | }
|
---|
148 | /**
|
---|
149 | * Sets the character set of mail message.
|
---|
150 | * Will be ignored if mimeType contains ....; Charset=... substring.
|
---|
151 | * @since Ant 1.6
|
---|
152 | */
|
---|
153 | public void setCharset(String charset) {
|
---|
154 | this.charset = charset;
|
---|
155 | }
|
---|
156 | /**
|
---|
157 | * Returns the charset of mail message.
|
---|
158 | *
|
---|
159 | * @return Charset of mail message.
|
---|
160 | * @since Ant 1.6
|
---|
161 | */
|
---|
162 | public String getCharset() {
|
---|
163 | return charset;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|