Browse Source

Added functionality to set the value for a node in the form of <node>value</node>.
Added method to retrieve the value of a node held in the inner xml tags eg. <node>value</node> would return "value" when called on "node"
Added Script Methods for Getting and Setting the value as detailed above (Lua contains Getter only inline with the rest of the functionality)

Alex Parlett 12 years ago
parent
commit
db38bcf6c0

+ 1 - 0
Source/Engine/LuaScript/pkgs/Resource/XMLElement.pkg

@@ -20,6 +20,7 @@ class XMLElement
     
     BoundingBox GetBoundingBox() const;
     
+	String GetValue() const;
     Color GetColor(const String name) const;
     float GetFloat(const String name) const;
     unsigned GetUInt(const String name) const;

+ 23 - 0
Source/Engine/Resource/XMLElement.cpp

@@ -222,6 +222,20 @@ XPathResultSet XMLElement::SelectPrepared(const XPathQuery& query) const
     return XPathResultSet(file_, &result);
 }
 
+bool XMLElement::SetValue(const String& value)
+{
+	return SetValue(value.CString());
+}
+
+bool XMLElement::SetValue(const char* value)
+{
+	if (!file_ || (!node_ && !xpathNode_))
+		return false;
+
+	pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
+	return node.append_child(pugi::node_pcdata).set_value(value);
+}
+
 bool XMLElement::SetAttribute(const String& name, const String& value)
 {
     return SetAttribute(name.CString(), value.CString());
@@ -563,6 +577,15 @@ bool XMLElement::HasAttribute(const char* name) const
     return !node.attribute(name).empty();
 }
 
+String XMLElement::GetValue() const
+{
+	if (!file_ || (!node_ && !xpathNode_))
+		return String::EMPTY;
+
+	const pugi::xml_node& node = xpathNode_ ? xpathNode_->node() : pugi::xml_node(node_);
+	return String(node.child_value());
+}
+
 String XMLElement::GetAttribute(const String& name) const
 {
     return String(GetAttributeCString(name.CString()));

+ 8 - 1
Source/Engine/Resource/XMLElement.h

@@ -88,6 +88,11 @@ public:
     /// Select elements/attributes using XPath query.
     XPathResultSet SelectPrepared(const XPathQuery& query) const;
 
+	/// Set the value for an inner node in the following format <node>value</node>.
+	bool SetValue(const String& value);
+	/// Set the value for an inner node in the following format
+	/// <node>value</node>. Must be used on the <node> element.
+	bool SetValue(const char* value);
     /// Set an attribute.
     bool SetAttribute(const String& name, const String& value);
     /// Set an attribute.
@@ -171,7 +176,9 @@ public:
     bool HasAttribute(const String& name) const;
     /// Return whether has an attribute.
     bool HasAttribute(const char* name) const;
-    /// Return attribute, or empty if missing.
+	/// Return inner value, or empty if missing for nodes like <node>value</node>
+	String GetValue() const;
+	/// Return attribute, or empty if missing.
     String GetAttribute(const String& name = String::EMPTY) const;
     /// Return attribute, or empty if missing.
     String GetAttribute(const char* name) const;

+ 2 - 0
Source/Engine/Script/ResourceAPI.cpp

@@ -225,6 +225,7 @@ static void RegisterXMLElement(asIScriptEngine* engine)
     engine->RegisterObjectMethod("XMLElement", "XMLElement SelectSinglePrepared(const XPathQuery&in)", asMETHOD(XMLElement, SelectSinglePrepared), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "XPathResultSet Select(const String&in)", asMETHOD(XMLElement, Select), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "XPathResultSet SelectPrepared(const XPathQuery&in)", asMETHOD(XMLElement, SelectPrepared), asCALL_THISCALL);
+	engine->RegisterObjectMethod("XMLElement", "bool SetValue(const String&in)", asMETHODPR(XMLElement, SetValue, (const String&), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool SetAttribute(const String&in, const String&in)", asMETHODPR(XMLElement, SetAttribute, (const String&, const String&), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool SetAttribute(const String&in)", asMETHODPR(XMLElement, SetAttribute, (const String&), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool SetBool(const String&in, bool)", asMETHOD(XMLElement, SetBool), asCALL_THISCALL);
@@ -244,6 +245,7 @@ static void RegisterXMLElement(asIScriptEngine* engine)
     engine->RegisterObjectMethod("XMLElement", "bool SetVector4(const String&in, const Vector4&in)", asMETHOD(XMLElement, SetVector4), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool SetVectorVariant(const String&in, const Variant&in)", asMETHOD(XMLElement, SetVectorVariant), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool HasAttribute(const String&in) const", asMETHODPR(XMLElement, HasAttribute, (const String&) const, bool), asCALL_THISCALL);
+	engine->RegisterObjectMethod("XMLElement", "String GetValue() const", asMETHOD(XMLElement, GetValue), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "String GetAttribute(const String&in arg0 = String()) const", asMETHODPR(XMLElement, GetAttribute, (const String&) const, String), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "String GetAttributeLower(const String&in) const", asMETHODPR(XMLElement, GetAttributeLower, (const String&) const, String), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "String GetAttributeUpper(const String&in) const", asMETHODPR(XMLElement, GetAttributeUpper, (const String&) const, String), asCALL_THISCALL);