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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs.i18n;
030:
031: import com.caucho.vfs.OutputStreamWithBuffer;
032:
033: import java.io.IOException;
034:
035: /**
036: * Implements an encoding char-to-byte writer for Java source generation
037: * using the '\\uxxxx' escapes for non-ascii characters.
038: */
039: public class JAVAWriter extends EncodingWriter {
040: private static JAVAWriter _writer = new JAVAWriter();
041:
042: /**
043: * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
044: */
045: public JAVAWriter() {
046: }
047:
048: /**
049: * Returns the encoding.
050: */
051: public String getJavaEncoding() {
052: return "JAVA";
053: }
054:
055: /**
056: * Create a Java source-code writer using on the WriteStream to send bytes.
057: *
058: * @param os the write stream receiving the bytes.
059: * @param javaEncoding the JDK name for the encoding.
060: *
061: * @return the UTF-8 writer.
062: */
063: public EncodingWriter create(String javaEncoding) {
064: return _writer;
065: }
066:
067: /**
068: * Writes a character to the output stream with the correct encoding.
069: *
070: * @param ch the character to write.
071: */
072: public void write(OutputStreamWithBuffer os, char ch)
073: throws IOException {
074: if (ch < 0x80)
075: os.write(ch);
076: else {
077: os.write('\\');
078: os.write('u');
079:
080: int b = (ch >> 12) & 0xf;
081: os.write(b < 10 ? b + '0' : b + 'a' - 10);
082: b = (ch >> 8) & 0xf;
083: os.write(b < 10 ? b + '0' : b + 'a' - 10);
084: b = (ch >> 4) & 0xf;
085: os.write(b < 10 ? b + '0' : b + 'a' - 10);
086: b = ch & 0xf;
087: os.write(b < 10 ? b + '0' : b + 'a' - 10);
088: }
089: }
090:
091: /**
092: * Writes into a character buffer using the correct encoding.
093: *
094: * @param cbuf character array with the data to write.
095: * @param off starting offset into the character array.
096: * @param len the number of characters to write.
097: */
098: public void write(OutputStreamWithBuffer os, char[] cbuf, int off,
099: int len) throws IOException {
100: for (int i = 0; i < len; i++) {
101: char ch = cbuf[off + i];
102:
103: if (ch < 0x80)
104: os.write(ch);
105: else {
106: os.write('\\');
107: os.write('u');
108:
109: int b = (ch >> 12) & 0xf;
110: os.write(b < 10 ? b + '0' : b + 'a' - 10);
111: b = (ch >> 8) & 0xf;
112: os.write(b < 10 ? b + '0' : b + 'a' - 10);
113: b = (ch >> 4) & 0xf;
114: os.write(b < 10 ? b + '0' : b + 'a' - 10);
115: b = ch & 0xf;
116: os.write(b < 10 ? b + '0' : b + 'a' - 10);
117: }
118: }
119: }
120: }
|