View Javadoc

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   * XMLMailBuilder¼ÂÁõ¤¬·Ñ¾µ¤¹¤?´ðÄ?¥¯¥é¥¹¡£
24   * 
25   * @since 1.1
26   * 
27   * @author Tomohiro Otsuka
28   * @version $Id: AbstractXMLMailBuilder.java,v 1.2 2004/09/17 23:07:16 otsuka Exp $
29   */
30  abstract class AbstractXMLMailBuilder {
31  
32  	protected Map documentBuilderCache;
33  
34  	/***
35  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
36  	 */
37  	public AbstractXMLMailBuilder() {
38  		documentBuilderCache = new HashMap();
39  	}
40  
41  	/***
42  	 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
43  	 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢XML¤Î¥³¥á¥ó¥È¤òº?½?¤·¤Þ¤»¤ó¡£
44  	 * 
45  	 * @param file XML¥Õ¥¡¥¤¥?
46  	 * @return DOM Document
47  	 * @throws IOException
48  	 * @throws SAXException
49  	 */
50  	protected synchronized Document getDocumentFromFile(File file, boolean ignoreComment)
51  																							throws SAXException,
52  																							IOException {
53  		DocumentBuilder db = createDocumentBuilder(ignoreComment);
54  		return db.parse(file);
55  	}
56  
57  	/***
58  	 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
59  	 * XML¤Î¥³¥á¥ó¥È¤ä²?¹Ô¤Ïº?½?¤µ¤?¤Þ¤¹¡£
60  	 * 
61  	 * @param file XML¥Õ¥¡¥¤¥?
62  	 * @return DOM Document
63  	 * @throws IOException
64  	 * @throws SAXException
65  	 */
66  	protected Document getDocumentFromFile(File file) throws SAXException, IOException {
67  		return getDocumentFromFile(file, true);
68  	}
69  
70  	/***
71  	 * DocumentBuilder¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
72  	 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢¥³¥á¥ó¥È¤òº?½?¤·¤Ê¤¤¤è¤¦¤ËÀßÄꤵ¤?¤¿DocumentBuilder¤òÀ¸À®¤·¤Þ¤¹¡£
73  	 * 
74  	 * @param ignoreComment
75  	 * @return DocumentBuilder
76  	 * @throws FactoryConfigurationError 
77  	 */
78  	protected DocumentBuilder createDocumentBuilder(boolean ignoreComment)
79  																			throws FactoryConfigurationError {
80  		Boolean dbKey = Boolean.valueOf(ignoreComment);
81  		DocumentBuilder db = (DocumentBuilder)documentBuilderCache.get(dbKey);
82  		if (db == null) {
83  			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
84  			dbf.setIgnoringComments(ignoreComment);
85  			dbf.setCoalescing(true);
86  			dbf.setIgnoringElementContentWhitespace(true);
87  			dbf.setValidating(true);
88  			try {
89  				db = dbf.newDocumentBuilder();
90  				documentBuilderCache.put(dbKey, db);
91  			} catch (ParserConfigurationException e) {
92  				// never be thrown
93  				throw new RuntimeException(e);
94  			}
95  		}
96  		return db;
97  	}
98  
99  	/***
100 	 * DocumentBuilder¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
101 	 * ¤³¤ÎDocumentBuilder¤ò»ÈÍѤ·¤ÆÀ¸À®¤µ¤?¤?DOM Document¤Ç¤Ï¡¢¸µ¤ÎXML¥Ç¡¼¥¿¤Ë¤¢¤?¥³¥á¥ó¥È¤Ïº?½?¤µ¤?¤Þ¤¹¡£
102 	 * 
103 	 * @return DocumentBuilder
104 	 * @throws FactoryConfigurationError 
105 	 */
106 	protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError {
107 		return createDocumentBuilder(true);
108 	}
109 
110 	/***
111 	 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
112 	 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢XML¤Î¥³¥á¥ó¥È¤òº?½?¤·¤Þ¤»¤ó¡£
113 	 * 
114 	 * @param ignoreComment
115 	 * @param classPath
116 	 * @return DOM Document
117 	 * @throws IOException
118 	 * @throws SAXException
119 	 */
120 	protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment)
121 																										throws SAXException,
122 																										IOException {
123 		InputStream is = getClass().getResourceAsStream(classPath);
124 		DocumentBuilder db = createDocumentBuilder(ignoreComment);
125 		try {
126 			return db.parse(is);
127 		} finally {
128 			if (is != null) {
129 				is.close();
130 			}
131 		}
132 	}
133 
134 	/***
135 	 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
136 	 * XML¤Î¥³¥á¥ó¥È¤ä²?¹Ô¤Ïº?½?¤µ¤?¤Þ¤¹¡£
137 	 * 
138 	 * @param classPath
139 	 * @return DOM Document
140 	 * @throws IOException
141 	 * @throws SAXException
142 	 */
143 	protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException {
144 		return getDocumentFromClassPath(classPath, true);
145 	}
146 
147 	/***
148 	 * @param root
149 	 * @param mail 
150 	 */
151 	protected void setReplyTo(Element root, Mail mail) {
152 		NodeList nodes = root.getElementsByTagName("replyTo");
153 		Element replyTo = (Element)nodes.item(0);
154 		if (replyTo != null && replyTo.getAttribute("email").length() > 0) {
155 			mail.setReplyTo(replyTo.getAttribute("email"));
156 		}
157 	}
158 
159 	/***
160 	 * @param root
161 	 * @param mail 
162 	 */
163 	protected void setText(Element root, Mail mail) {
164 		NodeList nodes = root.getElementsByTagName("body");
165 		Element bodyElem = (Element)nodes.item(0);
166 		if (bodyElem == null) {
167 			return;
168 		}
169 		String body = bodyElem.getFirstChild().getNodeValue();
170 		mail.setText(body.trim());
171 	}
172 
173 	/***
174 	 * HTMLËÜʸ¤ò¥»¥Ã¥È¡£
175 	 * 
176 	 * @param root
177 	 * @param mail
178 	 */
179 	protected void setHtml(Element root, Mail mail) {
180 		NodeList nodes = root.getElementsByTagName("html");
181 		Element htmlElem = (Element)nodes.item(0);
182 		if (htmlElem == null) {
183 			return;
184 		}
185 		String html = htmlElem.getFirstChild().getNodeValue();
186 		mail.setHtmlText(html.trim());
187 	}
188 
189 	/***
190 	 * @param root
191 	 * @param mail 
192 	 */
193 	protected void setSubject(Element root, Mail mail) {
194 		NodeList nodes = root.getElementsByTagName("subject");
195 		Element subjectElem = (Element)nodes.item(0);
196 		if (subjectElem == null) {
197 			return;
198 		}
199 		String subject = subjectElem.getFirstChild().getNodeValue();
200 		mail.setSubject(subject.trim());
201 	}
202 
203 	/***
204 	 * @param root
205 	 * @param mail 
206 	 */
207 	protected void setRecipients(Element root, Mail mail) {
208 		NodeList nodes = root.getElementsByTagName("recipients");
209 		Element recipientsElem = (Element)nodes.item(0);
210 		if (recipientsElem == null) {
211 			return;
212 		}
213 
214 		NodeList recipientElemList = recipientsElem.getChildNodes();
215 		for (int i = 0, max = recipientElemList.getLength(); i < max; i++) {
216 			Node node = recipientElemList.item(i);
217 			if (node.getNodeType() != Node.ELEMENT_NODE) {
218 				continue;
219 			}
220 			Element e = (Element)node;
221 			if ("to".equals(e.getNodeName())) { // to
222 				if (e.getAttribute("email").length() > 0) {
223 					if (e.getAttribute("name").length() > 0) {
224 						mail.addTo(e.getAttribute("email"), e.getAttribute("name"));
225 					} else {
226 						mail.addTo(e.getAttribute("email"));
227 					}
228 				}
229 			} else if ("cc".equals(e.getNodeName())) { // cc
230 				if (e.getAttribute("email").length() > 0) {
231 					if (e.getAttribute("name").length() > 0) {
232 						mail.addCc(e.getAttribute("email"), e.getAttribute("name"));
233 					} else {
234 						mail.addCc(e.getAttribute("email"));
235 					}
236 				}
237 			} else {
238 				if (e.getAttribute("email").length() > 0) { // bcc
239 					mail.addBcc(e.getAttribute("email"));
240 				}
241 			}
242 		}
243 	}
244 
245 	/***
246 	 * @param root
247 	 * @param mail 
248 	 */
249 	protected void setReturnPath(Element root, Mail mail) {
250 		NodeList nodes = root.getElementsByTagName("returnPath");
251 		Element returnPath = (Element)nodes.item(0);
252 		if (returnPath != null && returnPath.getAttribute("email").length() > 0) {
253 			mail.setReturnPath(returnPath.getAttribute("email"));
254 		}
255 	}
256 
257 	/***
258 	 * @param root
259 	 * @param mail 
260 	 */
261 	protected void setFrom(Element root, Mail mail) {
262 		NodeList nodes = root.getElementsByTagName("from");
263 		Element from = (Element)nodes.item(0);
264 		if (from != null && from.getAttribute("email").length() > 0) {
265 			if (from.getAttribute("name").length() > 0) {
266 				mail.setFrom(from.getAttribute("email"), from.getAttribute("name"));
267 			} else {
268 				mail.setFrom(from.getAttribute("email"));
269 			}
270 		}
271 	}
272 
273 }