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.FactoryConfigurationError; |
10 |
|
import javax.xml.parsers.ParserConfigurationException; |
11 |
|
import javax.xml.transform.OutputKeys; |
12 |
|
import javax.xml.transform.Transformer; |
13 |
|
import javax.xml.transform.TransformerException; |
14 |
|
import javax.xml.transform.TransformerFactory; |
15 |
|
import javax.xml.transform.dom.DOMSource; |
16 |
|
import javax.xml.transform.stream.StreamResult; |
17 |
|
|
18 |
|
import org.w3c.dom.Document; |
19 |
|
import org.w3c.dom.Element; |
20 |
|
|
21 |
|
import com.ozacc.mail.Mail; |
22 |
|
import com.ozacc.mail.xml.XMLBuildException; |
23 |
|
import com.ozacc.mail.xml.XMLBuilder; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class XMLBuilderImpl implements XMLBuilder { |
33 |
|
|
34 |
3 |
private String charset = "UTF-8"; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
6 |
public XMLBuilderImpl() {} |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
public XMLBuilderImpl(String charset) { |
48 |
0 |
super(); |
49 |
0 |
this.charset = charset; |
50 |
0 |
} |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public String getCharset() { |
58 |
0 |
return charset; |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
public void setCharset(String charset) { |
67 |
0 |
this.charset = charset; |
68 |
0 |
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
public Document buildDocument(Mail mail) throws XMLBuildException { |
74 |
3 |
Document doc = createNewDocument(); |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
3 |
Element mailElem = doc.createElement("mail"); |
81 |
|
|
82 |
|
|
83 |
3 |
if (mail.getReturnPath() != null) { |
84 |
0 |
InternetAddress returnPath = mail.getReturnPath(); |
85 |
0 |
Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath", |
86 |
0 |
doc); |
87 |
0 |
mailElem.appendChild(returnPathElem); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
3 |
if (mail.getFrom() != null) { |
92 |
3 |
InternetAddress from = mail.getFrom(); |
93 |
3 |
Element fromElem = convertInternetAddressIntoElement(from, "from", doc); |
94 |
3 |
mailElem.appendChild(fromElem); |
95 |
|
} |
96 |
|
|
97 |
3 |
if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) { |
98 |
3 |
Element recipientsElem = doc.createElement("recipients"); |
99 |
|
|
100 |
|
|
101 |
3 |
if (mail.getTo().length > 0) { |
102 |
6 |
for (int i = 0; i < mail.getTo().length; i++) { |
103 |
3 |
InternetAddress to = mail.getTo()[i]; |
104 |
3 |
Element toElem = convertInternetAddressIntoElement(to, "to", doc); |
105 |
3 |
recipientsElem.appendChild(toElem); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
3 |
if (mail.getCc().length > 0) { |
110 |
0 |
for (int i = 0; i < mail.getCc().length; i++) { |
111 |
0 |
InternetAddress cc = mail.getCc()[i]; |
112 |
0 |
Element ccElem = convertInternetAddressIntoElement(cc, "cc", doc); |
113 |
0 |
recipientsElem.appendChild(ccElem); |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
3 |
if (mail.getBcc().length > 0) { |
118 |
0 |
for (int i = 0; i < mail.getBcc().length; i++) { |
119 |
0 |
InternetAddress bcc = mail.getBcc()[i]; |
120 |
0 |
Element bccElem = convertInternetAddressIntoElement(bcc, "bcc", doc); |
121 |
0 |
recipientsElem.appendChild(bccElem); |
122 |
|
} |
123 |
|
} |
124 |
3 |
mailElem.appendChild(recipientsElem); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
3 |
if (mail.getReplyTo() != null) { |
129 |
0 |
InternetAddress replyTo = mail.getReplyTo(); |
130 |
0 |
Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo", doc); |
131 |
0 |
mailElem.appendChild(replyToElem); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
3 |
if (mail.getSubject() != null) { |
136 |
3 |
Element subjectElem = doc.createElement("subject"); |
137 |
3 |
subjectElem.appendChild(doc.createTextNode(mail.getSubject())); |
138 |
3 |
mailElem.appendChild(subjectElem); |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
3 |
if (mail.getText() != null) { |
143 |
3 |
Element bodyElem = doc.createElement("body"); |
144 |
3 |
bodyElem.appendChild(doc.createTextNode(mail.getText())); |
145 |
3 |
mailElem.appendChild(bodyElem); |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
3 |
if (mail.isHtmlMail()) { |
150 |
1 |
Element htmlElem = doc.createElement("html"); |
151 |
1 |
htmlElem.appendChild(doc.createCDATASection(mail.getHtmlText())); |
152 |
1 |
mailElem.appendChild(htmlElem); |
153 |
|
} |
154 |
|
|
155 |
3 |
doc.appendChild(mailElem); |
156 |
|
|
157 |
3 |
return doc; |
158 |
|
} |
159 |
|
|
160 |
|
public static Document createNewDocument() throws FactoryConfigurationError { |
161 |
3 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
162 |
|
try { |
163 |
3 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
164 |
3 |
Document doc = db.newDocument(); |
165 |
3 |
return doc; |
166 |
0 |
} catch (ParserConfigurationException e) { |
167 |
|
|
168 |
0 |
throw new XMLBuildException("", e); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
private Element convertInternetAddressIntoElement(InternetAddress address, String elemName, |
173 |
|
Document doc) { |
174 |
6 |
Element element = doc.createElement(elemName); |
175 |
6 |
element.setAttribute("email", address.getAddress()); |
176 |
6 |
if (address.getPersonal() != null) { |
177 |
3 |
element.setAttribute("name", address.getPersonal()); |
178 |
|
} |
179 |
6 |
return element; |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
public synchronized void saveDocument(Mail mail, File destFile) throws XMLBuildException { |
192 |
2 |
Document doc = buildDocument(mail); |
193 |
|
|
194 |
|
Transformer t; |
195 |
|
try { |
196 |
2 |
t = TransformerFactory.newInstance().newTransformer(); |
197 |
0 |
} catch (Exception e) { |
198 |
|
|
199 |
0 |
throw new XMLBuildException(e.getMessage()); |
200 |
|
} |
201 |
2 |
t.setOutputProperties(getOutputProperties()); |
202 |
|
|
203 |
2 |
DOMSource source = new DOMSource(doc); |
204 |
2 |
StreamResult result = new StreamResult(destFile); |
205 |
|
try { |
206 |
2 |
t.transform(source, result); |
207 |
0 |
} catch (TransformerException e) { |
208 |
0 |
throw new XMLBuildException("XML¥Õ¥¡¥¤¥?¤ÎÊݸ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
209 |
|
} |
210 |
2 |
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
private Properties getOutputProperties() { |
217 |
2 |
Properties p = new Properties(); |
218 |
2 |
p.put(OutputKeys.ENCODING, charset); |
219 |
2 |
p.put(OutputKeys.INDENT, "yes"); |
220 |
2 |
p.put(OutputKeys.DOCTYPE_PUBLIC, Mail.DOCTYPE_PUBLIC); |
221 |
2 |
p.put(OutputKeys.DOCTYPE_SYSTEM, Mail.DOCTYPE_SYSTEM); |
222 |
2 |
return p; |
223 |
|
} |
224 |
|
|
225 |
|
} |