Skip to content

Commit

Permalink
[XML] Implement setVariableByMultiname
Browse files Browse the repository at this point in the history
  • Loading branch information
alexp-sssup committed May 12, 2012
1 parent 4b3c0fe commit f386e5a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/scripting/toplevel/XML.cpp
Expand Up @@ -564,7 +564,11 @@ _NR<ASObject> XML::getVariableByMultiname(const multiname& name, GET_VARIABLE_OP
}

bool isAttr=name.isAttribute;
//Normalize the name to the string form
const tiny_string normalizedName=name.normalizedName();
//TODO: support namespaces
assert_and_throw(name.ns.size()>0 && name.ns[0].name=="");

const char *buf=normalizedName.raw_buf();
if(!normalizedName.empty() && normalizedName.charAt(0)=='@')
{
Expand All @@ -574,13 +578,9 @@ _NR<ASObject> XML::getVariableByMultiname(const multiname& name, GET_VARIABLE_OP
if(isAttr)
{
//Lookup attribute
//TODO: support namespaces
assert_and_throw(name.ns.size()>0 && name.ns[0].name=="");

if(normalizedName.empty())
return _MR(getAllAttributes());

//Normalize the name to the string form
assert(node);
//To have attributes we must be an Element
xmlpp::Element* element=dynamic_cast<xmlpp::Element*>(node);
Expand All @@ -606,9 +606,6 @@ _NR<ASObject> XML::getVariableByMultiname(const multiname& name, GET_VARIABLE_OP
else
{
//Lookup children
//TODO: support namespaces
assert_and_throw(name.ns.size()>0 && name.ns[0].name=="");
//Normalize the name to the string form
assert(node);
const xmlpp::Node::NodeList& children=node->get_children(buf);
xmlpp::Node::NodeList::const_iterator it=children.begin();
Expand All @@ -634,6 +631,34 @@ _NR<ASObject> XML::getVariableByMultiname(const multiname& name, GET_VARIABLE_OP
}
}

void XML::setVariableByMultiname(const multiname& name, ASObject* o)
{
bool isAttr=name.isAttribute;
//Normalize the name to the string form
const tiny_string normalizedName=name.normalizedName();
//TODO: support namespaces
assert_and_throw(name.ns.size()>0 && name.ns[0].name=="");

const char *buf=normalizedName.raw_buf();
if(!normalizedName.empty() && normalizedName.charAt(0)=='@')
{
isAttr=true;
buf+=1;
}
if(isAttr)
{
//To have attributes we must be an Element
xmlpp::Element* element=dynamic_cast<xmlpp::Element*>(node);
assert_and_throw(element);
element->set_attribute(name.name_s, o->toString());
}
else
{
xmlpp::Element* child=node->add_child(name.name_s);
child->add_child_text(o->toString());
}
}

bool XML::hasPropertyByMultiname(const multiname& name, bool considerDynamic)
{
if(node==NULL || considerDynamic == false)
Expand Down
1 change: 1 addition & 0 deletions src/scripting/toplevel/XML.h
Expand Up @@ -81,6 +81,7 @@ friend class XMLList;
void getDescendantsByQName(const tiny_string& name, const tiny_string& ns, XMLVector& ret);
_NR<ASObject> getVariableByMultiname(const multiname& name, GET_VARIABLE_OPTION opt);
bool hasPropertyByMultiname(const multiname& name, bool considerDynamic);
void setVariableByMultiname(const multiname& name, ASObject* o);
tiny_string toString();
void toXMLString_priv(xmlBufferPtr buf);
bool hasSimpleContent() const;
Expand Down

0 comments on commit f386e5a

Please sign in to comment.