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.3 2004/09/13 07:13:21 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 		doc.appendChild(mailElem);
156 
157 		return doc;
158 	}
159 
160 	private Element convertInternetAddressIntoElement(InternetAddress address, String elemName,
161 														Document doc) {
162 		Element element = doc.createElement(elemName);
163 		element.setAttribute("email", address.getAddress());
164 		if (address.getPersonal() != null) {
165 			element.setAttribute("name", address.getPersonal());
166 		}
167 		return element;
168 	}
169 
170 	/***
171 	 * »ØÄꤵ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éXML¥É¥­¥å¥á¥ó¥È¤òÀ¸À®¤·¡¢
172 	 * »ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤ËÊݸ¤·¤Þ¤¹¡£
173 	 * 
174 	 * ¤³¤Î¥á¥½¥Ã¥ÉÆâÉô¤Ç»ÈÍѤµ¤?¤?TransformerFactory¤¬¥¹¥?¥Ã¥É¥»¡¼¥Õ¤Ç¤Ï¤Ê¤¤¤¿¤á¡¢synchronzed¥á¥½¥Ã¥É¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
175 	 * 
176 	 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
177 	 * @see TransformerFactory
178 	 */
179 	public synchronized void saveDocument(Mail mail, File destFile) throws XMLBuildException {
180 		Document doc = buildDocument(mail);
181 
182 		Transformer t;
183 		try {
184 			t = TransformerFactory.newInstance().newTransformer();
185 		} catch (Exception e) {
186 			// never be thrown
187 			throw new XMLBuildException(e.getMessage());
188 		}
189 		t.setOutputProperties(getOutputProperties());
190 
191 		DOMSource source = new DOMSource(doc);
192 		StreamResult result = new StreamResult(destFile);
193 		try {
194 			t.transform(source, result);
195 		} catch (TransformerException e) {
196 			throw new XMLBuildException("XML¥Õ¥¡¥¤¥?¤ÎÊݸ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
197 		}
198 	}
199 
200 	/***
201 	 * ½ÐÎÏ¥×¥úÁѥƥ£¤òÀ¸À®¡£
202 	 * @return ½ÐÎÏ¥×¥úÁѥƥ£¤òÀßÄꤷ¤¿Properties¥¤¥ó¥¹¥¿¥ó¥¹
203 	 */
204 	private Properties getOutputProperties() {
205 		Properties p = new Properties();
206 		p.put(OutputKeys.ENCODING, charset);
207 		p.put(OutputKeys.INDENT, "yes");
208 		p.put(OutputKeys.DOCTYPE_PUBLIC, Mail.DOCTYPE_PUBLIC);
209 		p.put(OutputKeys.DOCTYPE_SYSTEM, Mail.DOCTYPE_SYSTEM);
210 		return p;
211 	}
212 
213 }