1 package com.ozacc.mail.impl;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.HashMap;
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.FactoryConfigurationError;
12 import javax.xml.parsers.ParserConfigurationException;
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;
20 import com.ozacc.mail.Mail;
23 * XMLMailBuilder実装が継承する基底クラス。
27 * @author Tomohiro Otsuka
28 * @version $Id: AbstractXMLMailBuilder.java,v 1.4.2.2 2004/11/25 08:00:49 otsuka Exp $
30 public abstract class AbstractXMLMailBuilder {
32 protected Map documentBuilderCache;
37 public AbstractXMLMailBuilder() {
38 documentBuilderCache = new HashMap();
42 * 指定されたXMLファイルを読み込み、DOM Documentを生成します。
43 * ignoreCommentが指定されている場合は、XMLのコメントを削除しません。
46 * @return DOM Document
48 * @throws SAXException
50 protected synchronized Document getDocumentFromFile(File file, boolean ignoreComment)
53 DocumentBuilder db = createDocumentBuilder(ignoreComment);
54 return db.parse(file);
58 * 指定されたXMLファイルを読み込み、DOM Documentを生成します。
62 * @return DOM Document
64 * @throws SAXException
66 protected Document getDocumentFromFile(File file) throws SAXException, IOException {
67 return getDocumentFromFile(file, true);
71 * DocumentBuilderインスタンスを生成します。
72 * ignoreCommentが指定されている場合は、コメントを削除しないように設定されたDocumentBuilderを生成します。
74 * @param ignoreComment
75 * @return DocumentBuilder
76 * @throws FactoryConfigurationError
78 protected DocumentBuilder createDocumentBuilder(boolean ignoreComment)
79 throws FactoryConfigurationError {
80 Boolean dbKey = Boolean.valueOf(ignoreComment);
81 DocumentBuilder db = (DocumentBuilder)documentBuilderCache.get(dbKey);
83 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
84 dbf.setIgnoringComments(ignoreComment);
85 dbf.setCoalescing(ignoreComment);
86 dbf.setIgnoringElementContentWhitespace(true);
87 dbf.setValidating(true);
89 db = dbf.newDocumentBuilder();
90 db.setEntityResolver(new DTDEntityResolver());
91 documentBuilderCache.put(dbKey, db);
92 } catch (ParserConfigurationException e) {
94 throw new RuntimeException(e);
101 * DocumentBuilderインスタンスを生成します。
102 * このDocumentBuilderを使用して生成されるDOM Documentでは、元のXMLデータにあるコメントは削除されます。
104 * @return DocumentBuilder
105 * @throws FactoryConfigurationError
107 protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError {
108 return createDocumentBuilder(true);
112 * 指定されたクラスパスのXMLファイルを読み込み、DOM Documentを生成します。
113 * ignoreCommentが指定されている場合は、XMLのコメントを削除しません。
115 * @param ignoreComment
117 * @return DOM Document
118 * @throws IOException
119 * @throws SAXException
121 protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment)
124 InputStream is = getClass().getResourceAsStream(classPath);
125 DocumentBuilder db = createDocumentBuilder(ignoreComment);
136 * 指定されたクラスパスのXMLファイルを読み込み、DOM Documentを生成します。
137 * XMLのコメントや改行は削除されます。
140 * @return DOM Document
141 * @throws IOException
142 * @throws SAXException
144 protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException {
145 return getDocumentFromClassPath(classPath, true);
152 protected void setReplyTo(Element root, Mail mail) {
153 NodeList nodes = root.getElementsByTagName("replyTo");
154 Element replyTo = (Element)nodes.item(0);
155 if (replyTo != null && replyTo.getAttribute("email").length() > 0) {
156 mail.setReplyTo(replyTo.getAttribute("email"));
164 protected void setText(Element root, Mail mail) {
165 NodeList nodes = root.getElementsByTagName("body");
166 Element bodyElem = (Element)nodes.item(0);
167 if (bodyElem == null) {
170 String body = bodyElem.getFirstChild().getNodeValue();
171 mail.setText(body.trim());
180 protected void setHtml(Element root, Mail mail) {
181 NodeList nodes = root.getElementsByTagName("html");
182 Element htmlElem = (Element)nodes.item(0);
183 if (htmlElem == null) {
186 String html = htmlElem.getFirstChild().getNodeValue();
187 mail.setHtmlText(html.trim());
194 protected void setSubject(Element root, Mail mail) {
195 NodeList nodes = root.getElementsByTagName("subject");
196 Element subjectElem = (Element)nodes.item(0);
197 if (subjectElem == null) {
200 String subject = subjectElem.getFirstChild().getNodeValue();
201 mail.setSubject(subject.trim());
208 protected void setRecipients(Element root, Mail mail) {
209 NodeList nodes = root.getElementsByTagName("recipients");
210 Element recipientsElem = (Element)nodes.item(0);
211 if (recipientsElem == null) {
215 NodeList recipientElemList = recipientsElem.getChildNodes();
216 for (int i = 0, max = recipientElemList.getLength(); i < max; i++) {
217 Node node = recipientElemList.item(i);
218 if (node.getNodeType() != Node.ELEMENT_NODE) {
221 Element e = (Element)node;
222 if ("to".equals(e.getNodeName())) { // to
223 if (e.getAttribute("email").length() > 0) {
224 if (e.getAttribute("name").length() > 0) {
225 mail.addTo(e.getAttribute("email"), e.getAttribute("name"));
227 mail.addTo(e.getAttribute("email"));
230 } else if ("cc".equals(e.getNodeName())) { // cc
231 if (e.getAttribute("email").length() > 0) {
232 if (e.getAttribute("name").length() > 0) {
233 mail.addCc(e.getAttribute("email"), e.getAttribute("name"));
235 mail.addCc(e.getAttribute("email"));
239 if (e.getAttribute("email").length() > 0) { // bcc
240 mail.addBcc(e.getAttribute("email"));
250 protected void setReturnPath(Element root, Mail mail) {
251 NodeList nodes = root.getElementsByTagName("returnPath");
252 Element returnPath = (Element)nodes.item(0);
253 if (returnPath != null && returnPath.getAttribute("email").length() > 0) {
254 mail.setReturnPath(returnPath.getAttribute("email"));
262 protected void setFrom(Element root, Mail mail) {
263 NodeList nodes = root.getElementsByTagName("from");
264 Element from = (Element)nodes.item(0);
265 if (from != null && from.getAttribute("email").length() > 0) {
266 if (from.getAttribute("name").length() > 0) {
267 mail.setFrom(from.getAttribute("email"), from.getAttribute("name"));
269 mail.setFrom(from.getAttribute("email"));