From 40ee629193214c43a178110345b1265825e7b5a1 Mon Sep 17 00:00:00 2001 From: marian Date: Thu, 24 Apr 2008 08:30:42 +0000 Subject: [PATCH] Added constructor and method allowing to copy an XML element from/to a NamedList. git-svn-id: http://voip.null.ro/svn/yate@1944 acf43c95-373e-0410-b603-e72c3f656dc1 --- libs/yjingle/xmlparser.cpp | 36 ++++++++++++++++++++++++++++++++++++ libs/yjingle/xmlparser.h | 18 ++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/libs/yjingle/xmlparser.cpp b/libs/yjingle/xmlparser.cpp index 6972b99a..fc61257a 100644 --- a/libs/yjingle/xmlparser.cpp +++ b/libs/yjingle/xmlparser.cpp @@ -167,6 +167,28 @@ XMLElement::XMLElement(TiXmlElement* element, bool owner) // name(),m_owner,this); } +// Build this XML element from a list containing name, attributes and text. +XMLElement::XMLElement(NamedList& src, const char* prefix) + : m_type(Unknown), m_owner(true), m_element(0) +{ + m_element = new TiXmlElement(src.getValue(prefix)); + DDebug(DebugAll,"XMLElement(%s) src=%s prefix=%s [%p]", + m_name.c_str(),src.c_str(),prefix,this); + String pref(String(prefix) + "."); + // Set text + const char* text = src.getValue(pref); + if (text) + m_element->LinkEndChild(new TiXmlText(text)); + // Add attributes + unsigned int n = src.count(); + for (unsigned int i = 0; i < n; i++) { + NamedString* ns = src.getParam(i); + if (ns && ns->name().startsWith(pref)) + setAttribute(ns->name().substr(pref.length()),*ns); + } + setType(); +} + XMLElement::~XMLElement() { if (m_owner && m_element) @@ -185,6 +207,20 @@ void XMLElement::toString(String& dest, bool unclose) const } } +// Put this element's name, text and attributes to a list of parameters +void XMLElement::toList(NamedList& dest, const char* prefix) +{ + DDebug(DebugAll,"XMLElement(%s) to list=%s prefix=%s [%p]", + m_name.c_str(),dest.c_str(),prefix,this); + dest.addParam(prefix,name()); + String pref(String(prefix) + "."); + const char* tmp = getText(); + if (tmp) + dest.addParam(pref,tmp); + for (const TiXmlAttribute* a = firstAttribute(); a; a = a->Next()) + dest.addParam(pref + a->Name(),a->Value()); +} + void XMLElement::setAttribute(const char* name, const char* value) { if (!(valid() && name && value)) diff --git a/libs/yjingle/xmlparser.h b/libs/yjingle/xmlparser.h index 2f67888c..b03fb3c5 100644 --- a/libs/yjingle/xmlparser.h +++ b/libs/yjingle/xmlparser.h @@ -163,6 +163,17 @@ public: */ XMLElement(Type type, NamedList* attributes = 0, const char* text = 0); + /** + * Constructor. + * Build this XML element from a list containing name, attributes and text. + * Element's name must be a parameter whose name must be equal to prefix. + * Element's text must be a parameter whose name is prefix followed by a dot. + * The list of attributes will be built from parameters starting with prefix.attributename + * @param src The list containing data used to build this XML element + * @param prefix The prefix used to search the list of parameters + */ + XMLElement(NamedList& src, const char* prefix); + /** * Destructor. Deletes the underlying TiXmlElement if owned */ @@ -211,6 +222,13 @@ public: */ void toString(String& dest, bool unclose = false) const; + /** + * Put this element's name, text and attributes to a list of parameters + * @param dest Destination list + * @param prefix Prefix to add to parameters + */ + void toList(NamedList& dest, const char* prefix); + /** * Set the value of an existing attribute or adds a new one * @param name Attribute's name