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