View Javadoc

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