OSDN Git Service

.gitignore を追加
[spring-ext/ozacc-mail.git] / src / java / com / ozacc / mail / fetch / impl / sk_jp / CorrectedContentTypeDataSource.java
1 /*\r
2  * @(#) $Id: CorrectedContentTypeDataSource.java,v 1.1.2.2 2004/10/24 10:27:40 otsuka Exp $\r
3  * $Revision: 1.1.2.2 $\r
4  * Copyright (c) 2000 Shin Kinoshita All Rights Reserved.\r
5  */\r
6 package com.ozacc.mail.fetch.impl.sk_jp;\r
7 \r
8 import java.io.IOException;\r
9 import java.io.InputStream;\r
10 import java.io.OutputStream;\r
11 import javax.activation.DataSource;\r
12 import javax.mail.MessageAware;\r
13 import javax.mail.MessageContext;\r
14 import javax.mail.MessagingException;\r
15 import javax.mail.Part;\r
16 import javax.mail.internet.ContentType;\r
17 import javax.mail.internet.ParseException;\r
18 \r
19 /**\r
20  * Content-Type:の不適合をISO-2022-JPに補正します。\r
21  * 使用方法は<PRE>\r
22  * Object o = new DataHandler(\r
23  *               new CorrectedContentTypeDataSource(part, charset)\r
24  *            ).getContent();\r
25  * </PRE><P>のようになります。</P><P>\r
26  * スレッドセーフではありませんので利用者側で排他制御を行ってください。\r
27  * </P>\r
28  * @author Shin\r
29  * @version $Revision: 1.1.2.2 $ $Date: 2004/10/24 10:27:40 $\r
30  */\r
31 class CorrectedContentTypeDataSource implements DataSource, MessageAware {\r
32 \r
33         protected DataSource source;\r
34 \r
35         protected String defaultCharset;\r
36 \r
37         protected String forceCharset;\r
38 \r
39         public CorrectedContentTypeDataSource() {}\r
40 \r
41         public CorrectedContentTypeDataSource(DataSource dataSource, String defaultCharset) {\r
42                 setDataSource(dataSource);\r
43                 setDefaultCharset(defaultCharset);\r
44         }\r
45 \r
46         public CorrectedContentTypeDataSource(Part part, String defaultCharset)\r
47                                                                                                                                                         throws MessagingException {\r
48                 setPart(part);\r
49                 setDefaultCharset(defaultCharset);\r
50         }\r
51 \r
52         public void setPart(Part part) throws MessagingException {\r
53                 // getDataHandler() method creates a implicit DataSource.\r
54                 setDataSource(part.getDataHandler().getDataSource());\r
55         }\r
56 \r
57         public void setDataSource(DataSource newSource) {\r
58                 source = newSource;\r
59         }\r
60 \r
61         public void setDefaultCharset(String defaultCharset) {\r
62                 this.defaultCharset = defaultCharset;\r
63         }\r
64 \r
65         /**\r
66          * 指定された文字コードで既存の文字コードを上書きします。\r
67          * \r
68          * @param forceCharset 強制的に適用する文字コード\r
69          * @author Tomohiro Otsuka\r
70          */\r
71         public void setForceCharset(String forceCharset) {\r
72                 this.forceCharset = forceCharset;\r
73         }\r
74 \r
75         public String getContentType() {\r
76                 ContentType contentType = null;\r
77                 try {\r
78                         contentType = new ContentType(source.getContentType());\r
79                 } catch (ParseException e) {\r
80                         return "text/plain; charset=" + defaultCharset;\r
81                 }\r
82                 String specifiedCharset = contentType.getParameter("charset");\r
83                 if (specifiedCharset == null) {\r
84                         // Content-Type:が存在しない場合は"text/plain"になってしまう。\r
85                         // 本当にtext/plainだった場合は正しくない事になるが、\r
86                         // charset=ISO-2022-JPにする場合は一応表示上は問題ない。\r
87                         contentType.setParameter("charset", defaultCharset);\r
88                 } else if (forceCharset != null) {\r
89                         contentType.setParameter("charset", forceCharset);\r
90                 }\r
91                 return contentType.toString();\r
92         }\r
93 \r
94         public String getName() {\r
95                 return source.getName();\r
96         }\r
97 \r
98         public InputStream getInputStream() throws IOException {\r
99                 return source.getInputStream();\r
100         }\r
101 \r
102         public OutputStream getOutputStream() throws IOException {\r
103                 return source.getOutputStream();\r
104         }\r
105 \r
106         public synchronized MessageContext getMessageContext() {\r
107                 if (source instanceof MessageAware) {\r
108                         return ((MessageAware)source).getMessageContext();\r
109                 }\r
110                 throw new RuntimeException(source + " isn't MessageAware.");\r
111         }\r
112 }