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