KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > i18n > JDKWriter


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 SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs.i18n;
31
32 import com.caucho.vfs.OutputStreamWithBuffer;
33
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStreamWriter JavaDoc;
36 import java.io.UnsupportedEncodingException JavaDoc;
37 import java.nio.charset.Charset JavaDoc;
38 import java.util.logging.Level JavaDoc;
39
40 /**
41  * Factory for JDK-based encoding writers.
42  */

43 public class JDKWriter extends EncodingWriter {
44   private String JavaDoc _javaEncoding;
45   
46   /**
47    * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
48    */

49   public JDKWriter()
50   {
51   }
52   /**
53    * Returns the Java encoding for the writer.
54    */

55   public String JavaDoc getJavaEncoding()
56   {
57     return _javaEncoding;
58   }
59   
60   /**
61    * Sets the Java encoding for the writer.
62    */

63   public void setJavaEncoding(String JavaDoc encoding)
64   {
65     _javaEncoding = encoding;
66   }
67   
68
69   /**
70    * Create a JDK-based reader.
71    *
72    * @param javaEncoding the JDK name for the encoding.
73    *
74    * @return an EncodingWriter
75    */

76   public EncodingWriter create(String JavaDoc javaEncoding)
77   {
78     try {
79       return new OutputStreamEncodingWriter(javaEncoding);
80     } catch (UnsupportedEncodingException JavaDoc e) {
81       log.log(Level.WARNING, e.toString(), e);
82       
83       return null;
84     }
85   }
86
87   /**
88    * JDKWriter is only a factory.
89    */

90   public void write(OutputStreamWithBuffer os, char ch)
91     throws IOException JavaDoc
92   {
93     throw new UnsupportedOperationException JavaDoc();
94   }
95
96   static class OutputStreamEncodingWriter extends EncodingWriter {
97     private Charset JavaDoc _charset;
98     private String JavaDoc _encoding;
99     private OutputStreamWriter JavaDoc _writer;
100     private OutputStreamWithBuffer _os;
101
102     OutputStreamEncodingWriter(String JavaDoc javaEncoding)
103       throws UnsupportedEncodingException JavaDoc
104     {
105       try {
106     _encoding = javaEncoding;
107     
108     if (Charset.isSupported(javaEncoding))
109       _charset = Charset.forName(javaEncoding);
110     else {
111       // server/054i
112
throw new UnsupportedEncodingException JavaDoc(javaEncoding);
113     }
114       } catch (java.nio.charset.UnsupportedCharsetException JavaDoc e) {
115     throw new UnsupportedEncodingException JavaDoc(e.getMessage());
116       }
117     }
118
119     /**
120      * Writes a char.
121      */

122     public void write(OutputStreamWithBuffer os, char ch)
123       throws IOException JavaDoc
124     {
125       if (_os != os) {
126     if (_charset != null)
127       _writer = new OutputStreamWriter JavaDoc(os, _charset);
128     else
129       _writer = new OutputStreamWriter JavaDoc(os, _encoding);
130
131     _os = os;
132       }
133       
134       _writer.write(ch);
135       _writer.flush();
136     }
137
138     /**
139      * Writes a char buffer.
140      */

141     public void write(OutputStreamWithBuffer os,
142               char []buf, int offset, int length)
143       throws IOException JavaDoc
144     {
145       if (_os != os) {
146     if (_charset != null)
147       _writer = new OutputStreamWriter JavaDoc(os, _charset);
148     else
149       _writer = new OutputStreamWriter JavaDoc(os, _encoding);
150
151     _os = os;
152       }
153       
154       _writer.write(buf, offset, length);
155       _writer.flush();
156     }
157
158     /**
159      * Creates the child.
160      */

161     public EncodingWriter create(String JavaDoc encoding)
162     {
163       throw new UnsupportedOperationException JavaDoc();
164     }
165   }
166 }
167
Popular Tags