OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / java / com / ozacc / mail / xml / impl / XMLBuilderImpl.java
1 package com.ozacc.mail.xml.impl;\r
2 \r
3 import java.io.File;\r
4 import java.util.Properties;\r
5 \r
6 import javax.mail.internet.InternetAddress;\r
7 import javax.xml.parsers.DocumentBuilder;\r
8 import javax.xml.parsers.DocumentBuilderFactory;\r
9 import javax.xml.parsers.FactoryConfigurationError;\r
10 import javax.xml.parsers.ParserConfigurationException;\r
11 import javax.xml.transform.OutputKeys;\r
12 import javax.xml.transform.Transformer;\r
13 import javax.xml.transform.TransformerException;\r
14 import javax.xml.transform.TransformerFactory;\r
15 import javax.xml.transform.dom.DOMSource;\r
16 import javax.xml.transform.stream.StreamResult;\r
17 \r
18 import org.w3c.dom.Document;\r
19 import org.w3c.dom.Element;\r
20 \r
21 import com.ozacc.mail.Mail;\r
22 import com.ozacc.mail.xml.XMLBuildException;\r
23 import com.ozacc.mail.xml.XMLBuilder;\r
24 \r
25 /**\r
26  * JDK 1.4以降の標準XMLライブラリを使用して実装されたXMLBuilder。\r
27  * \r
28  * @since 1.0\r
29  * @author Tomohiro Otsuka\r
30  * @version $Id: XMLBuilderImpl.java,v 1.4.2.1 2005/01/21 22:15:07 otsuka Exp $\r
31  */\r
32 public class XMLBuilderImpl implements XMLBuilder {\r
33 \r
34         private String charset = "UTF-8";\r
35 \r
36         /**\r
37          * コンストラクタ。\r
38          */\r
39         public XMLBuilderImpl() {}\r
40 \r
41         /**\r
42          * コンストラクタ。\r
43          * 出力XMLファイルの文字コードを指定します。デフォルトはUTF-8。\r
44          * \r
45          * @param charset 出力XMLファイルの文字コード\r
46          */\r
47         public XMLBuilderImpl(String charset) {\r
48                 super();\r
49                 this.charset = charset;\r
50         }\r
51 \r
52         /**\r
53          * 出力XMLファイルの文字コードを返します。\r
54          * \r
55          * @return 出力XMLファイルの文字コード\r
56          */\r
57         public String getCharset() {\r
58                 return charset;\r
59         }\r
60 \r
61         /**\r
62          * 出力XMLファイルの文字コードを指定します。デフォルトはUTF-8。\r
63          * \r
64          * @param charset 出力XMLファイルの文字コード\r
65          */\r
66         public void setCharset(String charset) {\r
67                 this.charset = charset;\r
68         }\r
69 \r
70         /**\r
71          * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)\r
72          */\r
73         public Document buildDocument(Mail mail) throws XMLBuildException {\r
74                 Document doc = createNewDocument();\r
75 \r
76                 /*DOMImplementation domImpl = doc.getImplementation();\r
77                  DocumentType docType = domImpl.createDocumentType("mail", Mail.DOCTYPE_PUBLIC, Mail.DOCTYPE_SYSTEM);\r
78                  doc.appendChild(docType);*/\r
79 \r
80                 Element mailElem = doc.createElement("mail");\r
81 \r
82                 // Return-Path\r
83                 if (mail.getReturnPath() != null) {\r
84                         InternetAddress returnPath = mail.getReturnPath();\r
85                         Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath",\r
86                                         doc);\r
87                         mailElem.appendChild(returnPathElem);\r
88                 }\r
89 \r
90                 // From\r
91                 if (mail.getFrom() != null) {\r
92                         InternetAddress from = mail.getFrom();\r
93                         Element fromElem = convertInternetAddressIntoElement(from, "from", doc);\r
94                         mailElem.appendChild(fromElem);\r
95                 }\r
96 \r
97                 if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {\r
98                         Element recipientsElem = doc.createElement("recipients");\r
99 \r
100                         // To\r
101                         if (mail.getTo().length > 0) {\r
102                                 for (int i = 0; i < mail.getTo().length; i++) {\r
103                                         InternetAddress to = mail.getTo()[i];\r
104                                         Element toElem = convertInternetAddressIntoElement(to, "to", doc);\r
105                                         recipientsElem.appendChild(toElem);\r
106                                 }\r
107                         }\r
108                         // Cc\r
109                         if (mail.getCc().length > 0) {\r
110                                 for (int i = 0; i < mail.getCc().length; i++) {\r
111                                         InternetAddress cc = mail.getCc()[i];\r
112                                         Element ccElem = convertInternetAddressIntoElement(cc, "cc", doc);\r
113                                         recipientsElem.appendChild(ccElem);\r
114                                 }\r
115                         }\r
116                         // Bcc\r
117                         if (mail.getBcc().length > 0) {\r
118                                 for (int i = 0; i < mail.getBcc().length; i++) {\r
119                                         InternetAddress bcc = mail.getBcc()[i];\r
120                                         Element bccElem = convertInternetAddressIntoElement(bcc, "bcc", doc);\r
121                                         recipientsElem.appendChild(bccElem);\r
122                                 }\r
123                         }\r
124                         mailElem.appendChild(recipientsElem);\r
125                 }\r
126 \r
127                 // Reply-To\r
128                 if (mail.getReplyTo() != null) {\r
129                         InternetAddress replyTo = mail.getReplyTo();\r
130                         Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo", doc);\r
131                         mailElem.appendChild(replyToElem);\r
132                 }\r
133 \r
134                 // Subject\r
135                 if (mail.getSubject() != null) {\r
136                         Element subjectElem = doc.createElement("subject");\r
137                         subjectElem.appendChild(doc.createTextNode(mail.getSubject()));\r
138                         mailElem.appendChild(subjectElem);\r
139                 }\r
140 \r
141                 // Body\r
142                 if (mail.getText() != null) {\r
143                         Element bodyElem = doc.createElement("body");\r
144                         bodyElem.appendChild(doc.createTextNode(mail.getText()));\r
145                         mailElem.appendChild(bodyElem);\r
146                 }\r
147 \r
148                 // Html\r
149                 if (mail.isHtmlMail()) {\r
150                         Element htmlElem = doc.createElement("html");\r
151                         htmlElem.appendChild(doc.createCDATASection(mail.getHtmlText()));\r
152                         mailElem.appendChild(htmlElem);\r
153                 }\r
154 \r
155                 doc.appendChild(mailElem);\r
156 \r
157                 return doc;\r
158         }\r
159 \r
160         public static Document createNewDocument() throws FactoryConfigurationError {\r
161                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r
162                 try {\r
163                         DocumentBuilder db = dbf.newDocumentBuilder();\r
164                         Document doc = db.newDocument();\r
165                         return doc;\r
166                 } catch (ParserConfigurationException e) {\r
167                         // never be thrown\r
168                         throw new XMLBuildException("", e);\r
169                 }\r
170         }\r
171 \r
172         private Element convertInternetAddressIntoElement(InternetAddress address, String elemName,\r
173                                                                                                                 Document doc) {\r
174                 Element element = doc.createElement(elemName);\r
175                 element.setAttribute("email", address.getAddress());\r
176                 if (address.getPersonal() != null) {\r
177                         element.setAttribute("name", address.getPersonal());\r
178                 }\r
179                 return element;\r
180         }\r
181 \r
182         /**\r
183          * 指定されたMailインスタンスからXMLドキュメントを生成し、\r
184          * 指定されたファイルに保存します。\r
185          * \r
186          * このメソッド内部で使用されるTransformerFactoryがスレッドセーフではないため、synchronzedメソッドになっています。\r
187          * \r
188          * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)\r
189          * @see TransformerFactory\r
190          */\r
191         public synchronized void saveDocument(Mail mail, File destFile) throws XMLBuildException {\r
192                 Document doc = buildDocument(mail);\r
193 \r
194                 Transformer t;\r
195                 try {\r
196                         t = TransformerFactory.newInstance().newTransformer();\r
197                 } catch (Exception e) {\r
198                         // never be thrown\r
199                         throw new XMLBuildException(e.getMessage());\r
200                 }\r
201                 t.setOutputProperties(getOutputProperties());\r
202 \r
203                 DOMSource source = new DOMSource(doc);\r
204                 StreamResult result = new StreamResult(destFile);\r
205                 try {\r
206                         t.transform(source, result);\r
207                 } catch (TransformerException e) {\r
208                         throw new XMLBuildException("XMLファイルの保存に失敗しました。", e);\r
209                 }\r
210         }\r
211 \r
212         /**\r
213          * 出力プロパティを生成。\r
214          * @return 出力プロパティを設定したPropertiesインスタンス\r
215          */\r
216         private Properties getOutputProperties() {\r
217                 Properties p = new Properties();\r
218                 p.put(OutputKeys.ENCODING, charset);\r
219                 p.put(OutputKeys.INDENT, "yes");\r
220                 p.put(OutputKeys.DOCTYPE_PUBLIC, Mail.DOCTYPE_PUBLIC);\r
221                 p.put(OutputKeys.DOCTYPE_SYSTEM, Mail.DOCTYPE_SYSTEM);\r
222                 return p;\r
223         }\r
224 \r
225 }