org.hisrc.jsonix.Jsonix.Context.js Maven / Gradle / Ivy
Jsonix.Context = Jsonix
.Class(Jsonix.Mapping.Styled, {
modules : [],
typeInfos : null,
typeNameKeyToTypeInfo : null,
elementInfos : null,
options : null,
substitutionMembersMap : null,
scopedElementInfosMap : null,
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)) {
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();
},
createModule : function(mapping) {
var module;
if (mapping instanceof this.mappingStyle.module) {
module = mapping;
} else {
module = new this.mappingStyle.module(mapping,
{
mappingStyle : this.mappingStyle
});
}
return module;
},
registerBuiltinTypeInfos : function() {
for ( var index = 0; index < this.builtinTypeInfos.length; index++) {
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];
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;
if (typeInfo.typeName && typeInfo.typeName.key)
{
this.typeNameKeyToTypeInfo[typeInfo.typeName.key] = typeInfo;
}
},
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;
}
},
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;
},
getTypeInfoByTypeNameKey : function(typeNameKey) {
return this.typeNameKeyToTypeInfo[typeNameKey];
},
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.");
},
getSubstitutionMembers : function(name) {
return this.substitutionMembersMap[Jsonix.XML.QName
.fromObject(name).key];
},
createMarshaller : function() {
return new this.mappingStyle.marshaller(this);
},
createUnmarshaller : function() {
return new this.mappingStyle.unmarshaller(this);
},
getNamespaceURI : function(prefix) {
Jsonix.Util.Ensure.ensureString(prefix);
return this.prefixNamespaces[prefix];
},
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;
}
},
/**
* Builtin type infos.
*/
builtinTypeInfos : [
Jsonix.Schema.XSD.AnyType.INSTANCE,
Jsonix.Schema.XSD.AnyURI.INSTANCE,
Jsonix.Schema.XSD.Base64Binary.INSTANCE,
Jsonix.Schema.XSD.Boolean.INSTANCE,
Jsonix.Schema.XSD.Byte.INSTANCE,
Jsonix.Schema.XSD.Calendar.INSTANCE,
Jsonix.Schema.XSD.Date.INSTANCE,
Jsonix.Schema.XSD.DateTime.INSTANCE,
Jsonix.Schema.XSD.Decimal.INSTANCE,
Jsonix.Schema.XSD.Double.INSTANCE,
Jsonix.Schema.XSD.Duration.INSTANCE,
Jsonix.Schema.XSD.Float.INSTANCE,
Jsonix.Schema.XSD.GDay.INSTANCE,
Jsonix.Schema.XSD.GMonth.INSTANCE,
Jsonix.Schema.XSD.GMonthDay.INSTANCE,
Jsonix.Schema.XSD.GYear.INSTANCE,
Jsonix.Schema.XSD.GYearMonth.INSTANCE,
Jsonix.Schema.XSD.HexBinary.INSTANCE,
Jsonix.Schema.XSD.ID.INSTANCE,
Jsonix.Schema.XSD.IDREF.INSTANCE,
Jsonix.Schema.XSD.IDREFS.INSTANCE,
Jsonix.Schema.XSD.Int.INSTANCE,
Jsonix.Schema.XSD.Integer.INSTANCE,
Jsonix.Schema.XSD.Language.INSTANCE,
Jsonix.Schema.XSD.Long.INSTANCE,
Jsonix.Schema.XSD.Name.INSTANCE,
Jsonix.Schema.XSD.NCName.INSTANCE,
Jsonix.Schema.XSD.NegativeInteger.INSTANCE,
Jsonix.Schema.XSD.NMToken.INSTANCE,
Jsonix.Schema.XSD.NMTokens.INSTANCE,
Jsonix.Schema.XSD.NonNegativeInteger.INSTANCE,
Jsonix.Schema.XSD.NonPositiveInteger.INSTANCE,
Jsonix.Schema.XSD.NormalizedString.INSTANCE,
Jsonix.Schema.XSD.Number.INSTANCE,
Jsonix.Schema.XSD.PositiveInteger.INSTANCE,
Jsonix.Schema.XSD.QName.INSTANCE,
Jsonix.Schema.XSD.Short.INSTANCE,
Jsonix.Schema.XSD.String.INSTANCE,
Jsonix.Schema.XSD.Strings.INSTANCE,
Jsonix.Schema.XSD.Time.INSTANCE,
Jsonix.Schema.XSD.Token.INSTANCE,
Jsonix.Schema.XSD.UnsignedByte.INSTANCE,
Jsonix.Schema.XSD.UnsignedInt.INSTANCE,
Jsonix.Schema.XSD.UnsignedLong.INSTANCE,
Jsonix.Schema.XSD.UnsignedShort.INSTANCE ],
CLASS_NAME : 'Jsonix.Context'
});
© 2015 - 2024 Weber Informatics LLC | Privacy Policy