1 package com.ozacc.mail.xml.impl;
2
3 import java.io.File;
4 import java.util.Properties;
5
6 import javax.mail.internet.InternetAddress;
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
10 import javax.xml.transform.OutputKeys;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerException;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.dom.DOMSource;
15 import javax.xml.transform.stream.StreamResult;
16
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19
20 import com.ozacc.mail.Mail;
21 import com.ozacc.mail.xml.XMLBuildException;
22 import com.ozacc.mail.xml.XMLBuilder;
23
24 /***
25 * JDK 1.4以降の標準XMLライブラリを使用して実装されたXMLBuilder。
26 *
27 * @since 1.0
28 * @author Tomohiro Otsuka
29 * @version $Id: XMLBuilderImpl.java,v 1.4 2004/09/18 00:39:17 otsuka Exp $
30 */
31 public class XMLBuilderImpl implements XMLBuilder {
32
33 private String charset = "UTF-8";
34
35 /***
36 * コンストラクタ。
37 */
38 public XMLBuilderImpl() {}
39
40 /***
41 * コンストラクタ。
42 * 出力XMLファイルの文字コードを指定します。デフォルトはUTF-8。
43 *
44 * @param charset 出力XMLファイルの文字コード
45 */
46 public XMLBuilderImpl(String charset) {
47 super();
48 this.charset = charset;
49 }
50
51 /***
52 * 出力XMLファイルの文字コードを返します。
53 *
54 * @return 出力XMLファイルの文字コード
55 */
56 public String getCharset() {
57 return charset;
58 }
59
60 /***
61 * 出力XMLファイルの文字コードを指定します。デフォルトはUTF-8。
62 *
63 * @param charset 出力XMLファイルの文字コード
64 */
65 public void setCharset(String charset) {
66 this.charset = charset;
67 }
68
69 /***
70 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
71 */
72 public Document buildDocument(Mail mail) throws XMLBuildException {
73 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
74 DocumentBuilder db;
75 try {
76 db = dbf.newDocumentBuilder();
77 } catch (ParserConfigurationException e) {
78
79 throw new XMLBuildException(e.getMessage());
80 }
81 Document doc = db.newDocument();
82
83
84
85
86
87 Element mailElem = doc.createElement("mail");
88
89
90 if (mail.getReturnPath() != null) {
91 InternetAddress returnPath = mail.getReturnPath();
92 Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath",
93 doc);
94 mailElem.appendChild(returnPathElem);
95 }
96
97
98 if (mail.getFrom() != null) {
99 InternetAddress from = mail.getFrom();
100 Element fromElem = convertInternetAddressIntoElement(from, "from", doc);
101 mailElem.appendChild(fromElem);
102 }
103
104 if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
105 Element recipientsElem = doc.createElement("recipients");
106
107
108 if (mail.getTo().length > 0) {
109 for (int i = 0; i < mail.getTo().length; i++) {
110 InternetAddress to = mail.getTo()[i];
111 Element toElem = convertInternetAddressIntoElement(to, "to", doc);
112 recipientsElem.appendChild(toElem);
113 }
114 }
115
116 if (mail.getCc().length > 0) {
117 for (int i = 0; i < mail.getCc().length; i++) {
118 InternetAddress cc = mail.getCc()[i];
119 Element ccElem = convertInternetAddressIntoElement(cc, "cc", doc);
120 recipientsElem.appendChild(ccElem);
121 }
122 }
123
124 if (mail.getBcc().length > 0) {
125 for (int i = 0; i < mail.getBcc().length; i++) {
126 InternetAddress bcc = mail.getBcc()[i];
127 Element bccElem = convertInternetAddressIntoElement(bcc, "bcc", doc);
128 recipientsElem.appendChild(bccElem);
129 }
130 }
131 mailElem.appendChild(recipientsElem);
132 }
133
134
135 if (mail.getReplyTo() != null) {
136 InternetAddress replyTo = mail.getReplyTo();
137 Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo", doc);
138 mailElem.appendChild(replyToElem);
139 }
140
141
142 if (mail.getSubject() != null) {
143 Element subjectElem = doc.createElement("subject");
144 subjectElem.appendChild(doc.createTextNode(mail.getSubject()));
145 mailElem.appendChild(subjectElem);
146 }
147
148
149 if (mail.getText() != null) {
150 Element bodyElem = doc.createElement("body");
151 bodyElem.appendChild(doc.createTextNode(mail.getText()));
152 mailElem.appendChild(bodyElem);
153 }
154
155
156 if (mail.isHtmlMail()) {
157 Element htmlElem = doc.createElement("html");
158 htmlElem.appendChild(doc.createCDATASection(mail.getHtmlText()));
159 mailElem.appendChild(htmlElem);
160 }
161
162 doc.appendChild(mailElem);
163
164 return doc;
165 }
166
167 private Element convertInternetAddressIntoElement(InternetAddress address, String elemName,
168 Document doc) {
169 Element element = doc.createElement(elemName);
170 element.setAttribute("email", address.getAddress());
171 if (address.getPersonal() != null) {
172 element.setAttribute("name", address.getPersonal());
173 }
174 return element;
175 }
176
177 /***
178 * 指定されたMailインスタンスからXMLドキュメントを生成し、
179 * 指定されたファイルに保存します。
180 *
181 * このメソッド内部で使用されるTransformerFactoryがスレッドセーフではないため、synchronzedメソッドになっています。
182 *
183 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
184 * @see TransformerFactory
185 */
186 public synchronized void saveDocument(Mail mail, File destFile) throws XMLBuildException {
187 Document doc = buildDocument(mail);
188
189 Transformer t;
190 try {
191 t = TransformerFactory.newInstance().newTransformer();
192 } catch (Exception e) {
193
194 throw new XMLBuildException(e.getMessage());
195 }
196 t.setOutputProperties(getOutputProperties());
197
198 DOMSource source = new DOMSource(doc);
199 StreamResult result = new StreamResult(destFile);
200 try {
201 t.transform(source, result);
202 } catch (TransformerException e) {
203 throw new XMLBuildException("XMLファイルの保存に失敗しました。", e);
204 }
205 }
206
207 /***
208 * 出力プロパティを生成。
209 * @return 出力プロパティを設定したPropertiesインスタンス
210 */
211 private Properties getOutputProperties() {
212 Properties p = new Properties();
213 p.put(OutputKeys.ENCODING, charset);
214 p.put(OutputKeys.INDENT, "yes");
215 p.put(OutputKeys.DOCTYPE_PUBLIC, Mail.DOCTYPE_PUBLIC);
216 p.put(OutputKeys.DOCTYPE_SYSTEM, Mail.DOCTYPE_SYSTEM);
217 return p;
218 }
219
220 }