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