Skip to content

Commit 649399c

Browse files
authored
Merge pull request #175 from jahow/add-cdata-type
Add CDATA support
2 parents f28e471 + 09a64b1 commit 649399c

File tree

9 files changed

+215
-60
lines changed

9 files changed

+215
-60
lines changed

dist/Jsonix-all.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,19 @@ Jsonix.XML.Output = Jsonix.Class({
16181618
return node;
16191619

16201620
},
1621+
writeCDATA : function(text) {
1622+
var node;
1623+
if (Jsonix.Util.Type.isFunction(this.document.createCDATASection)) {
1624+
node = this.document.createCDATASection(text);
1625+
}
1626+
else if (this.xmldom) {
1627+
node = this.xmldom.createCDATASection(text);
1628+
} else {
1629+
throw new Error("Could not create a CDATA section node.");
1630+
}
1631+
this.peek().appendChild(node);
1632+
return node;
1633+
},
16211634
writeAttribute : function(name, value) {
16221635
Jsonix.Util.Ensure.ensureString(value);
16231636
Jsonix.Util.Ensure.ensureObject(name);
@@ -1655,7 +1668,7 @@ Jsonix.XML.Output = Jsonix.Class({
16551668
}
16561669
this.declareNamespace(namespaceURI, prefix);
16571670
}
1658-
1671+
16591672
},
16601673
writeNode : function(node) {
16611674
var importedNode;
@@ -1764,7 +1777,7 @@ Jsonix.XML.Output = Jsonix.Class({
17641777
if (Jsonix.Util.Type.isString(p))
17651778
{
17661779
var oldp = nspItem[ns];
1767-
// If prefix is already declared and equals the proposed prefix
1780+
// If prefix is already declared and equals the proposed prefix
17681781
if (p === oldp)
17691782
{
17701783
// Nothing to do
@@ -1804,6 +1817,7 @@ Jsonix.XML.Output = Jsonix.Class({
18041817
},
18051818
CLASS_NAME : "Jsonix.XML.Output"
18061819
});
1820+
18071821
Jsonix.Mapping = {};
18081822
Jsonix.Mapping.Style = Jsonix.Class({
18091823
marshaller : null,
@@ -1866,6 +1880,7 @@ Jsonix.Binding.Marshalls.Element = Jsonix.Class({
18661880
}
18671881
}
18681882
var typeInfo = actualTypeInfo || declaredTypeInfo;
1883+
18691884
if (typeInfo) {
18701885
output.writeStartElement(elementValue.name);
18711886
if (actualTypeInfo && declaredTypeInfo !== actualTypeInfo) {
@@ -2145,32 +2160,32 @@ Jsonix.Model.ClassInfo = Jsonix
21452160
var n = mapping.name||mapping.n||undefined;
21462161
Jsonix.Util.Ensure.ensureString(n);
21472162
this.name = n;
2148-
2163+
21492164
var ln = mapping.localName||mapping.ln||null;
21502165
this.localName = ln;
21512166

21522167
var dens = mapping.defaultElementNamespaceURI||mapping.dens||mapping.targetNamespace||mapping.tns||'';
21532168
this.defaultElementNamespaceURI = dens;
2154-
2169+
21552170
var tns = mapping.targetNamespace||mapping.tns||mapping.defaultElementNamespaceURI||mapping.dens||this.defaultElementNamespaceURI;
21562171
this.targetNamespace = tns;
21572172

21582173
var dans = mapping.defaultAttributeNamespaceURI||mapping.dans||'';
21592174
this.defaultAttributeNamespaceURI = dans;
2160-
2175+
21612176
var bti = mapping.baseTypeInfo||mapping.bti||null;
21622177
this.baseTypeInfo = bti;
2163-
2178+
21642179
var inF = mapping.instanceFactory||mapping.inF||undefined;
21652180
if (Jsonix.Util.Type.exists(inF)) {
21662181
// TODO: should we support instanceFactory as functions?
21672182
// For the pure JSON configuration?
21682183
Jsonix.Util.Ensure.ensureFunction(inF);
21692184
this.instanceFactory = inF;
21702185
}
2171-
2186+
21722187
var tn = mapping.typeName||mapping.tn||undefined;
2173-
2188+
21742189
if (Jsonix.Util.Type.exists(tn))
21752190
{
21762191
if (Jsonix.Util.Type.isString(tn))
@@ -2185,14 +2200,14 @@ Jsonix.Model.ClassInfo = Jsonix
21852200
{
21862201
this.typeName = new Jsonix.XML.QName(tns, ln);
21872202
}
2188-
2203+
21892204
this.properties = [];
21902205
this.propertiesMap = {};
21912206
var ps = mapping.propertyInfos||mapping.ps||[];
21922207
Jsonix.Util.Ensure.ensureArray(ps);
21932208
for ( var index = 0; index < ps.length; index++) {
21942209
this.p(ps[index]);
2195-
}
2210+
}
21962211
},
21972212
getPropertyInfoByName : function(name) {
21982213
return this.propertiesMap[name];
@@ -2237,15 +2252,15 @@ Jsonix.Model.ClassInfo = Jsonix
22372252
unmarshal : function(context, input) {
22382253
this.build(context);
22392254
var result;
2240-
2255+
22412256
if (this.instanceFactory) {
22422257
result = new this.instanceFactory();
22432258
}
22442259
else
22452260
{
2246-
result = { TYPE_NAME : this.name };
2261+
result = { TYPE_NAME : this.name };
22472262
}
2248-
2263+
22492264
if (input.eventType !== 1) {
22502265
throw new Error("Parser must be on START_ELEMENT to read a class info.");
22512266
}
@@ -2526,6 +2541,7 @@ Jsonix.Model.ClassInfo.prototype.propertyInfoCreators = {
25262541
"v" : Jsonix.Model.ClassInfo.prototype.v,
25272542
"value" : Jsonix.Model.ClassInfo.prototype.v
25282543
};
2544+
25292545
Jsonix.Model.EnumLeafInfo = Jsonix.Class(Jsonix.Model.TypeInfo, {
25302546
name : null,
25312547
baseTypeInfo : 'String',
@@ -2948,6 +2964,9 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf
29482964
initialize : function(mapping) {
29492965
Jsonix.Util.Ensure.ensureObject(mapping);
29502966
Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this, [ mapping ]);
2967+
2968+
var c = mapping.asCDATA || mapping.c || false;
2969+
this.asCDATA = c;
29512970
},
29522971
unmarshal : function(context, input, scope) {
29532972
var text = input.getElementText();
@@ -2957,7 +2976,12 @@ Jsonix.Model.ValuePropertyInfo = Jsonix.Class(Jsonix.Model.SingleTypePropertyInf
29572976
if (!Jsonix.Util.Type.exists(value)) {
29582977
return;
29592978
}
2960-
output.writeCharacters(this.print(value, context, output, scope));
2979+
2980+
if (this.asCDATA) {
2981+
output.writeCDATA(this.print(value, context, output, scope));
2982+
} else {
2983+
output.writeCharacters(this.print(value, context, output, scope));
2984+
}
29612985
},
29622986
buildStructure : function(context, structure) {
29632987
Jsonix.Util.Ensure.ensureObject(structure);
@@ -5820,15 +5844,15 @@ Jsonix.Context = Jsonix
58205844
Jsonix.Util.Ensure.ensureObject(options);
58215845
if (Jsonix.Util.Type
58225846
.isObject(options.namespacePrefixes)) {
5823-
this.namespacePrefixes =
5847+
this.namespacePrefixes =
58245848
Jsonix.Util.Type.cloneObject(options.namespacePrefixes, {});
58255849
}
58265850
if (Jsonix.Util.Type
58275851
.isBoolean(options.supportXsiType)) {
5828-
this.supportXsiType = options.supportXsiType;
5852+
this.supportXsiType = options.supportXsiType;
58295853
}
58305854
}
5831-
5855+
58325856
// Initialize prefix/namespace mapping
58335857
for (var ns in this.namespacePrefixes)
58345858
{
@@ -5857,7 +5881,7 @@ Jsonix.Context = Jsonix
58575881
module = mapping;
58585882
} else {
58595883
mapping = Jsonix.Util.Type.cloneObject(mapping);
5860-
module = new this.mappingStyle.module(mapping,
5884+
module = new this.mappingStyle.module(mapping,
58615885
{
58625886
mappingStyle : this.mappingStyle
58635887
});
@@ -6098,6 +6122,7 @@ Jsonix.Context = Jsonix
60986122
Jsonix.Schema.XSD.UnsignedShort.INSTANCE ],
60996123
CLASS_NAME : 'Jsonix.Context'
61006124
});
6125+
61016126
// Complete Jsonix script is included above
61026127
return { Jsonix: Jsonix };
61036128
};

dist/Jsonix-min.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ if(Jsonix.Util.Type.isFunction(this.document.createTextNode)){d=this.document.cr
650650
}else{throw new Error("Could not create a text node.")
651651
}}this.peek().appendChild(d);
652652
return d
653+
},writeCDATA:function(c){var d;
654+
if(Jsonix.Util.Type.isFunction(this.document.createCDATASection)){d=this.document.createCDATASection(c)
655+
}else{if(this.xmldom){d=this.xmldom.createCDATASection(c)
656+
}else{throw new Error("Could not create a CDATA section node.")
657+
}}this.peek().appendChild(d);
658+
return d
653659
},writeAttribute:function(t,k){Jsonix.Util.Ensure.ensureString(k);
654660
Jsonix.Util.Ensure.ensureObject(t);
655661
var n=t.localPart||t.lp||null;
@@ -1149,13 +1155,16 @@ Jsonix.Util.Ensure.ensureObject(e.attributes);
11491155
var d=this.attributeName.key;
11501156
e.attributes[d]=this
11511157
},CLASS_NAME:"Jsonix.Model.AttributePropertyInfo"});
1152-
Jsonix.Model.ValuePropertyInfo=Jsonix.Class(Jsonix.Model.SingleTypePropertyInfo,{initialize:function(b){Jsonix.Util.Ensure.ensureObject(b);
1153-
Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this,[b])
1158+
Jsonix.Model.ValuePropertyInfo=Jsonix.Class(Jsonix.Model.SingleTypePropertyInfo,{initialize:function(d){Jsonix.Util.Ensure.ensureObject(d);
1159+
Jsonix.Model.SingleTypePropertyInfo.prototype.initialize.apply(this,[d]);
1160+
var c=d.asCDATA||d.c||false;
1161+
this.asCDATA=c
11541162
},unmarshal:function(e,f,h){var g=f.getElementText();
11551163
return this.unmarshalValue(g,e,f,h)
11561164
},marshal:function(g,e,f,h){if(!Jsonix.Util.Type.exists(g)){return
1157-
}f.writeCharacters(this.print(g,e,f,h))
1158-
},buildStructure:function(c,d){Jsonix.Util.Ensure.ensureObject(d);
1165+
}if(this.asCDATA){f.writeCDATA(this.print(g,e,f,h))
1166+
}else{f.writeCharacters(this.print(g,e,f,h))
1167+
}},buildStructure:function(c,d){Jsonix.Util.Ensure.ensureObject(d);
11591168
if(Jsonix.Util.Type.exists(d.elements)){throw new Error("The structure already defines element mappings, it cannot define a value property.")
11601169
}else{d.value=this
11611170
}},CLASS_NAME:"Jsonix.Model.ValuePropertyInfo"});

0 commit comments

Comments
 (0)