View Javadoc

1   package com.ozacc.mail.xml.impl;
2   
3   import java.io.File;
4   import java.util.Properties;
5   
6   import javax.mail.internet.InternetAddress;
7   import javax.xml.parsers.DocumentBuilder;
8   import javax.xml.parsers.DocumentBuilderFactory;
9   import javax.xml.parsers.ParserConfigurationException;
10  import javax.xml.transform.OutputKeys;
11  import javax.xml.transform.Transformer;
12  import javax.xml.transform.TransformerException;
13  import javax.xml.transform.TransformerFactory;
14  import javax.xml.transform.dom.DOMSource;
15  import javax.xml.transform.stream.StreamResult;
16  
17  import org.w3c.dom.Document;
18  import org.w3c.dom.Element;
19  
20  import com.ozacc.mail.Mail;
21  import com.ozacc.mail.xml.XMLBuildException;
22  import com.ozacc.mail.xml.XMLBuilder;
23  
24  /***
25   * JDK 1.4°Ê¹ß¤Îɸ½àXML¥é¥¤¥Ö¥é¥ê¤ò»ÈÍѤ·¤Æ¼ÂÁõ¤µ¤?¤¿XMLBuilder¡£
26   * 
27   * @since 1.0
28   * @author Tomohiro Otsuka
29   * @version $Id: XMLBuilderImpl.java,v 1.4 2004/09/18 00:39:17 otsuka Exp $
30   */
31  public class XMLBuilderImpl implements XMLBuilder {
32  
33  	private String charset = "UTF-8";
34  
35  	/***
36  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
37  	 */
38  	public XMLBuilderImpl() {}
39  
40  	/***
41  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
42  	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
43  	 * 
44  	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
45  	 */
46  	public XMLBuilderImpl(String charset) {
47  		super();
48  		this.charset = charset;
49  	}
50  
51  	/***
52  	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
53  	 * 
54  	 * @return ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
55  	 */
56  	public String getCharset() {
57  		return charset;
58  	}
59  
60  	/***
61  	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
62  	 * 
63  	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
64  	 */
65  	public void setCharset(String charset) {
66  		this.charset = charset;
67  	}
68  
69  	/***
70  	 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
71  	 */
72  	public Document buildDocument(Mail mail) throws XMLBuildException {
73  		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
74  		DocumentBuilder db;
75  		try {
76  			db = dbf.newDocumentBuilder();
77  		} catch (ParserConfigurationException e) {
78  			// never be thrown
79  			throw new XMLBuildException(e.getMessage());
80  		}
81  		Document doc = db.newDocument();
82  
83  		/*DOMImplementation domImpl = doc.getImplementation();
84  		 DocumentType docType = domImpl.createDocumentType("mail", Mail.DOCTYPE_PUBLIC, Mail.DOCTYPE_SYSTEM);
85  		 doc.appendChild(docType);*/
86  
87  		Element mailElem = doc.createElement("mail");
88  
89  		// Return-Path
90  		if (mail.getReturnPath() != null) {
91  			InternetAddress returnPath = mail.getReturnPath();
92  			Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath",
93  					doc);
94  			mailElem.appendChild(returnPathElem);
95  		}
96  
97  		// From
98  		if (mail.getFrom() != null) {
99  			InternetAddress from = mail.getFrom();
100 			Element fromElem = convertInternetAddressIntoElement(from, "from", doc);
101 			mailElem.appendChild(fromElem);
102 		}
103 
104 		if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
105 			Element recipientsElem = doc.createElement("recipients");
106 
107 			// To
108 			if (mail.getTo().length > 0) {
109 				for (int i = 0; i < mail.getTo().length; i++) {
110 					InternetAddress to = mail.getTo()[i];
111 					Element toElem = convertInternetAddressIntoElement(to, "to", doc);
112 					recipientsElem.appendChild(toElem);
113 				}
114 			}
115 			// Cc
116 			if (mail.getCc().length > 0) {
117 				for (int i = 0; i < mail.getCc().length; i++) {
118 					InternetAddress cc = mail.getCc()[i];
119 					Element ccElem = convertInternetAddressIntoElement(cc, "cc", doc);
120 					recipientsElem.appendChild(ccElem);
121 				}
122 			}
123 			// Bcc
124 			if (mail.getBcc().length > 0) {
125 				for (int i = 0; i < mail.getBcc().length; i++) {
126 					InternetAddress bcc = mail.getBcc()[i];
127 					Element bccElem = convertInternetAddressIntoElement(bcc, "bcc", doc);
128 					recipientsElem.appendChild(bccElem);
129 				}
130 			}
131 			mailElem.appendChild(recipientsElem);
132 		}
133 
134 		// Reply-To
135 		if (mail.getReplyTo() != null) {
136 			InternetAddress replyTo = mail.getReplyTo();
137 			Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo", doc);
138 			mailElem.appendChild(replyToElem);
139 		}
140 
141 		// Subject
142 		if (mail.getSubject() != null) {
143 			Element subjectElem = doc.createElement("subject");
144 			subjectElem.appendChild(doc.createTextNode(mail.getSubject()));
145 			mailElem.appendChild(subjectElem);
146 		}
147 
148 		// Body
149 		if (mail.getText() != null) {
150 			Element bodyElem = doc.createElement("body");
151 			bodyElem.appendChild(doc.createTextNode(mail.getText()));
152 			mailElem.appendChild(bodyElem);
153 		}
154 
155 		// Html
156 		if (mail.isHtmlMail()) {
157 			Element htmlElem = doc.createElement("html");
158 			htmlElem.appendChild(doc.createCDATASection(mail.getHtmlText()));
159 			mailElem.appendChild(htmlElem);
160 		}
161 
162 		doc.appendChild(mailElem);
163 
164 		return doc;
165 	}
166 
167 	private Element convertInternetAddressIntoElement(InternetAddress address, String elemName,
168 														Document doc) {
169 		Element element = doc.createElement(elemName);
170 		element.setAttribute("email", address.getAddress());
171 		if (address.getPersonal() != null) {
172 			element.setAttribute("name", address.getPersonal());
173 		}
174 		return element;
175 	}
176 
177 	/***
178 	 * »ØÄꤵ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éXML¥É¥­¥å¥á¥ó¥È¤òÀ¸À®¤·¡¢
179 	 * »ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤ËÊݸ¤·¤Þ¤¹¡£
180 	 * 
181 	 * ¤³¤Î¥á¥½¥Ã¥ÉÆâÉô¤Ç»ÈÍѤµ¤?¤?TransformerFactory¤¬¥¹¥?¥Ã¥É¥»¡¼¥Õ¤Ç¤Ï¤Ê¤¤¤¿¤á¡¢synchronzed¥á¥½¥Ã¥É¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
182 	 * 
183 	 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
184 	 * @see TransformerFactory
185 	 */
186 	public synchronized void saveDocument(Mail mail, File destFile) throws XMLBuildException {
187 		Document doc = buildDocument(mail);
188 
189 		Transformer t;
190 		try {
191 			t = TransformerFactory.newInstance().newTransformer();
192 		} catch (Exception e) {
193 			// never be thrown
194 			throw new XMLBuildException(e.getMessage());
195 		}
196 		t.setOutputProperties(getOutputProperties());
197 
198 		DOMSource source = new DOMSource(doc);
199 		StreamResult result = new StreamResult(destFile);
200 		try {
201 			t.transform(source, result);
202 		} catch (TransformerException e) {
203 			throw new XMLBuildException("XML¥Õ¥¡¥¤¥?¤ÎÊݸ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
204 		}
205 	}
206 
207 	/***
208 	 * ½ÐÎÏ¥×¥úÁѥƥ£¤òÀ¸À®¡£
209 	 * @return ½ÐÎÏ¥×¥úÁѥƥ£¤òÀßÄꤷ¤¿Properties¥¤¥ó¥¹¥¿¥ó¥¹
210 	 */
211 	private Properties getOutputProperties() {
212 		Properties p = new Properties();
213 		p.put(OutputKeys.ENCODING, charset);
214 		p.put(OutputKeys.INDENT, "yes");
215 		p.put(OutputKeys.DOCTYPE_PUBLIC, Mail.DOCTYPE_PUBLIC);
216 		p.put(OutputKeys.DOCTYPE_SYSTEM, Mail.DOCTYPE_SYSTEM);
217 		return p;
218 	}
219 
220 }