001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2007 Charles O Nutter <headius@headius.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby.util;
028:
029: import java.nio.charset.Charset;
030: import java.nio.charset.CharsetDecoder;
031: import java.nio.charset.CharsetEncoder;
032: import org.jruby.Ruby;
033: import org.jruby.runtime.builtin.IRubyObject;
034:
035: public class KCode {
036: public static final KCode NIL = new KCode(null);
037: public static final KCode NONE = new KCode("NONE");
038: public static final KCode UTF8 = new KCode("UTF8");
039: public static final KCode SJIS = new KCode("SJIS");
040: public static final KCode EUC = new KCode("EUC");
041:
042: private String kcode;
043:
044: private KCode(String kcode) {
045: this .kcode = kcode;
046: }
047:
048: public static KCode create(Ruby runtime, String lang) {
049: if (lang == null) {
050: return NIL;
051: }
052:
053: switch (lang.charAt(0)) {
054: case 'E':
055: case 'e':
056: runtime.getWarnings().warn(
057: "JRuby supports only Unicode regexp.");
058: return EUC;
059: case 'S':
060: case 's':
061: runtime.getWarnings().warn(
062: "JRuby supports only Unicode regexp.");
063: return SJIS;
064: case 'U':
065: case 'u':
066: return UTF8;
067: case 'N':
068: case 'n':
069: case 'A':
070: case 'a':
071: return NONE;
072: }
073: return NIL;
074: }
075:
076: public IRubyObject kcode(Ruby runtime) {
077: if (kcode == null) {
078: return runtime.getNil();
079: }
080: return runtime.newString(kcode);
081: }
082:
083: public CharsetDecoder decoder() {
084: if (this == UTF8) {
085: return Charset.forName("UTF-8").newDecoder();
086: }
087:
088: return Charset.forName("ISO-8859-1").newDecoder();
089: }
090:
091: public CharsetEncoder encoder() {
092: if (this == UTF8) {
093: return Charset.forName("UTF-8").newEncoder();
094: }
095:
096: return Charset.forName("ISO-8859-1").newEncoder();
097: }
098:
099: public int flags() {
100: return 0;
101: }
102: }
|