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.DocType;
10 import org.jdom.Document;
11 import org.jdom.Element;
12 import org.jdom.JDOMException;
13 import org.jdom.output.DOMOutputter;
14 import org.jdom.output.Format;
15 import org.jdom.output.XMLOutputter;
16
17 import com.ozacc.mail.Mail;
18 import com.ozacc.mail.xml.XMLBuildException;
19 import com.ozacc.mail.xml.XMLBuilder;
20
21 /***
22 * XMLBuilder¤Î¼ÂÁõ¥¯¥é¥¹¡£
23 *
24 * @author Tomohiro Otsuka
25 * @version $Id: JDomXMLBuilder.java,v 1.2 2004/09/05 17:32:52 otsuka Exp $
26 */
27 public class JDomXMLBuilder implements XMLBuilder {
28
29 private final DocType DOC_TYPE = new DocType("mail", "-//OZACC//DTD MAIL//EN",
30 "http://www.ozacc.com/library/dtd/ozacc-mail.dtd");
31
32 private String charset = "UTF-8";
33
34 /***
35 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
36 */
37 public JDomXMLBuilder() {}
38
39 /***
40 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
41 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
42 *
43 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
44 */
45 public JDomXMLBuilder(String charset) {
46 setCharset(charset);
47 }
48
49 /***
50 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
51 *
52 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
53 */
54 public void setCharset(String charset) {
55 this.charset = charset;
56 }
57
58 /***
59 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
60 *
61 * @return ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
62 */
63 public String getCharset() {
64 return charset;
65 }
66
67 /***
68 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
69 */
70 public org.w3c.dom.Document buildDocument(Mail mail) throws XMLBuildException {
71 Document doc = buildJDomDocument(mail);
72 DOMOutputter outputter = new DOMOutputter();
73 try {
74 return outputter.output(doc);
75 } catch (JDOMException e) {
76 throw new XMLBuildException("DOM Document¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
77 }
78 }
79
80 /***
81 * »ØÄꤵ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éJDOM¥É¥¥å¥á¥ó¥È¤òÀ¸À®¤·¤Þ¤¹¡£
82 *
83 * @return À¸À®¤µ¤?¤¿JDOM¥É¥¥å¥á¥ó¥È
84 */
85 public Document buildJDomDocument(Mail mail) {
86
87 Element mailElem = new Element("mail");
88
89
90 if (mail.getReturnPath() != null) {
91 InternetAddress returnPath = mail.getReturnPath();
92 Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath");
93 mailElem.addContent(returnPathElem);
94 }
95
96
97 if (mail.getFrom() != null) {
98 InternetAddress from = mail.getFrom();
99 Element fromElem = convertInternetAddressIntoElement(from, "from");
100 mailElem.addContent(fromElem);
101 }
102
103 if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
104 Element recipientsElem = new Element("recipients");
105
106
107 if (mail.getTo().length > 0) {
108 for (int i = 0; i < mail.getTo().length; i++) {
109 InternetAddress to = mail.getTo()[i];
110 Element toElem = convertInternetAddressIntoElement(to, "to");
111 recipientsElem.addContent(toElem);
112 }
113 }
114
115 if (mail.getCc().length > 0) {
116 for (int i = 0; i < mail.getCc().length; i++) {
117 InternetAddress cc = mail.getCc()[i];
118 Element ccElem = convertInternetAddressIntoElement(cc, "cc");
119 recipientsElem.addContent(ccElem);
120 }
121 }
122
123 if (mail.getBcc().length > 0) {
124 for (int i = 0; i < mail.getBcc().length; i++) {
125 InternetAddress bcc = mail.getBcc()[i];
126 Element bccElem = convertInternetAddressIntoElement(bcc, "bcc");
127 recipientsElem.addContent(bccElem);
128 }
129 }
130 mailElem.addContent(recipientsElem);
131 }
132
133
134 if (mail.getReplyTo() != null) {
135 InternetAddress replyTo = mail.getReplyTo();
136 Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo");
137 mailElem.addContent(replyToElem);
138 }
139
140
141 if (mail.getSubject() != null) {
142 Element subjectElem = new Element("subject");
143 subjectElem.setText(mail.getSubject());
144 mailElem.addContent(subjectElem);
145 }
146
147
148 if (mail.getText() != null) {
149 Element textElem = new Element("body");
150 textElem.setText(mail.getText());
151 mailElem.addContent(textElem);
152 }
153
154 Document doc = new Document(mailElem);
155 doc.setDocType(DOC_TYPE);
156 return doc;
157 }
158
159 /***
160 *
161 * @param address
162 * @param elemName
163 * @return
164 */
165 private Element convertInternetAddressIntoElement(InternetAddress address, String elemName) {
166 Element element = new Element(elemName);
167 element.setAttribute("email", address.getAddress());
168 if (address.getPersonal() != null) {
169 element.setAttribute("name", address.getPersonal());
170 }
171 return element;
172 }
173
174 /***
175 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
176 */
177 public void saveDocument(Mail mail, File destFile) throws XMLBuildException {
178
179 Document doc = buildJDomDocument(mail);
180
181
182 try {
183 FileOutputStream fos = new FileOutputStream(destFile);
184 Format format = Format.getPrettyFormat();
185 format.setEncoding(charset);
186 XMLOutputter outputter = new XMLOutputter(format);
187 outputter.output(doc, fos);
188 fos.close();
189 } catch (IOException e) {
190 throw new XMLBuildException("DOM Document¤Î¥Õ¥¡¥¤¥?½ÐÎϤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
191 }
192 }
193
194 }