Escape special characters contained in the attribute of an XML stream
Regular Expressions are very powerful but sometime not so easy to use, recently we have had the need to get a deserialize an object that cames from a database and that contains in each node the name of the property, its value and its datatype.
The raw binary data that we got are expressed with the following syntax
<property name="crop" value="" type="com.gnstudio.rikorda.core.model.canvas.vo::Crop" >
<property name="points" value="" type="__AS3__.vec::Vector.<flash.geom::Point>" >
<property type="__AS3__.vec::Vector.<flash.geom::Point>" >
<property name="point" value = "" type = "flash.geom::Point">
<property name="x" value="" type="Number" />
<property name="y" value="" type="Number" /></property></property>
In the Flash Platform when you try to read this UTF bytes and cast them to an XML object you get an exception because there is the < and the > chars in the attribute, in order to solve this issue we used a regular expression to get the value stored in the attribute. The regular expression uses a named group to access this data into the exec method and inside this method there is the logic to replace the < and > chars with the right HTML entities.
Some useful links:
http://gnosis.cx/publish/programming/regular_expressions.html
http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx
http://www.radsoftware.com.au/regexdesigner/
http://help.adobe.com/en_US/ActionScript/3.0_...3e3d118a9b90204-7e9a.html
http://livedocs.adobe.com/flex/3/html/help.ht...gular_Expressions_09.html
http://www.regular-expressions.info/examples.html
http://www.regular-expressions.info/brackets.html
http://wiki.tcl.tk/989
The raw binary data that we got are expressed with the following syntax
<property name="crop" value="" type="com.gnstudio.rikorda.core.model.canvas.vo::Crop" >
<property name="points" value="" type="__AS3__.vec::Vector.<flash.geom::Point>" >
<property type="__AS3__.vec::Vector.<flash.geom::Point>" >
<property name="point" value = "" type = "flash.geom::Point">
<property name="x" value="" type="Number" />
<property name="y" value="" type="Number" /></property></property>
In the Flash Platform when you try to read this UTF bytes and cast them to an XML object you get an exception because there is the < and the > chars in the attribute, in order to solve this issue we used a regular expression to get the value stored in the attribute. The regular expression uses a named group to access this data into the exec method and inside this method there is the logic to replace the < and > chars with the right HTML entities.
Some useful links:
http://gnosis.cx/publish/programming/regular_expressions.html
http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx
http://www.radsoftware.com.au/regexdesigner/
http://help.adobe.com/en_US/ActionScript/3.0_...3e3d118a9b90204-7e9a.html
http://livedocs.adobe.com/flex/3/html/help.ht...gular_Expressions_09.html
http://www.regular-expressions.info/examples.html
http://www.regular-expressions.info/brackets.html
http://wiki.tcl.tk/989
var content:String = '<property name="crop" value="" type="com.gnstudio.rikorda.core.model.canvas.vo::Crop" >' +
'<property name="points" value="" type="__AS3__.vec::Vector.<flash.geom::Point>" >' +
'<property type="__AS3__.vec::Vector.<flash.geom::Point>" >' +
'<property type="__AS3__.vec::Vector.<flash.geom::Point>" >' +
'<property name="point" value = "" type = "flash.geom::Point">' +
'<property name="x" value="" type="Number" />' +
'<property name="y" value="" type="Number" />' +
'</property>' +
'</property>';
trace("CONTENT BEFORE:", content, "\n");
var ltExp:RegExp = new RegExp("\\<", "g");
var gtExp:RegExp = new RegExp("\\>", "g");
var attributeName:String = "type";
var groupName:String = "attribute";
var attributes:RegExp = new RegExp("<(?:[^>\"']|\"[^\"]*\"|'[^']*')+?\\s" + attributeName + "\\s*=\\s*(?P<" + groupName + ">\"[^\"]*\"|'[^']*')(?:[^>\"']|\"[^\"]*\"|'[^']*')*>", "g");
var err:Error;
do {
try{
var data:String = attributes.exec(content)[groupName];
var clone:String = data;
var toReplace:Boolean;
if(clone.search(gtExp) > -1){
clone = clone.replace(gtExp, ">");
toReplace = true;
}
if(clone.search(ltExp) > -1){
clone = clone.replace(ltExp, "<");
toReplace = true;
}
if(toReplace){
var reg:RegExp = new RegExp("\\" + data, "g");
content = content.replace(reg, clone);
}
}catch(error:Error){
err = error
break;
}
} while ( !err);
trace("CONTENT AFTER:", content);




