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.4.2.2 2004/11/25 08:00:49 otsuka Exp $
29   */
30  public 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(ignoreComment);
86  			dbf.setIgnoringElementContentWhitespace(true);
87  			dbf.setValidating(true);
88  			try {
89  				db = dbf.newDocumentBuilder();
90  				db.setEntityResolver(new DTDEntityResolver());
91  				documentBuilderCache.put(dbKey, db);
92  			} catch (ParserConfigurationException e) {
93  				// never be thrown
94  				throw new RuntimeException(e);
95  			}
96  		}
97  		return db;
98  	}
99  
100 	/***
101 	 * DocumentBuilderインスタンスを生成します。
102 	 * このDocumentBuilderを使用して生成されるDOM Documentでは、元のXMLデータにあるコメントは削除されます。
103 	 * 
104 	 * @return DocumentBuilder
105 	 * @throws FactoryConfigurationError 
106 	 */
107 	protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError {
108 		return createDocumentBuilder(true);
109 	}
110 
111 	/***
112 	 * 指定されたクラスパスのXMLファイルを読み込み、DOM Documentを生成します。
113 	 * ignoreCommentが指定されている場合は、XMLのコメントを削除しません。
114 	 * 
115 	 * @param ignoreComment
116 	 * @param classPath
117 	 * @return DOM Document
118 	 * @throws IOException
119 	 * @throws SAXException
120 	 */
121 	protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment)
122 																										throws SAXException,
123 																										IOException {
124 		InputStream is = getClass().getResourceAsStream(classPath);
125 		DocumentBuilder db = createDocumentBuilder(ignoreComment);
126 		try {
127 			return db.parse(is);
128 		} finally {
129 			if (is != null) {
130 				is.close();
131 			}
132 		}
133 	}
134 
135 	/***
136 	 * 指定されたクラスパスのXMLファイルを読み込み、DOM Documentを生成します。
137 	 * XMLのコメントや改行は削除されます。
138 	 * 
139 	 * @param classPath
140 	 * @return DOM Document
141 	 * @throws IOException
142 	 * @throws SAXException
143 	 */
144 	protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException {
145 		return getDocumentFromClassPath(classPath, true);
146 	}
147 
148 	/***
149 	 * @param root
150 	 * @param mail 
151 	 */
152 	protected void setReplyTo(Element root, Mail mail) {
153 		NodeList nodes = root.getElementsByTagName("replyTo");
154 		Element replyTo = (Element)nodes.item(0);
155 		if (replyTo != null && replyTo.getAttribute("email").length() > 0) {
156 			mail.setReplyTo(replyTo.getAttribute("email"));
157 		}
158 	}
159 
160 	/***
161 	 * @param root
162 	 * @param mail 
163 	 */
164 	protected void setText(Element root, Mail mail) {
165 		NodeList nodes = root.getElementsByTagName("body");
166 		Element bodyElem = (Element)nodes.item(0);
167 		if (bodyElem == null) {
168 			return;
169 		}
170 		String body = bodyElem.getFirstChild().getNodeValue();
171 		mail.setText(body.trim());
172 	}
173 
174 	/***
175 	 * HTML本文をセット。
176 	 * 
177 	 * @param root
178 	 * @param mail
179 	 */
180 	protected void setHtml(Element root, Mail mail) {
181 		NodeList nodes = root.getElementsByTagName("html");
182 		Element htmlElem = (Element)nodes.item(0);
183 		if (htmlElem == null) {
184 			return;
185 		}
186 		String html = htmlElem.getFirstChild().getNodeValue();
187 		mail.setHtmlText(html.trim());
188 	}
189 
190 	/***
191 	 * @param root
192 	 * @param mail 
193 	 */
194 	protected void setSubject(Element root, Mail mail) {
195 		NodeList nodes = root.getElementsByTagName("subject");
196 		Element subjectElem = (Element)nodes.item(0);
197 		if (subjectElem == null) {
198 			return;
199 		}
200 		String subject = subjectElem.getFirstChild().getNodeValue();
201 		mail.setSubject(subject.trim());
202 	}
203 
204 	/***
205 	 * @param root
206 	 * @param mail 
207 	 */
208 	protected void setRecipients(Element root, Mail mail) {
209 		NodeList nodes = root.getElementsByTagName("recipients");
210 		Element recipientsElem = (Element)nodes.item(0);
211 		if (recipientsElem == null) {
212 			return;
213 		}
214 
215 		NodeList recipientElemList = recipientsElem.getChildNodes();
216 		for (int i = 0, max = recipientElemList.getLength(); i < max; i++) {
217 			Node node = recipientElemList.item(i);
218 			if (node.getNodeType() != Node.ELEMENT_NODE) {
219 				continue;
220 			}
221 			Element e = (Element)node;
222 			if ("to".equals(e.getNodeName())) { // to
223 				if (e.getAttribute("email").length() > 0) {
224 					if (e.getAttribute("name").length() > 0) {
225 						mail.addTo(e.getAttribute("email"), e.getAttribute("name"));
226 					} else {
227 						mail.addTo(e.getAttribute("email"));
228 					}
229 				}
230 			} else if ("cc".equals(e.getNodeName())) { // cc
231 				if (e.getAttribute("email").length() > 0) {
232 					if (e.getAttribute("name").length() > 0) {
233 						mail.addCc(e.getAttribute("email"), e.getAttribute("name"));
234 					} else {
235 						mail.addCc(e.getAttribute("email"));
236 					}
237 				}
238 			} else {
239 				if (e.getAttribute("email").length() > 0) { // bcc
240 					mail.addBcc(e.getAttribute("email"));
241 				}
242 			}
243 		}
244 	}
245 
246 	/***
247 	 * @param root
248 	 * @param mail 
249 	 */
250 	protected void setReturnPath(Element root, Mail mail) {
251 		NodeList nodes = root.getElementsByTagName("returnPath");
252 		Element returnPath = (Element)nodes.item(0);
253 		if (returnPath != null && returnPath.getAttribute("email").length() > 0) {
254 			mail.setReturnPath(returnPath.getAttribute("email"));
255 		}
256 	}
257 
258 	/***
259 	 * @param root
260 	 * @param mail 
261 	 */
262 	protected void setFrom(Element root, Mail mail) {
263 		NodeList nodes = root.getElementsByTagName("from");
264 		Element from = (Element)nodes.item(0);
265 		if (from != null && from.getAttribute("email").length() > 0) {
266 			if (from.getAttribute("name").length() > 0) {
267 				mail.setFrom(from.getAttribute("email"), from.getAttribute("name"));
268 			} else {
269 				mail.setFrom(from.getAttribute("email"));
270 			}
271 		}
272 	}
273 
274 }