Allow more flexibility in overriding CElement::toXml

The toXml method has a default implementation that recursively calls toXml on
the children and set their "Name" attribute.

With this approach, the toplevel element's name isn't set unless its overrided
implementation explicitly does so.

A first approach for fixing that is to set the XmlElement's name and
recursively call toXml on children (without setting their name because it will
be done by their toXml method). However, the "CXmlElement" being created may or
may not be the object on which the toXml method is called, in which case the
name will be set on the wrong XmlElement.

Instead, in CElement::toXml, we set the XmlElement's name and call a new
virtual method, childrenToXml which only recursively call toXml on children.

This gives full flexibility to elements to choose how they want to serialize
themselves and how they want to serialize their children.

CConfigurableDomain::toXml is modified to take that change into account.

Change-Id: Id12a1023e91cb000e55c242c13643b1db7d46871
Signed-off-by: David Wagner <david.wagner@intel.com>
diff --git a/parameter/ConfigurableDomain.cpp b/parameter/ConfigurableDomain.cpp
index 61a8d5b..f81baec 100644
--- a/parameter/ConfigurableDomain.cpp
+++ b/parameter/ConfigurableDomain.cpp
@@ -111,9 +111,15 @@
 // From IXmlSource
 void CConfigurableDomain::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
 {
+    base::toXml(xmlElement, serializingContext);
+
     // Sequence awareness
     xmlElement.setAttributeBoolean("SequenceAware", _bSequenceAware);
+}
 
+void CConfigurableDomain::childrenToXml(CXmlElement& xmlElement,
+                                        CXmlSerializingContext& serializingContext) const
+{
     // Configurations
     composeDomainConfigurations(xmlElement, serializingContext);
 
@@ -133,7 +139,7 @@
     xmlElement.createChild(xmlConfigurationsElement, "Configurations");
 
     // Delegate to base
-    base::toXml(xmlConfigurationsElement, serializingContext);
+    base::childrenToXml(xmlConfigurationsElement, serializingContext);
 }
 
 void CConfigurableDomain::composeConfigurableElements(CXmlElement& xmlElement) const
diff --git a/parameter/ConfigurableDomain.h b/parameter/ConfigurableDomain.h
index e91a5cb..a7cbf73 100644
--- a/parameter/ConfigurableDomain.h
+++ b/parameter/ConfigurableDomain.h
@@ -103,6 +103,8 @@
 
     // From IXmlSource
     virtual void toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const;
+    virtual void childrenToXml(CXmlElement& xmlElement,
+                               CXmlSerializingContext& serializingContext) const;
 
     // Class kind
     virtual std::string getKind() const;
diff --git a/parameter/ConfigurableDomains.cpp b/parameter/ConfigurableDomains.cpp
index f11a155..dd7ede3 100644
--- a/parameter/ConfigurableDomains.cpp
+++ b/parameter/ConfigurableDomains.cpp
@@ -104,7 +104,7 @@
     // Set attribute
     xmlElement.setAttributeString("SystemClassName", getName());
 
-    base::toXml(xmlElement, serializingContext);
+    base::childrenToXml(xmlElement, serializingContext);
 }
 
 // Configuration/Domains handling
diff --git a/parameter/Element.cpp b/parameter/Element.cpp
index 2c8393d..4ccf149 100755
--- a/parameter/Element.cpp
+++ b/parameter/Element.cpp
@@ -292,8 +292,8 @@
     return true;
 }
 
-// From IXmlSource
-void CElement::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
+void CElement::childrenToXml(CXmlElement& xmlElement,
+                             CXmlSerializingContext& serializingContext) const
 {
     // Browse children and propagate
     uint32_t uiNbChildren = getNbChildren();
@@ -308,15 +308,17 @@
 
         xmlElement.createChild(xmlChildElement, pChild->getKind());
 
-        // Set attributes
-        pChild->setXmlNameAttribute(xmlChildElement);
-
-
         // Propagate
         pChild->toXml(xmlChildElement, serializingContext);
     }
 }
 
+void CElement::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
+{
+    setXmlNameAttribute(xmlElement);
+    childrenToXml(xmlElement, serializingContext);
+}
+
 void CElement::setXmlNameAttribute(CXmlElement& xmlElement) const
 {
     // By default, set Name attribute if any
diff --git a/parameter/Element.h b/parameter/Element.h
index 8469ab6..4636dbd 100644
--- a/parameter/Element.h
+++ b/parameter/Element.h
@@ -94,6 +94,20 @@
     // From IXmlSource
     virtual void toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const;
 
+    /**
+     * Serialize the children to XML
+     *
+     * This method is virtual, to be derived in case a special treatment is
+     * needed before doing so.
+     *
+     * @param[in,out] xmlElement the XML Element below which the children must
+     *                be serialized (which may or may not be the CElement
+     *                object upon which this method is called)
+     * @param[in,out] serializingContext information about the serialization
+     */
+    virtual void childrenToXml(CXmlElement& xmlElement,
+                               CXmlSerializingContext& serializingContext) const;
+
     // Content structure dump
     void dumpContent(std::string& strContent, CErrorContext& errorContext, const uint32_t uiDepth = 0) const;