From 5ccd7100ee7850dcff6a688bfa2a95f7b4dee0c4 Mon Sep 17 00:00:00 2001 From: Olivier Guyot Date: Thu, 8 Jun 2017 12:10:58 +0200 Subject: [PATCH 1/2] added CDATA option for value property --- dist/Jsonix-all.js | 61 +++++++++++++------ dist/Jsonix-min.js | 17 ++++-- nodejs/scripts/jsonix.js | 61 +++++++++++++------ .../jsonix/Jsonix/Model/ValuePropertyInfo.js | 10 ++- .../org/hisrc/jsonix/Jsonix/XML/Output.js | 19 +++++- .../org/hisrc/jsonix/samples/one/One.js | 15 ++++- .../hisrc/jsonix/samples/one/test/OneTest.js | 28 ++++++++- 7 files changed, 164 insertions(+), 47 deletions(-) diff --git a/dist/Jsonix-all.js b/dist/Jsonix-all.js index ab2f9d12f..348e2ea30 100644 --- a/dist/Jsonix-all.js +++ b/dist/Jsonix-all.js @@ -1618,6 +1618,19 @@ Jsonix.XML.Output = Jsonix.Class({ return node; }, + writeCDATA : function(text) { + var node; + if (Jsonix.Util.Type.isFunction(this.document.createCDATASection)) { + node = this.document.createCDATASection(text); + } + else if (this.xmldom) { + node = this.xmldom.createCDATASection(text); + } else { + throw new Error("Could not create a CDATA section node."); + } + this.peek().appendChild(node); + return node; + }, writeAttribute : function(name, value) { Jsonix.Util.Ensure.ensureString(value); Jsonix.Util.Ensure.ensureObject(name); @@ -1655,7 +1668,7 @@ Jsonix.XML.Output = Jsonix.Class({ } this.declareNamespace(namespaceURI, prefix); } - + }, writeNode : function(node) { var importedNode; @@ -1764,7 +1777,7 @@ Jsonix.XML.Output = Jsonix.Class({ if (Jsonix.Util.Type.isString(p)) { var oldp = nspItem[ns]; - // If prefix is already declared and equals the proposed prefix + // If prefix is already declared and equals the proposed prefix if (p === oldp) { // Nothing to do @@ -1804,6 +1817,7 @@ Jsonix.XML.Output = Jsonix.Class({ }, CLASS_NAME : "Jsonix.XML.Output" }); + Jsonix.Mapping = {}; Jsonix.Mapping.Style = Jsonix.Class({ marshaller : null, @@ -1866,6 +1880,7 @@ Jsonix.Binding.Marshalls.Element = Jsonix.Class({ } } var typeInfo = actualTypeInfo || declaredTypeInfo; + if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { @@ -2145,22 +2160,22 @@ Jsonix.Model.ClassInfo = Jsonix 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? @@ -2168,9 +2183,9 @@ Jsonix.Model.ClassInfo = Jsonix 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)) @@ -2185,14 +2200,14 @@ Jsonix.Model.ClassInfo = Jsonix { 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]); - } + } }, getPropertyInfoByName : function(name) { return this.propertiesMap[name]; @@ -2237,15 +2252,15 @@ Jsonix.Model.ClassInfo = Jsonix unmarshal : function(context, input) { this.build(context); var result; - + if (this.instanceFactory) { result = new this.instanceFactory(); } else { - result = { TYPE_NAME : this.name }; + result = { TYPE_NAME : this.name }; } - + if (input.eventType !== 1) { throw new Error("Parser must be on START_ELEMENT to read a class info."); } @@ -2526,6 +2541,7 @@ Jsonix.Model.ClassInfo.prototype.propertyInfoCreators = { "v" : Jsonix.Model.ClassInfo.prototype.v, "value" : Jsonix.Model.ClassInfo.prototype.v }; + Jsonix.Model.EnumLeafInfo = Jsonix.Class(Jsonix.Model.TypeInfo, { name : null, baseTypeInfo : 'String', @@ -2948,6 +2964,9 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf initialize : function(mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this, [ mapping ]); + + var c = mapping.asCDATA || mapping.c || false; + this.asCDATA = c; }, unmarshal : function(context, input, scope) { var text = input.getElementText(); @@ -2957,7 +2976,12 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf if (!Jsonix.Util.Type.exists(value)) { return; } - output.writeCharacters(this.print(value, context, output, scope)); + + if (this.asCDATA) { + output.writeCDATA(this.print(value, context, output, scope)); + } else { + output.writeCharacters(this.print(value, context, output, scope)); + } }, buildStructure : function(context, structure) { Jsonix.Util.Ensure.ensureObject(structure); @@ -5820,15 +5844,15 @@ Jsonix.Context = Jsonix Jsonix.Util.Ensure.ensureObject(options); if (Jsonix.Util.Type .isObject(options.namespacePrefixes)) { - this.namespacePrefixes = + this.namespacePrefixes = Jsonix.Util.Type.cloneObject(options.namespacePrefixes, {}); } if (Jsonix.Util.Type .isBoolean(options.supportXsiType)) { - this.supportXsiType = options.supportXsiType; + this.supportXsiType = options.supportXsiType; } } - + // Initialize prefix/namespace mapping for (var ns in this.namespacePrefixes) { @@ -5857,7 +5881,7 @@ Jsonix.Context = Jsonix module = mapping; } else { mapping = Jsonix.Util.Type.cloneObject(mapping); - module = new this.mappingStyle.module(mapping, + module = new this.mappingStyle.module(mapping, { mappingStyle : this.mappingStyle }); @@ -6098,6 +6122,7 @@ Jsonix.Context = Jsonix Jsonix.Schema.XSD.UnsignedShort.INSTANCE ], CLASS_NAME : 'Jsonix.Context' }); + // Complete Jsonix script is included above return { Jsonix: Jsonix }; }; diff --git a/dist/Jsonix-min.js b/dist/Jsonix-min.js index a4f845426..c9c72e077 100644 --- a/dist/Jsonix-min.js +++ b/dist/Jsonix-min.js @@ -650,6 +650,12 @@ if(Jsonix.Util.Type.isFunction(this.document.createTextNode)){d=this.document.cr }else{throw new Error("Could not create a text node.") }}this.peek().appendChild(d); return d +},writeCDATA:function(c){var d; +if(Jsonix.Util.Type.isFunction(this.document.createCDATASection)){d=this.document.createCDATASection(c) +}else{if(this.xmldom){d=this.xmldom.createCDATASection(c) +}else{throw new Error("Could not create a CDATA section node.") +}}this.peek().appendChild(d); +return d },writeAttribute:function(t,k){Jsonix.Util.Ensure.ensureString(k); Jsonix.Util.Ensure.ensureObject(t); var n=t.localPart||t.lp||null; @@ -1149,13 +1155,16 @@ Jsonix.Util.Ensure.ensureObject(e.attributes); var d=this.attributeName.key; e.attributes[d]=this },CLASS_NAME:"Jsonix.Model.AttributePropertyInfo"}); -Jsonix.Model.ValuePropertyInfo=Jsonix.Class(Jsonix.Model.SingleTypePropertyInfo,{initialize:function(b){Jsonix.Util.Ensure.ensureObject(b); -Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this,[b]) +Jsonix.Model.ValuePropertyInfo=Jsonix.Class(Jsonix.Model.SingleTypePropertyInfo,{initialize:function(d){Jsonix.Util.Ensure.ensureObject(d); +Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this,[d]); +var c=d.asCDATA||d.c||false; +this.asCDATA=c },unmarshal:function(e,f,h){var g=f.getElementText(); return this.unmarshalValue(g,e,f,h) },marshal:function(g,e,f,h){if(!Jsonix.Util.Type.exists(g)){return -}f.writeCharacters(this.print(g,e,f,h)) -},buildStructure:function(c,d){Jsonix.Util.Ensure.ensureObject(d); +}if(this.asCDATA){f.writeCDATA(this.print(g,e,f,h)) +}else{f.writeCharacters(this.print(g,e,f,h)) +}},buildStructure:function(c,d){Jsonix.Util.Ensure.ensureObject(d); if(Jsonix.Util.Type.exists(d.elements)){throw new Error("The structure already defines element mappings, it cannot define a value property.") }else{d.value=this }},CLASS_NAME:"Jsonix.Model.ValuePropertyInfo"}); diff --git a/nodejs/scripts/jsonix.js b/nodejs/scripts/jsonix.js index ab2f9d12f..348e2ea30 100644 --- a/nodejs/scripts/jsonix.js +++ b/nodejs/scripts/jsonix.js @@ -1618,6 +1618,19 @@ Jsonix.XML.Output = Jsonix.Class({ return node; }, + writeCDATA : function(text) { + var node; + if (Jsonix.Util.Type.isFunction(this.document.createCDATASection)) { + node = this.document.createCDATASection(text); + } + else if (this.xmldom) { + node = this.xmldom.createCDATASection(text); + } else { + throw new Error("Could not create a CDATA section node."); + } + this.peek().appendChild(node); + return node; + }, writeAttribute : function(name, value) { Jsonix.Util.Ensure.ensureString(value); Jsonix.Util.Ensure.ensureObject(name); @@ -1655,7 +1668,7 @@ Jsonix.XML.Output = Jsonix.Class({ } this.declareNamespace(namespaceURI, prefix); } - + }, writeNode : function(node) { var importedNode; @@ -1764,7 +1777,7 @@ Jsonix.XML.Output = Jsonix.Class({ if (Jsonix.Util.Type.isString(p)) { var oldp = nspItem[ns]; - // If prefix is already declared and equals the proposed prefix + // If prefix is already declared and equals the proposed prefix if (p === oldp) { // Nothing to do @@ -1804,6 +1817,7 @@ Jsonix.XML.Output = Jsonix.Class({ }, CLASS_NAME : "Jsonix.XML.Output" }); + Jsonix.Mapping = {}; Jsonix.Mapping.Style = Jsonix.Class({ marshaller : null, @@ -1866,6 +1880,7 @@ Jsonix.Binding.Marshalls.Element = Jsonix.Class({ } } var typeInfo = actualTypeInfo || declaredTypeInfo; + if (typeInfo) { output.writeStartElement(elementValue.name); if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) { @@ -2145,22 +2160,22 @@ Jsonix.Model.ClassInfo = Jsonix 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? @@ -2168,9 +2183,9 @@ Jsonix.Model.ClassInfo = Jsonix 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)) @@ -2185,14 +2200,14 @@ Jsonix.Model.ClassInfo = Jsonix { 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]); - } + } }, getPropertyInfoByName : function(name) { return this.propertiesMap[name]; @@ -2237,15 +2252,15 @@ Jsonix.Model.ClassInfo = Jsonix unmarshal : function(context, input) { this.build(context); var result; - + if (this.instanceFactory) { result = new this.instanceFactory(); } else { - result = { TYPE_NAME : this.name }; + result = { TYPE_NAME : this.name }; } - + if (input.eventType !== 1) { throw new Error("Parser must be on START_ELEMENT to read a class info."); } @@ -2526,6 +2541,7 @@ Jsonix.Model.ClassInfo.prototype.propertyInfoCreators = { "v" : Jsonix.Model.ClassInfo.prototype.v, "value" : Jsonix.Model.ClassInfo.prototype.v }; + Jsonix.Model.EnumLeafInfo = Jsonix.Class(Jsonix.Model.TypeInfo, { name : null, baseTypeInfo : 'String', @@ -2948,6 +2964,9 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf initialize : function(mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this, [ mapping ]); + + var c = mapping.asCDATA || mapping.c || false; + this.asCDATA = c; }, unmarshal : function(context, input, scope) { var text = input.getElementText(); @@ -2957,7 +2976,12 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf if (!Jsonix.Util.Type.exists(value)) { return; } - output.writeCharacters(this.print(value, context, output, scope)); + + if (this.asCDATA) { + output.writeCDATA(this.print(value, context, output, scope)); + } else { + output.writeCharacters(this.print(value, context, output, scope)); + } }, buildStructure : function(context, structure) { Jsonix.Util.Ensure.ensureObject(structure); @@ -5820,15 +5844,15 @@ Jsonix.Context = Jsonix Jsonix.Util.Ensure.ensureObject(options); if (Jsonix.Util.Type .isObject(options.namespacePrefixes)) { - this.namespacePrefixes = + this.namespacePrefixes = Jsonix.Util.Type.cloneObject(options.namespacePrefixes, {}); } if (Jsonix.Util.Type .isBoolean(options.supportXsiType)) { - this.supportXsiType = options.supportXsiType; + this.supportXsiType = options.supportXsiType; } } - + // Initialize prefix/namespace mapping for (var ns in this.namespacePrefixes) { @@ -5857,7 +5881,7 @@ Jsonix.Context = Jsonix module = mapping; } else { mapping = Jsonix.Util.Type.cloneObject(mapping); - module = new this.mappingStyle.module(mapping, + module = new this.mappingStyle.module(mapping, { mappingStyle : this.mappingStyle }); @@ -6098,6 +6122,7 @@ Jsonix.Context = Jsonix Jsonix.Schema.XSD.UnsignedShort.INSTANCE ], CLASS_NAME : 'Jsonix.Context' }); + // Complete Jsonix script is included above return { Jsonix: Jsonix }; }; diff --git a/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/ValuePropertyInfo.js b/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/ValuePropertyInfo.js index ecfa77c69..e6ba396ea 100644 --- a/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/ValuePropertyInfo.js +++ b/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Model/ValuePropertyInfo.js @@ -2,6 +2,9 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf initialize : function(mapping) { Jsonix.Util.Ensure.ensureObject(mapping); Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this, [ mapping ]); + + var c = mapping.asCDATA || mapping.c || false; + this.asCDATA = c; }, unmarshal : function(context, input, scope) { var text = input.getElementText(); @@ -11,7 +14,12 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf if (!Jsonix.Util.Type.exists(value)) { return; } - output.writeCharacters(this.print(value, context, output, scope)); + + if (this.asCDATA) { + output.writeCDATA(this.print(value, context, output, scope)); + } else { + output.writeCharacters(this.print(value, context, output, scope)); + } }, buildStructure : function(context, structure) { Jsonix.Util.Ensure.ensureObject(structure); diff --git a/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/XML/Output.js b/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/XML/Output.js index 02da834ad..601c52166 100644 --- a/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/XML/Output.js +++ b/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/XML/Output.js @@ -95,6 +95,19 @@ Jsonix.XML.Output = Jsonix.Class({ return node; }, + writeCDATA : function(text) { + var node; + if (Jsonix.Util.Type.isFunction(this.document.createCDATASection)) { + node = this.document.createCDATASection(text); + } + else if (this.xmldom) { + node = this.xmldom.createCDATASection(text); + } else { + throw new Error("Could not create a CDATA section node."); + } + this.peek().appendChild(node); + return node; + }, writeAttribute : function(name, value) { Jsonix.Util.Ensure.ensureString(value); Jsonix.Util.Ensure.ensureObject(name); @@ -132,7 +145,7 @@ Jsonix.XML.Output = Jsonix.Class({ } this.declareNamespace(namespaceURI, prefix); } - + }, writeNode : function(node) { var importedNode; @@ -241,7 +254,7 @@ Jsonix.XML.Output = Jsonix.Class({ if (Jsonix.Util.Type.isString(p)) { var oldp = nspItem[ns]; - // If prefix is already declared and equals the proposed prefix + // If prefix is already declared and equals the proposed prefix if (p === oldp) { // Nothing to do @@ -280,4 +293,4 @@ Jsonix.XML.Output = Jsonix.Class({ return pnsItem[p]; }, CLASS_NAME : "Jsonix.XML.Output" -}); \ No newline at end of file +}); diff --git a/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/One.js b/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/One.js index 35ec953b2..bae2fb0e8 100644 --- a/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/One.js +++ b/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/One.js @@ -3,6 +3,9 @@ One = {}; One.ValueType = new Jsonix.Model.ClassInfo({ name : "One.ValueType" }); +One.ValueAsCDATAType = new Jsonix.Model.ClassInfo({ + name : "One.ValueAsCDATAType" +}); One.AnyAttributeType = new Jsonix.Model.ClassInfo({ name : "One.AnyAttributeType" }); @@ -35,6 +38,11 @@ One.ValueType.properties = [ new Jsonix.Model.ValuePropertyInfo({ name : "value", typeInfo : Jsonix.Schema.XSD.String.INSTANCE }) ]; +One.ValueAsCDATAType.properties = [ new Jsonix.Model.ValuePropertyInfo({ + name : "value", + typeInfo : Jsonix.Schema.XSD.String.INSTANCE, + asCDATA: true +}) ]; One.AnyAttributeType.properties = [ new Jsonix.Model.AnyAttributePropertyInfo({ name : "attributes" }) ]; @@ -211,7 +219,7 @@ One.AnyElementType.properties = [ new Jsonix.Model.AttributePropertyInfo({ }) ]; One.SimpleTypesType.properties = [ - // + // new Jsonix.Model.ElementPropertyInfo({ name : "date", typeInfo : Jsonix.Schema.XSD.DateAsDate.INSTANCE @@ -303,6 +311,9 @@ One.ElementMapType.properties = [ new Jsonix.Model.ElementMapPropertyInfo({ One.elementInfos = [ { elementName : new Jsonix.XML.QName('value'), typeInfo : One.ValueType +}, { + elementName : new Jsonix.XML.QName('valueAsCDATA'), + typeInfo : One.ValueAsCDATAType }, { elementName : new Jsonix.XML.QName('anyAttribute'), typeInfo : One.AnyAttributeType @@ -349,4 +360,4 @@ Two = { elementName : new Jsonix.XML.QName('string'), typeInfo : Jsonix.Schema.XSD.String.INSTANCE } ] -}; \ No newline at end of file +}; diff --git a/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/test/OneTest.js b/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/test/OneTest.js index 30989217c..c1931f1e7 100644 --- a/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/test/OneTest.js +++ b/scripts/src/test/javascript/org/hisrc/jsonix/samples/one/test/OneTest.js @@ -688,7 +688,7 @@ function testOneUnmarhshalUnexpectedElementType() { var text = '' + // '1356' + - // + // 'earth' + // ''; @@ -714,3 +714,29 @@ function testOneMarhshalUnexpectedElementType() { logger.debug(serializedNode); assertTrue(serializedNode.length > 5); } + +function testOneMarshalCDATAValueType() +{ + var context = new Jsonix.Context([ One ]); + var marshaller = context.createMarshaller(); + var value = { + name : { + localPart : "valueAsCDATA" + }, + value : { + value : 'test<>?\'"&' + } + }; + var result = marshaller.marshalString(value); + logger.debug(result); + assertTrue(result === '?\'"&]]>'); +} +function testOneUnmarshalCDATAValueType() +{ + var context = new Jsonix.Context([ One ]); + var unmarshaller = context.createUnmarshaller(); + var text = '?\'"&]]>'; + var result = unmarshaller.unmarshalString(text); + logger.debug(result.value.value); + assertTrue(result.value.value === 'test<>?\'"&'); +} From 09a64b1dab6ae9ff2991e97db4922381849c3b87 Mon Sep 17 00:00:00 2001 From: Olivier Guyot Date: Tue, 13 Jun 2017 15:54:41 +0200 Subject: [PATCH 2/2] added nodejs tests for CDATA --- nodejs/tests/basic/tests/One/Mappings.js | 13 +++++- nodejs/tests/basic/tests/one.js | 51 ++++++++++++++++++------ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/nodejs/tests/basic/tests/One/Mappings.js b/nodejs/tests/basic/tests/One/Mappings.js index 9a7282f73..5e61f6e79 100644 --- a/nodejs/tests/basic/tests/One/Mappings.js +++ b/nodejs/tests/basic/tests/One/Mappings.js @@ -5,6 +5,9 @@ module.exports.One = One; One.ValueType = new Jsonix.Model.ClassInfo({ name : "One.ValueType" }); +One.ValueAsCDATAType = new Jsonix.Model.ClassInfo({ + name : "One.ValueAsCDATAType" +}); One.AnyAttributeType = new Jsonix.Model.ClassInfo({ name : "One.AnyAttributeType" }); @@ -37,6 +40,11 @@ One.ValueType.properties = [ new Jsonix.Model.ValuePropertyInfo({ name : "value", typeInfo : Jsonix.Schema.XSD.String.INSTANCE }) ]; +One.ValueAsCDATAType.properties = [ new Jsonix.Model.ValuePropertyInfo({ + name : "value", + typeInfo : Jsonix.Schema.XSD.String.INSTANCE, + asCDATA: true +}) ]; One.AnyAttributeType.properties = [ new Jsonix.Model.AnyAttributePropertyInfo({ name : "attributes" }) ]; @@ -213,7 +221,7 @@ One.AnyElementType.properties = [ new Jsonix.Model.AttributePropertyInfo({ }) ]; One.SimpleTypesType.properties = [ - // + // new Jsonix.Model.ElementPropertyInfo({ name : "date", typeInfo : Jsonix.Schema.XSD.DateAsDate.INSTANCE @@ -305,6 +313,9 @@ One.ElementMapType.properties = [ new Jsonix.Model.ElementMapPropertyInfo({ One.elementInfos = [ { elementName : new Jsonix.XML.QName('value'), typeInfo : One.ValueType +}, { + elementName : new Jsonix.XML.QName('valueAsCDATA'), + typeInfo : One.ValueAsCDATAType }, { elementName : new Jsonix.XML.QName('anyAttribute'), typeInfo : One.AnyAttributeType diff --git a/nodejs/tests/basic/tests/one.js b/nodejs/tests/basic/tests/one.js index 6d8068cc0..8ee6cbc6d 100644 --- a/nodejs/tests/basic/tests/one.js +++ b/nodejs/tests/basic/tests/one.js @@ -81,7 +81,7 @@ module.exports = test.equal('c', result.value.attributes['{urn:c}c']); test.done(); }, - + "MarhshalAttributeType" : function(test) { var context = new Jsonix.Context([ One ], { namespacePrefixes : {} @@ -125,7 +125,7 @@ module.exports = test.equal('test', result.value.attribute); test.done(); }, - + "MarhshalElementType" : function(test) { var context = new Jsonix.Context([ One ]); var marshaller = context.createMarshaller(); @@ -145,7 +145,7 @@ module.exports = test.ok(serializedNode.length > 5); test.done(); }, - + "UnmarhshalElementType" : function(test) { var context = new Jsonix.Context([ One ]); var unmarshaller = context.createUnmarshaller(); @@ -190,7 +190,7 @@ module.exports = test.equal('f', result.value.items[2]); test.done(); }, - + "MarhshalElementsType" : function(test) { var context = new Jsonix.Context([ One ]); var marshaller = context.createMarshaller(); @@ -226,7 +226,7 @@ module.exports = test.ok(serializedNode.length > 5); test.done(); }, - + "UnmarhshalElementsType" : function(test) { var context = new Jsonix.Context([ One ]); var unmarshaller = context.createUnmarshaller(); @@ -282,7 +282,7 @@ module.exports = console.log(marshaller.marshalString(result)); test.done(); }, - + "MarhshalElementRefType" : function(test) { var context = new Jsonix.Context([ One, Two ], { namespacePrefixes : { @@ -390,7 +390,7 @@ module.exports = test.equal('twelve', result.value.mix[2]); test.done(); }, - + "UnmarshalElementRefsType" : function(test) { var context = new Jsonix.Context([ One ]); var unmarshaller = context.createUnmarshaller(); @@ -495,7 +495,7 @@ module.exports = } }, 'three', Jsonix.DOM.parse('four').documentElement ] } - + }; var node = marshaller.marshalDocument(value); var serializedNode = Jsonix.DOM.serialize(node); @@ -503,7 +503,7 @@ module.exports = test.ok(serializedNode.length > 5); test.done(); }, - + "UnmarhshalAnyElementType" : function(test) { var context = new Jsonix.Context([ One, Two ]); var unmarshaller = context.createUnmarshaller(); @@ -561,7 +561,7 @@ module.exports = test.ok(serializedNode.length > 5); test.done(); }, - + "UnmarhshalSimpleTypesType" : function(test) { var context = new Jsonix.Context([ One ]); var unmarshaller = context.createUnmarshaller(); @@ -598,7 +598,7 @@ module.exports = test.equal(1.1, result.value['double']); test.equal(2, result.value.integer); test.equal('three', result.value.string); - + // test.equal(2000, result.value.dates[0].getFullYear()); test.equal(2001, result.value.dates[1].getFullYear()); @@ -614,7 +614,7 @@ module.exports = test.equal(7, result.value.doublesList[2][1]); test.done(); }, - + "UnmarhshalMapElementType" : function(test) { var context = new Jsonix.Context([ One ]); var unmarshaller = context.createUnmarshaller(); @@ -727,5 +727,32 @@ module.exports = test.equal('string', result.name.localPart); test.equal('text', result.value); test.done(); + }, + "MarshalCDATAValueType" : function(test) + { + var context = new Jsonix.Context([ One ]); + var marshaller = context.createMarshaller(); + var value = { + name : { + localPart : "valueAsCDATA" + }, + value : { + value : 'test<>?\'"&' + } + }; + var result = marshaller.marshalString(value); + console.log(result); + test.ok(result === '?\'"&]]>'); + test.done(); + }, + "UnmarshalCDATAValueType": function (test) + { + var context = new Jsonix.Context([ One ]); + var unmarshaller = context.createUnmarshaller(); + var text = '?\'"&]]>'; + var result = unmarshaller.unmarshalString(text); + test.equal('string', result.name.localPart); + test.equal('text', result.value); + test.done(); } };