2014-09-10 02:18:11 +04:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-09-10 05:43:45 +04:00
|
|
|
var DataType = {
|
2014-10-14 13:18:38 +04:00
|
|
|
BOOLEAN: 0,
|
|
|
|
CHAR: 1,
|
2014-09-17 02:33:50 +04:00
|
|
|
BYTE: 2,
|
2014-10-14 13:18:38 +04:00
|
|
|
WCHAR: 3,
|
|
|
|
SHORT: 4,
|
2014-09-10 05:43:45 +04:00
|
|
|
USHORT: 5,
|
2014-10-14 13:18:38 +04:00
|
|
|
LONG: 6,
|
2014-09-10 05:43:45 +04:00
|
|
|
ULONG: 7,
|
2014-10-14 13:18:38 +04:00
|
|
|
FLOAT: 8,
|
|
|
|
DOUBLE: 9,
|
2014-09-10 05:43:45 +04:00
|
|
|
STRING: 10,
|
|
|
|
WSTRING: 11,
|
2014-10-14 13:18:38 +04:00
|
|
|
URI: 12,
|
2014-09-10 05:43:45 +04:00
|
|
|
METHOD: 13,
|
|
|
|
STRUCT: 14,
|
|
|
|
LIST: 15,
|
|
|
|
ARRAY: 16,
|
|
|
|
};
|
|
|
|
|
2014-09-10 02:18:11 +04:00
|
|
|
var DataEncoder = function() {
|
2014-09-10 04:03:21 +04:00
|
|
|
this.data = [];
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 04:07:56 +04:00
|
|
|
DataEncoder.START = 1;
|
|
|
|
DataEncoder.END = 2;
|
|
|
|
|
2014-09-10 02:18:11 +04:00
|
|
|
DataEncoder.prototype.putStart = function(tag, name) {
|
2014-09-10 04:03:21 +04:00
|
|
|
this.data.push({
|
2014-09-10 04:07:56 +04:00
|
|
|
type: DataEncoder.START,
|
2014-09-10 02:18:11 +04:00
|
|
|
tag: tag,
|
2014-09-10 04:03:21 +04:00
|
|
|
name: name,
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DataEncoder.prototype.putEnd = function(tag, name) {
|
2014-09-10 04:03:21 +04:00
|
|
|
this.data.push({
|
2014-09-10 04:07:56 +04:00
|
|
|
type: DataEncoder.END,
|
2014-09-10 04:03:21 +04:00
|
|
|
tag: tag,
|
|
|
|
name: name,
|
|
|
|
})
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 04:05:04 +04:00
|
|
|
DataEncoder.prototype.put = function(tag, name, value) {
|
2014-09-10 04:03:21 +04:00
|
|
|
this.data.push({
|
|
|
|
tag: tag,
|
2014-09-10 02:18:11 +04:00
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEncoder.prototype.getData = function() {
|
|
|
|
return JSON.stringify(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
var DataDecoder = function(data, offset, length) {
|
|
|
|
this.data = JSON.parse(util.decodeUtf8(new Uint8Array(data.buffer, offset, length)));
|
2014-09-17 05:02:18 +04:00
|
|
|
this.current = [];
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 04:03:21 +04:00
|
|
|
DataDecoder.prototype.find = function(tag, type) {
|
|
|
|
var elem;
|
2014-09-17 05:02:18 +04:00
|
|
|
var i = 0;
|
|
|
|
while (elem = this.data[i++]) {
|
2014-09-10 04:03:21 +04:00
|
|
|
if ((!type || elem.type == type) && elem.tag == tag) {
|
2014-09-17 05:02:18 +04:00
|
|
|
this.data = this.data.slice(i);
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elem.type == DataEncoder.END) {
|
|
|
|
break;
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 04:03:21 +04:00
|
|
|
DataDecoder.prototype.getStart = function(tag) {
|
2014-09-17 05:02:18 +04:00
|
|
|
var elem = this.find(tag, DataEncoder.START);
|
|
|
|
if (!elem) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.current.push(elem);
|
|
|
|
|
|
|
|
return true;
|
2014-09-10 04:03:21 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 02:18:11 +04:00
|
|
|
DataDecoder.prototype.getEnd = function(tag) {
|
2014-09-17 05:02:18 +04:00
|
|
|
var elem = this.find(tag, DataEncoder.END);
|
|
|
|
if (!elem) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this happens, a father has ended before a child
|
|
|
|
if (elem.tag != this.current[this.current.length - 1].tag ||
|
|
|
|
elem.name != this.current[this.current.length - 1].name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.current.pop();
|
|
|
|
|
|
|
|
return true;
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DataDecoder.prototype.getValue = function(tag) {
|
2014-09-17 05:02:18 +04:00
|
|
|
var elem = this.find(tag);
|
|
|
|
return elem ? elem.value : undefined;
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
2014-09-12 03:31:33 +04:00
|
|
|
DataDecoder.prototype.getNextValue = function() {
|
|
|
|
var elem = this.data.shift();
|
2014-09-17 05:02:18 +04:00
|
|
|
return elem ? elem.value : undefined;
|
2014-09-12 03:31:33 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 02:18:11 +04:00
|
|
|
DataDecoder.prototype.getName = function() {
|
2014-09-10 04:03:21 +04:00
|
|
|
return this.data[0].name;
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DataDecoder.prototype.getTag = function() {
|
2014-09-10 04:03:21 +04:00
|
|
|
return this.data[0].tag;
|
2014-09-10 02:18:11 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 02:33:50 +04:00
|
|
|
DataDecoder.prototype.getType = function() {
|
|
|
|
return this.data[0].type || -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.init.()V", function() {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.encoder = new DataEncoder();
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.putStart.(ILjava/lang/String;)V", function(tag, name) {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.encoder.putStart(tag, util.fromJavaString(name));
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.put.(ILjava/lang/String;Ljava/lang/String;)V", function(tag, name, value) {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.encoder.put(tag, util.fromJavaString(name), util.fromJavaString(value));
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.put.(ILjava/lang/String;J)V", function(tag, name, value, _) {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.encoder.put(tag, util.fromJavaString(name), value.toNumber());
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.put.(ILjava/lang/String;Z)V", function(tag, name, value) {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.encoder.put(tag, util.fromJavaString(name), value);
|
|
|
|
});
|
2014-09-12 03:21:39 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.putEnd.(ILjava/lang/String;)V", function(tag, name) {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.encoder.putEnd(tag, util.fromJavaString(name));
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 15:13:54 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataEncoder.getData.()[B", function() {
|
2014-10-15 00:29:45 +04:00
|
|
|
var data = this.encoder.getData();
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 15:13:54 +03:00
|
|
|
var array = util.newPrimitiveArray("B", data.length);
|
2014-09-10 04:07:56 +04:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
array[i] = data.charCodeAt(i);
|
|
|
|
}
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-15 00:29:45 +04:00
|
|
|
return array;
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.init.([BII)V", function(data, offset, length) {
|
2014-10-15 00:29:45 +04:00
|
|
|
this.decoder = new DataDecoder(data, offset, length);
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getStart.(I)V", function(tag) {
|
2014-10-15 00:29:45 +04:00
|
|
|
if (!this.decoder.getStart(tag)) {
|
|
|
|
throw new JavaException("java/io/IOException", "no start found " + tag);
|
2014-09-17 05:02:18 +04:00
|
|
|
}
|
2014-10-15 00:29:45 +04:00
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getEnd.(I)V", function(tag) {
|
2014-10-15 00:29:45 +04:00
|
|
|
if (!this.decoder.getEnd(tag)) {
|
|
|
|
throw new JavaException("java/io/IOException", "no end found " + tag);
|
2014-09-17 05:02:18 +04:00
|
|
|
}
|
2014-10-15 00:29:45 +04:00
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getString.(I)Ljava/lang/String;", function(tag) {
|
2014-10-15 00:29:45 +04:00
|
|
|
var str = this.decoder.getValue(tag);
|
2014-09-10 20:00:35 +04:00
|
|
|
if (str === undefined) {
|
2014-10-15 00:29:45 +04:00
|
|
|
throw new JavaException("java/io/IOException", "tag (" + tag + ") invalid");
|
2014-09-10 20:00:35 +04:00
|
|
|
}
|
2014-10-15 00:29:45 +04:00
|
|
|
return str;
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getInteger.(I)J", function(tag) {
|
2014-10-15 00:29:45 +04:00
|
|
|
var num = this.decoder.getValue(tag);
|
2014-09-10 20:00:35 +04:00
|
|
|
if (num === undefined) {
|
2014-10-15 00:29:45 +04:00
|
|
|
throw new JavaException("java/io/IOException", "tag (" + tag + ") invalid");
|
2014-09-10 20:00:35 +04:00
|
|
|
}
|
2014-10-15 00:29:45 +04:00
|
|
|
return Long.fromNumber(num);
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getBoolean.()Z", function() {
|
2014-10-15 00:29:45 +04:00
|
|
|
var val = this.decoder.getNextValue();
|
2014-09-12 03:31:33 +04:00
|
|
|
if (val === undefined) {
|
2014-10-15 00:29:45 +04:00
|
|
|
throw new JavaException("java/io/IOException");
|
2014-09-12 03:31:33 +04:00
|
|
|
}
|
2014-10-15 02:53:04 +04:00
|
|
|
return val === 1;
|
2014-10-15 00:29:45 +04:00
|
|
|
});
|
2014-09-12 03:31:33 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getName.()Ljava/lang/String;", function() {
|
2014-10-15 00:29:45 +04:00
|
|
|
var name = this.decoder.getName();
|
2014-09-10 20:00:35 +04:00
|
|
|
if (name === undefined) {
|
2014-10-15 00:29:45 +04:00
|
|
|
throw new JavaException("java/io/IOException");
|
2014-09-10 20:00:35 +04:00
|
|
|
}
|
2014-10-15 00:29:45 +04:00
|
|
|
return name;
|
|
|
|
});
|
2014-09-10 02:18:11 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.getType.()I", function() {
|
2014-10-15 00:29:45 +04:00
|
|
|
var tag = this.decoder.getTag();
|
2014-09-10 20:00:35 +04:00
|
|
|
if (tag === undefined) {
|
2014-10-15 00:29:45 +04:00
|
|
|
throw new JavaException("java/io/IOException");
|
2014-09-10 20:00:35 +04:00
|
|
|
}
|
2014-10-15 00:29:45 +04:00
|
|
|
return tag;
|
|
|
|
});
|
2014-09-17 02:33:50 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/nokia/mid/s40/codec/DataDecoder.listHasMoreItems.()Z", function() {
|
2014-10-15 00:29:45 +04:00
|
|
|
return this.decoder.getType() != DataEncoder.END;
|
|
|
|
});
|