| 1 |
|
package com.ozacc.mail.impl; |
| 2 |
|
|
| 3 |
|
import java.io.File; |
| 4 |
|
import java.io.IOException; |
| 5 |
|
import java.io.InputStream; |
| 6 |
|
import java.util.HashMap; |
| 7 |
|
import java.util.Map; |
| 8 |
|
|
| 9 |
|
import javax.xml.parsers.DocumentBuilder; |
| 10 |
|
import javax.xml.parsers.DocumentBuilderFactory; |
| 11 |
|
import javax.xml.parsers.FactoryConfigurationError; |
| 12 |
|
import javax.xml.parsers.ParserConfigurationException; |
| 13 |
|
|
| 14 |
|
import org.w3c.dom.Document; |
| 15 |
|
import org.w3c.dom.Element; |
| 16 |
|
import org.w3c.dom.Node; |
| 17 |
|
import org.w3c.dom.NodeList; |
| 18 |
|
import org.xml.sax.SAXException; |
| 19 |
|
|
| 20 |
|
import com.ozacc.mail.Mail; |
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
public abstract class AbstractXMLMailBuilder { |
| 31 |
|
|
| 32 |
|
protected Map documentBuilderCache; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
30 |
public AbstractXMLMailBuilder() { |
| 38 |
30 |
documentBuilderCache = new HashMap(); |
| 39 |
30 |
} |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
protected synchronized Document getDocumentFromFile(File file, boolean ignoreComment) |
| 51 |
|
throws SAXException, |
| 52 |
|
IOException { |
| 53 |
24 |
DocumentBuilder db = createDocumentBuilder(ignoreComment); |
| 54 |
24 |
return db.parse(file); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
protected Document getDocumentFromFile(File file) throws SAXException, IOException { |
| 67 |
23 |
return getDocumentFromFile(file, true); |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
protected DocumentBuilder createDocumentBuilder(boolean ignoreComment) |
| 79 |
|
throws FactoryConfigurationError { |
| 80 |
41 |
Boolean dbKey = Boolean.valueOf(ignoreComment); |
| 81 |
41 |
DocumentBuilder db = (DocumentBuilder)documentBuilderCache.get(dbKey); |
| 82 |
41 |
if (db == null) { |
| 83 |
31 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 84 |
31 |
dbf.setIgnoringComments(ignoreComment); |
| 85 |
31 |
dbf.setCoalescing(ignoreComment); |
| 86 |
31 |
dbf.setIgnoringElementContentWhitespace(true); |
| 87 |
31 |
dbf.setValidating(true); |
| 88 |
|
try { |
| 89 |
31 |
db = dbf.newDocumentBuilder(); |
| 90 |
31 |
db.setEntityResolver(new DTDEntityResolver()); |
| 91 |
31 |
documentBuilderCache.put(dbKey, db); |
| 92 |
0 |
} catch (ParserConfigurationException e) { |
| 93 |
|
|
| 94 |
0 |
throw new RuntimeException(e); |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
41 |
return db; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError { |
| 108 |
5 |
return createDocumentBuilder(true); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment) |
| 122 |
|
throws SAXException, |
| 123 |
|
IOException { |
| 124 |
12 |
InputStream is = getClass().getResourceAsStream(classPath); |
| 125 |
12 |
DocumentBuilder db = createDocumentBuilder(ignoreComment); |
| 126 |
|
try { |
| 127 |
12 |
return db.parse(is); |
| 128 |
0 |
} finally { |
| 129 |
12 |
if (is != null) { |
| 130 |
12 |
is.close(); |
| 131 |
|
} |
| 132 |
12 |
} |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException { |
| 145 |
9 |
return getDocumentFromClassPath(classPath, true); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
protected void setReplyTo(Element root, Mail mail) { |
| 153 |
36 |
NodeList nodes = root.getElementsByTagName("replyTo"); |
| 154 |
36 |
Element replyTo = (Element)nodes.item(0); |
| 155 |
36 |
if (replyTo != null && replyTo.getAttribute("email").length() > 0) { |
| 156 |
28 |
mail.setReplyTo(replyTo.getAttribute("email")); |
| 157 |
|
} |
| 158 |
36 |
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
protected void setText(Element root, Mail mail) { |
| 165 |
36 |
NodeList nodes = root.getElementsByTagName("body"); |
| 166 |
36 |
Element bodyElem = (Element)nodes.item(0); |
| 167 |
36 |
if (bodyElem == null) { |
| 168 |
0 |
return; |
| 169 |
|
} |
| 170 |
36 |
String body = bodyElem.getFirstChild().getNodeValue(); |
| 171 |
36 |
mail.setText(body.trim()); |
| 172 |
36 |
} |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
protected void setHtml(Element root, Mail mail) { |
| 181 |
36 |
NodeList nodes = root.getElementsByTagName("html"); |
| 182 |
36 |
Element htmlElem = (Element)nodes.item(0); |
| 183 |
36 |
if (htmlElem == null) { |
| 184 |
8 |
return; |
| 185 |
|
} |
| 186 |
28 |
String html = htmlElem.getFirstChild().getNodeValue(); |
| 187 |
28 |
mail.setHtmlText(html.trim()); |
| 188 |
28 |
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
protected void setSubject(Element root, Mail mail) { |
| 195 |
36 |
NodeList nodes = root.getElementsByTagName("subject"); |
| 196 |
36 |
Element subjectElem = (Element)nodes.item(0); |
| 197 |
36 |
if (subjectElem == null) { |
| 198 |
0 |
return; |
| 199 |
|
} |
| 200 |
36 |
String subject = subjectElem.getFirstChild().getNodeValue(); |
| 201 |
36 |
mail.setSubject(subject.trim()); |
| 202 |
36 |
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
protected void setRecipients(Element root, Mail mail) { |
| 209 |
36 |
NodeList nodes = root.getElementsByTagName("recipients"); |
| 210 |
36 |
Element recipientsElem = (Element)nodes.item(0); |
| 211 |
36 |
if (recipientsElem == null) { |
| 212 |
1 |
return; |
| 213 |
|
} |
| 214 |
|
|
| 215 |
35 |
NodeList recipientElemList = recipientsElem.getChildNodes(); |
| 216 |
182 |
for (int i = 0, max = recipientElemList.getLength(); i < max; i++) { |
| 217 |
147 |
Node node = recipientElemList.item(i); |
| 218 |
147 |
if (node.getNodeType() != Node.ELEMENT_NODE) { |
| 219 |
0 |
continue; |
| 220 |
|
} |
| 221 |
147 |
Element e = (Element)node; |
| 222 |
147 |
if ("to".equals(e.getNodeName())) { |
| 223 |
63 |
if (e.getAttribute("email").length() > 0) { |
| 224 |
63 |
if (e.getAttribute("name").length() > 0) { |
| 225 |
35 |
mail.addTo(e.getAttribute("email"), e.getAttribute("name")); |
| 226 |
|
} else { |
| 227 |
28 |
mail.addTo(e.getAttribute("email")); |
| 228 |
|
} |
| 229 |
|
} |
| 230 |
84 |
} else if ("cc".equals(e.getNodeName())) { |
| 231 |
56 |
if (e.getAttribute("email").length() > 0) { |
| 232 |
56 |
if (e.getAttribute("name").length() > 0) { |
| 233 |
28 |
mail.addCc(e.getAttribute("email"), e.getAttribute("name")); |
| 234 |
|
} else { |
| 235 |
28 |
mail.addCc(e.getAttribute("email")); |
| 236 |
|
} |
| 237 |
|
} |
| 238 |
|
} else { |
| 239 |
28 |
if (e.getAttribute("email").length() > 0) { |
| 240 |
28 |
mail.addBcc(e.getAttribute("email")); |
| 241 |
|
} |
| 242 |
|
} |
| 243 |
|
} |
| 244 |
35 |
} |
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
protected void setReturnPath(Element root, Mail mail) { |
| 251 |
36 |
NodeList nodes = root.getElementsByTagName("returnPath"); |
| 252 |
36 |
Element returnPath = (Element)nodes.item(0); |
| 253 |
36 |
if (returnPath != null && returnPath.getAttribute("email").length() > 0) { |
| 254 |
28 |
mail.setReturnPath(returnPath.getAttribute("email")); |
| 255 |
|
} |
| 256 |
36 |
} |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
protected void setFrom(Element root, Mail mail) { |
| 263 |
36 |
NodeList nodes = root.getElementsByTagName("from"); |
| 264 |
36 |
Element from = (Element)nodes.item(0); |
| 265 |
36 |
if (from != null && from.getAttribute("email").length() > 0) { |
| 266 |
35 |
if (from.getAttribute("name").length() > 0) { |
| 267 |
35 |
mail.setFrom(from.getAttribute("email"), from.getAttribute("name")); |
| 268 |
|
} else { |
| 269 |
0 |
mail.setFrom(from.getAttribute("email")); |
| 270 |
|
} |
| 271 |
|
} |
| 272 |
36 |
} |
| 273 |
|
|
| 274 |
|
} |