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