KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > i18n > IconvUtility


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.i18n;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.UnimplementedException;
34 import com.caucho.quercus.env.*;
35 import com.caucho.vfs.TempCharBuffer;
36 import com.caucho.vfs.TempStream;
37 import com.caucho.vfs.WriteStream;
38
39 import javax.mail.internet.MimeUtility JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.Reader JavaDoc;
42 import java.io.UnsupportedEncodingException JavaDoc;
43
44 public class IconvUtility {
45
46   public static BinaryValue decodeEncode(StringValue str,
47                               String JavaDoc inCharset,
48                               String JavaDoc outCharset)
49     throws UnsupportedEncodingException JavaDoc
50   {
51     return decodeEncode(str, inCharset, outCharset, 0, Integer.MAX_VALUE);
52   }
53
54   public static BinaryValue decodeEncode(StringValue str,
55                               String JavaDoc inCharset,
56                               String JavaDoc outCharset,
57                               int offset)
58     throws UnsupportedEncodingException JavaDoc
59   {
60     return decodeEncode(str, inCharset, outCharset, offset, Integer.MAX_VALUE);
61   }
62
63   /**
64    * Decodes and encodes to specified charsets at the same time.
65    */

66   public static BinaryValue decodeEncode(StringValue str,
67                               String JavaDoc inCharset,
68                               String JavaDoc outCharset,
69                               int offset,
70                               int length)
71     throws UnsupportedEncodingException JavaDoc
72   {
73     TempCharBuffer tb = TempCharBuffer.allocate();
74     char[] charBuf = tb.getBuffer();
75
76     try {
77       Reader JavaDoc in = str.toReader(inCharset);
78
79       TempStream ts = new TempStream();
80       WriteStream out = new WriteStream(ts);
81       out.setEncoding(outCharset);
82
83       while (offset > 0) {
84         if (in.read() < 0)
85           break;
86         offset--;
87       }
88
89       int sublen;
90
91       while (length > 0 &&
92              (sublen = in.read(charBuf, 0, charBuf.length)) >= 0) {
93
94         sublen = Math.min(length, sublen);
95
96         out.print(charBuf, 0, sublen);
97         length -= sublen;
98       }
99
100       out.flush();
101       return new TempBufferStringValue(ts.getHead());
102
103     } catch (IOException JavaDoc e) {
104       throw new QuercusModuleException(e.getMessage());
105     }
106
107     finally {
108       TempCharBuffer.free(tb);
109     }
110   }
111
112   /**
113    * Returns decoded Mime header/field.
114    */

115   public static BinaryValue decodeMime(Env env,
116                               CharSequence JavaDoc word,
117                               String JavaDoc charset)
118     throws UnsupportedEncodingException JavaDoc
119   {
120     StringValue str = new StringValueImpl(
121             MimeUtility.unfold(MimeUtility.decodeText(word.toString())));
122
123     return str.toBinaryValue(env, charset);
124   }
125
126   public static Value encodeMime(Env env,
127                               StringValue name,
128                               StringValue value,
129                               String JavaDoc inCharset,
130                               String JavaDoc outCharset,
131                               String JavaDoc scheme)
132     throws UnsupportedEncodingException JavaDoc
133   {
134     return encodeMime(env,
135                       name,
136                       value,
137                       inCharset,
138                       outCharset,
139                       scheme,
140                       "\r\n",
141                       76);
142   }
143
144   /**
145    * Encodes a MIME header.
146    *
147    * XXX: preferences
148    *
149    * @param field_name header field name
150    * @param field_value header field value
151    * @param preferences
152    */

153   /**
154    * Returns an encoded Mime header.
155    */

156   public static StringValue encodeMime(Env env,
157                               StringValue name,
158                               StringValue value,
159                               String JavaDoc inCharset,
160                               String JavaDoc outCharset,
161                               String JavaDoc scheme,
162                               String JavaDoc lineBreakChars,
163                               int lineLength)
164     throws UnsupportedEncodingException JavaDoc
165   {
166     name = name.toUnicodeValue(env, inCharset);
167     value = value.toUnicodeValue(env, inCharset);
168
169     StringBuilderValue sb = new StringBuilderValue();
170     sb.append(name);
171     sb.append(':');
172     sb.append(' ');
173
174     String JavaDoc word = encodeMimeWord(
175             value.toString(), outCharset, scheme, lineBreakChars, lineLength);
176
177     sb.append(MimeUtility.fold(sb.length(), word));
178
179     return sb;
180   }
181
182   public static String JavaDoc encodeMimeWord(String JavaDoc value,
183                               String JavaDoc charset,
184                               String JavaDoc scheme,
185                               String JavaDoc lineBreakChars,
186                               int lineLength)
187     throws UnsupportedEncodingException JavaDoc
188   {
189     if (lineLength != 76)
190       throw new UnimplementedException("Mime line length option");
191
192     if (! lineBreakChars.equals("\r\n"))
193       throw new UnimplementedException("Mime line break option");
194
195     return MimeUtility.encodeWord(value, charset, scheme);
196   }
197 }
198
Popular Tags