OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / java / com / ozacc / mail / impl / AbstractXMLMailBuilder.java
1 package com.ozacc.mail.impl;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.io.InputStream;\r
6 import java.util.HashMap;\r
7 import java.util.Map;\r
8 \r
9 import javax.xml.parsers.DocumentBuilder;\r
10 import javax.xml.parsers.DocumentBuilderFactory;\r
11 import javax.xml.parsers.FactoryConfigurationError;\r
12 import javax.xml.parsers.ParserConfigurationException;\r
13 \r
14 import org.w3c.dom.Document;\r
15 import org.w3c.dom.Element;\r
16 import org.w3c.dom.Node;\r
17 import org.w3c.dom.NodeList;\r
18 import org.xml.sax.SAXException;\r
19 \r
20 import com.ozacc.mail.Mail;\r
21 \r
22 /**\r
23  * XMLMailBuilder実装が継承する基底クラス。\r
24  * \r
25  * @since 1.1\r
26  * \r
27  * @author Tomohiro Otsuka\r
28  * @version $Id: AbstractXMLMailBuilder.java,v 1.4.2.2 2004/11/25 08:00:49 otsuka Exp $\r
29  */\r
30 public abstract class AbstractXMLMailBuilder {\r
31 \r
32         protected Map documentBuilderCache;\r
33 \r
34         /**\r
35          * コンストラクタ。\r
36          */\r
37         public AbstractXMLMailBuilder() {\r
38                 documentBuilderCache = new HashMap();\r
39         }\r
40 \r
41         /**\r
42          * 指定されたXMLファイルを読み込み、DOM Documentを生成します。\r
43          * ignoreCommentが指定されている場合は、XMLのコメントを削除しません。\r
44          * \r
45          * @param file XMLファイル\r
46          * @return DOM Document\r
47          * @throws IOException\r
48          * @throws SAXException\r
49          */\r
50         protected synchronized Document getDocumentFromFile(File file, boolean ignoreComment)\r
51                                                                                                                                                                                         throws SAXException,\r
52                                                                                                                                                                                         IOException {\r
53                 DocumentBuilder db = createDocumentBuilder(ignoreComment);\r
54                 return db.parse(file);\r
55         }\r
56 \r
57         /**\r
58          * 指定されたXMLファイルを読み込み、DOM Documentを生成します。\r
59          * XMLのコメントや改行は削除されます。\r
60          * \r
61          * @param file XMLファイル\r
62          * @return DOM Document\r
63          * @throws IOException\r
64          * @throws SAXException\r
65          */\r
66         protected Document getDocumentFromFile(File file) throws SAXException, IOException {\r
67                 return getDocumentFromFile(file, true);\r
68         }\r
69 \r
70         /**\r
71          * DocumentBuilderインスタンスを生成します。\r
72          * ignoreCommentが指定されている場合は、コメントを削除しないように設定されたDocumentBuilderを生成します。\r
73          * \r
74          * @param ignoreComment\r
75          * @return DocumentBuilder\r
76          * @throws FactoryConfigurationError \r
77          */\r
78         protected DocumentBuilder createDocumentBuilder(boolean ignoreComment)\r
79                                                                                                                                                         throws FactoryConfigurationError {\r
80                 Boolean dbKey = Boolean.valueOf(ignoreComment);\r
81                 DocumentBuilder db = (DocumentBuilder)documentBuilderCache.get(dbKey);\r
82                 if (db == null) {\r
83                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r
84                         dbf.setIgnoringComments(ignoreComment);\r
85                         dbf.setCoalescing(ignoreComment);\r
86                         dbf.setIgnoringElementContentWhitespace(true);\r
87                         dbf.setValidating(true);\r
88                         try {\r
89                                 db = dbf.newDocumentBuilder();\r
90                                 db.setEntityResolver(new DTDEntityResolver());\r
91                                 documentBuilderCache.put(dbKey, db);\r
92                         } catch (ParserConfigurationException e) {\r
93                                 // never be thrown\r
94                                 throw new RuntimeException(e);\r
95                         }\r
96                 }\r
97                 return db;\r
98         }\r
99 \r
100         /**\r
101          * DocumentBuilderインスタンスを生成します。\r
102          * このDocumentBuilderを使用して生成されるDOM Documentでは、元のXMLデータにあるコメントは削除されます。\r
103          * \r
104          * @return DocumentBuilder\r
105          * @throws FactoryConfigurationError \r
106          */\r
107         protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError {\r
108                 return createDocumentBuilder(true);\r
109         }\r
110 \r
111         /**\r
112          * 指定されたクラスパスのXMLファイルを読み込み、DOM Documentを生成します。\r
113          * ignoreCommentが指定されている場合は、XMLのコメントを削除しません。\r
114          * \r
115          * @param ignoreComment\r
116          * @param classPath\r
117          * @return DOM Document\r
118          * @throws IOException\r
119          * @throws SAXException\r
120          */\r
121         protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment)\r
122                                                                                                                                                                                                                 throws SAXException,\r
123                                                                                                                                                                                                                 IOException {\r
124                 InputStream is = getClass().getResourceAsStream(classPath);\r
125                 DocumentBuilder db = createDocumentBuilder(ignoreComment);\r
126                 try {\r
127                         return db.parse(is);\r
128                 } finally {\r
129                         if (is != null) {\r
130                                 is.close();\r
131                         }\r
132                 }\r
133         }\r
134 \r
135         /**\r
136          * 指定されたクラスパスのXMLファイルを読み込み、DOM Documentを生成します。\r
137          * XMLのコメントや改行は削除されます。\r
138          * \r
139          * @param classPath\r
140          * @return DOM Document\r
141          * @throws IOException\r
142          * @throws SAXException\r
143          */\r
144         protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException {\r
145                 return getDocumentFromClassPath(classPath, true);\r
146         }\r
147 \r
148         /**\r
149          * @param root\r
150          * @param mail \r
151          */\r
152         protected void setReplyTo(Element root, Mail mail) {\r
153                 NodeList nodes = root.getElementsByTagName("replyTo");\r
154                 Element replyTo = (Element)nodes.item(0);\r
155                 if (replyTo != null && replyTo.getAttribute("email").length() > 0) {\r
156                         mail.setReplyTo(replyTo.getAttribute("email"));\r
157                 }\r
158         }\r
159 \r
160         /**\r
161          * @param root\r
162          * @param mail \r
163          */\r
164         protected void setText(Element root, Mail mail) {\r
165                 NodeList nodes = root.getElementsByTagName("body");\r
166                 Element bodyElem = (Element)nodes.item(0);\r
167                 if (bodyElem == null) {\r
168                         return;\r
169                 }\r
170                 String body = bodyElem.getFirstChild().getNodeValue();\r
171                 mail.setText(body.trim());\r
172         }\r
173 \r
174         /**\r
175          * HTML本文をセット。\r
176          * \r
177          * @param root\r
178          * @param mail\r
179          */\r
180         protected void setHtml(Element root, Mail mail) {\r
181                 NodeList nodes = root.getElementsByTagName("html");\r
182                 Element htmlElem = (Element)nodes.item(0);\r
183                 if (htmlElem == null) {\r
184                         return;\r
185                 }\r
186                 String html = htmlElem.getFirstChild().getNodeValue();\r
187                 mail.setHtmlText(html.trim());\r
188         }\r
189 \r
190         /**\r
191          * @param root\r
192          * @param mail \r
193          */\r
194         protected void setSubject(Element root, Mail mail) {\r
195                 NodeList nodes = root.getElementsByTagName("subject");\r
196                 Element subjectElem = (Element)nodes.item(0);\r
197                 if (subjectElem == null) {\r
198                         return;\r
199                 }\r
200                 String subject = subjectElem.getFirstChild().getNodeValue();\r
201                 mail.setSubject(subject.trim());\r
202         }\r
203 \r
204         /**\r
205          * @param root\r
206          * @param mail \r
207          */\r
208         protected void setRecipients(Element root, Mail mail) {\r
209                 NodeList nodes = root.getElementsByTagName("recipients");\r
210                 Element recipientsElem = (Element)nodes.item(0);\r
211                 if (recipientsElem == null) {\r
212                         return;\r
213                 }\r
214 \r
215                 NodeList recipientElemList = recipientsElem.getChildNodes();\r
216                 for (int i = 0, max = recipientElemList.getLength(); i < max; i++) {\r
217                         Node node = recipientElemList.item(i);\r
218                         if (node.getNodeType() != Node.ELEMENT_NODE) {\r
219                                 continue;\r
220                         }\r
221                         Element e = (Element)node;\r
222                         if ("to".equals(e.getNodeName())) { // to\r
223                                 if (e.getAttribute("email").length() > 0) {\r
224                                         if (e.getAttribute("name").length() > 0) {\r
225                                                 mail.addTo(e.getAttribute("email"), e.getAttribute("name"));\r
226                                         } else {\r
227                                                 mail.addTo(e.getAttribute("email"));\r
228                                         }\r
229                                 }\r
230                         } else if ("cc".equals(e.getNodeName())) { // cc\r
231                                 if (e.getAttribute("email").length() > 0) {\r
232                                         if (e.getAttribute("name").length() > 0) {\r
233                                                 mail.addCc(e.getAttribute("email"), e.getAttribute("name"));\r
234                                         } else {\r
235                                                 mail.addCc(e.getAttribute("email"));\r
236                                         }\r
237                                 }\r
238                         } else {\r
239                                 if (e.getAttribute("email").length() > 0) { // bcc\r
240                                         mail.addBcc(e.getAttribute("email"));\r
241                                 }\r
242                         }\r
243                 }\r
244         }\r
245 \r
246         /**\r
247          * @param root\r
248          * @param mail \r
249          */\r
250         protected void setReturnPath(Element root, Mail mail) {\r
251                 NodeList nodes = root.getElementsByTagName("returnPath");\r
252                 Element returnPath = (Element)nodes.item(0);\r
253                 if (returnPath != null && returnPath.getAttribute("email").length() > 0) {\r
254                         mail.setReturnPath(returnPath.getAttribute("email"));\r
255                 }\r
256         }\r
257 \r
258         /**\r
259          * @param root\r
260          * @param mail \r
261          */\r
262         protected void setFrom(Element root, Mail mail) {\r
263                 NodeList nodes = root.getElementsByTagName("from");\r
264                 Element from = (Element)nodes.item(0);\r
265                 if (from != null && from.getAttribute("email").length() > 0) {\r
266                         if (from.getAttribute("name").length() > 0) {\r
267                                 mail.setFrom(from.getAttribute("email"), from.getAttribute("name"));\r
268                         } else {\r
269                                 mail.setFrom(from.getAttribute("email"));\r
270                         }\r
271                 }\r
272         }\r
273 \r
274 }