1 package com.ozacc.mail.impl;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import javax.xml.parsers.FactoryConfigurationError;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.xml.sax.SAXException;
13
14 import com.ozacc.mail.Mail;
15 import com.ozacc.mail.MailBuildException;
16 import com.ozacc.mail.MultipleMailBuilder;
17
18 /***
19 * メールデータのXMLファイルからMailインスタンスを生成するクラス。
20 * <p>
21 * ソースXMLを読み込む際に、DTDバリデーションが実行されますので妥当なXMLデータ(Valid XML Document)でなければいけません。
22 * メールデータXMLのDTDは、<a href="http://www.ozacc.com/library/dtd/ozacc-mail.dtd">http://www.ozacc.com/library/dtd/ozacc-mail.dtd</a>を参照。
23 *
24 * @since 1.0.1
25 * @author Tomohiro Otsuka
26 * @version $Id: XMLMailBuilderImpl.java,v 1.5.2.1 2005/01/21 22:16:31 otsuka Exp $
27 */
28 public class XMLMailBuilderImpl extends AbstractXMLMailBuilder implements MultipleMailBuilder {
29
30 private static Log log = LogFactory.getLog(XMLMailBuilderImpl.class);
31
32 /***
33 * コンストラクタ。
34 */
35 public XMLMailBuilderImpl() {
36 super();
37 }
38
39 /***
40 * @see com.ozacc.mail.MailBuilder#buildMail(java.lang.String)
41 */
42 public Mail buildMail(String classPath) throws MailBuildException {
43 Document doc = retrieveDocument(classPath);
44 return buildMail(doc.getDocumentElement());
45 }
46
47 /***
48 * @see com.ozacc.mail.MailBuilder#buildMail(java.io.File)
49 */
50 public Mail buildMail(File file) throws MailBuildException {
51 Document doc = retrieveDocument(file);
52 return buildMail(doc.getDocumentElement());
53 }
54
55 /***
56 * @param classPath
57 * @return
58 * @throws MailBuildException
59 */
60 private Document retrieveDocument(String classPath) throws MailBuildException {
61 try {
62 return getDocumentFromClassPath(classPath);
63 } catch (SAXException e) {
64 throw new MailBuildException("XMLのパースに失敗しました。" + e.getMessage(), e);
65 } catch (IOException e) {
66 throw new MailBuildException("XMLファイルの読み込みに失敗しました。", e);
67 }
68 }
69
70 /***
71 * @param file
72 * @return
73 * @throws MailBuildException
74 */
75 private Document retrieveDocument(File file) throws MailBuildException {
76 try {
77 return 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
85 /***
86 * 指定されたXMLのmail要素からMailインスタンスを生成します。
87 *
88 * @param root メールデータのmail要素
89 * @return 生成されたMailインスタンス
90 */
91 protected Mail buildMail(Element root) {
92 Mail mail = new Mail();
93 setReturnPath(root, mail);
94 setFrom(root, mail);
95 setRecipients(root, mail);
96 setReplyTo(root, mail);
97 setSubject(root, mail);
98 setText(root, mail);
99 setHtml(root, mail);
100 return mail;
101 }
102
103 /***
104 * @see com.ozacc.mail.MultipleMailBuilder#buildMail(java.lang.String, java.lang.String)
105 */
106 public Mail buildMail(String classPath, String mailId) throws MailBuildException {
107 if (mailId == null || "".equals(mailId)) {
108 throw new IllegalArgumentException("メールIDが指定されていません。");
109 }
110 Document doc = retrieveDocument(classPath);
111 if (Mail.DOCTYPE_PUBLIC.equals(doc.getDoctype().getPublicId())) {
112 throw new MailBuildException("指定されたクラスパスのXMLはシングルメールテンプレートです。[classPath='" + classPath
113 + "']");
114 }
115 Element mailElem = doc.getElementById(mailId);
116 return buildMail(mailElem);
117 }
118
119 /***
120 * @see com.ozacc.mail.MultipleMailBuilder#buildMail(java.io.File, java.lang.String)
121 */
122 public Mail buildMail(File file, String mailId) throws MailBuildException {
123 if (mailId == null || "".equals(mailId)) {
124 throw new IllegalArgumentException("メールIDが指定されていません。");
125 }
126 Document doc = retrieveDocument(file);
127 if (Mail.DOCTYPE_PUBLIC.equals(doc.getDoctype().getPublicId())) {
128 throw new MailBuildException("指定されたファイルのXMLはシングルメールテンプレートです。[filePath='"
129 + file.getAbsolutePath() + "']");
130 }
131 return buildMail(doc, mailId);
132 }
133
134 /***
135 * マルチプルメールテンプレートのXMLドキュメント上の指定されたIDが示すメールテンプレートからMailインスタンスを生成して返します。
136 *
137 * @param doc
138 * @param mailId
139 * @return 生成されたMailインスタンス
140 * @throws FactoryConfigurationError
141 */
142 protected Mail buildMail(Document doc, String mailId) throws FactoryConfigurationError {
143 Element mailElem = doc.getElementById(mailId);
144 if (mailElem == null) {
145 throw new MailBuildException("指定されたID[" + mailId + "]のメールデータは見つかりませんでした。");
146 }
147 log.debug(mailElem);
148 return buildMail(mailElem);
149 }
150
151 }