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