1 jls.loader.provide('jls.io.cs.Charset');
  2 
  3 jls.loader.require('jls.io.cs.CharDecoder');
  4 jls.loader.require('jls.io.cs.CharEncoder');
  5 
  6 /**
  7  * @namespace Provides character sets.
  8  * @see jls.io.cs.Charset
  9  * @name jls.io.cs
 10  */
 11 
 12 /**
 13  * @class This class represents a character set and the associated codec.
 14  * The default charsets are Cp1252, Cp850, ISO-8859-1, UTF-8 and ASCII.
 15  */
 16 jls.io.cs.Charset = jls.lang.Class.create( /** @lends jls.io.cs.Charset.prototype */
 17 {
 18     /**
 19      * Creates a charset.
 20      * 
 21      * @private
 22      */
 23     initialize : function(name) {
 24         this._name = name;
 25     },
 26     /**
 27      * Returns the charset name.
 28      *
 29      * @returns {String} The charset name.
 30      */
 31     getName : function() {
 32         return this._name;
 33     },
 34     /**
 35      * Decodes byte buffer and returns the string.
 36      *
 37      * @param {jls.lang.Buffer} buf The buffer to decode.
 38      * @returns {String} The decoded string.
 39      */
 40     decode : function(buf) {
 41         return '';
 42     },
 43     /**
 44      * Encodes a string and returns a buffer.
 45      *
 46      * @param {String} str The string to encode.
 47      * @returns {jls.lang.Buffer} The buffer.
 48      */
 49     encode : function(str) {
 50         return null;
 51     },
 52     /**
 53      * Creates a new decoder for this charset.
 54      *
 55      * @returns {jls.io.cs.CharDecoder} The buffer.
 56      */
 57     newDecoder : function() {
 58         return new jls.io.cs.CharDecoder(this);
 59     },
 60     /**
 61      * Creates a new encoder for this charset.
 62      *
 63      * @returns {jls.io.cs.CharEncoder} The buffer.
 64      */
 65     newEncoder : function() {
 66         return new jls.io.cs.CharEncoder(this);
 67     }
 68 });
 69 
 70 
 71 Object.extend(jls.io.cs.Charset, /** @lends jls.io.cs.Charset */
 72 {
 73 	_defaultCharset : null,
 74 	_charsetMapping : {
 75 		'Cp1252' : 'jls.io.cs.Cp1252',
 76 		'Cp850' : 'jls.io.cs.Cp850',
 77 		'ISO-8859-1' : 'jls.io.cs.ISO_8859_1',
 78 		'UTF-8' : 'jls.io.cs.UTF_8',
 79 		'ASCII' : 'jls.io.cs.Charset'
 80 	},
 81 	_availableCharsets : {},
 82     /**
 83      * Returns available charsets.
 84      * 
 85      * @returns {Array} The available charsets.
 86      */
 87 	availableCharsets : function() {
 88         return Object.values(jls.io.cs.Charset._availableCharsets);
 89     },
 90 	addCharset : function(cs) {
 91 		jls.io.cs.Charset._availableCharsets[cs.getName()] = cs;
 92     },
 93     /**
 94      * Returns the default charset.
 95      * 
 96      * @returns {jls.io.cs.Charset} The default charset.
 97      */
 98     defaultCharset : function() {
 99     	if (jls.io.cs.Charset._defaultCharset == null) {
100             var csn = _native.core.properties['file.encoding'];
101     		if (csn && jls.io.cs.Charset.isSupported(csn)) {
102     			jls.io.cs.Charset._defaultCharset = jls.io.cs.Charset.forName(csn);
103     		} else {
104         		jls.io.cs.Charset._defaultCharset = jls.io.cs.Charset.forName('ASCII');
105     		}
106     	}
107     	return jls.io.cs.Charset._defaultCharset;
108     },
109     /**
110      * Tells if the specified charset is supported.
111      * 
112      * @param {String} name The charset name.
113      * @returns {Boolean} true if the specified charset is supported.
114      */
115     isSupported : function(name) {
116     	if (typeof name != 'string') {
117 			throw new jls.lang.Exception('Invalid charset name');
118     	}
119     	if (name in jls.io.cs.Charset._availableCharsets) {
120 			return true;
121     	}
122     	if (name in jls.io.cs.Charset._charsetMapping) {
123             jls.loader.require(jls.io.cs.Charset._charsetMapping[name]);
124 			return name in jls.io.cs.Charset._availableCharsets;
125     	}
126 		return false;
127     },
128     /**
129      * Returns the specified charset.
130      * 
131      * @param {String} name The charset name.
132      * @returns {jls.io.cs.Charset} The charset.
133      */
134 	forName : function(name) {
135 		if (! jls.io.cs.Charset.isSupported(name)) {
136 			throw new jls.lang.Exception('Unsupported charset "' + name + '"');
137 		}
138         return jls.io.cs.Charset._availableCharsets[name];
139     }
140 });
141 
142 // static
143 jls.io.cs.Charset.addCharset(new jls.io.cs.Charset('ASCII'));
144 
145 
146