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.util.HashMap;
7 import java.util.Map;
8
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.FactoryConfigurationError;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16 import org.w3c.dom.Node;
17 import org.w3c.dom.NodeList;
18 import org.xml.sax.SAXException;
19
20 import com.ozacc.mail.Mail;
21 import com.ozacc.mail.MailBuildException;
22 import com.ozacc.mail.MailBuilder;
23
24 /***
25 * ¥á¡¼¥?¥Ç¡¼¥¿¤ÎXML¥Õ¥¡¥¤¥?¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤?¥¯¥é¥¹¡£
26 * <p>
27 * ¥½¡¼¥¹XML¤òÆÉ¤ß¹?¤àºÝ¤Ë¡¢DTD¥Ð¥?¥Ç¡¼¥·¥ç¥ó¤¬¼Â¹Ô¤µ¤?¤Þ¤¹¤Î¤ÇÂÅÅö¤ÊXML¥Ç¡¼¥¿(Valid XML Document)¤Ç¤Ê¤±¤?¤Ð¤¤¤±¤Þ¤»¤ó¡£
28 * ¥á¡¼¥?¥Ç¡¼¥¿XML¤ÎDTD¤Ï¡¢<a href="http://www.ozacc.com/library/dtd/ozacc-mail.dtd">http://www.ozacc.com/library/dtd/ozacc-mail.dtd</a>¤ò»²¾È¡£
29 *
30 * @since 1.0.1
31 * @author Tomohiro Otsuka
32 * @version $Id: XMLMailBuilderImpl.java,v 1.3 2004/09/15 09:04:07 otsuka Exp $
33 */
34 public class XMLMailBuilderImpl implements MailBuilder {
35
36 private Map documentBuilderCache;
37
38 /***
39 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
40 */
41 public XMLMailBuilderImpl() {
42 documentBuilderCache = new HashMap();
43 }
44
45 /***
46 * @see com.ozacc.mail.MailBuilder#buildMail(java.lang.String)
47 */
48 public Mail buildMail(String classPath) throws MailBuildException {
49 Document doc;
50 try {
51 doc = getDocumentFromClassPath(classPath);
52 } catch (SAXException e) {
53 throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e);
54 } catch (IOException e) {
55 throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
56 }
57
58 return build(doc);
59 }
60
61 /***
62 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
63 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢XML¤Î¥³¥á¥ó¥È¤òº?½?¤·¤Þ¤»¤ó¡£
64 *
65 * @param ignoreComment
66 * @param classPath
67 * @return DOM Document
68 * @throws IOException
69 * @throws SAXException
70 */
71 protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment)
72 throws SAXException,
73 IOException {
74 InputStream is = getClass().getResourceAsStream(classPath);
75 DocumentBuilder db = createDocumentBuilder(ignoreComment);
76 try {
77 return db.parse(is);
78 } finally {
79 if (is != null) {
80 is.close();
81 }
82 }
83 }
84
85 /***
86 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
87 * XML¤Î¥³¥á¥ó¥È¤ä²?¹Ô¤Ïº?½?¤µ¤?¤Þ¤¹¡£
88 *
89 * @param classPath
90 * @return DOM Document
91 * @throws IOException
92 * @throws SAXException
93 */
94 protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException {
95 return getDocumentFromClassPath(classPath, true);
96 }
97
98 /***
99 * @see com.ozacc.mail.MailBuilder#buildMail(java.io.File)
100 */
101 public Mail buildMail(File file) throws MailBuildException {
102 Document doc;
103 try {
104 doc = getDocumentFromFile(file);
105 } catch (SAXException e) {
106 throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e);
107 } catch (IOException e) {
108 throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
109 }
110
111 return build(doc);
112 }
113
114 /***
115 * DOM Document¤«¤éMail¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
116 *
117 * @param doc ¥á¡¼¥?¥Ç¡¼¥¿¤ÎDOM Document
118 * @return À¸À®¤µ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹
119 */
120 protected Mail build(Document doc) {
121 Element root = doc.getDocumentElement();
122
123 Mail mail = new Mail();
124 setReturnPath(root, mail);
125 setFrom(root, mail);
126 setRecipients(root, mail);
127 setReplyTo(root, mail);
128 setSubject(root, mail);
129 setText(root, mail);
130
131 return mail;
132 }
133
134 /***
135 * @param root
136 * @param mail
137 */
138 private void setReplyTo(Element root, Mail mail) {
139 NodeList nodes = root.getElementsByTagName("replyTo");
140 Element replyTo = (Element)nodes.item(0);
141 if (replyTo != null && replyTo.getAttribute("email").length() > 0) {
142 mail.setReplyTo(replyTo.getAttribute("email"));
143 }
144 }
145
146 /***
147 * @param root
148 * @param mail
149 */
150 private void setText(Element root, Mail mail) {
151 NodeList nodes = root.getElementsByTagName("body");
152 Element bodyElem = (Element)nodes.item(0);
153 if (bodyElem == null) {
154 return;
155 }
156 String body = bodyElem.getFirstChild().getNodeValue();
157 mail.setText(body.trim());
158 }
159
160 /***
161 * @param root
162 * @param mail
163 */
164 private void setSubject(Element root, Mail mail) {
165 NodeList nodes = root.getElementsByTagName("subject");
166 Element subjectElem = (Element)nodes.item(0);
167 if (subjectElem == null) {
168 return;
169 }
170 String subject = subjectElem.getFirstChild().getNodeValue();
171 mail.setSubject(subject.trim());
172 }
173
174 /***
175 * @param root
176 * @param mail
177 */
178 private void setRecipients(Element root, Mail mail) {
179 NodeList nodes = root.getElementsByTagName("recipients");
180 Element recipientsElem = (Element)nodes.item(0);
181 if (recipientsElem == null) {
182 return;
183 }
184
185 NodeList recipientElemList = recipientsElem.getChildNodes();
186 for (int i = 0, max = recipientElemList.getLength(); i < max; i++) {
187 Node node = recipientElemList.item(i);
188 if (node.getNodeType() != Node.ELEMENT_NODE) {
189 continue;
190 }
191 Element e = (Element)node;
192 if ("to".equals(e.getNodeName())) {
193 if (e.getAttribute("email").length() > 0) {
194 if (e.getAttribute("name").length() > 0) {
195 mail.addTo(e.getAttribute("email"), e.getAttribute("name"));
196 } else {
197 mail.addTo(e.getAttribute("email"));
198 }
199 }
200 } else if ("cc".equals(e.getNodeName())) {
201 if (e.getAttribute("email").length() > 0) {
202 if (e.getAttribute("name").length() > 0) {
203 mail.addCc(e.getAttribute("email"), e.getAttribute("name"));
204 } else {
205 mail.addCc(e.getAttribute("email"));
206 }
207 }
208 } else {
209 if (e.getAttribute("email").length() > 0) {
210 mail.addBcc(e.getAttribute("email"));
211 }
212 }
213 }
214 }
215
216 /***
217 * @param root
218 * @param mail
219 */
220 private void setReturnPath(Element root, Mail mail) {
221 NodeList nodes = root.getElementsByTagName("returnPath");
222 Element returnPath = (Element)nodes.item(0);
223 if (returnPath != null && returnPath.getAttribute("email").length() > 0) {
224 mail.setReturnPath(returnPath.getAttribute("email"));
225 }
226 }
227
228 /***
229 * @param root
230 * @param mail
231 */
232 private void setFrom(Element root, Mail mail) {
233 NodeList nodes = root.getElementsByTagName("from");
234 Element from = (Element)nodes.item(0);
235 if (from != null && from.getAttribute("email").length() > 0) {
236 if (from.getAttribute("name").length() > 0) {
237 mail.setFrom(from.getAttribute("email"), from.getAttribute("name"));
238 } else {
239 mail.setFrom(from.getAttribute("email"));
240 }
241 }
242 }
243
244 /***
245 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
246 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢XML¤Î¥³¥á¥ó¥È¤òº?½?¤·¤Þ¤»¤ó¡£
247 *
248 * @param file XML¥Õ¥¡¥¤¥?
249 * @return DOM Document
250 * @throws IOException
251 * @throws SAXException
252 */
253 protected synchronized Document getDocumentFromFile(File file, boolean ignoreComment)
254 throws SAXException,
255 IOException {
256 DocumentBuilder db = createDocumentBuilder(ignoreComment);
257 return db.parse(file);
258 }
259
260 /***
261 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
262 * XML¤Î¥³¥á¥ó¥È¤ä²?¹Ô¤Ïº?½?¤µ¤?¤Þ¤¹¡£
263 *
264 * @param file XML¥Õ¥¡¥¤¥?
265 * @return DOM Document
266 * @throws IOException
267 * @throws SAXException
268 */
269 protected Document getDocumentFromFile(File file) throws SAXException, IOException {
270 return getDocumentFromFile(file, true);
271 }
272
273 /***
274 * DocumentBuilder¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
275 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢¥³¥á¥ó¥È¤òº?½?¤·¤Ê¤¤¤è¤¦¤ËÀßÄꤵ¤?¤¿DocumentBuilder¤òÀ¸À®¤·¤Þ¤¹¡£
276 *
277 * @param ignoreComment
278 * @return DocumentBuilder
279 * @throws FactoryConfigurationError
280 */
281 protected DocumentBuilder createDocumentBuilder(boolean ignoreComment)
282 throws FactoryConfigurationError {
283 Boolean dbKey = Boolean.valueOf(ignoreComment);
284 DocumentBuilder db = (DocumentBuilder)documentBuilderCache.get(dbKey);
285 if (db == null) {
286 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
287 dbf.setIgnoringComments(ignoreComment);
288 dbf.setCoalescing(true);
289 dbf.setIgnoringElementContentWhitespace(true);
290 dbf.setValidating(true);
291 try {
292 db = dbf.newDocumentBuilder();
293 documentBuilderCache.put(dbKey, db);
294 } catch (ParserConfigurationException e) {
295
296 throw new RuntimeException(e);
297 }
298 }
299 return db;
300 }
301
302 /***
303 * DocumentBuilder¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
304 * ¤³¤ÎDocumentBuilder¤ò»ÈÍѤ·¤ÆÀ¸À®¤µ¤?¤?DOM Document¤Ç¤Ï¡¢¸µ¤ÎXML¥Ç¡¼¥¿¤Ë¤¢¤?¥³¥á¥ó¥È¤Ïº?½?¤µ¤?¤Þ¤¹¡£
305 *
306 * @return DocumentBuilder
307 * @throws FactoryConfigurationError
308 */
309 protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError {
310 return createDocumentBuilder(true);
311 }
312
313 }