001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs.i18n;
031:
032: import com.caucho.vfs.OutputStreamWithBuffer;
033:
034: import java.io.IOException;
035: import java.io.OutputStreamWriter;
036: import java.io.UnsupportedEncodingException;
037: import java.nio.charset.Charset;
038: import java.util.logging.Level;
039:
040: /**
041: * Factory for JDK-based encoding writers.
042: */
043: public class JDKWriter extends EncodingWriter {
044: private String _javaEncoding;
045:
046: /**
047: * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
048: */
049: public JDKWriter() {
050: }
051:
052: /**
053: * Returns the Java encoding for the writer.
054: */
055: public String getJavaEncoding() {
056: return _javaEncoding;
057: }
058:
059: /**
060: * Sets the Java encoding for the writer.
061: */
062: public void setJavaEncoding(String encoding) {
063: _javaEncoding = encoding;
064: }
065:
066: /**
067: * Create a JDK-based reader.
068: *
069: * @param javaEncoding the JDK name for the encoding.
070: *
071: * @return an EncodingWriter
072: */
073: public EncodingWriter create(String javaEncoding) {
074: try {
075: return new OutputStreamEncodingWriter(javaEncoding);
076: } catch (UnsupportedEncodingException e) {
077: log.log(Level.WARNING, e.toString(), e);
078:
079: return null;
080: }
081: }
082:
083: /**
084: * JDKWriter is only a factory.
085: */
086: public void write(OutputStreamWithBuffer os, char ch)
087: throws IOException {
088: throw new UnsupportedOperationException();
089: }
090:
091: static class OutputStreamEncodingWriter extends EncodingWriter {
092: private Charset _charset;
093: private String _encoding;
094: private OutputStreamWriter _writer;
095: private OutputStreamWithBuffer _os;
096:
097: OutputStreamEncodingWriter(String javaEncoding)
098: throws UnsupportedEncodingException {
099: try {
100: _encoding = javaEncoding;
101:
102: if (Charset.isSupported(javaEncoding))
103: _charset = Charset.forName(javaEncoding);
104: else {
105: // server/054i
106: throw new UnsupportedEncodingException(javaEncoding);
107: }
108: } catch (java.nio.charset.UnsupportedCharsetException e) {
109: throw new UnsupportedEncodingException(e.getMessage());
110: }
111: }
112:
113: /**
114: * Writes a char.
115: */
116: public void write(OutputStreamWithBuffer os, char ch)
117: throws IOException {
118: if (_os != os) {
119: if (_charset != null)
120: _writer = new OutputStreamWriter(os, _charset);
121: else
122: _writer = new OutputStreamWriter(os, _encoding);
123:
124: _os = os;
125: }
126:
127: _writer.write(ch);
128: _writer.flush();
129: }
130:
131: /**
132: * Writes a char buffer.
133: */
134: public void write(OutputStreamWithBuffer os, char[] buf,
135: int offset, int length) throws IOException {
136: if (_os != os) {
137: if (_charset != null)
138: _writer = new OutputStreamWriter(os, _charset);
139: else
140: _writer = new OutputStreamWriter(os, _encoding);
141:
142: _os = os;
143: }
144:
145: _writer.write(buf, offset, length);
146: _writer.flush();
147: }
148:
149: /**
150: * Creates the child.
151: */
152: public EncodingWriter create(String encoding) {
153: throw new UnsupportedOperationException();
154: }
155: }
156: }
|