1 package com.ozacc.mail.xml.impl;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6
7 import javax.mail.internet.InternetAddress;
8
9 import org.jdom.CDATA;
10 import org.jdom.DocType;
11 import org.jdom.Document;
12 import org.jdom.Element;
13 import org.jdom.JDOMException;
14 import org.jdom.output.DOMOutputter;
15 import org.jdom.output.Format;
16 import org.jdom.output.XMLOutputter;
17
18 import com.ozacc.mail.Mail;
19 import com.ozacc.mail.xml.XMLBuildException;
20 import com.ozacc.mail.xml.XMLBuilder;
21
22 /***
23 * XMLBuilderの実装クラス。
24 *
25 * @since 1.0
26 * @author Tomohiro Otsuka
27 * @version $Id: JDomXMLBuilder.java,v 1.6 2004/09/18 00:39:17 otsuka Exp $
28 */
29 public class JDomXMLBuilder implements XMLBuilder {
30
31 public static final String DOCTYPE_PUBLIC = "-//OZACC//DTD MAIL//EN";
32
33 public static final String DOCTYPE_SYSTEM = "http://www.ozacc.com/library/dtd/ozacc-mail.dtd";
34
35 private String charset = "UTF-8";
36
37 /***
38 * コンストラクタ。
39 */
40 public JDomXMLBuilder() {}
41
42 /***
43 * コンストラクタ。
44 * 出力XMLファイルの文字コードを指定します。デフォルトはUTF-8。
45 *
46 * @param charset 出力XMLファイルの文字コード
47 */
48 public JDomXMLBuilder(String charset) {
49 this();
50 setCharset(charset);
51 }
52
53 /***
54 * 出力XMLファイルの文字コードを指定します。デフォルトはUTF-8。
55 *
56 * @param charset 出力XMLファイルの文字コード
57 */
58 public void setCharset(String charset) {
59 this.charset = charset;
60 }
61
62 /***
63 * 出力XMLファイルの文字コードを返します。
64 *
65 * @return 出力XMLファイルの文字コード
66 */
67 public String getCharset() {
68 return charset;
69 }
70
71 /***
72 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
73 */
74 public org.w3c.dom.Document buildDocument(Mail mail) throws XMLBuildException {
75 Document doc = buildJDomDocument(mail);
76 DOMOutputter outputter = new DOMOutputter();
77 try {
78 return outputter.output(doc);
79 } catch (JDOMException e) {
80 throw new XMLBuildException("DOM Documentの生成に失敗しました。", e);
81 }
82 }
83
84 /***
85 * 指定されたMailインスタンスからJDOMドキュメントを生成します。
86 *
87 * @return 生成されたJDOMドキュメント
88 */
89 public Document buildJDomDocument(Mail mail) {
90
91 Element mailElem = new Element("mail");
92
93
94 if (mail.getReturnPath() != null) {
95 InternetAddress returnPath = mail.getReturnPath();
96 Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath");
97 mailElem.addContent(returnPathElem);
98 }
99
100
101 if (mail.getFrom() != null) {
102 InternetAddress from = mail.getFrom();
103 Element fromElem = convertInternetAddressIntoElement(from, "from");
104 mailElem.addContent(fromElem);
105 }
106
107 if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
108 Element recipientsElem = new Element("recipients");
109
110
111 if (mail.getTo().length > 0) {
112 for (int i = 0; i < mail.getTo().length; i++) {
113 InternetAddress to = mail.getTo()[i];
114 Element toElem = convertInternetAddressIntoElement(to, "to");
115 recipientsElem.addContent(toElem);
116 }
117 }
118
119 if (mail.getCc().length > 0) {
120 for (int i = 0; i < mail.getCc().length; i++) {
121 InternetAddress cc = mail.getCc()[i];
122 Element ccElem = convertInternetAddressIntoElement(cc, "cc");
123 recipientsElem.addContent(ccElem);
124 }
125 }
126
127 if (mail.getBcc().length > 0) {
128 for (int i = 0; i < mail.getBcc().length; i++) {
129 InternetAddress bcc = mail.getBcc()[i];
130 Element bccElem = convertInternetAddressIntoElement(bcc, "bcc");
131 recipientsElem.addContent(bccElem);
132 }
133 }
134 mailElem.addContent(recipientsElem);
135 }
136
137
138 if (mail.getReplyTo() != null) {
139 InternetAddress replyTo = mail.getReplyTo();
140 Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo");
141 mailElem.addContent(replyToElem);
142 }
143
144
145 if (mail.getSubject() != null) {
146 Element subjectElem = new Element("subject");
147 subjectElem.setText(mail.getSubject());
148 mailElem.addContent(subjectElem);
149 }
150
151
152 if (mail.getText() != null) {
153 Element textElem = new Element("body");
154 textElem.setText(mail.getText());
155 mailElem.addContent(textElem);
156 }
157
158
159 if (mail.isHtmlMail()) {
160 Element htmlElem = new Element("html");
161 htmlElem.setContent(new CDATA(mail.getHtmlText()));
162 mailElem.addContent(htmlElem);
163 }
164
165 Document doc = new Document(mailElem);
166 DocType docType = new DocType("mail", DOCTYPE_PUBLIC, DOCTYPE_SYSTEM);
167 doc.setDocType(docType);
168 return doc;
169 }
170
171 /***
172 *
173 * @param address
174 * @param elemName
175 * @return
176 */
177 private Element convertInternetAddressIntoElement(InternetAddress address, String elemName) {
178 Element element = new Element(elemName);
179 element.setAttribute("email", address.getAddress());
180 if (address.getPersonal() != null) {
181 element.setAttribute("name", address.getPersonal());
182 }
183 return element;
184 }
185
186 /***
187 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
188 */
189 public void saveDocument(Mail mail, File destFile) throws XMLBuildException {
190
191 Document doc = buildJDomDocument(mail);
192
193
194 try {
195 FileOutputStream fos = new FileOutputStream(destFile);
196 XMLOutputter outputter = getXMLOutputter();
197 outputter.output(doc, fos);
198 fos.close();
199 } catch (IOException e) {
200 throw new XMLBuildException("DOM Documentのファイル出力に失敗しました。", e);
201 }
202 }
203
204 public XMLOutputter getXMLOutputter() {
205 Format format = Format.getPrettyFormat();
206 format.setEncoding(charset);
207 XMLOutputter outputter = new XMLOutputter(format);
208 return outputter;
209 }
210 }