KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > handler > Text


1 /*
2  * Text.java
3  * Copyright (C) 2002 dog <dog@dog.net.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package gnu.mail.handler;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
24 import java.io.*;
25 import javax.activation.*;
26 import javax.mail.internet.ContentType JavaDoc;
27 import javax.mail.internet.MimeUtility JavaDoc;
28 import javax.mail.internet.ParseException JavaDoc;
29
30 /**
31  * A JAF data content handler for the text/* family of MIME content types.
32  * This provides the basic behaviour for any number of text-handling
33  * subtypes which simply need to override their default constructor to provide
34  * the correct MIME content-type and description.
35  */

36 public abstract class Text
37   implements DataContentHandler
38 {
39
40   /**
41    * Our favorite data flavor.
42    * Yum yum.
43    */

44   protected DataFlavor JavaDoc flavor;
45   
46   /**
47    * Constructor specifying the data flavor.
48    * @param mimeType the MIME content type
49    * @param description the description of the content type
50    */

51   protected Text(String JavaDoc mimeType, String JavaDoc description)
52   {
53     flavor = new ActivationDataFlavor(java.lang.String JavaDoc.class, mimeType,
54         description);
55   }
56
57   /**
58    * Returns an array of DataFlavor objects indicating the flavors the data
59    * can be provided in.
60    * @return the DataFlavors
61    */

62   public DataFlavor JavaDoc[] getTransferDataFlavors()
63   {
64     DataFlavor JavaDoc[] flavors = new DataFlavor JavaDoc[1];
65     flavors[0] = flavor;
66     return flavors;
67   }
68
69   /**
70    * Returns an object which represents the data to be transferred.
71    * The class of the object returned is defined by the representation class
72    * of the flavor.
73    * @param flavor the data flavor representing the requested type
74    * @param source the data source representing the data to be converted
75    * @return the constructed object
76    */

77   public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor, DataSource source)
78     throws UnsupportedFlavorException JavaDoc, IOException
79   {
80     if (this.flavor.equals(flavor))
81       return getContent(source);
82     return null;
83   }
84
85   /**
86    * Return an object representing the data in its most preferred form.
87    * Generally this will be the form described by the first data flavor
88    * returned by the <code>getTransferDataFlavors</code> method.
89    * @param source the data source representing the data to be converted
90    * @return the constructed object
91    */

92   public Object JavaDoc getContent(DataSource source)
93     throws IOException
94   {
95     InputStream in = source.getInputStream();
96     String JavaDoc mimeType = source.getContentType();
97     String JavaDoc charset = getJavaCharset(mimeType);
98     InputStreamReader reader = new InputStreamReader(in, charset);
99     char[] buf = new char[4096]; // TODO make configurable
100
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101     while (true)
102     {
103       int len = reader.read(buf);
104       if (len > -1)
105         buffer.append(new String JavaDoc(buf, 0, len));
106       else
107         break;
108     }
109     return buffer.toString();
110   }
111
112   /**
113    * Convert the object to a byte stream of the specified MIME type and
114    * write it to the output stream.
115    * @param object the object to be converted
116    * @param mimeType the requested MIME content type to write as
117    * @param out the output stream into which to write the converted object
118    */

119   public void writeTo(Object JavaDoc object, String JavaDoc mimeType, OutputStream out)
120     throws IOException
121   {
122     // We only handle strings or arrays of byte or char
123
String JavaDoc text = null;
124     if (object instanceof String JavaDoc)
125       text = (String JavaDoc)object;
126     else if (object instanceof byte[])
127       text = new String JavaDoc((byte[])object);
128     else if (object instanceof char[])
129       text = new String JavaDoc((char[])object);
130     else // Last attempt to get something useful
131
text = object.toString();
132     String JavaDoc charset = getJavaCharset(mimeType);
133     OutputStreamWriter writer = new OutputStreamWriter(out, charset);
134     writer.write(text);
135     writer.flush();
136   }
137
138   /**
139    * Returns the Java character set corresponding to the MIME charset
140    * parameter of the content type value.
141    * The default value is the Java character set mapped to the MIME charset
142    * "us-ascii".
143    * @return a java charset name
144    */

145   protected static String JavaDoc getJavaCharset(String JavaDoc contentType)
146   {
147     String JavaDoc charset = "us-ascii";
148     if (contentType!=null)
149     {
150       try
151       {
152         ContentType JavaDoc ct = new ContentType JavaDoc(contentType);
153         String JavaDoc ctCharset = ct.getParameter("charset");
154         if (ctCharset!=null)
155           charset = ctCharset;
156       }
157       catch (ParseException JavaDoc e)
158       {
159       }
160     }
161     return MimeUtility.javaCharset(charset);
162   }
163   
164 }
165
Popular Tags