1 |
|
package com.ozacc.mail.impl; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.io.IOException; |
5 |
|
|
6 |
|
import javax.xml.parsers.FactoryConfigurationError; |
7 |
|
|
8 |
|
import org.apache.commons.logging.Log; |
9 |
|
import org.apache.commons.logging.LogFactory; |
10 |
|
import org.w3c.dom.Document; |
11 |
|
import org.w3c.dom.Element; |
12 |
|
import org.xml.sax.SAXException; |
13 |
|
|
14 |
|
import com.ozacc.mail.Mail; |
15 |
|
import com.ozacc.mail.MailBuildException; |
16 |
|
import com.ozacc.mail.MultipleMailBuilder; |
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
5 |
public class XMLMailBuilderImpl extends AbstractXMLMailBuilder implements MultipleMailBuilder { |
29 |
|
|
30 |
5 |
private static Log log = LogFactory.getLog(XMLMailBuilderImpl.class); |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public XMLMailBuilderImpl() { |
36 |
30 |
super(); |
37 |
30 |
} |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
public Mail buildMail(String classPath) throws MailBuildException { |
43 |
9 |
Document doc = retrieveDocument(classPath); |
44 |
9 |
return buildMail(doc.getDocumentElement()); |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
public Mail buildMail(File file) throws MailBuildException { |
51 |
20 |
Document doc = retrieveDocument(file); |
52 |
20 |
return buildMail(doc.getDocumentElement()); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private Document retrieveDocument(String classPath) throws MailBuildException { |
61 |
|
try { |
62 |
9 |
return getDocumentFromClassPath(classPath); |
63 |
0 |
} catch (SAXException e) { |
64 |
0 |
throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e); |
65 |
0 |
} catch (IOException e) { |
66 |
0 |
throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private Document retrieveDocument(File file) throws MailBuildException { |
76 |
|
try { |
77 |
23 |
return getDocumentFromFile(file); |
78 |
0 |
} catch (SAXException e) { |
79 |
0 |
throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e); |
80 |
0 |
} catch (IOException e) { |
81 |
0 |
throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
protected Mail buildMail(Element root) { |
92 |
36 |
Mail mail = new Mail(); |
93 |
36 |
setReturnPath(root, mail); |
94 |
36 |
setFrom(root, mail); |
95 |
36 |
setRecipients(root, mail); |
96 |
36 |
setReplyTo(root, mail); |
97 |
36 |
setSubject(root, mail); |
98 |
36 |
setText(root, mail); |
99 |
36 |
setHtml(root, mail); |
100 |
36 |
return mail; |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public Mail buildMail(String classPath, String mailId) throws MailBuildException { |
107 |
0 |
if (mailId == null || "".equals(mailId)) { |
108 |
0 |
throw new IllegalArgumentException("¥á¡¼¥?ID¤¬»ØÄꤵ¤?¤Æ¤¤¤Þ¤»¤ó¡£"); |
109 |
|
} |
110 |
0 |
Document doc = retrieveDocument(classPath); |
111 |
0 |
if (Mail.DOCTYPE_PUBLIC.equals(doc.getDoctype().getPublicId())) { |
112 |
0 |
throw new MailBuildException("»ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¤Ï¥·¥ó¥°¥?¥á¡¼¥?¥Æ¥ó¥×¥?¡¼¥È¤Ç¤¹¡£[classPath='" + classPath |
113 |
0 |
+ "']"); |
114 |
|
} |
115 |
0 |
Element mailElem = doc.getElementById(mailId); |
116 |
0 |
return buildMail(mailElem); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
public Mail buildMail(File file, String mailId) throws MailBuildException { |
123 |
3 |
if (mailId == null || "".equals(mailId)) { |
124 |
0 |
throw new IllegalArgumentException("¥á¡¼¥?ID¤¬»ØÄꤵ¤?¤Æ¤¤¤Þ¤»¤ó¡£"); |
125 |
|
} |
126 |
3 |
Document doc = retrieveDocument(file); |
127 |
3 |
if (Mail.DOCTYPE_PUBLIC.equals(doc.getDoctype().getPublicId())) { |
128 |
0 |
throw new MailBuildException("»ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤ÎXML¤Ï¥·¥ó¥°¥?¥á¡¼¥?¥Æ¥ó¥×¥?¡¼¥È¤Ç¤¹¡£[filePath='" |
129 |
0 |
+ file.getAbsolutePath() + "']"); |
130 |
|
} |
131 |
3 |
return buildMail(doc, mailId); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
protected Mail buildMail(Document doc, String mailId) throws FactoryConfigurationError { |
143 |
3 |
Element mailElem = doc.getElementById(mailId); |
144 |
3 |
if (mailElem == null) { |
145 |
1 |
throw new MailBuildException("»ØÄꤵ¤?¤¿ID[" + mailId + "]¤Î¥á¡¼¥?¥Ç¡¼¥¿¤Ï¸«¤Ä¤«¤ê¤Þ¤»¤ó¤Ç¤·¤¿¡£"); |
146 |
|
} |
147 |
2 |
log.debug(mailElem); |
148 |
2 |
return buildMail(mailElem); |
149 |
|
} |
150 |
|
|
151 |
|
} |