1 package com.ozacc.mail.impl;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.StringReader;
7 import java.io.StringWriter;
8 import java.util.List;
9
10 import org.apache.velocity.VelocityContext;
11 import org.apache.velocity.app.Velocity;
12 import org.apache.velocity.exception.MethodInvocationException;
13 import org.apache.velocity.exception.ParseErrorException;
14 import org.apache.velocity.exception.ResourceNotFoundException;
15 import org.apache.velocity.runtime.log.LogSystem;
16 import org.jdom.Document;
17 import org.jdom.Element;
18 import org.jdom.JDOMException;
19 import org.jdom.input.SAXBuilder;
20 import org.jdom.output.XMLOutputter;
21
22 import com.ozacc.mail.Mail;
23 import com.ozacc.mail.MailBuildException;
24 import com.ozacc.mail.VelocityMailBuilder;
25
26 /***
27 * <a href="http://www.jdom.org/">JDOM</a>¤òÍøÍѤ·¤ÆXML¥Õ¥¡¥¤¥?¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤?¥¯¥é¥¹¡£
28 * <p>
29 * ¥½¡¼¥¹XML¤òÆÉ¤ß¹?¤àºÝ¤Ë¡¢DTD¥Ð¥?¥Ç¡¼¥·¥ç¥ó¤¬¼Â¹Ô¤µ¤?¤Þ¤¹¤Î¤ÇÂÅÅö¤ÊXML¥Ç¡¼¥¿(Valid XML Document)¤Ç¤Ê¤±¤?¤Ð¤¤¤±¤Þ¤»¤ó¡£
30 *
31 * TODO: ¥½¡¼¥¹XML¤«¤éDocumentÀ¸À®»?¤Ï¥³¥á¥ó¥È¤òº?½?¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤?¡£
32 *
33 * @since 1.0
34 * @author Tomohiro Otsuka
35 * @version $Id: JDomXMLMailBuilder.java,v 1.6 2004/09/15 05:00:11 otsuka Exp $
36 */
37 public class JDomXMLMailBuilder implements VelocityMailBuilder {
38
39 private LogSystem velocityLogSystem = new VelocityLogSystem();
40
41 /***
42 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
43 */
44 public JDomXMLMailBuilder() {}
45
46 /***
47 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¾å¤ÎXML¥Õ¥¡¥¤¥?¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
48 *
49 * @param classPath ¥á¡¼¥?ÆâÍÆ¤òµ½Ò¤·¤¿XML¥Õ¥¡¥¤¥?¤Î¥Ñ¥¹
50 * @return À¸À®¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹
51 * @throws MailBuildException Mail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤¿¾?¹?
52 */
53 public Mail buildMail(String classPath) throws MailBuildException {
54 Document doc = getDocumentFromClassPath(classPath);
55 return build(doc);
56 }
57
58 /***
59 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¾å¤ÎXML¥Õ¥¡¥¤¥?¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
60 * »ØÄꤵ¤?¤¿VelocityContext¤ò»È¤Ã¤Æ¡¢XML¥Õ¥¡¥¤¥?¤ÎÆâÍÆ¤òưŪ¤ËÀ¸À®¤Ç¤¤Þ¤¹¡£
61 *
62 * @param classPath ¥á¡¼¥?ÆâÍÆ¤òµ½Ò¤·¤¿XML¥Õ¥¡¥¤¥?¤Î¥Ñ¥¹
63 * @param context VelocityContext
64 * @return À¸À®¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹
65 * @throws MailBuildException Mail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤¿¾?¹?
66 */
67 public Mail buildMail(String classPath, VelocityContext context) throws MailBuildException {
68 Document doc = getDocumentFromClassPath(classPath);
69 try {
70 return build(doc, context);
71 } catch (Exception e) {
72 throw new MailBuildException("¥á¡¼¥?¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
73 }
74 }
75
76 /***
77 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
78 *
79 * @param file ¥á¡¼¥?ÆâÍÆ¤òµ½Ò¤·¤¿XML¥Õ¥¡¥¤¥?
80 * @return À¸À®¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹
81 * @throws MailBuildException Mail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤¿¾?¹?
82 */
83 public Mail buildMail(File file) throws MailBuildException {
84 Document doc = getDocumentFromFile(file);
85 return build(doc);
86 }
87
88 /***
89 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
90 * »ØÄꤵ¤?¤¿VelocityContext¤ò»È¤Ã¤Æ¡¢XML¥Õ¥¡¥¤¥?¤ÎÆâÍÆ¤òưŪ¤ËÀ¸À®¤Ç¤¤Þ¤¹¡£
91 *
92 * @param file ¥á¡¼¥?ÆâÍÆ¤òµ½Ò¤·¤¿XML¥Õ¥¡¥¤¥?
93 * @param context VelocityContext
94 * @return À¸À®¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹
95 * @throws MailBuildException Mail¥¤¥ó¥¹¥¿¥ó¥¹¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤¿¾?¹?
96 */
97 public Mail buildMail(File file, VelocityContext context) throws MailBuildException {
98 Document doc = getDocumentFromFile(file);
99 try {
100 return build(doc, context);
101 } catch (Exception e) {
102 throw new MailBuildException("¥á¡¼¥?¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
103 }
104 }
105
106 /***
107 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¾å¤Î¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ó¤Ç¡¢XML¥É¥¥å¥á¥ó¥È¤òÀ¸À®¤·¤Þ¤¹¡£
108 *
109 * @param classPath
110 * @return JDOM Document
111 */
112 private Document getDocumentFromClassPath(String classPath) throws MailBuildException {
113 InputStream is = getClass().getResourceAsStream(classPath);
114 SAXBuilder builder = new SAXBuilder(true);
115 Document doc;
116 try {
117 doc = builder.build(is);
118 is.close();
119 } catch (JDOMException e) {
120 throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e);
121 } catch (IOException e) {
122 throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
123 }
124 return doc;
125 }
126
127 /***
128 * »ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ó¤Ç¡¢XML¥É¥¥å¥á¥ó¥È¤òÀ¸À®¤·¤Þ¤¹¡£
129 *
130 * @param file
131 * @return JDOM Document
132 */
133 private Document getDocumentFromFile(File file) {
134 SAXBuilder builder = new SAXBuilder(true);
135 Document doc;
136 try {
137 doc = builder.build(file);
138 } catch (JDOMException e) {
139 throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e);
140 } catch (IOException e) {
141 throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
142 }
143 return doc;
144 }
145
146 /***
147 * XML¥É¥¥å¥á¥ó¥È¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
148 *
149 * @param doc
150 * @return Mail
151 */
152 private Mail build(Document doc) {
153 Element root = doc.getRootElement();
154
155 Mail mail = new Mail();
156 setFrom(root, mail);
157 setRecipients(root, mail);
158 setSubject(root, mail);
159 setBody(root, mail);
160 setReplyTo(root, mail);
161 setReturnPath(root, mail);
162
163 return mail;
164 }
165
166 /***
167 * VelocityContext¤ÈXML¥É¥¥å¥á¥ó¥È¤ò¥Þ¡¼¥¸¤µ¤»¡¢Mail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
168 *
169 * @param context
170 * @param doc
171 * @return Mail
172 *
173 * @throws Exception
174 * @throws ParseErrorException
175 * @throws MethodInvocationException
176 * @throws ResourceNotFoundException
177 * @throws IOException
178 * @throws JDOMException
179 */
180 private Mail build(Document doc, VelocityContext context) throws Exception,
181 ParseErrorException,
182 MethodInvocationException,
183 ResourceNotFoundException,
184 IOException, JDOMException {
185 XMLOutputter output = new XMLOutputter();
186 String xmlContent = output.outputString(doc);
187
188 Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, velocityLogSystem);
189 Velocity.init();
190 StringWriter w = new StringWriter();
191 Velocity.evaluate(context, w, "XML Mail Data", xmlContent);
192 StringReader reader = new StringReader(w.toString());
193
194 SAXBuilder builder = new SAXBuilder(true);
195 Document doc2 = builder.build(reader);
196
197 return build(doc2);
198 }
199
200 /***
201 * @param root
202 * @param mail
203 */
204 private void setReturnPath(Element root, Mail mail) {
205 Element returnPathElem = root.getChild("returnPath");
206 if (returnPathElem != null && returnPathElem.getAttributeValue("email") != null) {
207 mail.setReturnPath(returnPathElem.getAttributeValue("email"));
208 }
209 }
210
211 /***
212 * @param root
213 * @param mail
214 */
215 private void setReplyTo(Element root, Mail mail) {
216 Element replyToElem = root.getChild("replyTo");
217 if (replyToElem != null && replyToElem.getAttributeValue("email") != null) {
218 mail.setReplyTo(replyToElem.getAttributeValue("email"));
219 }
220 }
221
222 /***
223 * @param root
224 * @param mail
225 */
226 private void setBody(Element root, Mail mail) {
227 Element bodyElem = root.getChild("body");
228 if (bodyElem != null) {
229 mail.setText(bodyElem.getTextTrim());
230 }
231 }
232
233 /***
234 * @param root
235 * @param mail
236 */
237 private void setSubject(Element root, Mail mail) {
238 Element subjectElem = root.getChild("subject");
239 if (subjectElem != null) {
240 mail.setSubject(subjectElem.getTextTrim());
241 }
242 }
243
244 /***
245 * @param root
246 * @param mail
247 */
248 private void setRecipients(Element root, Mail mail) {
249 Element recipientsElem = root.getChild("recipients");
250 if (recipientsElem == null) {
251 return;
252 }
253
254 List recipientElemList = recipientsElem.getChildren();
255 for (int i = 0, max = recipientElemList.size(); i < max; i++) {
256 Element e = (Element)recipientElemList.get(i);
257 if ("to".equals(e.getName())) {
258 if (e.getAttributeValue("email") != null) {
259 if (e.getAttributeValue("name") != null) {
260 mail.addTo(e.getAttributeValue("email"), e.getAttributeValue("name"));
261 } else {
262 mail.addTo(e.getAttributeValue("email"));
263 }
264 }
265 } else if ("cc".equals(e.getName())) {
266 if (e.getAttributeValue("email") != null) {
267 if (e.getAttributeValue("name") != null) {
268 mail.addCc(e.getAttributeValue("email"), e.getAttributeValue("name"));
269 } else {
270 mail.addCc(e.getAttributeValue("email"));
271 }
272 }
273 } else {
274 if (e.getAttributeValue("email") != null) {
275 mail.addBcc(e.getAttributeValue("email"));
276 }
277 }
278 }
279 }
280
281 /***
282 * @param root
283 * @param mail
284 */
285 private void setFrom(Element root, Mail mail) {
286 Element fromElem = root.getChild("from");
287 if (fromElem != null && fromElem.getAttributeValue("email") != null) {
288 if (fromElem.getAttributeValue("name") != null) {
289 mail.setFrom(fromElem.getAttributeValue("email"), fromElem
290 .getAttributeValue("name"));
291 } else {
292 mail.setFrom(fromElem.getAttributeValue("email"));
293 }
294 }
295 }
296
297 }