Jsonix.Context = function () { this.initialize.apply(this, arguments); }
n/a
Jsonix.Request = function () { this.initialize.apply(this, arguments); }
n/a
Class = function () { var Class = function() { this.initialize.apply(this, arguments); }; var extended = {}; var empty = function() { }; var parent, initialize, Type; for (var i = 0, len = arguments.length; i < len; ++i) { Type = arguments[i]; if (typeof Type == "function") { // make the class passed as the first argument the superclass if (i === 0 && len > 1) { initialize = Type.prototype.initialize; // replace the initialize method with an empty function, // because we do not want to create a real instance here Type.prototype.initialize = empty; // the line below makes sure that the new class has a // superclass extended = new Type(); // restore the original initialize method if (initialize === undefined) { delete Type.prototype.initialize; } else { Type.prototype.initialize = initialize; } } // get the prototype of the superclass parent = Type.prototype; } else { // in this case we're extending with the prototype parent = Type; } Jsonix.Util.extend(extended, parent); } Class.prototype = extended; return Class; }
...
Jsonix.DOM.xlinkFixRequired = false;
}
}
return Jsonix.DOM.xlinkFixRequired;
}
};
Jsonix.Request = Jsonix
.Class({
// REWORK
factories : [ function() {
return new XMLHttpRequest();
}, function() {
return new ActiveXObject('Msxml2.XMLHTTP');
}, function() {
return new ActiveXObject("Msxml2.XMLHTTP.6.0");
...
Context = function () { this.initialize.apply(this, arguments); }
...
```javascript
// Include or require PO.js so that PO variable is available
// For instance, in node.js:
var PO = require('./mappings/PO').PO;
// First we construct a Jsonix context - a factory for unmarshaller (parser)
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);
// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();
// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
// This callback function will be provided
...
Request = function () { this.initialize.apply(this, arguments); }
...
// TODO log
}
}
throw new Error('Could not create XML HTTP transport.');
},
CLASS_NAME : 'Jsonix.Request'
});
Jsonix.Request.INSTANCE = new Jsonix.Request();
Jsonix.Request.PROXY = null;
Jsonix.Schema = {};
Jsonix.Model = {};
Jsonix.Util.Type = {
exists : function(value) {
return (typeof value !== 'undefined' && value !== null);
},
...
Marshaller = function () { this.initialize.apply(this, arguments); }
n/a
Unmarshaller = function () { this.initialize.apply(this, arguments); }
n/a
convertToNamedValue = function (elementValue, context, output, scope) { var name; var value; if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) { name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context); value = Jsonix.Util.Type.exists(elementValue.value) ? elementValue.value : null; return { name : name, value : value }; } else { for ( var propertyName in elementValue) { if (elementValue.hasOwnProperty(propertyName)) { name = Jsonix.XML.QName.fromObjectOrString(propertyName, context); value = elementValue[propertyName]; return { name : name, value : value }; } } } throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue } or {myElementName:elementValue} structure."); }
...
return undefined;
}
}
});
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
...
convertToTypedNamedValue = function (value, context, output, scope) { Jsonix.Util.Ensure.ensureObject(value); var elementValue = this.convertToNamedValue(value, context, output, scope); return { name : elementValue.name, value : elementValue.value, typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope) }; }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
getTypeInfoByElementName = function (name, context, scope) { var elementInfo = context.getElementInfo(name, scope); if (Jsonix.Util.Type.exists(elementInfo)) { return elementInfo.typeInfo; } else { return undefined; } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
initialize = function (context) { Jsonix.Util.Ensure.ensureObject(context); this.context = context; }
n/a
marshalDocument = function (value) { var output = new Jsonix.XML.Output({ namespacePrefixes : this.context.namespacePrefixes }); var doc = output.writeStartDocument(); this.marshalElement(value, this.context, output, undefined); output.writeEndDocument(); return doc; }
...
Jsonix.Binding.Marshaller = Jsonix.Class(Jsonix.Binding.Marshalls.Element, Jsonix.Binding.Marshalls.Element.AsElementRef, {
context : null,
initialize : function(context) {
Jsonix.Util.Ensure.ensureObject(context);
this.context = context;
},
marshalString : function(value) {
var doc = this.marshalDocument(value);
var text = Jsonix.DOM.serialize(doc);
return text;
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
marshalString = function (value) { var doc = this.marshalDocument(value); var text = Jsonix.DOM.serialize(doc); return text; }
...
// console.log("Loaded JSON from the file [" + jsonFile + "].");
// console.log(JSON.stringify(json, null, 4));
var marshaller = context.createMarshaller();
var marshalledXMLText = marshaller.marshalString(json);
// console.log("Marshalled JSON from the file [" + jsonFile + "] as XML:");
// console.log(marshalledXMLText);
var xmlFile = resource + '.xml';
var xmlData = fs.readFileSync(xmlFile);
...
convertFromTypedNamedValue = function (typedNamedValue, context, input, scope) { return { name : typedNamedValue.name, value : typedNamedValue.value }; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
getTypeInfoByElementName = function (name, context, scope) { var elementInfo = context.getElementInfo(name, scope); if (Jsonix.Util.Type.exists(elementInfo)) { return elementInfo.typeInfo; } else { return undefined; } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (context) { Jsonix.Util.Ensure.ensureObject(context); this.context = context; }
n/a
unmarshalDocument = function (doc, scope) { var input = new Jsonix.XML.Input(doc); var result = null; var callback = function(_result) { result = _result; }; input.nextTag(); this.unmarshalElement(this.context, input, scope, callback); return result; }
...
initialize : function(context) {
Jsonix.Util.Ensure.ensureObject(context);
this.context = context;
},
unmarshalString : function(text) {
Jsonix.Util.Ensure.ensureString(text);
var doc = Jsonix.DOM.parse(text);
return this.unmarshalDocument(doc);
},
unmarshalURL : function(url, callback, options) {
Jsonix.Util.Ensure.ensureString(url);
Jsonix.Util.Ensure.ensureFunction(callback);
if (Jsonix.Util.Type.exists(options)) {
Jsonix.Util.Ensure.ensureObject(options);
}
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalFile = function (fileName, callback, options) { if (typeof _jsonix_fs === 'undefined') { throw new Error("File unmarshalling is only available in environments which support file systems."); } Jsonix.Util.Ensure.ensureString(fileName); Jsonix.Util.Ensure.ensureFunction(callback); if (Jsonix.Util.Type.exists(options)) { Jsonix.Util.Ensure.ensureObject(options); } that = this; var fs = _jsonix_fs; fs.readFile(fileName, options, function(err, data) { if (err) { throw err; } else { var text = data.toString(); var doc = Jsonix.DOM.parse(text); callback(that.unmarshalDocument(doc)); } }); }
...
var roundtrip = function(test, resource, context) {
var xmlFile = resource + '.xml';
var unmarshallerOne = context.createUnmarshaller();
var unmarshallerTwo = context.createUnmarshaller();
var marshallerOne = context.createMarshaller();
var marshallerTwo = context.createMarshaller();
console.log('Unmarshalling [' + resource + '].');
unmarshallerOne.unmarshalFile(xmlFile, function(one) {
console.log('Unmarshalled one:');
console.log(JSON.stringify(one, null, 4));
var documentOne = marshallerOne.marshalDocument(one);
var two = unmarshallerTwo.unmarshalDocument(documentOne);
console.log('Unmarshalled two:');
console.log(JSON.stringify(one, null, 4));
var stringTwo = marshallerTwo.marshalString(two);
...
unmarshalString = function (text) { Jsonix.Util.Ensure.ensureString(text); var doc = Jsonix.DOM.parse(text); return this.unmarshalDocument(doc); }
...
test.equal(false, nonXsiContext.supportXsiType);
test.done();
},
},
"Unmarshalls" : {
"Root" : function(test) {
var unmarshaller = context.createUnmarshaller();
var data = unmarshaller.unmarshalString('<Expression xmlns="urn:GH70
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Literal">test</Expression
x3e;');
test.equal("GH70.Literal", data.value.TYPE_NAME);
test.equal("test", data.value.value);
test.done();
},
"ElementPropertyInfo" : function(test) {
var unmarshaller = context.createUnmarshaller();
var data = unmarshaller.unmarshalString('<And xmlns="urn:GH70"><Expression xmlns:xsi="http
://www.w3.org/2001/XMLSchema-instance" xsi:type="Literal">a</Expression><Expression xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Literal">b</Expression></And>
;');
...
unmarshalURL = function (url, callback, options) { Jsonix.Util.Ensure.ensureString(url); Jsonix.Util.Ensure.ensureFunction(callback); if (Jsonix.Util.Type.exists(options)) { Jsonix.Util.Ensure.ensureObject(options); } that = this; Jsonix.DOM.load(url, function(doc) { callback(that.unmarshalDocument(doc)); }, options); }
...
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);
// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();
// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
// This callback function will be provided
// with the result of the unmarshalling
function (unmarshalled) {
// Alice Smith
console.log(unmarshalled.value.shipTo.name);
// Baby Monitor
console.log(unmarshalled.value.items.item[1].productName);
...
Context = function () { this.initialize.apply(this, arguments); }
...
```javascript
// Include or require PO.js so that PO variable is available
// For instance, in node.js:
var PO = require('./mappings/PO').PO;
// First we construct a Jsonix context - a factory for unmarshaller (parser)
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);
// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();
// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
// This callback function will be provided
...
createMarshaller = function () { return new this.mappingStyle.marshaller(this); }
...
You can also `unmarshalString`, `unmarshalDocument` and (under node.js) `unmarshalFile`.
### Serialize JS as XML
```javascript
// Create a marshaller
var marshaller = context.createMarshaller();
// Marshal a JavaScript Object as XML (DOM Document)
var doc = marshaller.marshalDocument({
name: {
localPart: "purchaseOrder"
},
value: {
...
createModule = function (mapping) { var module; if (mapping instanceof this.mappingStyle.module) { module = mapping; } else { mapping = Jsonix.Util.Type.cloneObject(mapping); module = new this.mappingStyle.module(mapping, { mappingStyle : this.mappingStyle }); } return module; }
...
// Initialize modules
if (Jsonix.Util.Type.exists(mappings)) {
Jsonix.Util.Ensure.ensureArray(mappings);
// Initialize modules
var index, mapping, module;
for (index = 0; index < mappings.length; index++) {
mapping = mappings[index];
module = this.createModule(mapping);
this.modules[index] = module;
}
}
this.processModules();
},
createModule : function(mapping) {
var module;
...
createUnmarshaller = function () { return new this.mappingStyle.unmarshaller(this); }
...
var PO = require('./mappings/PO').PO;
// First we construct a Jsonix context - a factory for unmarshaller (parser)
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);
// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();
// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
// This callback function will be provided
// with the result of the unmarshalling
function (unmarshalled) {
// Alice Smith
...
getElementInfo = function (name, scope) { if (Jsonix.Util.Type.exists(scope)) { var scopeKey = scope.name; var scopedElementInfos = this.scopedElementInfosMap[scopeKey]; if (Jsonix.Util.Type.exists(scopedElementInfos)) { var scopedElementInfo = scopedElementInfos[name.key]; if (Jsonix.Util.Type.exists(scopedElementInfo)) { return scopedElementInfo; } } } var globalScopeKey = '##global'; var globalScopedElementInfos = this.scopedElementInfosMap[globalScopeKey]; if (Jsonix.Util.Type.exists(globalScopedElementInfos)) { var globalScopedElementInfo = globalScopedElementInfos[name.key]; if (Jsonix.Util.Type.exists(globalScopedElementInfo)) { return globalScopedElementInfo; } } return null; // // throw new Error("Element [" + name.key // + "] could not be found in the given context."); }
...
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
var elementInfo = context.getElementInfo(name, scope);
if (Jsonix.Util.Type.exists(elementInfo)) {
return elementInfo.typeInfo;
} else {
return undefined;
}
}
});
...
getNamespaceURI = function (prefix) { Jsonix.Util.Ensure.ensureString(prefix); return this.prefixNamespaces[prefix]; }
...
{
if (prefix === '' && Jsonix.Util.Type.isString(defaultNamespaceURI))
{
namespaceURI = defaultNamespaceURI;
}
else if (namespaceContext)
{
namespaceURI = namespaceContext.getNamespaceURI(prefix);
}
}
// If we don't have a namespace URI, assume '' by default
// TODO document the assumption
if (!Jsonix.Util.Type.isString(namespaceURI))
{
namespaceURI = defaultNamespaceURI || '';
...
getPrefix = function (namespaceURI, defaultPrefix) { Jsonix.Util.Ensure.ensureString(namespaceURI); var prefix = this.namespacePrefixes[namespaceURI]; if (Jsonix.Util.Type.isString(prefix)) { return prefix; } else { return defaultPrefix; } }
...
this.string = (namespaceURI !== '' ? ('{' + namespaceURI + '}') : '') + (prefix !==
x27;' ? (prefix + ':') : '') + localPart;
},
toString : function() {
return this.string;
},
// foo:bar
toCanonicalString: function(namespaceContext) {
var canonicalPrefix = namespaceContext ? namespaceContext.getPrefix(this.namespaceURI
, this.prefix) : this.prefix;
return this.prefix + (this.prefix === '' ? '' : ':') + this.localPart;
},
clone : function() {
return new Jsonix.XML.QName(this.namespaceURI, this.localPart, this.prefix);
},
equals : function(that) {
if (!that) {
...
getSubstitutionMembers = function (name) { return this.substitutionMembersMap[Jsonix.XML.QName .fromObject(name).key]; }
...
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
buildStructureElementTypeInfos : function(context, structure, elementTypeInfo) {
structure.elements[elementTypeInfo.elementName.key] = this;
var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName
);
if (Jsonix.Util.Type.isArray(substitutionMembers)) {
for (var jndex = 0; jndex < substitutionMembers.length; jndex++) {
var substitutionElementInfo = substitutionMembers[jndex];
this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo);
}
}
...
getTypeInfoByName = function (name) { return this.typeInfos[name]; }
...
return undefined;
}
if (Jsonix.Util.Type.isObject(value))
{
var typeName = value.TYPE_NAME;
if (Jsonix.Util.Type.isString(typeName))
{
var typeInfoByName = this.getTypeInfoByName(typeName);
if (typeInfoByName)
{
return typeInfoByName;
}
}
}
return undefined;
...
getTypeInfoByTypeName = function (typeName) { var tn = Jsonix.XML.QName.fromObjectOrString(typeName, this); return this.typeNameKeyToTypeInfo[tn.key]; }
...
}
});
test.equal("String", context.getTypeInfoByTypeNameKey("{http://www.w3.org/2001/XMLSchema}string").name);
test.equal("Zero.AType", context.getTypeInfoByTypeNameKey("{urn:orez}AType").name);
test.equal("Zero.BType", context.getTypeInfoByTypeNameKey("B").name);
test.equal("Zero.CType", context.getTypeInfoByTypeNameKey("{urn:c}C").name);
test.equal("Zero.DType", context.getTypeInfoByTypeNameKey("{urn:orez}D").name);
test.equal("String", context.getTypeInfoByTypeName({ns:'http://www.w3
.org/2001/XMLSchema', lp: 'string'}).name);
test.equal("Zero.AType", context.getTypeInfoByTypeName({ns:'urn:orez', lp: 'AType'}).name);
test.equal("Zero.AType", context.getTypeInfoByTypeName('orez:AType').name);
test.equal("Zero.BType", context.getTypeInfoByTypeName("B").name);
test.equal("Zero.CType", context.getTypeInfoByTypeName('{urn:c}C').name);
test.equal("Zero.CType", context.getTypeInfoByTypeName('c:C').name);
test.equal("Zero.DType", context.getTypeInfoByTypeName('orez:D').name);
test.done();
...
getTypeInfoByTypeNameKey = function (typeNameKey) { return this.typeNameKeyToTypeInfo[typeNameKey]; }
...
},
getTypeInfoByInputElement : function(context, input, scope) {
var xsiTypeInfo = null;
if (context.supportXsiType) {
var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE);
if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) {
var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope);
xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key);
}
}
var name = input.getName();
var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope);
return typeInfo;
},
getTypeInfoByElementName : function(name, context, scope) {
...
getTypeInfoByValue = function (value) { if (!Jsonix.Util.Type.exists(value)) { return undefined; } if (Jsonix.Util.Type.isObject(value)) { var typeName = value.TYPE_NAME; if (Jsonix.Util.Type.isString(typeName)) { var typeInfoByName = this.getTypeInfoByName(typeName); if (typeInfoByName) { return typeInfoByName; } } } return undefined; }
...
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
actualTypeInfo = typeInfoByValue;
}
}
var typeInfo = actualTypeInfo || declaredTypeInfo;
if (typeInfo) {
...
initialize = function (mappings, options) { Jsonix.Mapping.Styled.prototype.initialize.apply(this, [options]); this.modules = []; this.elementInfos = []; this.typeInfos = {}; this.typeNameKeyToTypeInfo = {}; this.registerBuiltinTypeInfos(); this.namespacePrefixes = {}; this.prefixNamespaces = {}; this.substitutionMembersMap = {}; this.scopedElementInfosMap = {}; // Initialize options if (Jsonix.Util.Type.exists(options)) { Jsonix.Util.Ensure.ensureObject(options); if (Jsonix.Util.Type .isObject(options.namespacePrefixes)) { this.namespacePrefixes = Jsonix.Util.Type.cloneObject(options.namespacePrefixes, {}); } if (Jsonix.Util.Type .isBoolean(options.supportXsiType)) { this.supportXsiType = options.supportXsiType; } } // Initialize prefix/namespace mapping for (var ns in this.namespacePrefixes) { if (this.namespacePrefixes.hasOwnProperty(ns)) { p = this.namespacePrefixes[ns]; this.prefixNamespaces[p] = ns; } } // Initialize modules if (Jsonix.Util.Type.exists(mappings)) { Jsonix.Util.Ensure.ensureArray(mappings); // Initialize modules var index, mapping, module; for (index = 0; index < mappings.length; index++) { mapping = mappings[index]; module = this.createModule(mapping); this.modules[index] = module; } } this.processModules(); }
n/a
processModules = function () { var index, module; for (index = 0; index < this.modules.length; index++) { module = this.modules[index]; module.registerTypeInfos(this); } for (index = 0; index < this.modules.length; index++) { module = this.modules[index]; module.buildTypeInfos(this); } for (index = 0; index < this.modules.length; index++) { module = this.modules[index]; module.registerElementInfos(this); } for (index = 0; index < this.modules.length; index++) { module = this.modules[index]; module.buildElementInfos(this); } }
...
var index, mapping, module;
for (index = 0; index < mappings.length; index++) {
mapping = mappings[index];
module = this.createModule(mapping);
this.modules[index] = module;
}
}
this.processModules();
},
createModule : function(mapping) {
var module;
if (mapping instanceof this.mappingStyle.module) {
module = mapping;
} else {
mapping = Jsonix.Util.Type.cloneObject(mapping);
...
registerBuiltinTypeInfos = function () { for ( var index = 0; index < this.builtinTypeInfos.length; index++) { this.registerTypeInfo(this.builtinTypeInfos[index]); } }
...
supportXsiType : true,
initialize : function(mappings, options) {
Jsonix.Mapping.Styled.prototype.initialize.apply(this, [options]);
this.modules = [];
this.elementInfos = [];
this.typeInfos = {};
this.typeNameKeyToTypeInfo = {};
this.registerBuiltinTypeInfos();
this.namespacePrefixes = {};
this.prefixNamespaces = {};
this.substitutionMembersMap = {};
this.scopedElementInfosMap = {};
// Initialize options
if (Jsonix.Util.Type.exists(options)) {
...
registerElementInfo = function (elementInfo, module) { Jsonix.Util.Ensure.ensureObject(elementInfo); this.elementInfos.push(elementInfo); if (Jsonix.Util.Type.exists(elementInfo.substitutionHead)) { var substitutionHead = elementInfo.substitutionHead; var substitutionHeadKey = substitutionHead.key; var substitutionMembers = this.substitutionMembersMap[substitutionHeadKey]; if (!Jsonix.Util.Type.isArray(substitutionMembers)) { substitutionMembers = []; this.substitutionMembersMap[substitutionHeadKey] = substitutionMembers; } substitutionMembers.push(elementInfo); } var scopeKey; if (Jsonix.Util.Type.exists(elementInfo.scope)) { scopeKey = this.resolveTypeInfo(elementInfo.scope, module).name; } else { scopeKey = '##global'; } var scopedElementInfos = this.scopedElementInfosMap[scopeKey]; if (!Jsonix.Util.Type.isObject(scopedElementInfos)) { scopedElementInfos = {}; this.scopedElementInfosMap[scopeKey] = scopedElementInfos; } scopedElementInfos[elementInfo.elementName.key] = elementInfo; }
...
var typeInfo = this.typeInfos[index];
typeInfo.build(context, this);
}
},
registerElementInfos : function(context) {
for (var index = 0; index < this.elementInfos.length; index++) {
var elementInfo = this.elementInfos[index];
context.registerElementInfo(elementInfo, this);
}
},
buildElementInfos : function(context) {
for (var index = 0; index < this.elementInfos.length; index++) {
var elementInfo = this.elementInfos[index];
elementInfo.build(context, this);
}
...
registerTypeInfo = function (typeInfo) { Jsonix.Util.Ensure.ensureObject(typeInfo); var n = typeInfo.name||typeInfo.n||null; Jsonix.Util.Ensure.ensureString(n); this.typeInfos[n] = typeInfo; if (typeInfo.typeName && typeInfo.typeName.key) { this.typeNameKeyToTypeInfo[typeInfo.typeName.key] = typeInfo; } }
...
mappingStyle : this.mappingStyle
});
return elementInfo;
},
registerTypeInfos : function(context) {
for (var index = 0; index < this.typeInfos.length; index++) {
var typeInfo = this.typeInfos[index];
context.registerTypeInfo(typeInfo, this);
}
},
buildTypeInfos : function(context) {
for (var index = 0; index < this.typeInfos.length; index++) {
var typeInfo = this.typeInfos[index];
typeInfo.build(context, this);
}
...
resolveTypeInfo = function (mapping, module) { if (!Jsonix.Util.Type.exists(mapping)) { return null; } else if (mapping instanceof Jsonix.Model.TypeInfo) { return mapping; } else if (Jsonix.Util.Type.isString(mapping)) { var typeInfoName; // If mapping starts with '.' consider it to be a local type name in this module if (mapping.length > 0 && mapping.charAt(0) === '.') { var n = module.name || module.n || undefined; Jsonix.Util.Ensure.ensureObject(module, 'Type info mapping can only be resolved if module is provided.'); Jsonix.Util.Ensure.ensureString(n, 'Type info mapping can only be resolved if module name is provided.'); typeInfoName = n + mapping; } else { typeInfoName = mapping; } if (!this.typeInfos[typeInfoName]) { throw new Error('Type info [' + typeInfoName + '] is not known in this context.'); } else { return this.typeInfos[typeInfoName]; } } else { Jsonix.Util.Ensure.ensureObject(module, 'Type info mapping can only be resolved if module is provided.'); var typeInfo = module.createTypeInfo(mapping); typeInfo.build(this, module); return typeInfo; } }
...
return this.propertiesMap[name];
},
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
...
createDocument = function () { // REWORK // Node.js if (typeof _jsonix_xmldom !== 'undefined') { return new (_jsonix_xmldom.DOMImplementation)().createDocument(); } else if (typeof document !== 'undefined' && Jsonix.Util.Type.exists(document.implementation) && Jsonix.Util.Type.isFunction( document.implementation.createDocument)) { return document.implementation.createDocument('', '', null); } else if (typeof ActiveXObject !== 'undefined') { return new ActiveXObject('MSXML2.DOMDocument'); } else { throw new Error('Error created the DOM document.'); } }
...
}
},
createDocument : function() {
// REWORK
// Node.js
if (typeof _jsonix_xmldom !== 'undefined')
{
return new (_jsonix_xmldom.DOMImplementation)().createDocument();
} else if (typeof document !== 'undefined' && Jsonix.Util.Type.exists(document.implementation) &&
; Jsonix.Util.Type.isFunction(document.implementation.createDocument)) {
return document.implementation.createDocument('', '', null);
} else if (typeof ActiveXObject !== 'undefined') {
return new ActiveXObject('MSXML2.DOMDocument');
} else {
throw new Error('Error created the DOM document.');
}
...
isDomImplementationAvailable = function () { if (typeof _jsonix_xmldom !== 'undefined') { return true; } else if (typeof document !== 'undefined' && Jsonix.Util.Type.exists(document.implementation) && Jsonix.Util.Type.isFunction( document.implementation.createDocument)) { return true; } else { return false; } }
...
pnsItem = Jsonix.Util.Type.isObject(pnsItem) ? pnsItem : this.pns[pnsItem];
return pnsItem[p];
},
CLASS_NAME : "Jsonix.XML.Input"
});
Jsonix.XML.Input.prototype.getAttributeValueNS = (Jsonix.DOM.isDomImplementationAvailable()) ? Jsonix.XML.Input.prototype.getAttributeValueNSViaElement : Jsonix.XML.Input.prototype.getAttributeValueNSViaAttribute;
Jsonix.XML.Input.prototype.getAttributeNodeNS = (Jsonix.DOM.isDomImplementationAvailable()) ? Jsonix.XML.Input.prototype.getAttributeNodeNSViaElement
: Jsonix.XML.Input.prototype.getAttributeNodeNSViaAttributes;
Jsonix.XML.Input.START_ELEMENT = 1;
Jsonix.XML.Input.END_ELEMENT = 2;
Jsonix.XML.Input.PROCESSING_INSTRUCTION = 3;
Jsonix.XML.Input.CHARACTERS = 4;
Jsonix.XML.Input.COMMENT = 5;
...
isXlinkFixRequired = function () { if (Jsonix.DOM.xlinkFixRequired === null) { if (typeof navigator === 'undefined') { Jsonix.DOM.xlinkFixRequired = false; } else if (!!navigator.userAgent && (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor))) { var doc = Jsonix.DOM.createDocument(); var el = doc.createElement('test'); el.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'urn:test'); doc.appendChild(el); var testString = Jsonix.DOM.serialize(doc); Jsonix.DOM.xlinkFixRequired = (testString.indexOf('xmlns:xlink') === -1); } else { Jsonix.DOM.xlinkFixRequired = false; } } return Jsonix.DOM.xlinkFixRequired; }
n/a
load = function (url, callback, options) { var request = Jsonix.Request.INSTANCE; request.issue( url, function(transport) { var result; if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement)) { result = transport.responseXML; } else if (Jsonix.Util.Type.isString(transport.responseText)) { result = Jsonix.DOM.parse(transport.responseText); } else { throw new Error('Response does not have valid [responseXML] or [responseText].'); } callback(result); }, function(transport) { throw new Error('Could not retrieve XML from URL [' + url + '].'); }, options); }
...
unmarshalURL : function(url, callback, options) {
Jsonix.Util.Ensure.ensureString(url);
Jsonix.Util.Ensure.ensureFunction(callback);
if (Jsonix.Util.Type.exists(options)) {
Jsonix.Util.Ensure.ensureObject(options);
}
that = this;
Jsonix.DOM.load(url, function(doc) {
callback(that.unmarshalDocument(doc));
}, options);
},
unmarshalFile : function(fileName, callback, options) {
if (typeof _jsonix_fs === 'undefined') {
throw new Error("File unmarshalling is only available in environments which support file systems.");
}
...
parse = function (text) { Jsonix.Util.Ensure.ensureExists(text); if (typeof _jsonix_xmldom !== 'undefined') { return (new (_jsonix_xmldom).DOMParser()).parseFromString(text, 'application/xml'); } else if (typeof DOMParser != 'undefined') { return (new DOMParser()).parseFromString(text, 'application/xml'); } else if (typeof ActiveXObject != 'undefined') { var doc = Jsonix.DOM.createDocument('', ''); doc.loadXML(text); return doc; } else { var url = 'data:text/xml;charset=utf-8,' + encodeURIComponent(text); var request = new XMLHttpRequest(); request.open('GET', url, false); if (request.overrideMimeType) { request.overrideMimeType("text/xml"); } request.send(null); return request.responseXML; } }
...
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement
)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
} else {
throw new Error('Response does not have valid [responseXML] or [responseText].');
}
callback(result);
}, function(transport) {
throw new Error('Could not retrieve XML from URL [' + url + '].');
...
serialize = function (node) { Jsonix.Util.Ensure.ensureExists(node); // REWORK // Node.js if (typeof _jsonix_xmldom !== 'undefined') { return (new (_jsonix_xmldom).XMLSerializer()).serializeToString(node); } else if (Jsonix.Util.Type.exists(XMLSerializer)) { return (new XMLSerializer()).serializeToString(node); } else if (Jsonix.Util.Type.exists(node.xml)) { return node.xml; } else { throw new Error('Could not serialize the node, neither XMLSerializer nor the [xml] property were found.'); } }
...
}
else if (!!navigator.userAgent && (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor
)))
{
var doc = Jsonix.DOM.createDocument();
var el = doc.createElement('test');
el.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'urn:test');
doc.appendChild(el);
var testString = Jsonix.DOM.serialize(doc);
Jsonix.DOM.xlinkFixRequired = (testString.indexOf('xmlns:xlink') === -1);
}
else
{
Jsonix.DOM.xlinkFixRequired = false;
}
}
...
Style = function () { this.initialize.apply(this, arguments); }
n/a
Styled = function () { this.initialize.apply(this, arguments); }
n/a
initialize = function () { }
n/a
initialize = function (options) { if (Jsonix.Util.Type.exists(options)) { Jsonix.Util.Ensure.ensureObject(options); if (Jsonix.Util.Type.isString(options.mappingStyle)) { var mappingStyle = Jsonix.Mapping.Style.STYLES[options.mappingStyle]; if (!mappingStyle) { throw new Error("Mapping style [" + options.mappingStyle + "] is not known."); } this.mappingStyle = mappingStyle; } else if (Jsonix.Util.Type.isObject(options.mappingStyle)) { this.mappingStyle = options.mappingStyle; } } if (!this.mappingStyle) { this.mappingStyle = Jsonix.Mapping.Style.STYLES.standard; } }
n/a
AbstractElementRefsPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
AbstractElementsPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
AnyAttributePropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
AnyElementPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
AttributePropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
ClassInfo = function () { this.initialize.apply(this, arguments); }
...
if (Jsonix.Util.Type.isObject(en)) {
this.elementName = Jsonix.XML.QName.fromObject(en);
} else if (Jsonix.Util.Type.isString(en)) {
this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en);
} else {
this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name);
}
this.entryTypeInfo = new Jsonix.Model.ClassInfo({
name : 'Map<' + k.name + ',' + v.name + '>',
propertyInfos : [ k, v ]
});
},
unmarshal : function(context, input, scope) {
var result = null;
...
ElementInfo = function () { this.initialize.apply(this, arguments); }
n/a
ElementMapPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
ElementPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
ElementRefPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
ElementRefsPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
ElementsPropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
EnumLeafInfo = function () { this.initialize.apply(this, arguments); }
n/a
Module = function () { this.initialize.apply(this, arguments); }
n/a
PropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
SingleTypePropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
TypeInfo = function () { this.initialize.apply(this, arguments); }
n/a
ValuePropertyInfo = function () { this.initialize.apply(this, arguments); }
n/a
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } // if (Jsonix.Util.Type.exists(structure.elements[key])) // { // // TODO better exception // throw new Error("The structure already defines an element for // the key [" // + key + "]."); // } else // { // structure.elements[key] = this; // } if ((this.allowDom || this.allowTypedObject)) { structure.any = this; } if (this.mixed && !Jsonix.Util.Type.exists(this.wrapperElementName)) { // if (Jsonix.Util.Type.exists(structure.mixed)) { // // TODO better exception // throw new Error("The structure already defines the mixed // property."); // } else // { structure.mixed = this; // } } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElementTypeInfos = function (context, structure, elementTypeInfo) { structure.elements[elementTypeInfo.elementName.key] = this; var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName); if (Jsonix.Util.Type.isArray(substitutionMembers)) { for (var jndex = 0; jndex < substitutionMembers.length; jndex++) { var substitutionElementInfo = substitutionMembers[jndex]; this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo); } } }
...
},
buildStructureElementTypeInfos : function(context, structure, elementTypeInfo) {
structure.elements[elementTypeInfo.elementName.key] = this;
var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName);
if (Jsonix.Util.Type.isArray(substitutionMembers)) {
for (var jndex = 0; jndex < substitutionMembers.length; jndex++) {
var substitutionElementInfo = substitutionMembers[jndex];
this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo);
}
}
},
CLASS_NAME : 'Jsonix.Model.AbstractElementRefsPropertyInfo'
});
Jsonix.Model.ElementRefPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElementRefsPropertyInfo, {
...
buildStructureElements = function (context, structure) { throw new Error("Abstract method [buildStructureElements]."); }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (typedNamedValue, context, input, scope) { return { name : typedNamedValue.name, value : typedNamedValue.value }; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
convertToNamedValue = function (elementValue, context, output, scope) { var name; var value; if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) { name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context); value = Jsonix.Util.Type.exists(elementValue.value) ? elementValue.value : null; return { name : name, value : value }; } else { for ( var propertyName in elementValue) { if (elementValue.hasOwnProperty(propertyName)) { name = Jsonix.XML.QName.fromObjectOrString(propertyName, context); value = elementValue[propertyName]; return { name : name, value : value }; } } } throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue } or {myElementName:elementValue} structure."); }
...
return undefined;
}
}
});
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
...
convertToTypedNamedValue = function (value, context, output, scope) { Jsonix.Util.Ensure.ensureObject(value); var elementValue = this.convertToNamedValue(value, context, output, scope); return { name : elementValue.name, value : elementValue.value, typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope) }; }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
doBuild = function (context, module) { throw new Error("Abstract method [doBuild]."); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getPropertyElementTypeInfo = function (elementName, context) { throw new Error("Abstract method [getPropertyElementTypeInfo]."); }
...
} else {
throw new Error("Unsupported content type, only objects are supported.");
}
}
},
getTypeInfoByElementName : function(elementName, context, scope) {
var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context);
if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) {
return propertyElementTypeInfo.typeInfo;
} else {
var contextElementTypeInfo = context.getElementInfo(elementName, scope);
if (Jsonix.Util.Type.exists(contextElementTypeInfo)) {
return contextElementTypeInfo.typeInfo;
} else {
...
getTypeInfoByElementName = function (elementName, context, scope) { var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context); if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) { return propertyElementTypeInfo.typeInfo; } else { var contextElementTypeInfo = context.getElementInfo(elementName, scope); if (Jsonix.Util.Type.exists(contextElementTypeInfo)) { return contextElementTypeInfo.typeInfo; } else { return undefined; } } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping, 'Mapping must be an object.'); Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]); var wen = mapping.wrapperElementName || mapping.wen || undefined; if (Jsonix.Util.Type.isObject(wen)) { this.wrapperElementName = Jsonix.XML.QName.fromObject(wen); } else if (Jsonix.Util.Type.isString(wen)) { this.wrapperElementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, wen); } else { this.wrapperElementName = null; } var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true); var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true); var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true); this.allowDom = dom; this.allowTypedObject = typed; this.mixed = mx; }
n/a
marshal = function (value, context, output, scope) { if (Jsonix.Util.Type.exists(value)) { if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } if (!this.collection) { this.marshalItem(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); for (var index = 0; index < value.length; index++) { var item = value[index]; this.marshalItem(item, context, output, scope); } } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
marshalItem = function (value, context, output, scope) { if (Jsonix.Util.Type.isString(value)) { if (!this.mixed) { // TODO throw new Error("Property is not mixed, can't handle string values."); } else { output.writeCharacters(value); } } else if (this.allowDom && Jsonix.Util.Type.exists(value.nodeType)) { // DOM node output.writeNode(value); } else if (Jsonix.Util.Type.isObject(value)) { this.marshalElement(value, context, output, scope); } else { if (this.mixed) { throw new Error("Unsupported content type, either objects or strings are supported."); } else { throw new Error("Unsupported content type, only objects are supported."); } } }
...
if (Jsonix.Util.Type.exists(value)) {
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
output.writeStartElement(this.wrapperElementName);
}
if (!this.collection) {
this.marshalItem(value, context, output, scope);
} else {
Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.');
for (var index = 0; index < value.length; index++) {
var item = value[index];
this.marshalItem(item, context, output, scope);
}
}
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; var et = input.eventType; if (et === Jsonix.XML.Input.START_ELEMENT) { if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } } else if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) { // Skip whitespace } else { // TODO better exception throw new Error("Illegal state: unexpected event type [" + et + "]."); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElements = function (context, structure) { throw new Error("Abstract method [buildStructureElements]."); }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (elementValue, context, input, scope) { return elementValue.value; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
doBuild = function (context, module) { throw new Error("Abstract method [doBuild]."); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getTypeInfoByElementName = function (name, context, scope) { var elementInfo = context.getElementInfo(name, scope); if (Jsonix.Util.Type.exists(elementInfo)) { return elementInfo.typeInfo; } else { return undefined; } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]); var wen = mapping.wrapperElementName||mapping.wen||undefined; if (Jsonix.Util.Type.isObject(wen)) { this.wrapperElementName = Jsonix.XML.QName.fromObject(wen); } else if (Jsonix.Util.Type.isString(wen)) { this.wrapperElementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, wen); } else { this.wrapperElementName = null; } }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.exists(value)) { // Do nothing return; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } if (!this.collection) { this.marshalElement(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value); // TODO Exception if not array for ( var index = 0; index < value.length; index++) { var item = value[index]; // TODO Exception if item does not exist this.marshalElement(item, context, output, scope); } } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); // if (Jsonix.Util.Type.exists(structure.anyAttribute)) // { // // TODO better exception // throw new Error("The structure already defines an any attribute // property."); // } else // { structure.anyAttribute = this; // } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
convertFromAttributeName = function (attributeName, context, input, scope) { return attributeName.key; }
...
if (attributeCount === 0) {
return null;
} else {
var result = {};
for ( var index = 0; index < attributeCount; index++) {
var value = input.getAttributeValue(index);
if (Jsonix.Util.Type.isString(value)) {
var propertyName = this.convertFromAttributeName(input.getAttributeName(index),
context, input, scope);
result[propertyName] = value;
}
}
return result;
}
},
marshal : function(value, context, output, scope) {
...
convertToAttributeName = function (propertyName, context, output, scope) { return Jsonix.XML.QName.fromObjectOrString(propertyName, context); }
...
// Nothing to do
return;
}
for ( var propertyName in value) {
if (value.hasOwnProperty(propertyName)) {
var propertyValue = value[propertyName];
if (Jsonix.Util.Type.isString(propertyValue)) {
var attributeName = this.convertToAttributeName(propertyName, context, output, scope
);
output.writeAttribute(attributeName, propertyValue);
}
}
}
},
convertFromAttributeName : function(attributeName, context, input, scope) {
return attributeName.key;
...
doBuild = function (context, module) { // Nothing to do }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]); }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.isObject(value)) { // Nothing to do return; } for ( var propertyName in value) { if (value.hasOwnProperty(propertyName)) { var propertyValue = value[propertyName]; if (Jsonix.Util.Type.isString(propertyValue)) { var attributeName = this.convertToAttributeName(propertyName, context, output, scope); output.writeAttribute(attributeName, propertyValue); } } } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var attributeCount = input.getAttributeCount(); if (attributeCount === 0) { return null; } else { var result = {}; for ( var index = 0; index < attributeCount; index++) { var value = input.getAttributeValue(index); if (Jsonix.Util.Type.isString(value)) { var propertyName = this.convertFromAttributeName(input.getAttributeName(index), context, input, scope); result[propertyName] = value; } } return result; } }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if ((this.allowDom || this.allowTypedObject)) { // if (Jsonix.Util.Type.exists(structure.any)) { // // TODO better exception // throw new Error("The structure already defines the any // property."); // } else // { structure.any = this; // } } if (this.mixed) { // if (Jsonix.Util.Type.exists(structure.mixed)) { // // TODO better exception // throw new Error("The structure already defines the mixed // property."); // } else // { structure.mixed = this; // } } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
convertFromTypedNamedValue = function (typedNamedValue, context, input, scope) { return { name : typedNamedValue.name, value : typedNamedValue.value }; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
convertToNamedValue = function (elementValue, context, output, scope) { var name; var value; if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) { name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context); value = Jsonix.Util.Type.exists(elementValue.value) ? elementValue.value : null; return { name : name, value : value }; } else { for ( var propertyName in elementValue) { if (elementValue.hasOwnProperty(propertyName)) { name = Jsonix.XML.QName.fromObjectOrString(propertyName, context); value = elementValue[propertyName]; return { name : name, value : value }; } } } throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue } or {myElementName:elementValue} structure."); }
...
return undefined;
}
}
});
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
...
convertToTypedNamedValue = function (value, context, output, scope) { Jsonix.Util.Ensure.ensureObject(value); var elementValue = this.convertToNamedValue(value, context, output, scope); return { name : elementValue.name, value : elementValue.value, typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope) }; }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
doBuild = function (context, module) { // Nothing to do }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getTypeInfoByElementName = function (name, context, scope) { var elementInfo = context.getElementInfo(name, scope); if (Jsonix.Util.Type.exists(elementInfo)) { return elementInfo.typeInfo; } else { return undefined; } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]); var dom = Jsonix.Util.Type.defaultValue(mapping.allowDom, mapping.dom, true); var typed = Jsonix.Util.Type.defaultValue(mapping.allowTypedObject, mapping.typed, true); var mx = Jsonix.Util.Type.defaultValue(mapping.mixed, mapping.mx, true); this.allowDom = dom; this.allowTypedObject = typed; this.mixed = mx; }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.exists(value)) { return; } if (!this.collection) { this.marshalItem(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value); for (var index = 0; index < value.length; index++) { this.marshalItem(value[index], context, output, scope); } } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
marshalItem = function (value, context, output, scope) { if (this.mixed && Jsonix.Util.Type.isString(value)) { // Mixed output.writeCharacters(value); } else if (this.allowDom && Jsonix.Util.Type.exists(value.nodeType)) { // DOM node output.writeNode(value); } else { if (this.allowTypedObject) { this.marshalElement(value, context, output, scope); } } }
...
if (Jsonix.Util.Type.exists(value)) {
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
output.writeStartElement(this.wrapperElementName);
}
if (!this.collection) {
this.marshalItem(value, context, output, scope);
} else {
Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.');
for (var index = 0; index < value.length; index++) {
var item = value[index];
this.marshalItem(item, context, output, scope);
}
}
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; var et = input.eventType; if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (this.mixed && (et === Jsonix.XML.Input.SPACE)) { // Whitespace // return null; } else if (et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) { // return null; } else { // TODO better exception throw new Error("Illegal state: unexpected event type [" + et + "]."); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); Jsonix.Util.Ensure.ensureObject(structure.attributes); var key = this.attributeName.key; // if (Jsonix.Util.Type.exists(structure.attributes[key])) { // // TODO better exception // throw new Error("The structure already defines an attribute for the key // [" // + key + "]."); // } else // { structure.attributes[key] = this; // } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
doBuild = function (context, module) { this.typeInfo = context.resolveTypeInfo(this.typeInfo, module); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this, [ mapping ]); var an = mapping.attributeName||mapping.an||undefined; if (Jsonix.Util.Type.isObject(an)) { this.attributeName = Jsonix.XML.QName.fromObject(an); } else if (Jsonix.Util.Type.isString(an)) { this.attributeName = new Jsonix.XML.QName(this.defaultAttributeNamespaceURI, an); } else { this.attributeName = new Jsonix.XML.QName(this.defaultAttributeNamespaceURI, this.name); } }
n/a
marshal = function (value, context, output, scope) { if (Jsonix.Util.Type.exists(value)) { output.writeAttribute(this.attributeName, this.print(value, context, output, scope)); } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
parse = function (value, context, input, scope) { return this.typeInfo.parse(value, context, input, scope); }
...
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement
)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
} else {
throw new Error('Response does not have valid [responseXML] or [responseText].');
}
callback(result);
}, function(transport) {
throw new Error('Could not retrieve XML from URL [' + url + '].');
...
print = function (value, context, output, scope) { return this.typeInfo.reprint(value, context, output, scope); }
...
}
}
var typeInfo = actualTypeInfo || declaredTypeInfo;
if (typeInfo) {
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output
, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var attributeCount = input.getAttributeCount(); var result = null; for ( var index = 0; index < attributeCount; index++) { var attributeNameKey = input.getAttributeNameKey(index); if (this.attributeName.key === attributeNameKey) { var attributeValue = input.getAttributeValue(index); if (Jsonix.Util.Type.isString(attributeValue)) { result = this.unmarshalValue(attributeValue, context, input, scope); } } } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalValue = function (value, context, input, scope) { return this.parse(value, context, input, scope); }
...
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
if (this.isMarshallable(value, context, scope))
{
// TODO This must be reworked
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
...
a = function (mapping) { this.addDefaultNamespaces(mapping); return this.addProperty(new this.mappingStyle.attributePropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
...
6%�����1��_��;��=b�PK���z� � PK u��G 1 com/sun/xml/xsom/impl/SchemaSetImpl$AnyType.class�Xy|W��
d7��%���B����M"%��&�IP���!��.3�$���jP���z���Z�)��U��Z���z��,���d3�}����)�O�ͼ��~��z��ν�� V�| ���Ÿ�o �
o��� �x��O�;%������=���|�}<�8�����`�� >��<"�x �8 я�(Nq
��� �A��c��Ǹ��\�� >�'%<D%>�'�) �b ��$��?���9 �$|�!��Ƕk�hDѣU��p�H�C�ñШA/�p2��h��FEqS��=cI��2�
n�� @�9�U���� R���j:;5iq�\�P]� Q���kIDI��Z\�J
�z�2����DD��)��ߝIW�Aj���f����Pi��˾U�4�hP5���-@����K�XЌ��ԕ��PX]������MS�R�:�KE��� ����dԦ��}�A%S⃡0���g��n"��Ǎ��
v��,8CI�{�K�-�5S�Su�ɟsH{�2�X�ݒI��㊙� {�P�iZ���\2~��)�����F�UҬtR�Mz"�4.T��-=J3�@t��е$b15bj�8I7�X�f��6ӞM���<1˶�`��>�4�M�+1:v��<řF�M�4#������۪'���G��tj����w�Y,> SkN�ZG�1-�M��r���Pm�K�<��a~^���"��- �>:��iҌ���T�mzb��UP��,ב��֦�M�ST��u8i�1\l%m�p�w��P"j� �K1��#�<6���ar�b�5C#Ŗt��:a��'�ZKδ��Ɇ�J2�g3�|[*n�W��2��ҕ8�^�$�S��zk��ASO����M=
ٚ��d��QC��.
�@~�ը�����r�,�DK"���,�f�dq�
�X5��$?1鍄˦����1�����Y�Px�
6#'`���l�ue��-�xi�r�q��j�ց�M��`�"u%8)��Ҙ���G�@��SdD����u��`1���RbLt�r�hݻMn��Msԯ u�DJ��m��2MisV��d�D�<0d
�ɆPhddd��+�`��U�V��;;&:I��"��% _fX6}[�[)��r�WdԢ����U�M-S9�
�X�����uq1 ���*�����?ߖ���#��������Z%�PƏ�c ?��S�L���� ��q~EeS���2~��$�V��8��e�M2���)���N�u�-�O2�����+��:%�]�?�J?g�d�?%�
Kƿ�)�ȕ 2F�[�Uh�1�U�t�T;��vb]#���#R��V�V�\>{x*W��TtC�M6&")n��4� 4�� 3�DcOM.a(�!^�y)�mK�*u��.Ȱ<��;�]�#K���pߋ��vn�BA�L!*W��m��]-����j��7����э���m����S�s����<�G;�=�ny��o��;Yp�g�M� [���SG�U�>�(��)E�y��͡���oz�~��ݳ�}��z��skώ=����n\�OhV�i����Q^"l�ݝ��}�������ž�%����L���Q��g��݉������X�o�nD��eo �[6�vH1��Q���P^-��4s�zD�@J�����l�0*T�����\�n����2-�Ь)�T���dS&h��J,�Mʾ�'�
C��ә�j���J,G5
05(D�� �]JKyF�U��
��p9=�\B ��u'�j�++(+,<
_�Y�w����(: �ދ��,J�����B{��QH��p��i��磧��q��p�`��g�(c#X�F��]�5�&�6����42�g8��%K�Z��I3v
=��,�K��ELS4�ɡXA#_+(|8��B��/�:r�~n\��[\`|5�;`>r�Dn�������O�����BO���4r���*�3��ɷͥ���k�d6��fu{֨ݓF��]㉣''
;ǵ4ׁ�L�(������Es[<q����Js�<ql���Msa�8���a��[s�UW�z�g���ǎi8����/�c�����N������]�#�+
No���� ����Ldm&r�y=�L���B�ˡL��!r y5�� �j��;_V�\�
�cD���^�M�1�!�{��!�"5��D�3�"�#6�����a�d������q�*���~���Ӿ.FIk�Н3��f*+ פ����d���TZK>s���0BO�R9.�s,;!�
x3c;�"_���: DހWf��3�8s[Sx�_�W;�<G#5h;���1�����c.C��q�c8��P��$�wՏ��_��i�U�Hv�Q��՟�����?S7�e(��nD1��Z�� v��[Q
�nC#���6�;,�K� �����G9n�#��Z9S?p3n!��H�V�FZO�Ҟ��f�P�����{�F�F�m7m�8.e�6Yfv�;fVr��qX�H5�+���r�d .i(�(G}E[��Uٞx�����
x26;C�����$g܅�nr�=�c�eGp��k9a3��a6���(t��.{=��'w4�
...
aa = function (mapping) { this.addDefaultNamespaces(mapping); return this .addProperty(new this.mappingStyle.anyAttributePropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
addDefaultNamespaces = function (mapping) { if (Jsonix.Util.Type.isObject(mapping)) { if (!Jsonix.Util.Type .isString(mapping.defaultElementNamespaceURI)) { mapping.defaultElementNamespaceURI = this.defaultElementNamespaceURI; } if (!Jsonix.Util.Type .isString(mapping.defaultAttributeNamespaceURI)) { mapping.defaultAttributeNamespaceURI = this.defaultAttributeNamespaceURI; } } }
...
propertyInfoCreator.call(this, mapping);
} else {
throw new Error("Unknown property info type [" + type + "].");
}
}
},
aa : function(mapping) {
this.addDefaultNamespaces(mapping);
return this
.addProperty(new this.mappingStyle.anyAttributePropertyInfo(
mapping, {
mappingStyle : this.mappingStyle
}));
},
ae : function(mapping) {
...
addProperty = function (property) { this.properties.push(property); this.propertiesMap[property.name] = property; return this; }
...
ps : function() {
return this;
},
p : function(mapping) {
Jsonix.Util.Ensure.ensureObject(mapping);
// If mapping is an instance of the property class
if (mapping instanceof Jsonix.Model.PropertyInfo) {
this.addProperty(mapping);
}
// Else create it via generic mapping configuration
else {
mapping = Jsonix.Util.Type.cloneObject(mapping);
var type = mapping.type||mapping.t||'element';
// Locate the creator function
if (Jsonix.Util.Type
...
ae = function (mapping) { this.addDefaultNamespaces(mapping); return this .addProperty(new this.mappingStyle.anyElementPropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
b = function (baseTypeInfo) { Jsonix.Util.Ensure.ensureObject(baseTypeInfo); this.baseTypeInfo = baseTypeInfo; return this; }
n/a
build = function (context, module) { if (!this.built) { this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module); if (Jsonix.Util.Type.exists(this.baseTypeInfo)) { this.baseTypeInfo.build(context, module); } // Build properties in this context for ( var index = 0; index < this.properties.length; index++) { var propertyInfo = this.properties[index]; propertyInfo.build(context, module); } // Build the structure var structure = { elements : null, attributes : {}, anyAttribute : null, value : null, any : null }; this.buildStructure(context, structure); this.structure = structure; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { if (Jsonix.Util.Type.exists(this.baseTypeInfo)) { this.baseTypeInfo.buildStructure(context, structure); } for ( var index = 0; index < this.properties.length; index++) { var propertyInfo = this.properties[index]; propertyInfo.buildStructure(context, structure); } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
destroy = function () { }
n/a
e = function (mapping) { this.addDefaultNamespaces(mapping); return this.addProperty(new this.mappingStyle.elementPropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
em = function (mapping) { this.addDefaultNamespaces(mapping); return this .addProperty(new this.mappingStyle.elementMapPropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
er = function (mapping) { this.addDefaultNamespaces(mapping); return this .addProperty(new this.mappingStyle.elementRefPropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
ers = function (mapping) { this.addDefaultNamespaces(mapping); return this .addProperty(new this.mappingStyle.elementRefsPropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
es = function (mapping) { this.addDefaultNamespaces(mapping); return this.addProperty(new this.mappingStyle.elementsPropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
getPropertyInfoByName = function (name) { return this.propertiesMap[name]; }
n/a
initialize = function (mapping, options) { Jsonix.Model.TypeInfo.prototype.initialize.apply(this, []); Jsonix.Mapping.Styled.prototype.initialize.apply(this, [options]); Jsonix.Util.Ensure.ensureObject(mapping); var n = mapping.name||mapping.n||undefined; Jsonix.Util.Ensure.ensureString(n); this.name = n; var ln = mapping.localName||mapping.ln||null; this.localName = ln; var dens = mapping.defaultElementNamespaceURI||mapping.dens||mapping.targetNamespace||mapping.tns||''; this.defaultElementNamespaceURI = dens; var tns = mapping.targetNamespace||mapping.tns||mapping.defaultElementNamespaceURI||mapping.dens||this.defaultElementNamespaceURI ; this.targetNamespace = tns; var dans = mapping.defaultAttributeNamespaceURI||mapping.dans||''; this.defaultAttributeNamespaceURI = dans; var bti = mapping.baseTypeInfo||mapping.bti||null; this.baseTypeInfo = bti; var inF = mapping.instanceFactory||mapping.inF||undefined; if (Jsonix.Util.Type.exists(inF)) { // TODO: should we support instanceFactory as functions? // For the pure JSON configuration? Jsonix.Util.Ensure.ensureFunction(inF); this.instanceFactory = inF; } var tn = mapping.typeName||mapping.tn||undefined; if (Jsonix.Util.Type.exists(tn)) { if (Jsonix.Util.Type.isString(tn)) { this.typeName = new Jsonix.XML.QName(this.targetNamespace, tn); } else { this.typeName = Jsonix.XML.QName.fromObject(tn); } } else if (Jsonix.Util.Type.exists(ln)) { this.typeName = new Jsonix.XML.QName(tns, ln); } this.properties = []; this.propertiesMap = {}; var ps = mapping.propertyInfos||mapping.ps||[]; Jsonix.Util.Ensure.ensureArray(ps); for ( var index = 0; index < ps.length; index++) { this.p(ps[index]); } }
n/a
isBasedOn = function (typeInfo) { var currentTypeInfo = this; while (currentTypeInfo) { if (typeInfo === currentTypeInfo) { return true; } currentTypeInfo = currentTypeInfo.baseTypeInfo; } return false; }
...
if (actualTypeInfo && actualTypeInfo.typeName) {
for (var jndex = 0; jndex < this.elementTypeInfos.length; jndex++) {
var eti = this.elementTypeInfos[jndex];
var ti = eti.typeInfo;
// TODO Can be optimized
// Find an element type info which has a type info that is a
// supertype of the actual type info
if (actualTypeInfo.isBasedOn(ti)) {
var en = eti.elementName;
return {
name : en,
value : value,
typeInfo : ti
};
}
...
isInstance = function (value, context, scope) { if (this.instanceFactory) { return value instanceof this.instanceFactory; } else { return Jsonix.Util.Type.isObject(value) && Jsonix.Util.Type.isString(value.TYPE_NAME) && value.TYPE_NAME === this.name; } }
...
// TODO throw an error
throw new Error("The passed value [" + value + "] is not an object and there is no single suitable property
to marshal it.");
}
}
},
// Checks if the value is marshallable
isMarshallable : function(value, context, scope) {
return this.isInstance(value, context, scope) || (Jsonix.Util.Type.isObject(value
) && !Jsonix.Util.Type.isArray(value));
},
isInstance : function(value, context, scope) {
if (this.instanceFactory) {
return value instanceof this.instanceFactory;
}
else {
return Jsonix.Util.Type.isObject(value) && Jsonix.Util.Type.isString(value.TYPE_NAME) && value.TYPE_NAME
=== this.name;
...
isMarshallable = function (value, context, scope) { return this.isInstance(value, context, scope) || (Jsonix.Util.Type.isObject(value) && !Jsonix.Util.Type.isArray(value)); }
...
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
if (this.isMarshallable(value, context, scope))
{
// TODO This must be reworked
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.marshal(value, context, output);
}
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
...
marshal = function (value, context, output, scope) { if (this.isMarshallable(value, context, scope)) { // TODO This must be reworked if (Jsonix.Util.Type.exists(this.baseTypeInfo)) { this.baseTypeInfo.marshal(value, context, output); } for ( var index = 0; index < this.properties.length; index++) { var propertyInfo = this.properties[index]; var propertyValue = value[propertyInfo.name]; if (Jsonix.Util.Type.exists(propertyValue)) { propertyInfo.marshal(propertyValue, context, output, this); } } } else { // Otherwise if there is just one property, use this property to marshal if (this.structure.value) { var valuePropertyInfo = this.structure.value; valuePropertyInfo.marshal(value, context, output, this); } else if (this.properties.length === 1) { var singlePropertyInfo = this.properties[0]; singlePropertyInfo.marshal(value, context, output, this); } else { // TODO throw an error throw new Error("The passed value [" + value + "] is not an object and there is no single suitable property to marshal it."); } } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
p = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); // If mapping is an instance of the property class if (mapping instanceof Jsonix.Model.PropertyInfo) { this.addProperty(mapping); } // Else create it via generic mapping configuration else { mapping = Jsonix.Util.Type.cloneObject(mapping); var type = mapping.type||mapping.t||'element'; // Locate the creator function if (Jsonix.Util.Type .isFunction(this.propertyInfoCreators[type])) { var propertyInfoCreator = this.propertyInfoCreators[type]; // Call the creator function propertyInfoCreator.call(this, mapping); } else { throw new Error("Unknown property info type [" + type + "]."); } } }
...
}
this.properties = [];
this.propertiesMap = {};
var ps = mapping.propertyInfos||mapping.ps||[];
Jsonix.Util.Ensure.ensureArray(ps);
for ( var index = 0; index < ps.length; index++) {
this.p(ps[index]);
}
},
getPropertyInfoByName : function(name) {
return this.propertiesMap[name];
},
// Obsolete
destroy : function() {
...
ps = function () { return this; }
n/a
unmarshal = function (context, input) { this.build(context); var result; if (this.instanceFactory) { result = new this.instanceFactory(); } else { result = { TYPE_NAME : this.name }; } if (input.eventType !== 1) { throw new Error("Parser must be on START_ELEMENT to read a class info."); } // Read attributes if (Jsonix.Util.Type.exists(this.structure.attributes)) { var attributeCount = input.getAttributeCount(); if (attributeCount !== 0) { for ( var index = 0; index < attributeCount; index++) { var attributeNameKey = input .getAttributeNameKey(index); if (Jsonix.Util.Type .exists(this.structure.attributes[attributeNameKey])) { var attributeValue = input .getAttributeValue(index); if (Jsonix.Util.Type.isString(attributeValue)) { var attributePropertyInfo = this.structure.attributes[attributeNameKey]; this.unmarshalPropertyValue(context, input, attributePropertyInfo, result, attributeValue); } } } } } // Read any attribute if (Jsonix.Util.Type.exists(this.structure.anyAttribute)) { var propertyInfo = this.structure.anyAttribute; this .unmarshalProperty(context, input, propertyInfo, result); } // Read elements if (Jsonix.Util.Type.exists(this.structure.elements)) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { // New sub-element starts var elementNameKey = input.getNameKey(); if (Jsonix.Util.Type .exists(this.structure.elements[elementNameKey])) { var elementPropertyInfo = this.structure.elements[elementNameKey]; this.unmarshalProperty(context, input, elementPropertyInfo, result); } else if (Jsonix.Util.Type .exists(this.structure.any)) { // TODO Refactor var anyPropertyInfo = this.structure.any; this.unmarshalProperty(context, input, anyPropertyInfo, result); } else { // TODO optionally report a validation error that the element is not expected et = input.skipElement(); } } else if ((et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { if (Jsonix.Util.Type.exists(this.structure.mixed)) { // Characters and structure has a mixed property 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 ) { // Ignore } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } } else if (Jsonix.Util.Type.exists(this.structure.value)) { var valuePropertyInfo = this.structure.value; this.unmarshalProperty(context, input, valuePropertyInfo, result); } else { // Just skip everything input.nextTag(); } if (input.eventType !== 2) { throw new Error("Illegal state: must be END_ELEMENT."); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalProperty = function (context, input, propertyInfo, result) { var propertyValue = propertyInfo .unmarshal(context, input, this); propertyInfo.setProperty(result, propertyValue); }
...
}
}
}
// Read any attribute
if (Jsonix.Util.Type.exists(this.structure.anyAttribute)) {
var propertyInfo = this.structure.anyAttribute;
this
.unmarshalProperty(context, input, propertyInfo,
result);
}
// Read elements
if (Jsonix.Util.Type.exists(this.structure.elements)) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
...
unmarshalPropertyValue = function (context, input, propertyInfo, result, value) { var propertyValue = propertyInfo.unmarshalValue(value, context, input, this); propertyInfo.setProperty(result, propertyValue); }
...
.getAttributeNameKey(index);
if (Jsonix.Util.Type
.exists(this.structure.attributes[attributeNameKey])) {
var attributeValue = input
.getAttributeValue(index);
if (Jsonix.Util.Type.isString(attributeValue)) {
var attributePropertyInfo = this.structure.attributes[attributeNameKey];
this.unmarshalPropertyValue(context, input,
attributePropertyInfo, result,
attributeValue);
}
}
}
}
}
...
v = function (mapping) { this.addDefaultNamespaces(mapping); return this.addProperty(new this.mappingStyle.valuePropertyInfo( mapping, { mappingStyle : this.mappingStyle })); }
n/a
build = function (context, module) { // If element info is not yet built if (!this.built) { this.typeInfo = context.resolveTypeInfo(this.typeInfo, module); this.scope = context.resolveTypeInfo(this.scope, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); var dens = mapping.defaultElementNamespaceURI||mapping.dens||''; this.defaultElementNamespaceURI = dens; var en = mapping.elementName || mapping.en||undefined; if (Jsonix.Util.Type.isObject(en)) { this.elementName = Jsonix.XML.QName.fromObject(en); } else { Jsonix.Util.Ensure.ensureString(en); this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en); } var ti = mapping.typeInfo||mapping.ti||'String'; this.typeInfo = ti; var sh = mapping.substitutionHead||mapping.sh||null; this.substitutionHead = sh; var sc = mapping.scope||mapping.sc||null; this.scope = sc; }
n/a
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElements = function (context, structure) { structure.elements[this.elementName.key] = this; }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (elementValue, context, input, scope) { var entry = elementValue.value; var result = {}; if (Jsonix.Util.Type.isString(entry[this.key.name])) { result[entry[this.key.name]] = entry[this.value.name]; } return result; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
doBuild = function (context, module) { this.entryTypeInfo.build(context, module); // TODO get property by name this.key = this.entryTypeInfo.properties[0]; this.value = this.entryTypeInfo.properties[1]; }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getTypeInfoByElementName = function (name, context, scope) { var elementInfo = context.getElementInfo(name, scope); if (Jsonix.Util.Type.exists(elementInfo)) { return elementInfo.typeInfo; } else { return undefined; } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { return this.entryTypeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.AbstractElementsPropertyInfo.prototype.initialize.apply(this, [ mapping ]); // TODO Ensure correct argument var k = mapping.key || mapping.k || undefined; Jsonix.Util.Ensure.ensureObject(k); var v = mapping.value || mapping.v || undefined; Jsonix.Util.Ensure.ensureObject(v); // TODO Ensure correct argument var en = mapping.elementName || mapping.en || undefined; if (Jsonix.Util.Type.isObject(en)) { this.elementName = Jsonix.XML.QName.fromObject(en); } else if (Jsonix.Util.Type.isString(en)) { this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en); } else { this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name); } this.entryTypeInfo = new Jsonix.Model.ClassInfo({ name : 'Map<' + k.name + ',' + v.name + '>', propertyInfos : [ k, v ] }); }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.exists(value)) { // Do nothing return; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } this.marshalElement(value, context, output, scope); if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { if (!!value) { for ( var attributeName in value) { if (value.hasOwnProperty(attributeName)) { var attributeValue = value[attributeName]; if (!this.collection) { var singleEntry = {}; singleEntry[this.key.name] = attributeName; singleEntry[this.value.name] = attributeValue; output.writeStartElement(this.elementName); this.entryTypeInfo.marshal(singleEntry, context, output, scope); output.writeEndElement(); } else { for (var index = 0; index < attributeValue.length; index++) { var collectionEntry = {}; collectionEntry[this.key.name] = attributeName; collectionEntry[this.value.name] = attributeValue[index]; output.writeStartElement(this.elementName); this.entryTypeInfo.marshal(collectionEntry, context, output, scope); output.writeEndElement(); } } } } } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { Jsonix.Util.Ensure.ensureObject(value, 'Map property requires an object.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = {}; } var map = object[this.name]; for ( var attributeName in value) { if (value.hasOwnProperty(attributeName)) { var attributeValue = value[attributeName]; if (this.collection) { if (!Jsonix.Util.Type.exists(map[attributeName])) { map[attributeName] = []; } for (var index = 0; index < attributeValue.length; index++) { map[attributeName].push(attributeValue[index]); } } else { map[attributeName] = attributeValue; } } } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (Jsonix.Util.Type.exists(value)) { Jsonix.Util.Ensure.ensureObject(value, 'Map property requires an object.'); if (!Jsonix.Util.Type.exists(result)) { result = {}; } for ( var attributeName in value) { if (value.hasOwnProperty(attributeName)) { var attributeValue = value[attributeName]; if (that.collection) { if (!Jsonix.Util.Type.exists(result[attributeName])) { result[attributeName] = []; } result[attributeName].push(attributeValue); } else { if (!Jsonix.Util.Type.exists(result[attributeName])) { result[attributeName] = attributeValue; } else { // TODO Report validation error throw new Error("Value was already set."); } } } } } }; if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElements = function (context, structure) { structure.elements[this.elementName.key] = this; }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (elementValue, context, input, scope) { return elementValue.value; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
convertToTypedNamedValue = function (value, context, output, scope) { return { name : this.elementName, value : value, typeInfo : this.typeInfo }; }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
doBuild = function (context, module) { this.typeInfo = context.resolveTypeInfo(this.typeInfo, module); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getTypeInfoByElementName = function (elementName, context, scope) { return this.typeInfo; }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.AbstractElementsPropertyInfo.prototype.initialize.apply(this, [ mapping ]); var ti = mapping.typeInfo || mapping.ti || 'String'; if (Jsonix.Util.Type.isObject(ti)) { this.typeInfo = ti; } else { Jsonix.Util.Ensure.ensureString(ti); this.typeInfo = ti; } var en = mapping.elementName || mapping.en || undefined; if (Jsonix.Util.Type.isObject(en)) { this.elementName = Jsonix.XML.QName.fromObject(en); } else if (Jsonix.Util.Type.isString(en)) { this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en); } else { this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name); } }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.exists(value)) { // Do nothing return; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } if (!this.collection) { this.marshalElement(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value); // TODO Exception if not array for ( var index = 0; index < value.length; index++) { var item = value[index]; // TODO Exception if item does not exist this.marshalElement(item, context, output, scope); } } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } // if (Jsonix.Util.Type.exists(structure.elements[key])) // { // // TODO better exception // throw new Error("The structure already defines an element for // the key [" // + key + "]."); // } else // { // structure.elements[key] = this; // } if ((this.allowDom || this.allowTypedObject)) { structure.any = this; } if (this.mixed && !Jsonix.Util.Type.exists(this.wrapperElementName)) { // if (Jsonix.Util.Type.exists(structure.mixed)) { // // TODO better exception // throw new Error("The structure already defines the mixed // property."); // } else // { structure.mixed = this; // } } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElementTypeInfos = function (context, structure, elementTypeInfo) { structure.elements[elementTypeInfo.elementName.key] = this; var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName); if (Jsonix.Util.Type.isArray(substitutionMembers)) { for (var jndex = 0; jndex < substitutionMembers.length; jndex++) { var substitutionElementInfo = substitutionMembers[jndex]; this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo); } } }
...
},
buildStructureElementTypeInfos : function(context, structure, elementTypeInfo) {
structure.elements[elementTypeInfo.elementName.key] = this;
var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName);
if (Jsonix.Util.Type.isArray(substitutionMembers)) {
for (var jndex = 0; jndex < substitutionMembers.length; jndex++) {
var substitutionElementInfo = substitutionMembers[jndex];
this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo);
}
}
},
CLASS_NAME : 'Jsonix.Model.AbstractElementRefsPropertyInfo'
});
Jsonix.Model.ElementRefPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElementRefsPropertyInfo, {
...
buildStructureElements = function (context, structure) { this.buildStructureElementTypeInfos(context, structure, this); }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (typedNamedValue, context, input, scope) { return { name : typedNamedValue.name, value : typedNamedValue.value }; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
convertToNamedValue = function (elementValue, context, output, scope) { var name; var value; if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) { name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context); value = Jsonix.Util.Type.exists(elementValue.value) ? elementValue.value : null; return { name : name, value : value }; } else { for ( var propertyName in elementValue) { if (elementValue.hasOwnProperty(propertyName)) { name = Jsonix.XML.QName.fromObjectOrString(propertyName, context); value = elementValue[propertyName]; return { name : name, value : value }; } } } throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue } or {myElementName:elementValue} structure."); }
...
return undefined;
}
}
});
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
...
convertToTypedNamedValue = function (value, context, output, scope) { Jsonix.Util.Ensure.ensureObject(value); var elementValue = this.convertToNamedValue(value, context, output, scope); return { name : elementValue.name, value : elementValue.value, typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope) }; }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
doBuild = function (context, module) { this.typeInfo = context.resolveTypeInfo(this.typeInfo, module); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getPropertyElementTypeInfo = function (elementName, context) { var name = Jsonix.XML.QName.fromObjectOrString(elementName, context); if (name.key === this.elementName.key) { return this; } else { return null; } }
...
} else {
throw new Error("Unsupported content type, only objects are supported.");
}
}
},
getTypeInfoByElementName : function(elementName, context, scope) {
var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context);
if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) {
return propertyElementTypeInfo.typeInfo;
} else {
var contextElementTypeInfo = context.getElementInfo(elementName, scope);
if (Jsonix.Util.Type.exists(contextElementTypeInfo)) {
return contextElementTypeInfo.typeInfo;
} else {
...
getTypeInfoByElementName = function (elementName, context, scope) { var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context); if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) { return propertyElementTypeInfo.typeInfo; } else { var contextElementTypeInfo = context.getElementInfo(elementName, scope); if (Jsonix.Util.Type.exists(contextElementTypeInfo)) { return contextElementTypeInfo.typeInfo; } else { return undefined; } } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.AbstractElementRefsPropertyInfo.prototype.initialize.apply(this, [ mapping ]); // TODO Ensure correct argument var ti = mapping.typeInfo || mapping.ti || 'String'; if (Jsonix.Util.Type.isObject(ti)) { this.typeInfo = ti; } else { Jsonix.Util.Ensure.ensureString(ti); this.typeInfo = ti; } var en = mapping.elementName || mapping.en || undefined; if (Jsonix.Util.Type.isObject(en)) { this.elementName = Jsonix.XML.QName.fromObject(en); } else if (Jsonix.Util.Type.isString(en)) { this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en); } else { this.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, this.name); } }
n/a
marshal = function (value, context, output, scope) { if (Jsonix.Util.Type.exists(value)) { if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } if (!this.collection) { this.marshalItem(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); for (var index = 0; index < value.length; index++) { var item = value[index]; this.marshalItem(item, context, output, scope); } } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
marshalItem = function (value, context, output, scope) { if (Jsonix.Util.Type.isString(value)) { if (!this.mixed) { // TODO throw new Error("Property is not mixed, can't handle string values."); } else { output.writeCharacters(value); } } else if (this.allowDom && Jsonix.Util.Type.exists(value.nodeType)) { // DOM node output.writeNode(value); } else if (Jsonix.Util.Type.isObject(value)) { this.marshalElement(value, context, output, scope); } else { if (this.mixed) { throw new Error("Unsupported content type, either objects or strings are supported."); } else { throw new Error("Unsupported content type, only objects are supported."); } } }
...
if (Jsonix.Util.Type.exists(value)) {
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
output.writeStartElement(this.wrapperElementName);
}
if (!this.collection) {
this.marshalItem(value, context, output, scope);
} else {
Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.');
for (var index = 0; index < value.length; index++) {
var item = value[index];
this.marshalItem(item, context, output, scope);
}
}
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; var et = input.eventType; if (et === Jsonix.XML.Input.START_ELEMENT) { if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } } else if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) { // Skip whitespace } else { // TODO better exception throw new Error("Illegal state: unexpected event type [" + et + "]."); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } // if (Jsonix.Util.Type.exists(structure.elements[key])) // { // // TODO better exception // throw new Error("The structure already defines an element for // the key [" // + key + "]."); // } else // { // structure.elements[key] = this; // } if ((this.allowDom || this.allowTypedObject)) { structure.any = this; } if (this.mixed && !Jsonix.Util.Type.exists(this.wrapperElementName)) { // if (Jsonix.Util.Type.exists(structure.mixed)) { // // TODO better exception // throw new Error("The structure already defines the mixed // property."); // } else // { structure.mixed = this; // } } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElementTypeInfos = function (context, structure, elementTypeInfo) { structure.elements[elementTypeInfo.elementName.key] = this; var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName); if (Jsonix.Util.Type.isArray(substitutionMembers)) { for (var jndex = 0; jndex < substitutionMembers.length; jndex++) { var substitutionElementInfo = substitutionMembers[jndex]; this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo); } } }
...
},
buildStructureElementTypeInfos : function(context, structure, elementTypeInfo) {
structure.elements[elementTypeInfo.elementName.key] = this;
var substitutionMembers = context.getSubstitutionMembers(elementTypeInfo.elementName);
if (Jsonix.Util.Type.isArray(substitutionMembers)) {
for (var jndex = 0; jndex < substitutionMembers.length; jndex++) {
var substitutionElementInfo = substitutionMembers[jndex];
this.buildStructureElementTypeInfos(context, structure, substitutionElementInfo);
}
}
},
CLASS_NAME : 'Jsonix.Model.AbstractElementRefsPropertyInfo'
});
Jsonix.Model.ElementRefPropertyInfo = Jsonix.Class(Jsonix.Model.AbstractElementRefsPropertyInfo, {
...
buildStructureElements = function (context, structure) { for (var index = 0; index < this.elementTypeInfos.length; index++) { var elementTypeInfo = this.elementTypeInfos[index]; this.buildStructureElementTypeInfos(context, structure, elementTypeInfo); } }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (typedNamedValue, context, input, scope) { return { name : typedNamedValue.name, value : typedNamedValue.value }; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
convertToNamedValue = function (elementValue, context, output, scope) { var name; var value; if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) { name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context); value = Jsonix.Util.Type.exists(elementValue.value) ? elementValue.value : null; return { name : name, value : value }; } else { for ( var propertyName in elementValue) { if (elementValue.hasOwnProperty(propertyName)) { name = Jsonix.XML.QName.fromObjectOrString(propertyName, context); value = elementValue[propertyName]; return { name : name, value : value }; } } } throw new Error("Invalid element value [" + elementValue + "]. Element values must either have {name:'myElementName', value: elementValue } or {myElementName:elementValue} structure."); }
...
return undefined;
}
}
});
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
...
convertToTypedNamedValue = function (value, context, output, scope) { Jsonix.Util.Ensure.ensureObject(value); var elementValue = this.convertToNamedValue(value, context, output, scope); return { name : elementValue.name, value : elementValue.value, typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope) }; }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
doBuild = function (context, module) { this.elementTypeInfosMap = {}; var etiti, etien; for (var index = 0; index < this.elementTypeInfos.length; index++) { var elementTypeInfo = this.elementTypeInfos[index]; Jsonix.Util.Ensure.ensureObject(elementTypeInfo); etiti = elementTypeInfo.typeInfo || elementTypeInfo.ti || 'String'; elementTypeInfo.typeInfo = context.resolveTypeInfo(etiti, module); etien = elementTypeInfo.elementName || elementTypeInfo.en || undefined; elementTypeInfo.elementName = Jsonix.XML.QName.fromObjectOrString(etien, context, this.defaultElementNamespaceURI); this.elementTypeInfosMap[elementTypeInfo.elementName.key] = elementTypeInfo.typeInfo; } }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getPropertyElementTypeInfo = function (elementName, context) { var name = Jsonix.XML.QName.fromObjectOrString(elementName, context); var typeInfo = this.elementTypeInfosMap[name.key]; if (Jsonix.Util.Type.exists(typeInfo)) { return { elementName : name, typeInfo : typeInfo }; } else { return null; } }
...
} else {
throw new Error("Unsupported content type, only objects are supported.");
}
}
},
getTypeInfoByElementName : function(elementName, context, scope) {
var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context);
if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) {
return propertyElementTypeInfo.typeInfo;
} else {
var contextElementTypeInfo = context.getElementInfo(elementName, scope);
if (Jsonix.Util.Type.exists(contextElementTypeInfo)) {
return contextElementTypeInfo.typeInfo;
} else {
...
getTypeInfoByElementName = function (elementName, context, scope) { var propertyElementTypeInfo = this.getPropertyElementTypeInfo(elementName, context); if (Jsonix.Util.Type.exists(propertyElementTypeInfo)) { return propertyElementTypeInfo.typeInfo; } else { var contextElementTypeInfo = context.getElementInfo(elementName, scope); if (Jsonix.Util.Type.exists(contextElementTypeInfo)) { return contextElementTypeInfo.typeInfo; } else { return undefined; } } }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.AbstractElementRefsPropertyInfo.prototype.initialize.apply(this, [ mapping ]); // TODO Ensure correct arguments var etis = mapping.elementTypeInfos || mapping.etis || []; Jsonix.Util.Ensure.ensureArray(etis); this.elementTypeInfos = []; for (var index = 0; index < etis.length; index++) { this.elementTypeInfos[index] = Jsonix.Util.Type.cloneObject(etis[index]); } }
n/a
marshal = function (value, context, output, scope) { if (Jsonix.Util.Type.exists(value)) { if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } if (!this.collection) { this.marshalItem(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); for (var index = 0; index < value.length; index++) { var item = value[index]; this.marshalItem(item, context, output, scope); } } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
marshalItem = function (value, context, output, scope) { if (Jsonix.Util.Type.isString(value)) { if (!this.mixed) { // TODO throw new Error("Property is not mixed, can't handle string values."); } else { output.writeCharacters(value); } } else if (this.allowDom && Jsonix.Util.Type.exists(value.nodeType)) { // DOM node output.writeNode(value); } else if (Jsonix.Util.Type.isObject(value)) { this.marshalElement(value, context, output, scope); } else { if (this.mixed) { throw new Error("Unsupported content type, either objects or strings are supported."); } else { throw new Error("Unsupported content type, only objects are supported."); } } }
...
if (Jsonix.Util.Type.exists(value)) {
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
output.writeStartElement(this.wrapperElementName);
}
if (!this.collection) {
this.marshalItem(value, context, output, scope);
} else {
Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.');
for (var index = 0; index < value.length; index++) {
var item = value[index];
this.marshalItem(item, context, output, scope);
}
}
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; var et = input.eventType; if (et === Jsonix.XML.Input.START_ELEMENT) { if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } } else if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION) { // Skip whitespace } else { // TODO better exception throw new Error("Illegal state: unexpected event type [" + et + "]."); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); if (Jsonix.Util.Type.exists(structure.value)) { // TODO better exception throw new Error("The structure already defines a value property."); } else if (!Jsonix.Util.Type.exists(structure.elements)) { structure.elements = {}; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { structure.elements[this.wrapperElementName.key] = this; } else { this.buildStructureElements(context, structure); } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
buildStructureElements = function (context, structure) { for (var index = 0; index < this.elementTypeInfos.length; index++) { var elementTypeInfo = this.elementTypeInfos[index]; structure.elements[elementTypeInfo.elementName.key] = this; } }
...
} else if (!Jsonix.Util.Type.exists(structure.elements)) {
structure.elements = {};
}
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
structure.elements[this.wrapperElementName.key] = this;
} else {
this.buildStructureElements(context, structure);
}
},
buildStructureElements : function(context, structure) {
throw new Error("Abstract method [buildStructureElements].");
},
CLASS_NAME : 'Jsonix.Model.AbstractElementsPropertyInfo'
});
...
convertFromTypedNamedValue = function (elementValue, context, input, scope) { return elementValue.value; }
...
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope
);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
...
convertToTypedNamedValue = function (value, context, output, scope) { for (var index = 0; index < this.elementTypeInfos.length; index++) { var elementTypeInfo = this.elementTypeInfos[index]; var typeInfo = elementTypeInfo.typeInfo; if (typeInfo.isInstance(value, context, scope)) { var elementName = elementTypeInfo.elementName; return { name : elementName, value : value, typeInfo : typeInfo }; } } // If xsi:type is supported if (context.supportXsiType) { // Find the actual type var actualTypeInfo = context.getTypeInfoByValue(value); if (actualTypeInfo && actualTypeInfo.typeName) { for (var jndex = 0; jndex < this.elementTypeInfos.length; jndex++) { var eti = this.elementTypeInfos[jndex]; var ti = eti.typeInfo; // TODO Can be optimized // Find an element type info which has a type info that is a // supertype of the actual type info if (actualTypeInfo.isBasedOn(ti)) { var en = eti.elementName; return { name : en, value : value, typeInfo : ti }; } } } } // TODO harmonize error handling. See also marshallElement. Error must // only be on one place. throw new Error("Could not find an element with type info supporting the value [" + value + "]."); }
...
});
Jsonix.Binding = {};
Jsonix.Binding.Marshalls = {
};
Jsonix.Binding.Marshalls.Element = Jsonix.Class({
marshalElement : function(value, context, output, scope) {
var elementValue = this.convertToTypedNamedValue(value, context, output, scope);
var declaredTypeInfo = elementValue.typeInfo;
var actualTypeInfo = undefined;
if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value))
{
var typeInfoByValue = context.getTypeInfoByValue(elementValue.value);
if (typeInfoByValue && typeInfoByValue.typeName)
{
...
doBuild = function (context, module) { this.elementTypeInfosMap = {}; var etiti, etien; for (var index = 0; index < this.elementTypeInfos.length; index++) { var elementTypeInfo = this.elementTypeInfos[index]; Jsonix.Util.Ensure.ensureObject(elementTypeInfo); etiti = elementTypeInfo.typeInfo || elementTypeInfo.ti || 'String'; elementTypeInfo.typeInfo = context.resolveTypeInfo(etiti, module); etien = elementTypeInfo.elementName || elementTypeInfo.en || undefined; elementTypeInfo.elementName = Jsonix.XML.QName.fromObjectOrString(etien, context, this.defaultElementNamespaceURI); this.elementTypeInfosMap[elementTypeInfo.elementName.key] = elementTypeInfo.typeInfo; } }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
getTypeInfoByElementName = function (elementName, context, scope) { return this.elementTypeInfosMap[elementName.key]; }
...
Jsonix.Binding.Marshalls.Element.AsElementRef = Jsonix.Class({
convertToTypedNamedValue : function(value, context, output, scope) {
Jsonix.Util.Ensure.ensureObject(value);
var elementValue = this.convertToNamedValue(value, context, output, scope);
return {
name : elementValue.name,
value : elementValue.value,
typeInfo : this.getTypeInfoByElementName(elementValue.name, context, scope)
};
},
convertToNamedValue : function(elementValue, context, output, scope) {
var name;
var value;
if (Jsonix.Util.Type.exists(elementValue.name) && !Jsonix.Util.Type.isUndefined(elementValue.value)) {
name = Jsonix.XML.QName.fromObjectOrString(elementValue.name, context);
...
getTypeInfoByInputElement = function (context, input, scope) { var xsiTypeInfo = null; if (context.supportXsiType) { var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema.XSI.TYPE); if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) { var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope); xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key); } } var name = input.getName(); var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope); return typeInfo; }
...
Jsonix.Binding.Unmarshalls.Element = Jsonix.Class({
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.AbstractElementsPropertyInfo.prototype.initialize.apply(this, [ mapping ]); var etis = mapping.elementTypeInfos || mapping.etis || []; Jsonix.Util.Ensure.ensureArray(etis); this.elementTypeInfos = []; for (var index = 0; index < etis.length; index++) { this.elementTypeInfos[index] = Jsonix.Util.Type.cloneObject(etis[index]); } }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.exists(value)) { // Do nothing return; } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeStartElement(this.wrapperElementName); } if (!this.collection) { this.marshalElement(value, context, output, scope); } else { Jsonix.Util.Ensure.ensureArray(value); // TODO Exception if not array for ( var index = 0; index < value.length; index++) { var item = value[index]; // TODO Exception if item does not exist this.marshalElement(item, context, output, scope); } } if (Jsonix.Util.Type.exists(this.wrapperElementName)) { output.writeEndElement(); } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
marshalElement = function (value, context, output, scope) { var elementValue = this.convertToTypedNamedValue(value, context, output, scope); var declaredTypeInfo = elementValue.typeInfo; var actualTypeInfo = undefined; if (context.supportXsiType && Jsonix.Util.Type.exists(elementValue.value)) { var typeInfoByValue = context.getTypeInfoByValue(elementValue.value); if (typeInfoByValue && typeInfoByValue.typeName) { actualTypeInfo = typeInfoByValue; } } var typeInfo = actualTypeInfo || declaredTypeInfo; if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { var xsiTypeName = actualTypeInfo.typeName; var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope); output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType); } if (Jsonix.Util.Type.exists(elementValue.value)) { typeInfo.marshal(elementValue.value, context, output, scope); } output.writeEndElement(); } else { throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its type."); } }
...
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var result = null; var that = this; var callback = function(value) { if (that.collection) { if (result === null) { result = []; } result.push(value); } else { if (result === null) { result = value; } else { // TODO Report validation error throw new Error("Value already set."); } } }; if (Jsonix.Util.Type.exists(this.wrapperElementName)) { this.unmarshalWrapperElement(context, input, scope, callback); } else { this.unmarshalElement(context, input, scope, callback); } return result; }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalElement = function (context, input, scope, callback) { if (input.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next element."); } var typeInfo = this.getTypeInfoByInputElement(context, input, scope); var name = input.getName(); var elementValue; if (this.allowTypedObject) { if (Jsonix.Util.Type.exists(typeInfo)) { var value = typeInfo.unmarshal(context, input, scope); var typedNamedValue = { name : name, value : value, typeInfo : typeInfo }; elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope); } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context and the property does not allow DOM content."); } } else if (this.allowDom) { elementValue = input.getElement(); } else { throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at the same time ."); } callback(elementValue); }
...
Jsonix.Binding.Unmarshalls.WrapperElement = Jsonix.Class({
mixed : false,
unmarshalWrapperElement : function(context, input, scope, callback) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
this.unmarshalElement(context, input, scope, callback);
} else
// Characters
if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
callback(input.getText());
} else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION
) {
// Skip whitespace
} else {
...
unmarshalWrapperElement = function (context, input, scope, callback) { var et = input.next(); while (et !== Jsonix.XML.Input.END_ELEMENT) { if (et === Jsonix.XML.Input.START_ELEMENT) { this.unmarshalElement(context, input, scope, callback); } else // Characters if (this.mixed && (et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE )) { callback(input.getText()); } else if (et === Jsonix.XML.Input.SPACE || et === Jsonix.XML.Input.COMMENT || et === Jsonix.XML.Input.PROCESSING_INSTRUCTION ) { // Skip whitespace } else { throw new Error("Illegal state: unexpected event type [" + et + "]."); } et = input.next(); } }
...
// TODO Report validation error
throw new Error("Value already set.");
}
}
};
if (Jsonix.Util.Type.exists(this.wrapperElementName)) {
this.unmarshalWrapperElement(context, input, scope, callback);
} else {
this.unmarshalElement(context, input, scope, callback);
}
return result;
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module); this.baseTypeInfo.build(context, module); var items = this.entries; var entries = {}; var keys = []; var values = []; var index = 0; var key; var value; // If values is an array, process individual items if (Jsonix.Util.Type.isArray(items)) { // Build properties in this context for (index = 0; index < items.length; index++) { value = items[index]; if (Jsonix.Util.Type.isString(value)) { key = value; if (!(Jsonix.Util.Type.isFunction(this.baseTypeInfo.parse))) { throw new Error('Enum value is provided as string but the base type ['+this.baseTypeInfo.name+'] of the enum info [' + this .name + '] does not implement the parse method.'); } // Using null as input since input is not available value = this.baseTypeInfo.parse(value, context, null, this); } else { if (this.baseTypeInfo.isInstance(value, context, this)) { if (!(Jsonix.Util.Type.isFunction(this.baseTypeInfo.print))) { throw new Error('The base type ['+this.baseTypeInfo.name+'] of the enum info [' + this.name + '] does not implement the print method, unable to produce the enum key as string.'); } // Using null as output since output is not available at this moment key = this.baseTypeInfo.print(value, context, null, this); } else { throw new Error('Enum value [' + value + '] is not an instance of the enum base type [' + this.baseTypeInfo.name + '].'); } } entries[key] = value; keys[index] = key; values[index] = value; } } else if (Jsonix.Util.Type.isObject(items)) { for (key in items) { if (items.hasOwnProperty(key)) { value = items[key]; if (Jsonix.Util.Type.isString(value)) { if (!(Jsonix.Util.Type.isFunction(this.baseTypeInfo.parse))) { throw new Error('Enum value is provided as string but the base type ['+this.baseTypeInfo.name+'] of the enum info [' + this .name + '] does not implement the parse method.'); } // Using null as input since input is not available value = this.baseTypeInfo.parse(value, context, null, this); } else { if (!this.baseTypeInfo.isInstance(value, context, this)) { throw new Error('Enum value [' + value + '] is not an instance of the enum base type [' + this.baseTypeInfo.name + '].'); } } entries[key] = value; keys[index] = key; values[index] = value; index++; } } } else { throw new Error('Enum values must be either an array or an object.'); } this.entries = entries; this.keys = keys; this.values = values; this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
initialize = function (mapping) { Jsonix.Model.TypeInfo.prototype.initialize.apply(this, []); Jsonix.Util.Ensure.ensureObject(mapping); var n = mapping.name||mapping.n||undefined; Jsonix.Util.Ensure.ensureString(n); this.name = n; var bti = mapping.baseTypeInfo||mapping.bti||'String'; this.baseTypeInfo = bti; var vs = mapping.values||mapping.vs||undefined; Jsonix.Util.Ensure.ensureExists(vs); if (!(Jsonix.Util.Type.isObject(vs) || Jsonix.Util.Type.isArray(vs))) { throw new Error('Enum values must be either an array or an object.'); } else { this.entries = vs; } }
n/a
isBasedOn = function (typeInfo) { var currentTypeInfo = this; while (currentTypeInfo) { if (typeInfo === currentTypeInfo) { return true; } currentTypeInfo = currentTypeInfo.baseTypeInfo; } return false; }
...
if (actualTypeInfo && actualTypeInfo.typeName) {
for (var jndex = 0; jndex < this.elementTypeInfos.length; jndex++) {
var eti = this.elementTypeInfos[jndex];
var ti = eti.typeInfo;
// TODO Can be optimized
// Find an element type info which has a type info that is a
// supertype of the actual type info
if (actualTypeInfo.isBasedOn(ti)) {
var en = eti.elementName;
return {
name : en,
value : value,
typeInfo : ti
};
}
...
isInstance = function (value, context, scope) { for (var index = 0; index < this.values.length; index++) { if (this.values[index] === value) { return true; } } return false; }
...
// TODO throw an error
throw new Error("The passed value [" + value + "] is not an object and there is no single suitable property
to marshal it.");
}
}
},
// Checks if the value is marshallable
isMarshallable : function(value, context, scope) {
return this.isInstance(value, context, scope) || (Jsonix.Util.Type.isObject(value
) && !Jsonix.Util.Type.isArray(value));
},
isInstance : function(value, context, scope) {
if (this.instanceFactory) {
return value instanceof this.instanceFactory;
}
else {
return Jsonix.Util.Type.isObject(value) && Jsonix.Util.Type.isString(value.TYPE_NAME) && value.TYPE_NAME
=== this.name;
...
marshal = function (value, context, output, scope) { if (Jsonix.Util.Type.exists(value)) { output.writeCharacters(this.reprint(value, context, output, scope)); } }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
parse = function (text, context, input, scope) { Jsonix.Util.Ensure.ensureString(text); if (this.entries.hasOwnProperty(text)) { return this.entries[text]; } else { throw new Error('Value [' + text + '] is invalid for the enum type [' + this.name + '].'); } }
...
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement
)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
} else {
throw new Error('Response does not have valid [responseXML] or [responseText].');
}
callback(result);
}, function(transport) {
throw new Error('Could not retrieve XML from URL [' + url + '].');
...
print = function (value, context, output, scope) { for (var index = 0; index < this.values.length; index++) { if (this.values[index] === value) { return this.keys[index]; } } throw new Error('Value [' + value + '] is invalid for the enum type [' + this.name + '].'); }
...
}
}
var typeInfo = actualTypeInfo || declaredTypeInfo;
if (typeInfo) {
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output
, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
...
reprint = function (value, context, output, scope) { if (Jsonix.Util.Type.isString(value) && !this.isInstance(value, context, scope)) { // Using null as input since input is not available return this.print(this.parse(value, context, null, scope), context, output, scope); } else { return this.print(value, context, output, scope); } }
...
},
unmarshal : function(context, input, scope) {
var text = input.getElementText();
return this.parse(text, context, input, scope);
},
marshal : function(value, context, output, scope) {
if (Jsonix.Util.Type.exists(value)) {
output.writeCharacters(this.reprint(value, context, output, scope));
}
},
reprint : function(value, context, output, scope) {
if (Jsonix.Util.Type.isString(value) && !this.isInstance(value, context, scope)) {
// Using null as input since input is not available
return this.print(this.parse(value, context, null, scope), context, output, scope);
} else {
...
unmarshal = function (context, input, scope) { var text = input.getElementText(); return this.parse(text, context, input, scope); }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
buildElementInfos = function (context) { for (var index = 0; index < this.elementInfos.length; index++) { var elementInfo = this.elementInfos[index]; elementInfo.build(context, this); } }
...
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.registerElementInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.buildElementInfos(this);
}
},
registerTypeInfo : function(typeInfo) {
Jsonix.Util.Ensure.ensureObject(typeInfo);
var n = typeInfo.name||typeInfo.n||null;
Jsonix.Util.Ensure.ensureString(n);
this.typeInfos[n] = typeInfo;
...
buildTypeInfos = function (context) { for (var index = 0; index < this.typeInfos.length; index++) { var typeInfo = this.typeInfos[index]; typeInfo.build(context, this); } }
...
var index, module;
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.registerTypeInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.buildTypeInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.registerElementInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
...
createClassInfo = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); var dens = mapping.defaultElementNamespaceURI || mapping.dens || this.defaultElementNamespaceURI; mapping.defaultElementNamespaceURI = dens; var tns = mapping.targetNamespace || mapping.tns || this.targetNamespace; mapping.targetNamespace = tns; var dans = mapping.defaultAttributeNamespaceURI || mapping.dans || this.defaultAttributeNamespaceURI; mapping.defaultAttributeNamespaceURI = dans; this.initializeNames(mapping); // Now both name an local name are initialized var classInfo = new this.mappingStyle.classInfo(mapping, { mappingStyle : this.mappingStyle }); return classInfo; }
n/a
createElementInfo = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); mapping = Jsonix.Util.Type.cloneObject(mapping); var dens = mapping.defaultElementNamespaceURI || mapping.dens || this.defaultElementNamespaceURI; mapping.defaultElementNamespaceURI = dens; var en = mapping.elementName || mapping.en || undefined; Jsonix.Util.Ensure.ensureExists(en); var ti = mapping.typeInfo || mapping.ti || 'String'; Jsonix.Util.Ensure.ensureExists(ti); mapping.typeInfo = ti; if (Jsonix.Util.Type.isObject(en)) { mapping.elementName = Jsonix.XML.QName.fromObject(en); } else if (Jsonix.Util.Type.isString(en)) { mapping.elementName = new Jsonix.XML.QName(this.defaultElementNamespaceURI, en); } else { throw new Error('Element info [' + mapping + '] must provide an element name.'); } var sh = mapping.substitutionHead || mapping.sh || null; if (Jsonix.Util.Type.exists(sh)) { if (Jsonix.Util.Type.isObject(sh)) { mapping.substitutionHead = Jsonix.XML.QName.fromObject(sh); } else { Jsonix.Util.Ensure.ensureString(sh); mapping.substitutionHead = new Jsonix.XML.QName(this.defaultElementNamespaceURI, sh); } } var elementInfo = new this.mappingStyle.elementInfo(mapping, { mappingStyle : this.mappingStyle }); return elementInfo; }
...
}
},
initializeElementInfos : function(elementInfoMappings) {
Jsonix.Util.Ensure.ensureArray(elementInfoMappings);
var index, elementInfoMapping, elementInfo;
for (index = 0; index < elementInfoMappings.length; index++) {
elementInfoMapping = elementInfoMappings[index];
elementInfo = this.createElementInfo(elementInfoMapping);
this.elementInfos.push(elementInfo);
}
},
createTypeInfo : function(mapping) {
Jsonix.Util.Ensure.ensureObject(mapping);
var typeInfo;
// If mapping is already a type info, do nothing
...
createEnumLeafInfo = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); this.initializeNames(mapping); // Now both name an local name are initialized var enumLeafInfo = new this.mappingStyle.enumLeafInfo(mapping, { mappingStyle : this.mappingStyle }); return enumLeafInfo; }
n/a
createList = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); var ti = mapping.baseTypeInfo || mapping.typeInfo || mapping.bti || mapping.ti || 'String'; var tn = mapping.typeName || mapping.tn || null; if (Jsonix.Util.Type.exists(tn)) { if (Jsonix.Util.Type.isString(tn)) { tn = new Jsonix.XML.QName(this.targetNamespace, tn); } else { tn = Jsonix.XML.QName.fromObject(tn); } } var s = mapping.separator || mapping.sep || ' '; Jsonix.Util.Ensure.ensureExists(ti); return new Jsonix.Schema.XSD.List(ti, tn, s); }
n/a
createTypeInfo = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); var typeInfo; // If mapping is already a type info, do nothing if (mapping instanceof Jsonix.Model.TypeInfo) { typeInfo = mapping; } // Else create it via generic mapping configuration else { mapping = Jsonix.Util.Type.cloneObject(mapping); var type = mapping.type || mapping.t || 'classInfo'; // Locate the creator function if (Jsonix.Util.Type.isFunction(this.typeInfoCreators[type])) { var typeInfoCreator = this.typeInfoCreators[type]; // Call the creator function typeInfo = typeInfoCreator.call(this, mapping); } else { throw new Error("Unknown type info type [" + type + "]."); } } return typeInfo; }
...
}
},
initializeTypeInfos : function(typeInfoMappings) {
Jsonix.Util.Ensure.ensureArray(typeInfoMappings);
var index, typeInfoMapping, typeInfo;
for (index = 0; index < typeInfoMappings.length; index++) {
typeInfoMapping = typeInfoMappings[index];
typeInfo = this.createTypeInfo(typeInfoMapping);
this.typeInfos.push(typeInfo);
}
},
initializeElementInfos : function(elementInfoMappings) {
Jsonix.Util.Ensure.ensureArray(elementInfoMappings);
var index, elementInfoMapping, elementInfo;
for (index = 0; index < elementInfoMappings.length; index++) {
...
cs = function () { return this; }
n/a
es = function () { return this; }
n/a
initialize = function (mapping, options) { Jsonix.Mapping.Styled.prototype.initialize.apply(this, [ options ]); this.typeInfos = []; this.elementInfos = []; if (typeof mapping !== 'undefined') { Jsonix.Util.Ensure.ensureObject(mapping); var n = mapping.name || mapping.n || null; this.name = n; var dens = mapping.defaultElementNamespaceURI || mapping.dens || mapping.targetNamespace || mapping.tns || ''; this.defaultElementNamespaceURI = dens; var tns = mapping.targetNamespace || mapping.tns || mapping.defaultElementNamespaceURI || mapping.dens || this.defaultElementNamespaceURI ; this.targetNamespace = tns; var dans = mapping.defaultAttributeNamespaceURI || mapping.dans || ''; this.defaultAttributeNamespaceURI = dans; // Initialize type infos var tis = mapping.typeInfos || mapping.tis || []; this.initializeTypeInfos(tis); // Backwards compatibility: class infos can also be defined // as properties of the schema, for instance Schema.MyType for ( var typeInfoName in mapping) { if (mapping.hasOwnProperty(typeInfoName)) { if (mapping[typeInfoName] instanceof this.mappingStyle.classInfo) { this.typeInfos.push(mapping[typeInfoName]); } } } var eis = mapping.elementInfos || mapping.eis || []; // Initialize element infos this.initializeElementInfos(eis); } }
n/a
initializeElementInfos = function (elementInfoMappings) { Jsonix.Util.Ensure.ensureArray(elementInfoMappings); var index, elementInfoMapping, elementInfo; for (index = 0; index < elementInfoMappings.length; index++) { elementInfoMapping = elementInfoMappings[index]; elementInfo = this.createElementInfo(elementInfoMapping); this.elementInfos.push(elementInfo); } }
...
if (mapping[typeInfoName] instanceof this.mappingStyle.classInfo) {
this.typeInfos.push(mapping[typeInfoName]);
}
}
}
var eis = mapping.elementInfos || mapping.eis || [];
// Initialize element infos
this.initializeElementInfos(eis);
}
},
initializeTypeInfos : function(typeInfoMappings) {
Jsonix.Util.Ensure.ensureArray(typeInfoMappings);
var index, typeInfoMapping, typeInfo;
for (index = 0; index < typeInfoMappings.length; index++) {
typeInfoMapping = typeInfoMappings[index];
...
initializeNames = function (mapping) { var ln = mapping.localName || mapping.ln || null; mapping.localName = ln; var n = mapping.name || mapping.n || null; mapping.name = n; // Calculate both name as well as localName // name is provided if (Jsonix.Util.Type.isString(mapping.name)) { // Obsolete code below // // localName is not provided // if (!Jsonix.Util.Type.isString(mapping.localName)) { // // But module name is provided // if (Jsonix.Util.Type.isString(this.name)) { // // If name starts with module name, use second part // // as local name // if (mapping.name.indexOf(this.name + '.') === 0) { // mapping.localName = mapping.name // .substring(this.name.length + 1); // } // // Else use name as local name // else { // mapping.localName = mapping.name; // } // } // // Module name is not provided, use name as local name // else { // mapping.localName = mapping.name; // } // } if (mapping.name.length > 0 && mapping.name.charAt(0) === '.' && Jsonix.Util.Type.isString(this.name)) { mapping.name = this.name + mapping.name; } } // name is not provided but local name is provided else if (Jsonix.Util.Type.isString(ln)) { // Module name is provided if (Jsonix.Util.Type.isString(this.name)) { mapping.name = this.name + '.' + ln; } // Module name is not provided else { mapping.name = ln; } } else { throw new Error("Neither [name/n] nor [localName/ln] was provided for the class info."); } }
...
Jsonix.Util.Ensure.ensureObject(mapping);
var dens = mapping.defaultElementNamespaceURI || mapping.dens || this.defaultElementNamespaceURI;
mapping.defaultElementNamespaceURI = dens;
var tns = mapping.targetNamespace || mapping.tns || this.targetNamespace;
mapping.targetNamespace = tns;
var dans = mapping.defaultAttributeNamespaceURI || mapping.dans || this.defaultAttributeNamespaceURI;
mapping.defaultAttributeNamespaceURI = dans;
this.initializeNames(mapping);
// Now both name an local name are initialized
var classInfo = new this.mappingStyle.classInfo(mapping, {
mappingStyle : this.mappingStyle
});
return classInfo;
},
createEnumLeafInfo : function(mapping) {
...
initializeTypeInfos = function (typeInfoMappings) { Jsonix.Util.Ensure.ensureArray(typeInfoMappings); var index, typeInfoMapping, typeInfo; for (index = 0; index < typeInfoMappings.length; index++) { typeInfoMapping = typeInfoMappings[index]; typeInfo = this.createTypeInfo(typeInfoMapping); this.typeInfos.push(typeInfo); } }
...
this.defaultElementNamespaceURI = dens;
var tns = mapping.targetNamespace || mapping.tns || mapping.defaultElementNamespaceURI || mapping.dens || this.defaultElementNamespaceURI
;
this.targetNamespace = tns;
var dans = mapping.defaultAttributeNamespaceURI || mapping.dans || '';
this.defaultAttributeNamespaceURI = dans;
// Initialize type infos
var tis = mapping.typeInfos || mapping.tis || [];
this.initializeTypeInfos(tis);
// Backwards compatibility: class infos can also be defined
// as properties of the schema, for instance Schema.MyType
for ( var typeInfoName in mapping) {
if (mapping.hasOwnProperty(typeInfoName)) {
if (mapping[typeInfoName] instanceof this.mappingStyle.classInfo) {
this.typeInfos.push(mapping[typeInfoName]);
...
registerElementInfos = function (context) { for (var index = 0; index < this.elementInfos.length; index++) { var elementInfo = this.elementInfos[index]; context.registerElementInfo(elementInfo, this); } }
...
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.buildTypeInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.registerElementInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.buildElementInfos(this);
}
},
registerTypeInfo : function(typeInfo) {
...
registerTypeInfos = function (context) { for (var index = 0; index < this.typeInfos.length; index++) { var typeInfo = this.typeInfos[index]; context.registerTypeInfo(typeInfo, this); } }
...
this.registerTypeInfo(this.builtinTypeInfos[index]);
}
},
processModules : function() {
var index, module;
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.registerTypeInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
module.buildTypeInfos(this);
}
for (index = 0; index < this.modules.length; index++) {
module = this.modules[index];
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { throw new Error("Abstract method [buildStructure]."); }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
doBuild = function (context, module) { throw new Error("Abstract method [doBuild]."); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); var n = mapping.name || mapping.n || undefined; Jsonix.Util.Ensure.ensureString(n); this.name = n; var dens = mapping.defaultElementNamespaceURI || mapping.dens || mapping.targetNamespace || mapping.tns || ''; this.defaultElementNamespaceURI = dens; var tns = mapping.targetNamespace || mapping.tns || mapping.defaultElementNamespaceURI || mapping.dens || this.defaultElementNamespaceURI ; this.targetNamespace = tns; var dans = mapping.defaultAttributeNamespaceURI || mapping.dans || ''; this.defaultAttributeNamespaceURI = dans; var col = mapping.collection || mapping.col || false; this.collection = col; var rq = mapping.required || mapping.rq || false; this.required = rq; if (this.collection) { var mno; if (Jsonix.Util.Type.isNumber(mapping.minOccurs)) { mno = mapping.minOccurs; } else if (Jsonix.Util.Type.isNumber(mapping.mno)) { mno = mapping.mno; } else { mno = 1; } this.minOccurs = mno; var mxo; if (Jsonix.Util.Type.isNumber(mapping.maxOccurs)) { mxo = mapping.maxOccurs; } else if (Jsonix.Util.Type.isNumber(mapping.mxo)) { mxo = mapping.mxo; } else { mxo = null; } this.maxOccurs = mxo; } }
n/a
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { throw new Error("Abstract method [buildStructure]."); }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
doBuild = function (context, module) { this.typeInfo = context.resolveTypeInfo(this.typeInfo, module); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.PropertyInfo.prototype.initialize.apply(this, [ mapping ]); var ti = mapping.typeInfo || mapping.ti || 'String'; this.typeInfo = ti; }
n/a
parse = function (value, context, input, scope) { return this.typeInfo.parse(value, context, input, scope); }
...
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement
)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
} else {
throw new Error('Response does not have valid [responseXML] or [responseText].');
}
callback(result);
}, function(transport) {
throw new Error('Could not retrieve XML from URL [' + url + '].');
...
print = function (value, context, output, scope) { return this.typeInfo.reprint(value, context, output, scope); }
...
}
}
var typeInfo = actualTypeInfo || declaredTypeInfo;
if (typeInfo) {
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output
, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshalValue = function (value, context, input, scope) { return this.parse(value, context, input, scope); }
...
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
if (this.isMarshallable(value, context, scope))
{
// TODO This must be reworked
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
...
initialize = function () { }
n/a
isBasedOn = function (typeInfo) { var currentTypeInfo = this; while (currentTypeInfo) { if (typeInfo === currentTypeInfo) { return true; } currentTypeInfo = currentTypeInfo.baseTypeInfo; } return false; }
...
if (actualTypeInfo && actualTypeInfo.typeName) {
for (var jndex = 0; jndex < this.elementTypeInfos.length; jndex++) {
var eti = this.elementTypeInfos[jndex];
var ti = eti.typeInfo;
// TODO Can be optimized
// Find an element type info which has a type info that is a
// supertype of the actual type info
if (actualTypeInfo.isBasedOn(ti)) {
var en = eti.elementName;
return {
name : en,
value : value,
typeInfo : ti
};
}
...
build = function (context, module) { if (!this.built) { this.doBuild(context, module); this.built = true; } }
...
// Obsolete
destroy : function() {
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.build(context, module);
}
// Build properties in this context
for ( var index = 0; index < this.properties.length; index++) {
var propertyInfo = this.properties[index];
propertyInfo.build(context, module);
}
...
buildStructure = function (context, structure) { Jsonix.Util.Ensure.ensureObject(structure); // if (Jsonix.Util.Type.exists(structure.value)) { // // TODO better exception // throw new Error("The structure already defines a value // property."); // } else if (Jsonix.Util.Type.exists(structure.elements)) { // TODO better exception throw new Error("The structure already defines element mappings, it cannot define a value property."); } else { structure.value = this; } }
...
var structure = {
elements : null,
attributes : {},
anyAttribute : null,
value : null,
any : null
};
this.buildStructure(context, structure);
this.structure = structure;
}
},
buildStructure : function(context, structure) {
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
this.baseTypeInfo.buildStructure(context, structure);
}
...
doBuild = function (context, module) { this.typeInfo = context.resolveTypeInfo(this.typeInfo, module); }
...
mxo = null;
}
this.maxOccurs = mxo;
}
},
build : function(context, module) {
if (!this.built) {
this.doBuild(context, module);
this.built = true;
}
},
doBuild : function(context, module) {
throw new Error("Abstract method [doBuild].");
},
buildStructure : function(context, structure) {
...
initialize = function (mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this, [ mapping ]); }
n/a
marshal = function (value, context, output, scope) { if (!Jsonix.Util.Type.exists(value)) { return; } output.writeCharacters(this.print(value, context, output, scope)); }
...
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
...
parse = function (value, context, input, scope) { return this.typeInfo.parse(value, context, input, scope); }
...
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement
)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
} else {
throw new Error('Response does not have valid [responseXML] or [responseText].');
}
callback(result);
}, function(transport) {
throw new Error('Could not retrieve XML from URL [' + url + '].');
...
print = function (value, context, output, scope) { return this.typeInfo.reprint(value, context, output, scope); }
...
}
}
var typeInfo = actualTypeInfo || declaredTypeInfo;
if (typeInfo) {
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output
, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
...
setProperty = function (object, value) { if (Jsonix.Util.Type.exists(value)) { if (this.collection) { Jsonix.Util.Ensure.ensureArray(value, 'Collection property requires an array value.'); if (!Jsonix.Util.Type.exists(object[this.name])) { object[this.name] = []; } for (var index = 0; index < value.length; index++) { object[this.name].push(value[index]); } } else { object[this.name] = value; } } }
...
throw new Error("Illegal state: must be END_ELEMENT.");
}
return result;
},
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
...
unmarshal = function (context, input, scope) { var text = input.getElementText(); return this.unmarshalValue(text, context, input, scope); }
...
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
...
unmarshalValue = function (value, context, input, scope) { return this.parse(value, context, input, scope); }
...
unmarshalProperty : function(context, input, propertyInfo, result) {
var propertyValue = propertyInfo
.unmarshal(context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
unmarshalPropertyValue : function(context, input, propertyInfo,
result, value) {
var propertyValue = propertyInfo.unmarshalValue(value, context, input, this);
propertyInfo.setProperty(result, propertyValue);
},
marshal : function(value, context, output, scope) {
if (this.isMarshallable(value, context, scope))
{
// TODO This must be reworked
if (Jsonix.Util.Type.exists(this.baseTypeInfo)) {
...
Request = function () { this.initialize.apply(this, arguments); }
...
// TODO log
}
}
throw new Error('Could not create XML HTTP transport.');
},
CLASS_NAME : 'Jsonix.Request'
});
Jsonix.Request.INSTANCE = new Jsonix.Request();
Jsonix.Request.PROXY = null;
Jsonix.Schema = {};
Jsonix.Model = {};
Jsonix.Util.Type = {
exists : function(value) {
return (typeof value !== 'undefined' && value !== null);
},
...
createTransport = function () { for ( var index = 0, length = this.factories.length; index < length; index++) { try { var transport = this.factories[index](); if (transport !== null) { return transport; } } catch (e) { // TODO log } } throw new Error('Could not create XML HTTP transport.'); }
...
}
if (Jsonix.Util.Type.exists(options)) {
Jsonix.Util.Ensure.ensureObject(options);
} else {
options = {};
}
var transport = this.createTransport();
var method = Jsonix.Util.Type.isString(options.method) ? options.method
: 'GET';
var async = Jsonix.Util.Type.isBoolean(options.async) ? options.async
: true;
var proxy = Jsonix.Util.Type.isString(options.proxy) ? options.proxy
: Jsonix.Request.PROXY;
...
handleTransport = function (transport, onSuccess, onFailure) { if (transport.readyState == 4) { if (!transport.status || (transport.status >= 200 && transport.status < 300)) { onSuccess(transport); } if (transport.status && (transport.status < 200 || transport.status >= 300)) { onFailure(transport); } } }
...
}
}
var data = Jsonix.Util.Type.exists(options.data) ? options.data
: null;
if (!async) {
transport.send(data);
this.handleTransport(transport, onSuccess, onFailure);
} else {
var that = this;
if (typeof window !== 'undefined') {
transport.onreadystatechange = function() {
that.handleTransport(transport, onSuccess,
onFailure);
...
initialize = function () { }
n/a
issue = function (url, onSuccess, onFailure, options) { Jsonix.Util.Ensure.ensureString(url); if (Jsonix.Util.Type.exists(onSuccess)) { Jsonix.Util.Ensure.ensureFunction(onSuccess); } else { onSuccess = function() { }; } if (Jsonix.Util.Type.exists(onFailure)) { Jsonix.Util.Ensure.ensureFunction(onFailure); } else { onFailure = function() { }; } if (Jsonix.Util.Type.exists(options)) { Jsonix.Util.Ensure.ensureObject(options); } else { options = {}; } var transport = this.createTransport(); var method = Jsonix.Util.Type.isString(options.method) ? options.method : 'GET'; var async = Jsonix.Util.Type.isBoolean(options.async) ? options.async : true; var proxy = Jsonix.Util.Type.isString(options.proxy) ? options.proxy : Jsonix.Request.PROXY; var user = Jsonix.Util.Type.isString(options.user) ? options.user : null; var password = Jsonix.Util.Type.isString(options.password) ? options.password : null; if (Jsonix.Util.Type.isString(proxy) && (url.indexOf("http") === 0)) { url = proxy + encodeURIComponent(url); } if (Jsonix.Util.Type.isString(user)) { transport.open(method, url, async, user, password); } else { transport.open(method, url, async); } if (Jsonix.Util.Type.isObject(options.headers)) { for ( var header in options.headers) { if (options.headers.hasOwnProperty(header)) { transport.setRequestHeader(header, options.headers[header]); } } } var data = Jsonix.Util.Type.exists(options.data) ? options.data : null; if (!async) { transport.send(data); this.handleTransport(transport, onSuccess, onFailure); } else { var that = this; if (typeof window !== 'undefined') { transport.onreadystatechange = function() { that.handleTransport(transport, onSuccess, onFailure); }; window.setTimeout(function() { transport.send(data); }, 0); } else { transport.onreadystatechange = function() { that.handleTransport(transport, onSuccess, onFailure); }; transport.send(data); } } return transport; }
...
return request.responseXML;
}
},
load : function(url, callback, options) {
var request = Jsonix.Request.INSTANCE;
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement
)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
...
0 = function () { return new XMLHttpRequest(); }
n/a
1 = function () { return new ActiveXObject('Msxml2.XMLHTTP'); }
n/a
2 = function () { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
n/a
3 = function () { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
n/a
4 = function () { return new ActiveXObject('Microsoft.XMLHTTP'); }
n/a
5 = function () { // Node.js if (typeof _jsonix_xmlhttprequest !== 'undefined') { var XMLHttpRequest = _jsonix_xmlhttprequest.XMLHttpRequest; return new XMLHttpRequest(); } else { return null; } }
...
�U*]��l+��H������s���x揉e�]cD<F¼n�T��^.3� �W��?8#�'��g��iײ���yNug����S��n�Ƴ����~�O��AsN��A��D �^�7��[���x�Ӝ��O6=�W
��^<Kc�,H���&�t�:�t�!�G�� ņuD�'��uľ�34������A��-N�?�[���N������D&���=�k���D"��r�3D�/vE
���S �Vo �zn
�p?Ā{a�4E?���-(�h�~�˧\����6'K<u�ȥC#N�6?��gP�8�6�w�?��@Ӄ�|8�l���z4Ѽ�cb-h���ۻt��H�/hE��4�w��`D���� ���
x3c;)�5)����]�/�BH�.U���,���qh�m�h���T�!�G8q#�@zE�E��<^�N� �
�Ԏf�g����NcX��e����'��a�"��q�'P�/�1t��M��A�PK�Oa�[ $ PK u��G 5 org/apache/commons
/collections/SequencedHashMap.class�XxTg�~�sfr���$B��d��&¥PRhH4m
�E�� �L�L��u]��n�V�ֺu]�mi-ui����Rm�뮷uW��nmŮR[/����O����s2�-$�3����w��۟o���G4��%X���ߥ�~��,<�ϔ�yV��?������e�s��
X�ݵ�e��4?�����Oe�������Lz?��yYxI�����A��� ~�_�k�F����j5��4�d�K��,��^��,�_ K����i~+��,�>��n�B M�*����R�2,er��+�|�
X��bUL`U"M@���l!E���sA5MM��T�f���AU�ʥ��Y�R�ٖ�c�*K͵T��T��r�;�N*�N���D�I�Ć��a["�:�P<�$���3���3������u������
0�N_:.��=#N���klx���P���N�"F*.����bb�#��@������dkr$�VP��-m��u(Lk%n:�Ho�
�8&�dm���mm2P�g�������m���][7�l1?��.vR���v'5L&�ul�7�_|���������X���D�*�t�B�Z��5�I����]N��خΔu&��@�lp�
x27;�Qtu<O�P����H��sv��r �C�<
���$���v�
$c���>��U;
�܂ �.5(�O�'b�Lp�1�
MQ;B��d@�$�$�!}�.�� �ۊ�
���wrk_2��q��s�4��Ğ��]�Hs��2��j��E�O��nJ�mУ=-��l!0����H{<5�s�CK'�˳�`v��vE� ڴ�&͚�qgL��瞶��`�;�`��(N�
k,�_Q�~I4���l)���:Fy�8@�Yї�H�8#���%����7�ĸ\D�^��ܵ���%�����=�1�=�
����5�[aΤ�kGv�v�H��G,b愛��,�6C�s&,�z
ݺ�B��dB�k06D���s�\5Yj^Ɗ��ϡ�^D-�x��9H����;
_'���n��}G�{ǽh_9A�M� �Y$F�̥J��=�v]��Z:y�fX�o;H�Dl@d�l�'57 ���x³��eKͷ�:�{S�{�*�{$��3����7�b}}��pmSS���/;����
z�q�I�e
%=���N:�7��2�p��s8u~������P.�ٳ�+�y
�,=ɑT����9+K����M�P��c6ﷱ
...
extend = function (destination, source) { destination = destination || {}; if (source) { /*jslint forin: true */ for ( var property in source) { var value = source[property]; if (value !== undefined) { destination[property] = value; } } /** * IE doesn't include the toString property when iterating over an * object's properties with the for(property in object) syntax. * Explicitly check if the source has its own toString property. */ /* * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative * prototype object" when calling hawOwnProperty if the source object is * an instance of window.Event. */ // REWORK // Node.js sourceIsEvt = typeof window !== 'undefined' && window !== null && typeof window.Event === "function" && source instanceof window .Event; if (!sourceIsEvt && source.hasOwnProperty && source.hasOwnProperty('toString')) { destination.toString = source.toString; } } return destination; }
...
}
// get the prototype of the superclass
parent = Type.prototype;
} else {
// in this case we're extending with the prototype
parent = Type;
}
Jsonix.Util.extend(extended, parent);
}
Class.prototype = extended;
return Class;
};
Jsonix.XML = {
XMLNS_NS : 'http://www.w3.org/2000/xmlns/',
XMLNS_P : 'xmlns'
...
Calendar = function () { this.initialize.apply(this, arguments); }
...
Jsonix.XML.Calendar.MAX_TIMEZONE = 14 * 60;
Jsonix.XML.Calendar.DAYS_IN_MONTH = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
Jsonix.XML.Calendar.fromObject = function(object) {
Jsonix.Util.Ensure.ensureObject(object);
if (Jsonix.Util.Type.isString(object.CLASS_NAME) && object.CLASS_NAME === 'Jsonix.XML.Calendar') {
return object;
}
return new Jsonix.XML.Calendar(object);
};
Jsonix.XML.Calendar.validateYear = function(year) {
if (year === 0) {
throw new Error('Invalid year [' + year + ']. Year must not be [0].');
}
};
Jsonix.XML.Calendar.validateMonth = function(month) {
...
Input = function () { this.initialize.apply(this, arguments); }
...
var text = data.toString();
var doc = Jsonix.DOM.parse(text);
callback(that.unmarshalDocument(doc));
}
});
},
unmarshalDocument : function(doc, scope) {
var input = new Jsonix.XML.Input(doc);
var result = null;
var callback = function(_result) {
result = _result;
};
input.nextTag();
this.unmarshalElement(this.context, input, scope, callback);
return result;
...
Output = function () { this.initialize.apply(this, arguments); }
...
},
marshalString : function(value) {
var doc = this.marshalDocument(value);
var text = Jsonix.DOM.serialize(doc);
return text;
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
...
QName = function () { this.initialize.apply(this, arguments); }
...
},
// foo:bar
toCanonicalString: function(namespaceContext) {
var canonicalPrefix = namespaceContext ? namespaceContext.getPrefix(this.namespaceURI, this.prefix) : this.prefix;
return this.prefix + (this.prefix === '' ? '' : ':') + this.localPart;
},
clone : function() {
return new Jsonix.XML.QName(this.namespaceURI, this.localPart, this.prefix);
},
equals : function(that) {
if (!that) {
return false;
} else {
return (this.namespaceURI == that.namespaceURI) && (this.localPart == that.localPart);
}
...
initialize = function (data) { Jsonix.Util.Ensure.ensureObject(data); // Year if (Jsonix.Util.Type.exists(data.year)) { Jsonix.Util.Ensure.ensureInteger(data.year); Jsonix.XML.Calendar.validateYear(data.year); this.year = data.year; } else { this.year = NaN; } // Month if (Jsonix.Util.Type.exists(data.month)) { Jsonix.Util.Ensure.ensureInteger(data.month); Jsonix.XML.Calendar.validateMonth(data.month); this.month = data.month; } else { this.month = NaN; } // Day if (Jsonix.Util.Type.exists(data.day)) { Jsonix.Util.Ensure.ensureInteger(data.day); if (Jsonix.Util.NumberUtils.isInteger(data.year) && Jsonix.Util.NumberUtils.isInteger(data.month)) { Jsonix.XML.Calendar.validateYearMonthDay(data.year, data.month, data.day); } else if (Jsonix.Util.NumberUtils.isInteger(data.month)) { Jsonix.XML.Calendar.validateMonthDay(data.month, data.day); } else { Jsonix.XML.Calendar.validateDay(data.day); } this.day = data.day; } else { this.day = NaN; } // Hour if (Jsonix.Util.Type.exists(data.hour)) { Jsonix.Util.Ensure.ensureInteger(data.hour); Jsonix.XML.Calendar.validateHour(data.hour); this.hour = data.hour; } else { this.hour = NaN; } // Minute if (Jsonix.Util.Type.exists(data.minute)) { Jsonix.Util.Ensure.ensureInteger(data.minute); Jsonix.XML.Calendar.validateMinute(data.minute); this.minute = data.minute; } else { this.minute = NaN; } // Second if (Jsonix.Util.Type.exists(data.second)) { Jsonix.Util.Ensure.ensureInteger(data.second); Jsonix.XML.Calendar.validateSecond(data.second); this.second = data.second; } else { this.second = NaN; } // Fractional second if (Jsonix.Util.Type.exists(data.fractionalSecond)) { Jsonix.Util.Ensure.ensureNumber(data.fractionalSecond); Jsonix.XML.Calendar.validateFractionalSecond(data.fractionalSecond); this.fractionalSecond = data.fractionalSecond; } else { this.fractionalSecond = NaN; } // Timezone if (Jsonix.Util.Type.exists(data.timezone)) { if (Jsonix.Util.Type.isNaN(data.timezone)) { this.timezone = NaN; } else { Jsonix.Util.Ensure.ensureInteger(data.timezone); Jsonix.XML.Calendar.validateTimezone(data.timezone); this.timezone = data.timezone; } } else { this.timezone = NaN; } var initialDate = new Date(0); initialDate.setUTCFullYear(this.year || 1970); initialDate.setUTCMonth(this.month - 1 || 0); initialDate.setUTCDate(this.day || 1); initialDate.setUTCHours(this.hour || 0); initialDate.setUTCMinutes(this.minute || 0); initialDate.setUTCSeconds(this.second || 0); initialDate.setUTCMilliseconds((this.fractionalSecond || 0) * 1000); var timezoneOffset = -60000 * (this.timezone || 0); this.date = new Date(initialDate.getTime() + timezoneOffset); }
n/a
enter = function (node) { var nodeType = node.nodeType; this.node = node; this.attributes = null; // Document node if (nodeType === 1) { // START_ELEMENT this.eventType = 1; this.pushNS(node); return this.eventType; } else if (nodeType === 2) { // ATTRIBUTE this.eventType = 10; return this.eventType; } else if (nodeType === 3) { var nodeValue = node.nodeValue; if (Jsonix.Util.StringUtils.isEmpty(nodeValue)) { // SPACE this.eventType = 6; } else { // CHARACTERS this.eventType = 4; } return this.eventType; } else if (nodeType === 4) { // CDATA this.eventType = 12; return this.eventType; } else if (nodeType === 5) { // ENTITY_REFERENCE_NODE = 5 // ENTITY_REFERENCE this.eventType = 9; return this.eventType; } else if (nodeType === 6) { // ENTITY_DECLARATION this.eventType = 15; return this.eventType; } else if (nodeType === 7) { // PROCESSING_INSTRUCTION this.eventType = 3; return this.eventType; } else if (nodeType === 8) { // COMMENT this.eventType = 5; return this.eventType; } else if (nodeType === 9) { // START_DOCUMENT this.eventType = 7; return this.eventType; } else if (nodeType === 10) { // DTD this.eventType = 12; return this.eventType; } else if (nodeType === 12) { // NOTATION_DECLARATION this.eventType = 14; return this.eventType; } else { // DOCUMENT_FRAGMENT_NODE = 11 throw new Error("Node type [" + nodeType + '] is not supported.'); } }
...
}
} else {
return true;
}
},
next : function() {
if (this.eventType === null) {
return this.enter(this.root);
}
// START_DOCUMENT
if (this.eventType === 7) {
var documentElement = this.node.documentElement;
if (documentElement) {
return this.enter(documentElement);
} else {
...
getAttributeCount = function () { var attributes = this.retrieveAttributes(); return attributes.length; }
...
if (input.eventType !== 1) {
throw new Error("Parser must be on START_ELEMENT to read a class info.");
}
// Read attributes
if (Jsonix.Util.Type.exists(this.structure.attributes)) {
var attributeCount = input.getAttributeCount();
if (attributeCount !== 0) {
for ( var index = 0; index < attributeCount; index++) {
var attributeNameKey = input
.getAttributeNameKey(index);
if (Jsonix.Util.Type
.exists(this.structure.attributes[attributeNameKey])) {
var attributeValue = input
...
getAttributeName = function (index) { var attributes = this.retrieveAttributes(); if (index < 0 || index >= attributes.length) { throw new Error("Invalid attribute index [" + index + "]."); } var attribute = attributes[index]; if (Jsonix.Util.Type.isString(attribute.namespaceURI)) { return new Jsonix.XML.QName(attribute.namespaceURI, attribute.nodeName); } else { return new Jsonix.XML.QName(attribute.nodeName); } }
...
if (attributeCount === 0) {
return null;
} else {
var result = {};
for ( var index = 0; index < attributeCount; index++) {
var value = input.getAttributeValue(index);
if (Jsonix.Util.Type.isString(value)) {
var propertyName = this.convertFromAttributeName(input.getAttributeName(index),
context, input, scope);
result[propertyName] = value;
}
}
return result;
}
},
marshal : function(value, context, output, scope) {
...
getAttributeNameKey = function (index) { var attributes = this.retrieveAttributes(); if (index < 0 || index >= attributes.length) { throw new Error("Invalid attribute index [" + index + "]."); } var attribute = attributes[index]; return Jsonix.XML.QName.key(attribute.namespaceURI, attribute.nodeName); }
...
// Read attributes
if (Jsonix.Util.Type.exists(this.structure.attributes)) {
var attributeCount = input.getAttributeCount();
if (attributeCount !== 0) {
for ( var index = 0; index < attributeCount; index++) {
var attributeNameKey = input
.getAttributeNameKey(index);
if (Jsonix.Util.Type
.exists(this.structure.attributes[attributeNameKey])) {
var attributeValue = input
.getAttributeValue(index);
if (Jsonix.Util.Type.isString(attributeValue)) {
var attributePropertyInfo = this.structure.attributes[attributeNameKey];
this.unmarshalPropertyValue(context, input,
...
getAttributeNodeNS = function (namespaceURI, localPart) { var element = this.retrieveElement(); return element.getAttributeNodeNS(namespaceURI, localPart); }
...
},
getAttributeValueNS : null,
getAttributeValueNSViaElement : function(namespaceURI, localPart) {
var element = this.retrieveElement();
return element.getAttributeNS(namespaceURI, localPart);
},
getAttributeValueNSViaAttribute : function(namespaceURI, localPart) {
var attributeNode = this.getAttributeNodeNS(namespaceURI, localPart);
if (Jsonix.Util.Type.exists(attributeNode)) {
return attributeNode.nodeValue;
}
else
{
return null;
}
...
getAttributeNodeNSViaAttributes = function (namespaceURI, localPart) { var attributeNode = null; var attributes = this.retrieveAttributes(); var potentialNode, fullName; for (var i = 0, len = attributes.length; i < len; ++i) { potentialNode = attributes[i]; if (potentialNode.namespaceURI === namespaceURI) { fullName = (potentialNode.prefix) ? (potentialNode.prefix + ':' + localPart) : localPart; if (fullName === potentialNode.nodeName) { attributeNode = potentialNode; break; } } } return attributeNode; }
n/a
getAttributeNodeNSViaElement = function (namespaceURI, localPart) { var element = this.retrieveElement(); return element.getAttributeNodeNS(namespaceURI, localPart); }
n/a
getAttributeValue = function (index) { var attributes = this.retrieveAttributes(); if (index < 0 || index >= attributes.length) { throw new Error("Invalid attribute index [" + index + "]."); } var attribute = attributes[index]; return attribute.value; }
...
if (attributeCount !== 0) {
for ( var index = 0; index < attributeCount; index++) {
var attributeNameKey = input
.getAttributeNameKey(index);
if (Jsonix.Util.Type
.exists(this.structure.attributes[attributeNameKey])) {
var attributeValue = input
.getAttributeValue(index);
if (Jsonix.Util.Type.isString(attributeValue)) {
var attributePropertyInfo = this.structure.attributes[attributeNameKey];
this.unmarshalPropertyValue(context, input,
attributePropertyInfo, result,
attributeValue);
}
}
...
getAttributeValueNS = function (namespaceURI, localPart) { var element = this.retrieveElement(); return element.getAttributeNS(namespaceURI, localPart); }
...
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed
objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at
the same time.");
}
callback(elementValue);
},
getTypeInfoByInputElement : function(context, input, scope) {
var xsiTypeInfo = null;
if (context.supportXsiType) {
var xsiType = input.getAttributeValueNS(Jsonix.Schema.XSI.NAMESPACE_URI, Jsonix.Schema
.XSI.TYPE);
if (Jsonix.Util.StringUtils.isNotBlank(xsiType)) {
var xsiTypeName = Jsonix.Schema.XSD.QName.INSTANCE.parse(xsiType, context, input, scope);
xsiTypeInfo = context.getTypeInfoByTypeNameKey(xsiTypeName.key);
}
}
var name = input.getName();
var typeInfo = xsiTypeInfo ? xsiTypeInfo : this.getTypeInfoByElementName(name, context, scope);
...
getAttributeValueNSViaAttribute = function (namespaceURI, localPart) { var attributeNode = this.getAttributeNodeNS(namespaceURI, localPart); if (Jsonix.Util.Type.exists(attributeNode)) { return attributeNode.nodeValue; } else { return null; } }
n/a
getAttributeValueNSViaElement = function (namespaceURI, localPart) { var element = this.retrieveElement(); return element.getAttributeNS(namespaceURI, localPart); }
n/a
getElement = function () { if (this.eventType === 1 || this.eventType === 2) { // Go to the END_ELEMENT this.eventType = 2; return this.node; } else { throw new Error("Parser must be on START_ELEMENT or END_ELEMENT to return current element."); } }
...
var typedNamedValue = {
name : name,
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as is not known in this context
and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed
objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at
the same time.");
...
getElementText = function () { if (this.eventType != 1) { throw new Error("Parser must be on START_ELEMENT to read next text."); } var et = this.next(); var content = ''; while (et !== 2) { if (et === 4 || et === 12 || et === 6 || et === 9) { content = content + this.getText(); } else if (et === 3 || et === 5) { // Skip PI or comment } else if (et === 8) { // End document throw new Error("Unexpected end of document when reading element text content."); } else if (et === 1) { // End element // TODO location throw new Error("Element text content may not contain START_ELEMENT."); } else { // TODO location throw new Error("Unexpected event type [" + et + "]."); } et = this.next(); } return content; }
...
this.entries = entries;
this.keys = keys;
this.values = values;
this.built = true;
}
},
unmarshal : function(context, input, scope) {
var text = input.getElementText();
return this.parse(text, context, input, scope);
},
marshal : function(value, context, output, scope) {
if (Jsonix.Util.Type.exists(value)) {
output.writeCharacters(this.reprint(value, context, output, scope));
}
},
...
getName = function () { var node = this.node; if (Jsonix.Util.Type.isString(node.nodeName)) { if (Jsonix.Util.Type.isString(node.namespaceURI)) { return new Jsonix.XML.QName(node.namespaceURI, node.nodeName); } else { return new Jsonix.XML.QName(node.nodeName); } } else { return null; } }
...
allowTypedObject : true,
allowDom : false,
unmarshalElement : function(context, input, scope, callback) {
if (input.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next element.");
}
var typeInfo = this.getTypeInfoByInputElement(context, input, scope);
var name = input.getName();
var elementValue;
if (this.allowTypedObject) {
if (Jsonix.Util.Type.exists(typeInfo)) {
var value = typeInfo.unmarshal(context, input, scope);
var typedNamedValue = {
name : name,
value : value,
...
getNameKey = function () { var node = this.node; if (Jsonix.Util.Type.isString(node.nodeName)) { return Jsonix.XML.QName.key(node.namespaceURI, node.nodeName); } else { return null; } }
...
// Read elements
if (Jsonix.Util.Type.exists(this.structure.elements)) {
var et = input.next();
while (et !== Jsonix.XML.Input.END_ELEMENT) {
if (et === Jsonix.XML.Input.START_ELEMENT) {
// New sub-element starts
var elementNameKey = input.getNameKey();
if (Jsonix.Util.Type
.exists(this.structure.elements[elementNameKey])) {
var elementPropertyInfo = this.structure.elements[elementNameKey];
this.unmarshalProperty(context, input,
elementPropertyInfo, result);
} else if (Jsonix.Util.Type
.exists(this.structure.any)) {
...
getNamespaceURI = function (p) { var pindex = this.pns.length - 1; var pnsItem = this.pns[pindex]; pnsItem = Jsonix.Util.Type.isObject(pnsItem) ? pnsItem : this.pns[pnsItem]; return pnsItem[p]; }
...
{
if (prefix === '' && Jsonix.Util.Type.isString(defaultNamespaceURI))
{
namespaceURI = defaultNamespaceURI;
}
else if (namespaceContext)
{
namespaceURI = namespaceContext.getNamespaceURI(prefix);
}
}
// If we don't have a namespace URI, assume '' by default
// TODO document the assumption
if (!Jsonix.Util.Type.isString(namespaceURI))
{
namespaceURI = defaultNamespaceURI || '';
...
getText = function () { return this.node.nodeValue; }
...
if (this.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next text.");
}
var et = this.next();
var content = '';
while (et !== 2) {
if (et === 4 || et === 12 || et === 6 || et === 9) {
content = content + this.getText();
} else if (et === 3 || et === 5) {
// Skip PI or comment
} else if (et === 8) {
// End document
throw new Error("Unexpected end of document when reading element text content.");
} else if (et === 1) {
// End element
...
hasNext = function () { // No current node, we've not started yet if (this.node === null) { return true; } else if (this.node === this.root) { var nodeType = this.node.nodeType; // Root node is document, last event type is END_DOCUMENT if (nodeType === 9 && this.eventType === 8) { return false; } // Root node is element, last event type is END_ELEMENT else if (nodeType === 1 && this.eventType === 2) { return false; } else { return true; } } else { return true; } }
...
var doc = Jsonix.DOM
.parse('<!DOCTYPE a [<!ENTITY g2 "g2" >]><a>b<c>d</c>e<
;f> <g>h</g><![CDATA[g1]]>&g2;</f><!-- a --></a>');
test.notEqual(null, doc);
{
var input = new Jsonix.XML.Input(doc);
var result = [];
while (input.hasNext()) {
var eventType = input.next();
var name = input.getName();
result.push(eventType);
}
// entity reference 9
// attribute 10
// dtd 11
...
initialize = function (node) { Jsonix.Util.Ensure.ensureExists(node); this.root = node; var rootPnsItem = { '' : '' }; rootPnsItem[Jsonix.XML.XMLNS_P] = Jsonix.XML.XMLNS_NS; this.pns = [rootPnsItem]; }
n/a
leave = function (node) { if (node.nodeType === 9) { if (this.eventType == 8) { throw new Error("Invalid state."); } else { this.node = node; this.attributes = null; // END_ELEMENT this.eventType = 8; return this.eventType; } } else if (node.nodeType === 1) { if (this.eventType == 2) { var nextSibling = node.nextSibling; if (nextSibling) { return this.enter(nextSibling); } } else { this.node = node; this.attributes = null; // END_ELEMENT this.eventType = 2; this.popNS(); return this.eventType; } } var nextSibling1 = node.nextSibling; if (nextSibling1) { return this.enter(nextSibling1); } else { var parentNode = node.parentNode; this.node = parentNode; this.attributes = null; if (parentNode.nodeType === 9) { this.eventType = 8; } else { this.eventType = 2; } return this.eventType; } }
...
}
// START_DOCUMENT
if (this.eventType === 7) {
var documentElement = this.node.documentElement;
if (documentElement) {
return this.enter(documentElement);
} else {
return this.leave(this.node);
}
} else if (this.eventType === 1) {
var firstChild = this.node.firstChild;
if (firstChild) {
return this.enter(firstChild);
} else {
return this.leave(this.node);
...
next = function () { if (this.eventType === null) { return this.enter(this.root); } // START_DOCUMENT if (this.eventType === 7) { var documentElement = this.node.documentElement; if (documentElement) { return this.enter(documentElement); } else { return this.leave(this.node); } } else if (this.eventType === 1) { var firstChild = this.node.firstChild; if (firstChild) { return this.enter(firstChild); } else { return this.leave(this.node); } } else if (this.eventType === 2) { var nextSibling = this.node.nextSibling; if (nextSibling) { return this.enter(nextSibling); } else { return this.leave(this.node); } } else { return this.leave(this.node); } }
...
return null;
}
},
getText : function() {
return this.node.nodeValue;
},
nextTag : function() {
var et = this.next();
// TODO isWhiteSpace
while (et === 7 || et === 4 || et === 12 || et === 6 || et === 3 || et === 5) {
et = this.next();
}
if (et !== 1 && et !== 2) {
// TODO location
throw new Error('Expected start or end tag.');
...
nextTag = function () { var et = this.next(); // TODO isWhiteSpace while (et === 7 || et === 4 || et === 12 || et === 6 || et === 3 || et === 5) { et = this.next(); } if (et !== 1 && et !== 2) { // TODO location throw new Error('Expected start or end tag.'); } return et; }
...
skipElement : function() {
if (this.eventType !== Jsonix.XML.Input.START_ELEMENT) {
throw new Error("Parser must be on START_ELEMENT to skip element.");
}
var numberOfOpenTags = 1;
var et;
do {
et = this.nextTag();
numberOfOpenTags += (et === Jsonix.XML.Input.START_ELEMENT) ? 1 : -1;
} while (numberOfOpenTags > 0);
return et;
},
getElementText : function() {
if (this.eventType != 1) {
throw new Error("Parser must be on START_ELEMENT to read next text.");
...
popNS = function () { this.pns.pop(); }
...
return this.enter(nextSibling);
}
} else {
this.node = node;
this.attributes = null;
// END_ELEMENT
this.eventType = 2;
this.popNS();
return this.eventType;
}
}
var nextSibling1 = node.nextSibling;
if (nextSibling1) {
return this.enter(nextSibling1);
...
pushNS = function (node) { var pindex = this.pns.length - 1; var parentPnsItem = this.pns[pindex]; var pnsItem = Jsonix.Util.Type.isObject(parentPnsItem) ? pindex : parentPnsItem; this.pns.push(pnsItem); pindex++; var reference = true; if (node.attributes) { var attributes = node.attributes; var alength = attributes.length; if (alength > 0) { // If given node has attributes for (var aindex = 0; aindex < alength; aindex++) { var attribute = attributes[aindex]; var attributeName = attribute.nodeName; var p = null; var ns = null; var isNS = false; if (attributeName === 'xmlns') { p = ''; ns = attribute.value; isNS = true; } else if (attributeName.substring(0, 6) === 'xmlns:') { p = attributeName.substring(6); ns = attribute.value; isNS = true; } // Attribute is a namespace declaration if (isNS) { if (reference) { pnsItem = Jsonix.Util.Type.cloneObject(this.pns[pnsItem], {}); this.pns[pindex] = pnsItem; reference = false; } pnsItem[p] = ns; } } } } }
...
var nodeType = node.nodeType;
this.node = node;
this.attributes = null;
// Document node
if (nodeType === 1) {
// START_ELEMENT
this.eventType = 1;
this.pushNS(node);
return this.eventType;
} else if (nodeType === 2) {
// ATTRIBUTE
this.eventType = 10;
return this.eventType;
} else if (nodeType === 3) {
var nodeValue = node.nodeValue;
...
retrieveAttributes = function () { var attributes; if (this.attributes) { attributes = this.attributes; } else if (this.eventType === 1) { attributes = this.node.attributes; this.attributes = attributes; } else if (this.eventType === 10) { attributes = this.node.parentNode.attributes; this.attributes = attributes; } else { throw new Error("Attributes can only be retrieved for START_ELEMENT or ATTRIBUTE nodes."); } return attributes; }
...
this.attributes = attributes;
} else {
throw new Error("Attributes can only be retrieved for START_ELEMENT or ATTRIBUTE nodes.");
}
return attributes;
},
getAttributeCount : function() {
var attributes = this.retrieveAttributes();
return attributes.length;
},
getAttributeName : function(index) {
var attributes = this.retrieveAttributes();
if (index < 0 || index >= attributes.length) {
throw new Error("Invalid attribute index [" + index + "].");
}
...
retrieveElement = function () { var element; if (this.eventType === 1) { element = this.node; } else if (this.eventType === 10) { element = this.node.parentNode; } else { throw new Error("Element can only be retrieved for START_ELEMENT or ATTRIBUTE nodes."); } return element; }
...
throw new Error("Invalid attribute index [" + index + "].");
}
var attribute = attributes[index];
return attribute.value;
},
getAttributeValueNS : null,
getAttributeValueNSViaElement : function(namespaceURI, localPart) {
var element = this.retrieveElement();
return element.getAttributeNS(namespaceURI, localPart);
},
getAttributeValueNSViaAttribute : function(namespaceURI, localPart) {
var attributeNode = this.getAttributeNodeNS(namespaceURI, localPart);
if (Jsonix.Util.Type.exists(attributeNode)) {
return attributeNode.nodeValue;
}
...
skipElement = function () { if (this.eventType !== Jsonix.XML.Input.START_ELEMENT) { throw new Error("Parser must be on START_ELEMENT to skip element."); } var numberOfOpenTags = 1; var et; do { et = this.nextTag(); numberOfOpenTags += (et === Jsonix.XML.Input.START_ELEMENT) ? 1 : -1; } while (numberOfOpenTags > 0); return et; }
...
// TODO Refactor
var anyPropertyInfo = this.structure.any;
this.unmarshalProperty(context, input,
anyPropertyInfo, result);
} else {
// TODO optionally report a validation error that the element is not expected
et = input.skipElement();
}
} else if ((et === Jsonix.XML.Input.CHARACTERS || et === Jsonix.XML.Input.CDATA || et === Jsonix.XML.Input.ENTITY_REFERENCE
)) {
if (Jsonix.Util.Type.exists(this.structure.mixed))
{
// Characters and structure has a mixed property
var mixedPropertyInfo = this.structure.mixed;
this.unmarshalProperty(context, input,
...
declareNamespace = function (ns, p) { var index = this.pns.length - 1; var pnsItem = this.pns[index]; var reference; if (Jsonix.Util.Type.isNumber(pnsItem)) { // Resolve the reference reference = true; pnsItem = this.pns[pnsItem]; } else { reference = false; } // If this prefix is mapped to a different namespace and must be redeclared if (pnsItem[p] !== ns) { if (p === '') { this.writeAttribute({lp : Jsonix.XML.XMLNS_P}, ns); } else { this.writeAttribute({ns : Jsonix.XML.XMLNS_NS, lp : p, p : Jsonix.XML.XMLNS_P}, ns); } if (reference) { // If this was a reference, clone it and replace the reference pnsItem = Jsonix.Util.Type.cloneObject(pnsItem, {}); this.pns[index] = pnsItem; } pnsItem[p] = ns; } }
...
element = this.xmldom.createNode(1, qualifiedName, namespaceURI);
} else {
throw new Error("Could not create an element node.");
}
this.peek().appendChild(element);
this.push(element);
this.declareNamespace(namespaceURI, prefix);
if (this.documentElement === null)
{
this.documentElement = element;
this.declareNamespaces();
}
return element;
},
...
declareNamespaces = function () { var index = this.nsp.length - 1; var nspItem = this.nsp[index]; nspItem = Jsonix.Util.Type.isNumber(nspItem) ? this.nsp[nspItem] : nspItem; var ns, p; for (ns in nspItem) { if (nspItem.hasOwnProperty(ns)) { p = nspItem[ns]; this.declareNamespace(ns, p); } } }
...
}
this.peek().appendChild(element);
this.push(element);
this.declareNamespace(namespaceURI, prefix);
if (this.documentElement === null)
{
this.documentElement = element;
this.declareNamespaces();
}
return element;
},
writeEndElement : function() {
return this.pop();
},
writeCharacters : function(text) {
...
destroy = function () { this.xmldom = null; }
n/a
getNamespaceURI = function (p) { var pindex = this.pns.length - 1; var pnsItem = this.pns[pindex]; pnsItem = Jsonix.Util.Type.isObject(pnsItem) ? pnsItem : this.pns[pnsItem]; return pnsItem[p]; }
...
{
if (prefix === '' && Jsonix.Util.Type.isString(defaultNamespaceURI))
{
namespaceURI = defaultNamespaceURI;
}
else if (namespaceContext)
{
namespaceURI = namespaceContext.getNamespaceURI(prefix);
}
}
// If we don't have a namespace URI, assume '' by default
// TODO document the assumption
if (!Jsonix.Util.Type.isString(namespaceURI))
{
namespaceURI = defaultNamespaceURI || '';
...
getPrefix = function (ns, p) { var index = this.nsp.length - 1; var nspItem = this.nsp[index]; var reference; if (Jsonix.Util.Type.isNumber(nspItem)) { // This is a reference, the item is the index of the parent item reference = true; nspItem = this.nsp[nspItem]; } else { reference = false; } if (Jsonix.Util.Type.isString(p)) { var oldp = nspItem[ns]; // If prefix is already declared and equals the proposed prefix if (p === oldp) { // Nothing to do } else { // If this was a reference, we have to clone it now if (reference) { nspItem = Jsonix.Util.Type.cloneObject(nspItem, {}); this.nsp[index] = nspItem; } nspItem[ns] = p; } } else { p = nspItem[ns]; if (!Jsonix.Util.Type.exists(p)) { p = 'p' + (this.namespacePrefixIndex++); // If this was a reference, we have to clone it now if (reference) { nspItem = Jsonix.Util.Type.cloneObject(nspItem, {}); this.nsp[index] = nspItem; } nspItem[ns] = p; } } return p; }
...
this.string = (namespaceURI !== '' ? ('{' + namespaceURI + '}') : '') + (prefix !==
x27;' ? (prefix + ':') : '') + localPart;
},
toString : function() {
return this.string;
},
// foo:bar
toCanonicalString: function(namespaceContext) {
var canonicalPrefix = namespaceContext ? namespaceContext.getPrefix(this.namespaceURI
, this.prefix) : this.prefix;
return this.prefix + (this.prefix === '' ? '' : ':') + this.localPart;
},
clone : function() {
return new Jsonix.XML.QName(this.namespaceURI, this.localPart, this.prefix);
},
equals : function(that) {
if (!that) {
...
initialize = function (options) { // REWORK if (typeof ActiveXObject !== 'undefined') { this.xmldom = new ActiveXObject("Microsoft.XMLDOM"); } else { this.xmldom = null; } this.nodes = []; var rootNspItem = { '' : '' }; rootNspItem[Jsonix.XML.XMLNS_NS] = Jsonix.XML.XMLNS_P; if (Jsonix.Util.Type.isObject(options)) { if (Jsonix.Util.Type.isObject(options.namespacePrefixes)) { Jsonix.Util.Type.cloneObject(options.namespacePrefixes, rootNspItem); } } this.nsp = [rootNspItem]; var rootPnsItem = { '' : '' }; rootPnsItem[Jsonix.XML.XMLNS_P] = Jsonix.XML.XMLNS_NS; this.pns = [rootPnsItem]; }
n/a
peek = function () { return this.nodes[this.nodes.length - 1]; }
...
}
else if (this.xmldom) {
element = this.xmldom.createNode(1, qualifiedName, namespaceURI);
} else {
throw new Error("Could not create an element node.");
}
this.peek().appendChild(element);
this.push(element);
this.declareNamespace(namespaceURI, prefix);
if (this.documentElement === null)
{
this.documentElement = element;
this.declareNamespaces();
}
...
pop = function () { this.popNS(); var result = this.nodes.pop(); return result; }
...
pnsItem[p] = ns;
}
}
}
}
},
popNS : function () {
this.pns.pop();
},
getNamespaceURI : function (p) {
var pindex = this.pns.length - 1;
var pnsItem = this.pns[pindex];
pnsItem = Jsonix.Util.Type.isObject(pnsItem) ? pnsItem : this.pns[pnsItem];
return pnsItem[p];
},
...
popNS = function () { this.nsp.pop(); this.pns.pop(); }
...
return this.enter(nextSibling);
}
} else {
this.node = node;
this.attributes = null;
// END_ELEMENT
this.eventType = 2;
this.popNS();
return this.eventType;
}
}
var nextSibling1 = node.nextSibling;
if (nextSibling1) {
return this.enter(nextSibling1);
...
push = function (node) { this.nodes.push(node); this.pushNS(); return node; }
...
if (separatorChars.indexOf(str.charAt(i)) >= 0) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.push(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
...
pushNS = function () { var nindex = this.nsp.length - 1; var pindex = this.pns.length - 1; var parentNspItem = this.nsp[nindex]; var parentPnsItem = this.pns[pindex]; var nspItem = Jsonix.Util.Type.isObject(parentNspItem) ? nindex : parentNspItem; var pnsItem = Jsonix.Util.Type.isObject(parentPnsItem) ? pindex : parentPnsItem; this.nsp.push(nspItem); this.pns.push(pnsItem); }
...
var nodeType = node.nodeType;
this.node = node;
this.attributes = null;
// Document node
if (nodeType === 1) {
// START_ELEMENT
this.eventType = 1;
this.pushNS(node);
return this.eventType;
} else if (nodeType === 2) {
// ATTRIBUTE
this.eventType = 10;
return this.eventType;
} else if (nodeType === 3) {
var nodeValue = node.nodeValue;
...
writeAttribute = function (name, value) { Jsonix.Util.Ensure.ensureString(value); Jsonix.Util.Ensure.ensureObject(name); var localPart = name.localPart || name.lp || null; Jsonix.Util.Ensure.ensureString(localPart); var ns = name.namespaceURI || name.ns || null; var namespaceURI = Jsonix.Util.Type.isString(ns) ? ns : ''; var p = name.prefix || name.p || null; var prefix = this.getPrefix(namespaceURI, p); var qualifiedName = (!prefix ? localPart : prefix + ':' + localPart); var node = this.peek(); if (namespaceURI === '') { node.setAttribute(qualifiedName, value); } else { if (node.setAttributeNS) { node.setAttributeNS(namespaceURI, qualifiedName, value); } else { if (this.xmldom) { var attribute = this.document.createNode(2, qualifiedName, namespaceURI); attribute.nodeValue = value; node.setAttributeNode(attribute); } else if (namespaceURI === Jsonix.XML.XMLNS_NS) { // XMLNS namespace may be processed unqualified node.setAttribute(qualifiedName, value); } else { throw new Error("The [setAttributeNS] method is not implemented"); } } this.declareNamespace(namespaceURI, prefix); } }
...
reference = false;
}
// If this prefix is mapped to a different namespace and must be redeclared
if (pnsItem[p] !== ns)
{
if (p === '')
{
this.writeAttribute({lp : Jsonix.XML.XMLNS_P}, ns);
}
else
{
this.writeAttribute({ns : Jsonix.XML.XMLNS_NS, lp : p, p : Jsonix.XML.XMLNS_P}, ns);
}
if (reference)
{
...
writeCharacters = function (text) { var node; if (Jsonix.Util.Type.isFunction(this.document.createTextNode)) { node = this.document.createTextNode(text); } else if (this.xmldom) { node = this.xmldom.createTextNode(text); } else { throw new Error("Could not create a text node."); } this.peek().appendChild(node); return node; }
...
},
unmarshal : function(context, input, scope) {
var text = input.getElementText();
return this.parse(text, context, input, scope);
},
marshal : function(value, context, output, scope) {
if (Jsonix.Util.Type.exists(value)) {
output.writeCharacters(this.reprint(value, context, output, scope));
}
},
reprint : function(value, context, output, scope) {
if (Jsonix.Util.Type.isString(value) && !this.isInstance(value, context, scope)) {
// Using null as input since input is not available
return this.print(this.parse(value, context, null, scope), context, output, scope);
} else {
...
writeEndDocument = function () { return this.pop(); }
...
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
CLASS_NAME : 'Jsonix.Binding.Marshaller.Simplified'
});
...
writeEndElement = function () { return this.pop(); }
...
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
}
output.writeEndElement();
} else {
throw new Error("Element [" + elementValue.name.key + "] is not known in this context, could not determine its
type.");
}
},
getTypeInfoByElementName : function(name, context, scope) {
var elementInfo = context.getElementInfo(name, scope);
if (Jsonix.Util.Type.exists(elementInfo)) {
...
writeNode = function (node) { var importedNode; if (Jsonix.Util.Type.exists(this.document.importNode)) { importedNode = this.document.importNode(node, true); } else { importedNode = node; } this.peek().appendChild(importedNode); return importedNode; }
...
// TODO
throw new Error("Property is not mixed, can't handle string values.");
} else {
output.writeCharacters(value);
}
} else if (this.allowDom && Jsonix.Util.Type.exists(value.nodeType)) {
// DOM node
output.writeNode(value);
} else if (Jsonix.Util.Type.isObject(value)) {
this.marshalElement(value, context, output, scope);
} else {
if (this.mixed) {
throw new Error("Unsupported content type, either objects or strings are supported.");
} else {
...
writeStartDocument = function () { // TODO Check var doc = Jsonix.DOM.createDocument(); this.document = doc; return this.push(doc); }
...
return text;
},
marshalDocument : function(value) {
var output = new Jsonix.XML.Output({
namespacePrefixes : this.context.namespacePrefixes
});
var doc = output.writeStartDocument();
this.marshalElement(value, this.context, output, undefined);
output.writeEndDocument();
return doc;
},
CLASS_NAME : 'Jsonix.Binding.Marshaller'
});
Jsonix.Binding.Marshaller.Simplified = Jsonix.Class(Jsonix.Binding.Marshaller, {
...
writeStartElement = function (name) { Jsonix.Util.Ensure.ensureObject(name); var localPart = name.localPart || name.lp || null; Jsonix.Util.Ensure.ensureString(localPart); var ns = name.namespaceURI || name.ns || null; var namespaceURI = Jsonix.Util.Type.isString(ns) ? ns : ''; var p = name.prefix || name.p; var prefix = this.getPrefix(namespaceURI, p); var qualifiedName = (!prefix ? localPart : prefix + ':' + localPart); var element; if (Jsonix.Util.Type.isFunction(this.document.createElementNS)) { element = this.document.createElementNS(namespaceURI, qualifiedName); } else if (this.xmldom) { element = this.xmldom.createNode(1, qualifiedName, namespaceURI); } else { throw new Error("Could not create an element node."); } this.peek().appendChild(element); this.push(element); this.declareNamespace(namespaceURI, prefix); if (this.documentElement === null) { this.documentElement = element; this.declareNamespaces(); } return element; }
...
if (typeInfoByValue && typeInfoByValue.typeName)
{
actualTypeInfo = typeInfoByValue;
}
}
var typeInfo = actualTypeInfo || declaredTypeInfo;
if (typeInfo) {
output.writeStartElement(elementValue.name);
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
var xsiTypeName = actualTypeInfo.typeName;
var xsiType = Jsonix.Schema.XSD.QName.INSTANCE.print(xsiTypeName, context, output, scope);
output.writeAttribute(Jsonix.Schema.XSI.TYPE_QNAME, xsiType);
}
if (Jsonix.Util.Type.exists(elementValue.value)) {
typeInfo.marshal(elementValue.value, context, output, scope);
...
clone = function () { return new Jsonix.XML.QName(this.namespaceURI, this.localPart, this.prefix); }
n/a
equals = function (that) { if (!that) { return false; } else { return (this.namespaceURI == that.namespaceURI) && (this.localPart == that.localPart); } }
n/a
initialize = function (one, two, three) { var namespaceURI; var localPart; var prefix; var key; var string; if (!Jsonix.Util.Type.exists(two)) { namespaceURI = ''; localPart = one; prefix = ''; } else if (!Jsonix.Util.Type.exists(three)) { namespaceURI = Jsonix.Util.Type.exists(one) ? one : ''; localPart = two; var colonPosition = two.indexOf(':'); if (colonPosition > 0 && colonPosition < two.length) { prefix = two.substring(0, colonPosition); localPart = two.substring(colonPosition + 1); } else { prefix = ''; localPart = two; } } else { namespaceURI = Jsonix.Util.Type.exists(one) ? one : ''; localPart = two; prefix = Jsonix.Util.Type.exists(three) ? three : ''; } this.namespaceURI = namespaceURI; this.localPart = localPart; this.prefix = prefix; this.key = (namespaceURI !== '' ? ('{' + namespaceURI + '}') : '') + localPart; this.string = (namespaceURI !== '' ? ('{' + namespaceURI + '}') : '') + (prefix !== '' ? (prefix + ':') : '') + localPart; }
n/a
toCanonicalString = function (namespaceContext) { var canonicalPrefix = namespaceContext ? namespaceContext.getPrefix(this.namespaceURI, this.prefix) : this.prefix; return this.prefix + (this.prefix === '' ? '' : ':') + this.localPart; }
...
value : typedNamedValue.value
};
}
});
Jsonix.Binding.Unmarshalls.Element.AsSimplifiedElementRef = Jsonix.Class({
convertFromTypedNamedValue : function(typedNamedValue, context, input, scope) {
var propertyName = typedNamedValue.name.toCanonicalString(context);
var value = {};
value[propertyName] = typedNamedValue.value;
return value;
}
});
Jsonix.Binding.Marshaller = Jsonix.Class(Jsonix.Binding.Marshalls.Element, Jsonix.Binding.Marshalls.Element.AsElementRef, {
context : null,
...
toString = function () { return this.string; }
...
value : value,
typeInfo : typeInfo
};
elementValue = this.convertFromTypedNamedValue(typedNamedValue, context, input, scope);
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled
as is not known in this context and the property does not allow DOM content.");
}
} else if (this.allowDom) {
elementValue = input.getElement();
} else {
throw new Error("Element [" + name.toString() + "] could not be unmarshalled as the property neither allows typed
objects nor DOM as content. This is a sign of invalid mappings, do not use [allowTypedObject : false] and [allowDom : false] at
the same time.");
}
callback(elementValue);
...