public function parse($node:XMLNode, $parent:Object):Object {
// Parse XML data and returns corresponding Object
do {
var obj = new Object(); // create empty Object we parse into
var nName = $node.nodeName; // buffer Nodename
trace(nName+" < building node: "+" type: "+$node.nodeType);
for (var i in $node.attributes) { // set Attributes
trace(nName+" < att: "+i+" - value: "+eval("$node.attributes."+i));
obj[i] = convertType($node.attributes[i]);
}
switch (nName) { // node "Filter Section"
case null : // It's a content Node, rename Nodename to "cNode"
nName = "cNode";
trace(nName+" < content: "+$node.nodeValue);
addHiddenAttribute(obj, "_ct", convertType($node.nodeValue)); // Assign Content we tried to convert to a Number or Boolean before
break;
case "html" : // do not recurse into html, set as content instead
addHiddenAttribute(obj, "_ct", $node.toString());
break;
default : // check if node has any Child Nodes
if ($node.firstChild != null ) {
trace($node.firstChild.nodeName+" < recursing into - type: "+$node.firstChild.nodeType);
obj._fc = true; // set flag for firstchild evaluation
addHiddenAttribute(obj, "_lc", parse( $node.firstChild, obj)); // set "lastChild" Relation and Parse
}
}
// set "parent, firstChild, nextSibling, previousSibling" Relations
if ($parent != undefined) { addHiddenAttribute(obj, "_pt", $parent); }
if ($parent._fc == true) { addHiddenAttribute($parent, "_fc", obj); }
if (oldObj != undefined) { addHiddenAttribute(obj, "_ps", oldObj); addHiddenAttribute(oldObj, "_ns", obj); }
var oldObj = obj; // remember current object for setting "Sibling" Relations
// Now it's time to insert the newly generated Object in the Object Tree!
var id = obj.id; // check for "id" Attribute and try to convert to Number, if possible
if (obj.id != undefined) { // id, so we have to create an indexed array
delete obj.id; // dont need this anymore
if (!isNaN(id)) { id = Number(id); } // try to convert id to Number, if possible
if ( $parent[nName] == undefined) { $parent[nName] = new Array; } // create Array if it doesn't exist
$parent[nName][id] = obj; // insert into indexed Array
continue; // continue do...while loop with next Sibling
}
if ($parent[nName] == undefined) { // check if there is already another Object with the same name
$parent[nName] = obj; // no, first Object to insert
} else { // another Object with the same name exists
if ($parent[nName].length == undefined) { // no id, does "regular" Array exist?
var temp = $parent[nName]; // no, create Array first
$parent[nName] = new Array();
$parent[nName].push(temp);
}
$parent[nName].push(obj); // push Object into regular Array
}
} while ($node=$node.nextSibling);
return obj; // return Object after no more Siblings to follow
}
private function setValidObjectName($name:String):String {
// replace "." and "-" in Nodenames with "_"
$name = $name.split(".").join("_");
$name = $name.split("-").join("_");
return $name;
}
private function convertType($value) { // tries to convert a String into a Number or a Booelan
if (isNaN($value)) { // first, check if Value is a Number
switch ($value) { // no, check if Attribute can be converted to a Boolean
case "true" : return true;
case "false" : return false;
default : return $value; // no Boolean, return original value
}
} else {
return Number($value); // yes, assign converted Number
}
}
// the Next Function actuallay resides in the global Library
private function addHiddenAttribute($object:Object, $att:String, $value:Object):Void {
// adds a hidden Property and assign a value to it
$object[$att] = $value;
var attArr = $att.split();
_global.ASSetPropFlags($object, attArr, 1, true);
}