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