Created
February 17, 2014 10:53
-
-
Save deoxxa/9048523 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- jsonix-original.js 2014-02-17 21:50:01.000000000 +1100 | |
+++ jsonix-runtime.js.x 2014-02-17 21:36:23.000000000 +1100 | |
@@ -1,10 +1,4 @@ | |
-var Jsonix = { | |
- singleFile: true | |
-}; | |
- | |
-if (typeof require === "function") { | |
- module.exports.Jsonix = Jsonix; | |
-} | |
+var Jsonix = exports.Jsonix = {}; | |
Jsonix.Util = {}; | |
@@ -66,7 +60,7 @@ | |
} else if (typeof ActiveXObject !== "undefined") { | |
return new ActiveXObject("MSXML2.DOMDocument"); | |
} else { | |
- throw "Error created the DOM document."; | |
+ throw new Error("Error created the DOM document."); | |
} | |
}, | |
serialize: function(node) { | |
@@ -78,7 +72,7 @@ | |
} else if (Jsonix.Util.Type.exists(node.xml)) { | |
return node.xml; | |
} else { | |
- throw "Could not serialize the node, neither XMLSerializer nor the [xml] property were found."; | |
+ throw new Error("Could not serialize the node, neither XMLSerializer nor the [xml] property were found."); | |
} | |
}, | |
parse: function(text) { | |
@@ -111,11 +105,11 @@ | |
} else if (Jsonix.Util.Type.isString(transport.responseText)) { | |
result = Jsonix.DOM.parse(transport.responseText); | |
} else { | |
- throw "Response does not have valid [responseXML] or [responseText]."; | |
+ throw new Error("Response does not have valid [responseXML] or [responseText]."); | |
} | |
callback(result); | |
}, function(transport) { | |
- throw "Could not retrieve XML from URL [" + url + "]."; | |
+ throw new Error("Could not retrieve XML from URL [" + url + "]."); | |
}, options); | |
} | |
}; | |
@@ -220,7 +214,7 @@ | |
} | |
} catch (e) {} | |
} | |
- throw "Could not create XML HTTP transport."; | |
+ throw new Error("Could not create XML HTTP transport."); | |
}, | |
CLASS_NAME: "Jsonix.Request" | |
}); | |
@@ -515,54 +509,54 @@ | |
Jsonix.Util.Ensure = { | |
ensureBoolean: function(value) { | |
if (!Jsonix.Util.Type.isBoolean(value)) { | |
- throw "Argument [" + value + "] must be a boolean."; | |
+ throw new Error("Argument [" + value + "] must be a boolean."); | |
} | |
}, | |
ensureString: function(value) { | |
if (!Jsonix.Util.Type.isString(value)) { | |
- throw "Argument [" + value + "] must be a string."; | |
+ throw new Error("Argument [" + value + "] must be a string."); | |
} | |
}, | |
ensureNumber: function(value) { | |
if (!Jsonix.Util.Type.isNumber(value)) { | |
- throw "Argument [" + value + "] must be a number."; | |
+ throw new Error("Argument [" + value + "] must be a number."); | |
} | |
}, | |
ensureNumberOrNaN: function(value) { | |
if (!Jsonix.Util.Type.isNumberOrNaN(value)) { | |
- throw "Argument [" + value + "] must be a number or NaN."; | |
+ throw new Error("Argument [" + value + "] must be a number or NaN."); | |
} | |
}, | |
ensureInteger: function(value) { | |
if (!Jsonix.Util.Type.isNumber(value)) { | |
- throw "Argument must be an integer, but it is not a number."; | |
+ throw new Error("Argument must be an integer, but it is not a number."); | |
} else if (!Jsonix.Util.NumberUtils.isInteger(value)) { | |
- throw "Argument [" + value + "] must be an integer."; | |
+ throw new Error("Argument [" + value + "] must be an integer."); | |
} | |
}, | |
ensureDate: function(value) { | |
if (!(value instanceof Date)) { | |
- throw "Argument [" + value + "] must be a date."; | |
+ throw new Error("Argument [" + value + "] must be a date."); | |
} | |
}, | |
ensureObject: function(value) { | |
if (!Jsonix.Util.Type.isObject(value)) { | |
- throw "Argument [" + value + "] must be an object."; | |
+ throw new Error("Argument [" + value + "] must be an object."); | |
} | |
}, | |
ensureArray: function(value) { | |
if (!Jsonix.Util.Type.isArray(value)) { | |
- throw "Argument [" + value + "] must be an array."; | |
+ throw new Error("Argument [" + value + "] must be an array."); | |
} | |
}, | |
ensureFunction: function(value) { | |
if (!Jsonix.Util.Type.isFunction(value)) { | |
- throw "Argument [" + value + "] must be a function."; | |
+ throw new Error("Argument [" + value + "] must be a function."); | |
} | |
}, | |
ensureExists: function(value) { | |
if (!Jsonix.Util.Type.exists(value)) { | |
- throw "Argument [" + value + "] does not exist."; | |
+ throw new Error("Argument [" + value + "] does not exist."); | |
} | |
} | |
}; | |
@@ -689,7 +683,7 @@ | |
if (data.year >= -9999 && data.year <= 9999) { | |
this.year = data.year; | |
} else { | |
- throw "Invalid year [" + data.year + "]."; | |
+ throw new Error("Invalid year [" + data.year + "]."); | |
} | |
} else { | |
this.year = NaN; | |
@@ -699,7 +693,7 @@ | |
if (data.month >= 1 && data.month <= 12) { | |
this.month = data.month; | |
} else { | |
- throw "Invalid month [" + data.month + "]."; | |
+ throw new Error("Invalid month [" + data.month + "]."); | |
} | |
} else { | |
this.month = NaN; | |
@@ -709,7 +703,7 @@ | |
if (data.day >= 1 && data.day <= 31) { | |
this.day = data.day; | |
} else { | |
- throw "Invalid day [" + data.day + "]."; | |
+ throw new Error("Invalid day [" + data.day + "]."); | |
} | |
} else { | |
this.day = NaN; | |
@@ -719,7 +713,7 @@ | |
if (data.hour >= 0 && data.hour <= 23) { | |
this.hour = data.hour; | |
} else { | |
- throw "Invalid hour [" + data.hour + "]."; | |
+ throw new Error("Invalid hour [" + data.hour + "]."); | |
} | |
} else { | |
this.hour = NaN; | |
@@ -729,7 +723,7 @@ | |
if (data.minute >= 0 && data.minute <= 59) { | |
this.minute = data.minute; | |
} else { | |
- throw "Invalid minute [" + data.minute + "]."; | |
+ throw new Error("Invalid minute [" + data.minute + "]."); | |
} | |
} else { | |
this.minute = NaN; | |
@@ -739,7 +733,7 @@ | |
if (data.second >= 0 && data.second <= 59) { | |
this.second = data.second; | |
} else { | |
- throw "Invalid second [" + data.second + "]."; | |
+ throw new Error("Invalid second [" + data.second + "]."); | |
} | |
} else { | |
this.second = NaN; | |
@@ -749,7 +743,7 @@ | |
if (data.fractionalSecond >= 0 && data.fractionalSecond < 1) { | |
this.fractionalSecond = data.fractionalSecond; | |
} else { | |
- throw "Invalid fractional second [" + data.fractionalSecond + "]."; | |
+ throw new Error("Invalid fractional second [" + data.fractionalSecond + "]."); | |
} | |
} else { | |
this.fractionalSecond = NaN; | |
@@ -762,7 +756,7 @@ | |
if (data.timezone >= -1440 && data.timezone < 1440) { | |
this.timezone = data.timezone; | |
} else { | |
- throw "Invalid timezone [" + data.timezone + "]."; | |
+ throw new Error("Invalid timezone [" + data.timezone + "]."); | |
} | |
} | |
} else { | |
@@ -877,13 +871,13 @@ | |
this.eventType = 14; | |
return this.eventType; | |
} else { | |
- throw "Node type [" + nodeType + "] is not supported."; | |
+ throw new Error("Node type [" + nodeType + "] is not supported."); | |
} | |
}, | |
leave: function(node) { | |
if (node.nodeType === 9) { | |
if (this.eventType == 8) { | |
- throw "Invalid state."; | |
+ throw new Error("Invalid state."); | |
} else { | |
this.node = node; | |
this.attributes = null; | |
@@ -947,13 +941,13 @@ | |
et = this.next(); | |
} | |
if (et !== 1 && et !== 2) { | |
- throw "Expected start or end tag."; | |
+ throw new Error("Expected start or end tag."); | |
} | |
return et; | |
}, | |
getElementText: function() { | |
if (this.eventType != 1) { | |
- throw "Parser must be on START_ELEMENT to read next text."; | |
+ throw new Error("Parser must be on START_ELEMENT to read next text."); | |
} | |
var et = this.next(); | |
var content = ""; | |
@@ -961,11 +955,11 @@ | |
if (et === 4 || et === 12 || et === 6 || et === 9) { | |
content = content + this.getText(); | |
} else if (et === 3 || et === 5) {} else if (et === 8) { | |
- throw "Unexpected end of document when reading element text content."; | |
+ throw new Error("Unexpected end of document when reading element text content."); | |
} else if (et === 1) { | |
- throw "Element text content may not contain START_ELEMENT."; | |
+ throw new Error("Element text content may not contain START_ELEMENT."); | |
} else { | |
- throw "Unexpected event type [" + et + "]."; | |
+ throw new Error("Unexpected event type [" + et + "]."); | |
} | |
et = this.next(); | |
} | |
@@ -982,7 +976,7 @@ | |
attributes = this.node.parentNode.attributes; | |
this.attributes = attributes; | |
} else { | |
- throw "Number of attributes can only be retrieved for START_ELEMENT or ATTRIBUTE."; | |
+ throw new Error("Number of attributes can only be retrieved for START_ELEMENT or ATTRIBUTE."); | |
} | |
return attributes.length; | |
}, | |
@@ -997,10 +991,10 @@ | |
attributes = this.node.parentNode.attributes; | |
this.attributes = attributes; | |
} else { | |
- throw "Attribute name can only be retrieved for START_ELEMENT or ATTRIBUTE."; | |
+ throw new Error("Attribute name can only be retrieved for START_ELEMENT or ATTRIBUTE."); | |
} | |
if (index < 0 || index >= attributes.length) { | |
- throw "Invalid attribute index [" + index + "]."; | |
+ throw new Error("Invalid attribute index [" + index + "]."); | |
} | |
var attribute = attributes[index]; | |
if (Jsonix.Util.Type.isString(attribute.namespaceURI)) { | |
@@ -1020,10 +1014,10 @@ | |
attributes = this.node.parentNode.attributes; | |
this.attributes = attributes; | |
} else { | |
- throw "Attribute name key can only be retrieved for START_ELEMENT or ATTRIBUTE."; | |
+ throw new Error("Attribute name key can only be retrieved for START_ELEMENT or ATTRIBUTE."); | |
} | |
if (index < 0 || index >= attributes.length) { | |
- throw "Invalid attribute index [" + index + "]."; | |
+ throw new Error("Invalid attribute index [" + index + "]."); | |
} | |
var attribute = attributes[index]; | |
return Jsonix.XML.QName.key(attribute.namespaceURI, attribute.nodeName); | |
@@ -1039,10 +1033,10 @@ | |
attributes = this.node.parentNode.attributes; | |
this.attributes = attributes; | |
} else { | |
- throw "Attribute value can only be retrieved for START_ELEMENT or ATTRIBUTE."; | |
+ throw new Error("Attribute value can only be retrieved for START_ELEMENT or ATTRIBUTE."); | |
} | |
if (index < 0 || index >= attributes.length) { | |
- throw "Invalid attribute index [" + index + "]."; | |
+ throw new Error("Invalid attribute index [" + index + "]."); | |
} | |
var attribute = attributes[index]; | |
return attribute.nodeValue; | |
@@ -1052,7 +1046,7 @@ | |
this.eventType = 2; | |
return this.node; | |
} else { | |
- throw "Parser must be on START_ELEMENT or END_ELEMENT to return current element."; | |
+ throw new Error("Parser must be on START_ELEMENT or END_ELEMENT to return current element."); | |
} | |
}, | |
CLASS_NAME: "Jsonix.XML.Input" | |
@@ -1139,7 +1133,7 @@ | |
} else if (this.xmldom) { | |
element = this.xmldom.createNode(1, qualifiedName, namespaceURI); | |
} else { | |
- throw "Could not create an element node."; | |
+ throw new Error("Could not create an element node."); | |
} | |
this.peek().appendChild(element); | |
return this.push(element); | |
@@ -1154,7 +1148,7 @@ | |
} else if (this.xmldom) { | |
node = this.xmldom.createTextNode(text); | |
} else { | |
- throw "Could not create an text node."; | |
+ throw new Error("Could not create an text node."); | |
} | |
this.peek().appendChild(node); | |
return node; | |
@@ -1179,7 +1173,7 @@ | |
attribute.nodeValue = value; | |
node.setAttributeNode(attribute); | |
} else { | |
- throw "setAttributeNS not implemented"; | |
+ throw new Error("setAttributeNS not implemented"); | |
} | |
} | |
} | |
@@ -1242,7 +1236,7 @@ | |
Jsonix.Model.TypeInfo.prototype.initialize.apply(this, []); | |
}, | |
isInstance: function(value) { | |
- throw "Abstract method [isInstance]."; | |
+ throw new Error("Abstract method [isInstance]."); | |
}, | |
CLASS_NAME: "Jsonix.Schema.XSD.AnyType" | |
}); | |
@@ -1449,7 +1443,7 @@ | |
} else if (text === "false" || text === "0") { | |
return false; | |
} else { | |
- throw "Either [true], [1], [0] or [false] expected as boolean value."; | |
+ throw new Error("Either [true], [1], [0] or [false] expected as boolean value."); | |
} | |
}, | |
isInstance: function(value) { | |
@@ -1465,100 +1459,24 @@ | |
Jsonix.Schema.XSD.Base64Binary = Jsonix.Class(Jsonix.Schema.XSD.AnySimpleType, { | |
name: "Base64Binary", | |
typeName: Jsonix.Schema.XSD.qname("base64Binary"), | |
- charToByte: {}, | |
- byteToChar: [], | |
initialize: function() { | |
Jsonix.Schema.XSD.AnySimpleType.prototype.initialize.apply(this); | |
- var charTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | |
- for (var i = 0; i < charTable.length; i++) { | |
- var _char = charTable.charAt(i); | |
- var _byte = charTable.charCodeAt(i); | |
- this.byteToChar[i] = _char; | |
- this.charToByte[_char] = i; | |
- } | |
}, | |
print: function(value) { | |
- Jsonix.Util.Ensure.ensureArray(value); | |
return this.encode(value); | |
}, | |
parse: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
return this.decode(text); | |
}, | |
- encode: function(uarray) { | |
- var output = ""; | |
- var byte0; | |
- var byte1; | |
- var byte2; | |
- var char0; | |
- var char1; | |
- var char2; | |
- var char3; | |
- var i = 0; | |
- var j = 0; | |
- var length = uarray.length; | |
- for (i = 0; i < length; i += 3) { | |
- byte0 = uarray[i] & 255; | |
- char0 = this.byteToChar[byte0 >> 2]; | |
- if (i + 1 < length) { | |
- byte1 = uarray[i + 1] & 255; | |
- char1 = this.byteToChar[(byte0 & 3) << 4 | byte1 >> 4]; | |
- if (i + 2 < length) { | |
- byte2 = uarray[i + 2] & 255; | |
- char2 = this.byteToChar[(byte1 & 15) << 2 | byte2 >> 6]; | |
- char3 = this.byteToChar[byte2 & 63]; | |
- } else { | |
- char2 = this.byteToChar[(byte1 & 15) << 2]; | |
- char3 = "="; | |
- } | |
- } else { | |
- char1 = this.byteToChar[(byte0 & 3) << 4]; | |
- char2 = "="; | |
- char3 = "="; | |
- } | |
- output = output + char0 + char1 + char2 + char3; | |
- } | |
- return output; | |
+ encode: function(value) { | |
+ return value.toString("base64"); | |
}, | |
decode: function(text) { | |
- input = text.replace(/[^A-Za-z0-9\+\/\=]/g, ""); | |
- var length = input.length / 4 * 3; | |
- if (input.charAt(input.length - 1) === "=") { | |
- length--; | |
- } | |
- if (input.charAt(input.length - 2) === "=") { | |
- length--; | |
- } | |
- var uarray = new Array(length); | |
- var byte0; | |
- var byte1; | |
- var byte2; | |
- var char0; | |
- var char1; | |
- var char2; | |
- var char3; | |
- var i = 0; | |
- var j = 0; | |
- for (i = 0; i < length; i += 3) { | |
- char0 = this.charToByte[input.charAt(j++)]; | |
- char1 = this.charToByte[input.charAt(j++)]; | |
- char2 = this.charToByte[input.charAt(j++)]; | |
- char3 = this.charToByte[input.charAt(j++)]; | |
- byte0 = char0 << 2 | char1 >> 4; | |
- byte1 = (char1 & 15) << 4 | char2 >> 2; | |
- byte2 = (char2 & 3) << 6 | char3; | |
- uarray[i] = byte0; | |
- if (char2 != 64) { | |
- uarray[i + 1] = byte1; | |
- } | |
- if (char3 != 64) { | |
- uarray[i + 2] = byte2; | |
- } | |
- } | |
- return uarray; | |
+ return new Buffer(text, "base64"); | |
}, | |
isInstance: function(value) { | |
- return Jsonix.Util.Type.isArray(value); | |
+ return Buffer.isBuffer(value); | |
}, | |
CLASS_NAME: "Jsonix.Schema.XSD.Base64Binary" | |
}); | |
@@ -1570,48 +1488,21 @@ | |
Jsonix.Schema.XSD.HexBinary = Jsonix.Class(Jsonix.Schema.XSD.AnySimpleType, { | |
name: "HexBinary", | |
typeName: Jsonix.Schema.XSD.qname("hexBinary"), | |
- charToQuartet: {}, | |
- byteToDuplet: [], | |
initialize: function() { | |
Jsonix.Schema.XSD.AnySimpleType.prototype.initialize.apply(this); | |
- var charTableUpperCase = "0123456789ABCDEF"; | |
- var charTableLowerCase = charTableUpperCase.toLowerCase(); | |
- var i; | |
- for (i = 0; i < 16; i++) { | |
- this.charToQuartet[charTableUpperCase.charAt(i)] = i; | |
- if (i >= 10) { | |
- this.charToQuartet[charTableLowerCase.charAt(i)] = i; | |
- } | |
- } | |
- for (i = 0; i < 256; i++) { | |
- this.byteToDuplet[i] = charTableUpperCase[i >> 4] + charTableUpperCase[i & 15]; | |
- } | |
}, | |
print: function(value) { | |
- Jsonix.Util.Ensure.ensureArray(value); | |
return this.encode(value); | |
}, | |
parse: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
return this.decode(text); | |
}, | |
- encode: function(uarray) { | |
- var output = ""; | |
- for (var i = 0; i < uarray.length; i++) { | |
- output = output + this.byteToDuplet[uarray[i] & 255]; | |
- } | |
- return output; | |
+ encode: function(value) { | |
+ return value.toString("hex"); | |
}, | |
decode: function(text) { | |
- var input = text.replace(/[^A-Fa-f0-9]/g, ""); | |
- var length = input.length >> 1; | |
- var uarray = new Array(length); | |
- for (var i = 0; i < length; i++) { | |
- var char0 = input.charAt(2 * i); | |
- var char1 = input.charAt(2 * i + 1); | |
- uarray[i] = this.charToQuartet[char0] << 4 | this.charToQuartet[char1]; | |
- } | |
- return uarray; | |
+ return new Buffer(text, "hex"); | |
}, | |
isInstance: function(value) { | |
return Jsonix.Util.Type.isArray(value); | |
@@ -1934,7 +1825,7 @@ | |
} else if (data.length >= 8 && data.charAt(2) === ":" && data.charAt(5) === ":") { | |
return this.parseTime(text); | |
} else { | |
- throw "Value [" + text + "] does not match dateTime, date or time patterns."; | |
+ throw new Error("Value [" + text + "] does not match dateTime, date or time patterns."); | |
} | |
}, | |
parseDateTime: function(text) { | |
@@ -1943,7 +1834,7 @@ | |
var sign = negative ? -1 : 1; | |
var dateTimeWithTimeZone = negative ? text.substring(1) : text; | |
if (dateTimeWithTimeZone.length < 19 || dateTimeWithTimeZone.charAt(4) !== "-" || dateTimeWithTimeZone.charAt(7) !== "-" || dateTimeWithTimeZone.charAt(10) !== "T" || dateTimeWithTimeZone.charAt(13) !== ":" || dateTimeWithTimeZone.charAt(16) !== ":") { | |
- throw "Date time string [" + dateTimeWithTimeZone + "] must be a string in format ['-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?]."; | |
+ throw new Error("Date time string [" + dateTimeWithTimeZone + "] must be a string in format ['-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?]."); | |
} | |
var timeZoneIndex; | |
var plusIndex = dateTimeWithTimeZone.indexOf("+", 19); | |
@@ -2049,10 +1940,10 @@ | |
parseDateString: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 10) { | |
- throw "Date string [" + text + "] must be 10 characters long."; | |
+ throw new Error("Date string [" + text + "] must be 10 characters long."); | |
} | |
if (text.charAt(4) !== "-" || text.charAt(7) !== "-") { | |
- throw "Date string [" + text + "] must be a string in format [yyyy '-' mm '-' ss ]."; | |
+ throw new Error("Date string [" + text + "] must be a string in format [yyyy '-' mm '-' ss ]."); | |
} | |
var year = this.parseYear(text.substring(0, 4)); | |
var month = this.parseMonth(text.substring(5, 7)); | |
@@ -2066,7 +1957,7 @@ | |
parseTimeString: function(timeString) { | |
Jsonix.Util.Ensure.ensureString(timeString); | |
if (timeString.length < 8 || timeString.charAt(2) !== ":" || timeString.charAt(5) !== ":") { | |
- throw "Time string [" + timeString + "] must be a string in format [hh ':' mm ':' ss ('.' s+)?]."; | |
+ throw new Error("Time string [" + timeString + "] must be a string in format [hh ':' mm ':' ss ('.' s+)?]."); | |
} | |
var hourString = timeString.substring(0, 2); | |
var minuteString = timeString.substring(3, 5); | |
@@ -2091,7 +1982,7 @@ | |
return 0; | |
} else { | |
if (text.length !== 6) { | |
- throw "Time zone must be an empty string, 'Z' or a string in format [('+' | '-') hh ':' mm]."; | |
+ throw new Error("Time zone must be an empty string, 'Z' or a string in format [('+' | '-') hh ':' mm]."); | |
} | |
var signString = text.charAt(0); | |
var sign; | |
@@ -2100,7 +1991,7 @@ | |
} else if (signString === "-") { | |
sign = -1; | |
} else { | |
- throw "First character of the time zone [" + text + "] must be '+' or '-'."; | |
+ throw new Error("First character of the time zone [" + text + "] must be '+' or '-'."); | |
} | |
var hour = this.parseHour(text.substring(1, 3)); | |
var minute = this.parseMinute(text.substring(4, 6)); | |
@@ -2110,7 +2001,7 @@ | |
parseYear: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 4) { | |
- throw "Year [" + text + "] must be a four-digit number."; | |
+ throw new Error("Year [" + text + "] must be a four-digit number."); | |
} | |
var year = Number(text); | |
Jsonix.Util.Ensure.ensureInteger(year); | |
@@ -2119,7 +2010,7 @@ | |
parseMonth: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 2) { | |
- throw "Month [" + text + "] must be a two-digit number."; | |
+ throw new Error("Month [" + text + "] must be a two-digit number."); | |
} | |
var month = Number(text); | |
Jsonix.Util.Ensure.ensureInteger(month); | |
@@ -2128,7 +2019,7 @@ | |
parseDay: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 2) { | |
- throw "Day [" + text + "] must be a two-digit number."; | |
+ throw new Error("Day [" + text + "] must be a two-digit number."); | |
} | |
var day = Number(text); | |
Jsonix.Util.Ensure.ensureInteger(day); | |
@@ -2137,7 +2028,7 @@ | |
parseHour: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 2) { | |
- throw "Hour [" + text + "] must be a two-digit number."; | |
+ throw new Error("Hour [" + text + "] must be a two-digit number."); | |
} | |
var hour = Number(text); | |
Jsonix.Util.Ensure.ensureInteger(hour); | |
@@ -2146,7 +2037,7 @@ | |
parseMinute: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 2) { | |
- throw "Minute [" + text + "] must be a two-digit number."; | |
+ throw new Error("Minute [" + text + "] must be a two-digit number."); | |
} | |
var minute = Number(text); | |
Jsonix.Util.Ensure.ensureInteger(minute); | |
@@ -2155,7 +2046,7 @@ | |
parseSecond: function(text) { | |
Jsonix.Util.Ensure.ensureString(text); | |
if (text.length !== 2) { | |
- throw "Second [" + text + "] must be a two-digit number."; | |
+ throw new Error("Second [" + text + "] must be a two-digit number."); | |
} | |
var second = Number(text); | |
Jsonix.Util.Ensure.ensureNumber(second); | |
@@ -2172,6 +2063,9 @@ | |
} | |
}, | |
print: function(value) { | |
+ if (typeof value === "string") { | |
+ return value; | |
+ } | |
Jsonix.Util.Ensure.ensureObject(value); | |
if (Jsonix.Util.NumberUtils.isInteger(value.year) && Jsonix.Util.NumberUtils.isInteger(value.month) && Jsonix.Util.NumberUtils.isInteger(value.day) && Jsonix.Util.NumberUtils.isInteger(value.hour) && Jsonix.Util.NumberUtils.isInteger(value.minute) && Jsonix.Util.NumberUtils.isInteger(value.second)) { | |
return this.printDateTime(value); | |
@@ -2180,7 +2074,7 @@ | |
} else if (Jsonix.Util.NumberUtils.isInteger(value.hour) && Jsonix.Util.NumberUtils.isInteger(value.minute) && Jsonix.Util.NumberUtils.isInteger(value.second)) { | |
return this.printTime(value); | |
} else { | |
- throw "Value [" + value + "] is not recognized as dateTime, date or time."; | |
+ throw new Error("Value [" + value + "] is not recognized as dateTime, date or time."); | |
} | |
}, | |
printDateTime: function(value) { | |
@@ -2307,7 +2201,7 @@ | |
printFractionalSecond: function(value) { | |
Jsonix.Util.Ensure.ensureNumber(value); | |
if (value < 0 || value >= 1) { | |
- throw "Fractional second [" + value + "] must be between 0 and 1."; | |
+ throw new Error("Fractional second [" + value + "] must be between 0 and 1."); | |
} else if (value === 0) { | |
return ""; | |
} else { | |
@@ -2324,13 +2218,13 @@ | |
Jsonix.Util.Ensure.ensureInteger(value); | |
Jsonix.Util.Ensure.ensureInteger(length); | |
if (length <= 0) { | |
- throw "Length [" + value + "] must be positive."; | |
+ throw new Error("Length [" + value + "] must be positive."); | |
} | |
if (value < 0) { | |
- throw "Value [" + value + "] must not be negative."; | |
+ throw new Error("Value [" + value + "] must not be negative."); | |
} | |
if (value >= Math.pow(10, length)) { | |
- throw "Value [" + value + "] must be less than [" + Math.pow(10, length) + "]."; | |
+ throw new Error("Value [" + value + "] must be less than [" + Math.pow(10, length) + "]."); | |
} | |
var result = String(value); | |
for (var i = result.length; i < length; i++) { | |
@@ -2425,7 +2319,7 @@ | |
Jsonix.Util.Ensure.ensureDate(value); | |
var time = value.getTime(); | |
if (time <= -864e5 && time >= 864e5) { | |
- throw "Invalid time [" + value + "]."; | |
+ throw new Error("Invalid time [" + value + "]."); | |
} | |
if (time >= 0) { | |
return this.printTime(new Jsonix.XML.Calendar({ | |
@@ -2689,7 +2583,7 @@ | |
TYPE_NAME: this.name | |
}; | |
if (input.eventType !== 1) { | |
- throw "Parser must be on START_ELEMENT to read a class info."; | |
+ throw new Error("Parser must be on START_ELEMENT to read a class info."); | |
} | |
if (Jsonix.Util.Type.exists(this.structure.attributes)) { | |
var attributeCount = input.getAttributeCount(); | |
@@ -2722,13 +2616,13 @@ | |
var anyPropertyInfo = this.structure.any; | |
this.unmarshalProperty(context, input, anyPropertyInfo, result); | |
} else { | |
- throw "Unexpected element [" + elementNameKey + "]."; | |
+ throw new Error("Unexpected element [" + elementNameKey + "]."); | |
} | |
} else if ((et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE) && Jsonix.Util.Type.exists(this.structure.mixed)) { | |
var mixedPropertyInfo = this.structure.mixed; | |
this.unmarshalProperty(context, input, mixedPropertyInfo, result); | |
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) {} else { | |
- throw "Illegal state: unexpected event type [" + et + "]."; | |
+ throw new Error("Illegal state: unexpected event type [" + et + "]."); | |
} | |
et = input.next(); | |
} | |
@@ -2739,7 +2633,7 @@ | |
input.nextTag(); | |
} | |
if (input.eventType !== 2) { | |
- throw "Illegal state: must be END_ELEMENT."; | |
+ throw new Error("Illegal state: must be END_ELEMENT."); | |
} | |
return result; | |
}, | |
@@ -2785,7 +2679,7 @@ | |
var propertyInfoCreator = this.propertyInfoCreators[type]; | |
propertyInfoCreator.call(this, property); | |
} else { | |
- throw "Unknown property info type [" + type + "]."; | |
+ throw new Error("Unknown property info type [" + type + "]."); | |
} | |
} | |
}, | |
@@ -2912,10 +2806,10 @@ | |
} | |
}, | |
doBuild: function(context, module) { | |
- throw "Abstract method [doBuild]."; | |
+ throw new Error("Abstract method [doBuild]."); | |
}, | |
buildStructure: function(context, structure) { | |
- throw "Abstract method [buildStructure]."; | |
+ throw new Error("Abstract method [buildStructure]."); | |
}, | |
setProperty: function(object, value) { | |
if (Jsonix.Util.Type.exists(value)) { | |
@@ -3065,7 +2959,7 @@ | |
buildStructure: function(context, structure) { | |
Jsonix.Util.Ensure.ensureObject(structure); | |
if (Jsonix.Util.Type.exists(structure.elements)) { | |
- throw "The structure already defines element mappings, it cannot define a value property."; | |
+ throw new Error("The structure already defines element mappings, it cannot define a value property."); | |
} else { | |
structure.value = this; | |
} | |
@@ -3100,7 +2994,7 @@ | |
if (result === null) { | |
result = value; | |
} else { | |
- throw "Value already set."; | |
+ throw new Error("Value already set."); | |
} | |
} | |
}; | |
@@ -3117,13 +3011,13 @@ | |
if (et === Jsonix.XML.Input.START_ELEMENT) { | |
this.unmarshalElement(context, input, callback); | |
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) {} else { | |
- throw "Illegal state: unexpected event type [" + et + "]."; | |
+ throw new Error("Illegal state: unexpected event type [" + et + "]."); | |
} | |
et = input.next(); | |
} | |
}, | |
unmarshalElement: function(context, input, callback) { | |
- throw "Abstract method [unmarshalElement]."; | |
+ throw new Error("Abstract method [unmarshalElement]."); | |
}, | |
marshal: function(context, scope, value, output) { | |
if (!Jsonix.Util.Type.exists(value)) { | |
@@ -3146,7 +3040,7 @@ | |
} | |
}, | |
marshalElement: function(context, value, output) { | |
- throw "Abstract method [marshalElement]."; | |
+ throw new Error("Abstract method [marshalElement]."); | |
}, | |
marshalElementTypeInfo: function(context, value, elementName, typeInfo, output) { | |
output.writeStartElement(elementName); | |
@@ -3156,7 +3050,7 @@ | |
buildStructure: function(context, structure) { | |
Jsonix.Util.Ensure.ensureObject(structure); | |
if (Jsonix.Util.Type.exists(structure.value)) { | |
- throw "The structure already defines a value property."; | |
+ throw new Error("The structure already defines a value property."); | |
} else if (!Jsonix.Util.Type.exists(structure.elements)) { | |
structure.elements = {}; | |
} | |
@@ -3167,7 +3061,7 @@ | |
} | |
}, | |
buildStructureElements: function(context, structure) { | |
- throw "Abstract method [buildStructureElements]."; | |
+ throw new Error("Abstract method [buildStructureElements]."); | |
}, | |
CLASS_NAME: "Jsonix.Model.AbstractElementsPropertyInfo" | |
}); | |
@@ -3225,7 +3119,7 @@ | |
if (Jsonix.Util.Type.exists(typeInfo)) { | |
return callback(typeInfo.unmarshal(context, input)); | |
} | |
- throw "Element [" + elementNameKey + "] is not known in this context"; | |
+ throw new Error("Element [" + elementNameKey + "] is not known in this context"); | |
}, | |
marshalElement: function(context, value, output) { | |
for (var index = 0; index < this.elementTypeInfos.length; index++) { | |
@@ -3237,7 +3131,7 @@ | |
return; | |
} | |
} | |
- throw "Could not find an element with type info supporting the value [" + value + "]."; | |
+ throw new Error("Could not find an element with type info supporting the value [" + value + "]."); | |
}, | |
doBuild: function(context, module) { | |
this.elementTypeInfosMap = {}; | |
@@ -3312,7 +3206,7 @@ | |
if (!Jsonix.Util.Type.exists(result[attributeName])) { | |
result[attributeName] = attributeValue; | |
} else { | |
- throw "Value already set."; | |
+ throw new Error("Value already set."); | |
} | |
} | |
} | |
@@ -3443,7 +3337,7 @@ | |
return value; | |
} | |
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) {} else { | |
- throw "Illegal state: unexpected event type [" + et + "]."; | |
+ throw new Error("Illegal state: unexpected event type [" + et + "]."); | |
} | |
}, | |
unmarshalWrapperElement: function(context, scope, input) { | |
@@ -3463,7 +3357,7 @@ | |
if (result === null) { | |
result = value; | |
} else { | |
- throw "Value already set."; | |
+ throw new Error("Value already set."); | |
} | |
} | |
} else if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE)) { | |
@@ -3477,11 +3371,11 @@ | |
if (result === null) { | |
result = text; | |
} else { | |
- throw "Value already set."; | |
+ throw new Error("Value already set."); | |
} | |
} | |
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) {} else { | |
- throw "Illegal state: unexpected event type [" + et + "]."; | |
+ throw new Error("Illegal state: unexpected event type [" + et + "]."); | |
} | |
et = input.next(); | |
} | |
@@ -3522,7 +3416,7 @@ | |
marshalItem: function(context, scope, value, output) { | |
if (Jsonix.Util.Type.isString(value)) { | |
if (!this.mixed) { | |
- throw "Property is not mixed, can't handle string values."; | |
+ throw new Error("Property is not mixed, can't handle string values."); | |
} else { | |
output.writeCharacters(value); | |
} | |
@@ -3530,9 +3424,9 @@ | |
this.marshalElement(context, scope, value, output); | |
} else { | |
if (this.mixed) { | |
- throw "Unsupported content type, either objects or strings are supported."; | |
+ throw new Error("Unsupported content type, either objects or strings are supported."); | |
} else { | |
- throw "Unsupported content type, only objects are supported."; | |
+ throw new Error("Unsupported content type, only objects are supported."); | |
} | |
} | |
}, | |
@@ -3557,17 +3451,17 @@ | |
if (Jsonix.Util.Type.exists(contextElementTypeInfo)) { | |
return contextElementTypeInfo.typeInfo; | |
} else { | |
- throw "Element [" + elementName.key + "] is not known in this context."; | |
+ throw new Error("Element [" + elementName.key + "] is not known in this context."); | |
} | |
} | |
}, | |
getPropertyElementTypeInfo: function(elementName) { | |
- throw "Abstract method [getPropertyElementTypeInfo]."; | |
+ throw new Error("Abstract method [getPropertyElementTypeInfo]."); | |
}, | |
buildStructure: function(context, structure) { | |
Jsonix.Util.Ensure.ensureObject(structure); | |
if (Jsonix.Util.Type.exists(structure.value)) { | |
- throw "The structure already defines a value property."; | |
+ throw new Error("The structure already defines a value property."); | |
} else if (!Jsonix.Util.Type.exists(structure.elements)) { | |
structure.elements = {}; | |
} | |
@@ -3581,7 +3475,7 @@ | |
} | |
}, | |
buildStructureElements: function(context, structure) { | |
- throw "Abstract method [buildStructureElements]."; | |
+ throw new Error("Abstract method [buildStructureElements]."); | |
}, | |
buildStructureElementTypeInfos: function(context, structure, elementTypeInfo) { | |
structure.elements[elementTypeInfo.elementName.key] = this; | |
@@ -3725,7 +3619,7 @@ | |
} else if (et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) { | |
return null; | |
} else { | |
- throw "Illegal state: unexpected event type [" + et + "]."; | |
+ throw new Error("Illegal state: unexpected event type [" + et + "]."); | |
} | |
}, | |
unmarshalElement: function(context, scope, input) { | |
@@ -3742,7 +3636,7 @@ | |
} else if (this.allowDom) { | |
value = input.getElement(); | |
} else { | |
- throw "Element [" + name.toString() + "] is not known in this context and property does not allow DOM."; | |
+ throw new Error("Element [" + name.toString() + "] is not known in this context and property does not allow DOM."); | |
} | |
if (this.collection) { | |
return [ value ]; | |
@@ -3778,7 +3672,7 @@ | |
adapter.marshal(context, value.value, output, typeInfo); | |
output.writeEndElement(); | |
} else { | |
- throw "Element [" + name.toString() + "] is not known in this context"; | |
+ throw new Error("Element [" + name.toString() + "] is not known in this context"); | |
} | |
} | |
}, | |
@@ -3786,7 +3680,7 @@ | |
buildStructure: function(context, structure) { | |
Jsonix.Util.Ensure.ensureObject(structure); | |
if (Jsonix.Util.Type.exists(structure.value)) { | |
- throw "The structure already defines a value property."; | |
+ throw new Error("The structure already defines a value property."); | |
} else if (!Jsonix.Util.Type.exists(structure.elements)) { | |
structure.elements = {}; | |
} | |
@@ -3865,7 +3759,7 @@ | |
var typeInfoCreator = this.typeInfoCreators[type]; | |
typeInfo = typeInfoCreator.call(this, mapping); | |
} else { | |
- throw "Unknown type info type [" + type + "]."; | |
+ throw new Error("Unknown type info type [" + type + "]."); | |
} | |
} | |
return typeInfo; | |
@@ -3897,7 +3791,7 @@ | |
options.name = options.localName; | |
} | |
} else { | |
- throw "Neither [name] nor [localName] was provided for the class info."; | |
+ throw new Error("Neither [name] nor [localName] was provided for the class info."); | |
} | |
var classInfo = new Jsonix.Model.ClassInfo(options); | |
return classInfo; | |
@@ -3919,7 +3813,7 @@ | |
} else if (Jsonix.Util.Type.isString(options.elementName)) { | |
options.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, options.elementName); | |
} else { | |
- throw "Element info [" + options + "] must provide an element name."; | |
+ throw new Error("Element info [" + options + "] must provide an element name."); | |
} | |
if (Jsonix.Util.Type.exists(options.substitutionHead)) { | |
if (Jsonix.Util.Type.isObject(options.substitutionHead)) { | |
@@ -4076,7 +3970,7 @@ | |
} | |
var scopeKey; | |
if (Jsonix.Util.Type.exists(elementInfo.scope)) { | |
- scopeKey = elementInfo.scope.name; | |
+ scopeKey = elementInfo.scope; | |
} else { | |
scopeKey = "##global"; | |
} | |
@@ -4149,7 +4043,7 @@ | |
var name = Jsonix.XML.QName.fromObject(value.name); | |
var elementDeclaration = this.context.getElementInfo(name); | |
if (!Jsonix.Util.Type.exists(elementDeclaration)) { | |
- throw "Could not find element declaration for the element [" + name.key + "]."; | |
+ throw new Error("Could not find element declaration for the element [" + name.key + "]."); | |
} | |
Jsonix.Util.Ensure.ensureObject(elementDeclaration.typeInfo); | |
var typeInfo = elementDeclaration.typeInfo; | |
@@ -4210,13 +4104,13 @@ | |
}, | |
unmarshalElementNode: function(input) { | |
if (input.eventType != 1) { | |
- throw "Parser must be on START_ELEMENT to read next text."; | |
+ throw new Error("Parser must be on START_ELEMENT to read next text."); | |
} | |
var result = null; | |
var name = Jsonix.XML.QName.fromObject(input.getName()); | |
var elementDeclaration = this.context.getElementInfo(name); | |
if (!Jsonix.Util.Type.exists(elementDeclaration)) { | |
- throw "Could not find element declaration for the element [" + name.key + "]."; | |
+ throw new Error("Could not find element declaration for the element [" + name.key + "]."); | |
} | |
Jsonix.Util.Ensure.ensureObject(elementDeclaration.typeInfo); | |
var typeInfo = elementDeclaration.typeInfo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment