View Javadoc

1   package com.ozacc.mail.xml.impl;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.IOException;
6   
7   import javax.mail.internet.InternetAddress;
8   
9   import org.jdom.DocType;
10  import org.jdom.Document;
11  import org.jdom.Element;
12  import org.jdom.JDOMException;
13  import org.jdom.output.DOMOutputter;
14  import org.jdom.output.Format;
15  import org.jdom.output.XMLOutputter;
16  
17  import com.ozacc.mail.Mail;
18  import com.ozacc.mail.xml.XMLBuildException;
19  import com.ozacc.mail.xml.XMLBuilder;
20  
21  /***
22   * XMLBuilder¤Î¼ÂÁõ¥¯¥é¥¹¡£
23   * 
24   * @author Tomohiro Otsuka
25   * @version $Id: JDomXMLBuilder.java,v 1.3 2004/09/07 14:36:51 otsuka Exp $
26   */
27  public class JDomXMLBuilder implements XMLBuilder {
28  
29  	private String charset = "UTF-8";
30  
31  	/***
32  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
33  	 */
34  	public JDomXMLBuilder() {}
35  
36  	/***
37  	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
38  	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
39  	 * 
40  	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
41  	 */
42  	public JDomXMLBuilder(String charset) {
43  		setCharset(charset);
44  	}
45  
46  	/***
47  	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
48  	 * 
49  	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
50  	 */
51  	public void setCharset(String charset) {
52  		this.charset = charset;
53  	}
54  
55  	/***
56  	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
57  	 * 
58  	 * @return ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
59  	 */
60  	public String getCharset() {
61  		return charset;
62  	}
63  
64  	/***
65  	 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
66  	 */
67  	public org.w3c.dom.Document buildDocument(Mail mail) throws XMLBuildException {
68  		Document doc = buildJDomDocument(mail);
69  		DOMOutputter outputter = new DOMOutputter();
70  		try {
71  			return outputter.output(doc);
72  		} catch (JDOMException e) {
73  			throw new XMLBuildException("DOM Document¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
74  		}
75  	}
76  
77  	/***
78  	 * »ØÄꤵ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éJDOM¥É¥­¥å¥á¥ó¥È¤òÀ¸À®¤·¤Þ¤¹¡£
79  	 * 
80  	 * @return À¸À®¤µ¤?¤¿JDOM¥É¥­¥å¥á¥ó¥È
81  	 */
82  	public Document buildJDomDocument(Mail mail) {
83  
84  		Element mailElem = new Element("mail");
85  
86  		// Return-Path
87  		if (mail.getReturnPath() != null) {
88  			InternetAddress returnPath = mail.getReturnPath();
89  			Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath");
90  			mailElem.addContent(returnPathElem);
91  		}
92  
93  		// From
94  		if (mail.getFrom() != null) {
95  			InternetAddress from = mail.getFrom();
96  			Element fromElem = convertInternetAddressIntoElement(from, "from");
97  			mailElem.addContent(fromElem);
98  		}
99  
100 		if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
101 			Element recipientsElem = new Element("recipients");
102 
103 			// To
104 			if (mail.getTo().length > 0) {
105 				for (int i = 0; i < mail.getTo().length; i++) {
106 					InternetAddress to = mail.getTo()[i];
107 					Element toElem = convertInternetAddressIntoElement(to, "to");
108 					recipientsElem.addContent(toElem);
109 				}
110 			}
111 			// Cc
112 			if (mail.getCc().length > 0) {
113 				for (int i = 0; i < mail.getCc().length; i++) {
114 					InternetAddress cc = mail.getCc()[i];
115 					Element ccElem = convertInternetAddressIntoElement(cc, "cc");
116 					recipientsElem.addContent(ccElem);
117 				}
118 			}
119 			// Bcc
120 			if (mail.getBcc().length > 0) {
121 				for (int i = 0; i < mail.getBcc().length; i++) {
122 					InternetAddress bcc = mail.getBcc()[i];
123 					Element bccElem = convertInternetAddressIntoElement(bcc, "bcc");
124 					recipientsElem.addContent(bccElem);
125 				}
126 			}
127 			mailElem.addContent(recipientsElem);
128 		}
129 
130 		// Reply-To
131 		if (mail.getReplyTo() != null) {
132 			InternetAddress replyTo = mail.getReplyTo();
133 			Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo");
134 			mailElem.addContent(replyToElem);
135 		}
136 
137 		// Subject
138 		if (mail.getSubject() != null) {
139 			Element subjectElem = new Element("subject");
140 			subjectElem.setText(mail.getSubject());
141 			mailElem.addContent(subjectElem);
142 		}
143 
144 		// Body
145 		if (mail.getText() != null) {
146 			Element textElem = new Element("body");
147 			textElem.setText(mail.getText());
148 			mailElem.addContent(textElem);
149 		}
150 
151 		Document doc = new Document(mailElem);
152 		DocType docType = new DocType("mail", "-//OZACC//DTD MAIL//EN",
153 				"http://www.ozacc.com/library/dtd/ozacc-mail.dtd");
154 		doc.setDocType(docType);
155 		return doc;
156 	}
157 
158 	/***
159 	 * 
160 	 * @param address
161 	 * @param elemName
162 	 * @return
163 	 */
164 	private Element convertInternetAddressIntoElement(InternetAddress address, String elemName) {
165 		Element element = new Element(elemName);
166 		element.setAttribute("email", address.getAddress());
167 		if (address.getPersonal() != null) {
168 			element.setAttribute("name", address.getPersonal());
169 		}
170 		return element;
171 	}
172 
173 	/***
174 	 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
175 	 */
176 	public void saveDocument(Mail mail, File destFile) throws XMLBuildException {
177 		// JDOM Document¤òÀ¸À®
178 		Document doc = buildJDomDocument(mail);
179 
180 		// ¥Õ¥¡¥¤¥?½ÐÎÏ
181 		try {
182 			FileOutputStream fos = new FileOutputStream(destFile);
183 			Format format = Format.getPrettyFormat();
184 			format.setEncoding(charset);
185 			XMLOutputter outputter = new XMLOutputter(format);
186 			outputter.output(doc, fos);
187 			fos.close();
188 		} catch (IOException e) {
189 			throw new XMLBuildException("DOM Document¤Î¥Õ¥¡¥¤¥?½ÐÎϤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
190 		}
191 	}
192 
193 }