| 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 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 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 |
3 |
private String charset = "UTF-8"; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
6 |
public JDomXMLBuilder() {} |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public JDomXMLBuilder(String charset) { |
| 49 |
0 |
this(); |
| 50 |
0 |
setCharset(charset); |
| 51 |
0 |
} |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
public void setCharset(String charset) { |
| 59 |
0 |
this.charset = charset; |
| 60 |
0 |
} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
public String getCharset() { |
| 68 |
0 |
return charset; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
public org.w3c.dom.Document buildDocument(Mail mail) throws XMLBuildException { |
| 75 |
1 |
Document doc = buildJDomDocument(mail); |
| 76 |
1 |
DOMOutputter outputter = new DOMOutputter(); |
| 77 |
|
try { |
| 78 |
1 |
return outputter.output(doc); |
| 79 |
0 |
} catch (JDOMException e) { |
| 80 |
0 |
throw new XMLBuildException("DOM Document¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
public Document buildJDomDocument(Mail mail) { |
| 90 |
|
|
| 91 |
3 |
Element mailElem = new Element("mail"); |
| 92 |
|
|
| 93 |
|
|
| 94 |
3 |
if (mail.getReturnPath() != null) { |
| 95 |
0 |
InternetAddress returnPath = mail.getReturnPath(); |
| 96 |
0 |
Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath"); |
| 97 |
0 |
mailElem.addContent(returnPathElem); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
3 |
if (mail.getFrom() != null) { |
| 102 |
3 |
InternetAddress from = mail.getFrom(); |
| 103 |
3 |
Element fromElem = convertInternetAddressIntoElement(from, "from"); |
| 104 |
3 |
mailElem.addContent(fromElem); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
3 |
if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) { |
| 108 |
3 |
Element recipientsElem = new Element("recipients"); |
| 109 |
|
|
| 110 |
|
|
| 111 |
3 |
if (mail.getTo().length > 0) { |
| 112 |
6 |
for (int i = 0; i < mail.getTo().length; i++) { |
| 113 |
3 |
InternetAddress to = mail.getTo()[i]; |
| 114 |
3 |
Element toElem = convertInternetAddressIntoElement(to, "to"); |
| 115 |
3 |
recipientsElem.addContent(toElem); |
| 116 |
|
} |
| 117 |
|
} |
| 118 |
|
|
| 119 |
3 |
if (mail.getCc().length > 0) { |
| 120 |
0 |
for (int i = 0; i < mail.getCc().length; i++) { |
| 121 |
0 |
InternetAddress cc = mail.getCc()[i]; |
| 122 |
0 |
Element ccElem = convertInternetAddressIntoElement(cc, "cc"); |
| 123 |
0 |
recipientsElem.addContent(ccElem); |
| 124 |
|
} |
| 125 |
|
} |
| 126 |
|
|
| 127 |
3 |
if (mail.getBcc().length > 0) { |
| 128 |
0 |
for (int i = 0; i < mail.getBcc().length; i++) { |
| 129 |
0 |
InternetAddress bcc = mail.getBcc()[i]; |
| 130 |
0 |
Element bccElem = convertInternetAddressIntoElement(bcc, "bcc"); |
| 131 |
0 |
recipientsElem.addContent(bccElem); |
| 132 |
|
} |
| 133 |
|
} |
| 134 |
3 |
mailElem.addContent(recipientsElem); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
3 |
if (mail.getReplyTo() != null) { |
| 139 |
0 |
InternetAddress replyTo = mail.getReplyTo(); |
| 140 |
0 |
Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo"); |
| 141 |
0 |
mailElem.addContent(replyToElem); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
3 |
if (mail.getSubject() != null) { |
| 146 |
3 |
Element subjectElem = new Element("subject"); |
| 147 |
3 |
subjectElem.setText(mail.getSubject()); |
| 148 |
3 |
mailElem.addContent(subjectElem); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
3 |
if (mail.getText() != null) { |
| 153 |
3 |
Element textElem = new Element("body"); |
| 154 |
3 |
textElem.setText(mail.getText()); |
| 155 |
3 |
mailElem.addContent(textElem); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
3 |
if (mail.isHtmlMail()) { |
| 160 |
1 |
Element htmlElem = new Element("html"); |
| 161 |
1 |
htmlElem.setContent(new CDATA(mail.getHtmlText())); |
| 162 |
1 |
mailElem.addContent(htmlElem); |
| 163 |
|
} |
| 164 |
|
|
| 165 |
3 |
Document doc = new Document(mailElem); |
| 166 |
3 |
DocType docType = new DocType("mail", DOCTYPE_PUBLIC, DOCTYPE_SYSTEM); |
| 167 |
3 |
doc.setDocType(docType); |
| 168 |
3 |
return doc; |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
private Element convertInternetAddressIntoElement(InternetAddress address, String elemName) { |
| 178 |
6 |
Element element = new Element(elemName); |
| 179 |
6 |
element.setAttribute("email", address.getAddress()); |
| 180 |
6 |
if (address.getPersonal() != null) { |
| 181 |
3 |
element.setAttribute("name", address.getPersonal()); |
| 182 |
|
} |
| 183 |
6 |
return element; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
public void saveDocument(Mail mail, File destFile) throws XMLBuildException { |
| 190 |
|
|
| 191 |
2 |
Document doc = buildJDomDocument(mail); |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
try { |
| 195 |
2 |
FileOutputStream fos = new FileOutputStream(destFile); |
| 196 |
2 |
XMLOutputter outputter = getXMLOutputter(); |
| 197 |
2 |
outputter.output(doc, fos); |
| 198 |
2 |
fos.close(); |
| 199 |
0 |
} catch (IOException e) { |
| 200 |
0 |
throw new XMLBuildException("DOM Document¤Î¥Õ¥¡¥¤¥?½ÐÎϤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
| 201 |
|
} |
| 202 |
2 |
} |
| 203 |
|
|
| 204 |
|
public XMLOutputter getXMLOutputter() { |
| 205 |
2 |
Format format = Format.getPrettyFormat(); |
| 206 |
2 |
format.setEncoding(charset); |
| 207 |
2 |
XMLOutputter outputter = new XMLOutputter(format); |
| 208 |
2 |
return outputter; |
| 209 |
|
} |
| 210 |
|
} |