PFW: Add setDomainsXML command to import domains from string

BZ: 55694

Unable possibility to import the configurable domains from a string

Create one command that can be called through the command
line of the parameter-framework. The command setDomainXML
allows to import configurable domains with settings from
a string provided as first argument containing the xml description.

Change-Id: I6db7ccb8ba61b5e4c8ba81579a68f40e887d1534
Signed-off-by: Georges-Henri Baron <georges-henrix.baron@intel.com>
Reviewed-on: http://android.intel.com:8080/65408
Reviewed-by: cactus <cactus@intel.com>
Reviewed-by: Dixon, CharlesX <charlesx.dixon@intel.com>
Reviewed-by: Rocard, KevinX <kevinx.rocard@intel.com>
Tested-by: Dixon, CharlesX <charlesx.dixon@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
diff --git a/parameter/ParameterMgr.cpp b/parameter/ParameterMgr.cpp
index 4003753..0bef3df 100644
--- a/parameter/ParameterMgr.cpp
+++ b/parameter/ParameterMgr.cpp
@@ -1,4 +1,4 @@
-/*
+ /*
  * INTEL CONFIDENTIAL
  * Copyright © 2011 Intel 
  * Corporation All Rights Reserved.
@@ -74,6 +74,7 @@
 #include "XmlFileDocSink.h"
 #include "XmlFileDocSource.h"
 #include "XmlStringDocSink.h"
+#include "XmlStringDocSource.h"
 #include "XmlMemoryDocSink.h"
 #include "XmlMemoryDocSource.h"
 #include "Utility.h"
@@ -244,12 +245,16 @@
             "<file path>", "Export settings to binary file" },
     { "importSettings", &CParameterMgr::importSettingsCommmandProcess, 1,
             "<file path>", "Import settings from binary file" },
-    { "getDomainsXML", &CParameterMgr::getDomainsXMLCommmandProcess, 0 ,
+    { "getDomainsWithSettingsXML",
+            &CParameterMgr::getConfigurableDomainsWithSettingsXMLCommmandProcess, 0,
             "", "Print domains including settings as XML" },
-
+    { "setDomainsWithSettingsXML",
+            &CParameterMgr::setConfigurableDomainsWithSettingsXMLCommmandProcess, 1,
+            "<xml configurable domains>", "Import domains including settings from XML string" },
     /// Structure Export
     { "getSystemClassXML", &CParameterMgr::getSystemClassXMLCommmandProcess, 0 ,
             "", "Print parameter structure as XML" }
+
 };
 
 // Remote command parsers array Size
@@ -1362,24 +1367,35 @@
 }
 
 /// Settings Import/Export
-CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::exportConfigurableDomainsToXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
+CParameterMgr::CCommandHandler::CommandStatus
+        CParameterMgr::exportConfigurableDomainsToXMLCommmandProcess(
+                const IRemoteCommand& remoteCommand, string& strResult)
 {
-    return exportDomainsXml(remoteCommand.getArgument(0), false, strResult) ? CCommandHandler::EDone : CCommandHandler::EFailed;
+    string strFileName = remoteCommand.getArgument(0);
+    return exportDomainsXml(strFileName, false, true, strResult) ?
+            CCommandHandler::EDone : CCommandHandler::EFailed;
 }
 
-CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::importConfigurableDomainsFromXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
+CParameterMgr::CCommandHandler::CommandStatus
+        CParameterMgr::importConfigurableDomainsFromXMLCommmandProcess(
+                const IRemoteCommand& remoteCommand, string& strResult)
 {
-    return importDomainsXml(remoteCommand.getArgument(0), false, strResult) ? CCommandHandler::EDone : CCommandHandler::EFailed;
+    return importDomainsXml(remoteCommand.getArgument(0), false, true, strResult) ?
+            CCommandHandler::EDone : CCommandHandler::EFailed;
 }
 
-CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::exportConfigurableDomainsWithSettingsToXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
+CParameterMgr::CCommandHandler::CommandStatus
+        CParameterMgr::exportConfigurableDomainsWithSettingsToXMLCommmandProcess(
+                const IRemoteCommand& remoteCommand, string& strResult)
 {
-    return exportDomainsXml(remoteCommand.getArgument(0), true, strResult) ? CCommandHandler::EDone : CCommandHandler::EFailed;
+    string strFileName = remoteCommand.getArgument(0);
+    return exportDomainsXml(strFileName, true, true, strResult) ?
+            CCommandHandler::EDone : CCommandHandler::EFailed;
 }
 
 CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::importConfigurableDomainsWithSettingsFromXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
 {
-    return importDomainsXml(remoteCommand.getArgument(0), true, strResult) ? CCommandHandler::EDone : CCommandHandler::EFailed;
+    return importDomainsXml(remoteCommand.getArgument(0), true, true, strResult) ? CCommandHandler::EDone : CCommandHandler::EFailed;
 }
 
 CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::exportSettingsCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
@@ -1392,13 +1408,13 @@
     return importDomainsBinary(remoteCommand.getArgument(0), strResult) ? CCommandHandler::EDone : CCommandHandler::EFailed;
 }
 
-/// GUI commands
-
-CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::getDomainsXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
+CParameterMgr::CCommandHandler::CommandStatus
+        CParameterMgr::getConfigurableDomainsWithSettingsXMLCommmandProcess(
+                const IRemoteCommand& remoteCommand, string& strResult)
 {
     (void)remoteCommand;
 
-    if (!getDomainsXMLString(strResult, true)) {
+    if (!exportDomainsXml(strResult, true, false, strResult)) {
 
         return CCommandHandler::EFailed;
     }
@@ -1406,7 +1422,17 @@
     return CCommandHandler::ESucceeded;
 }
 
-CParameterMgr::CCommandHandler::CommandStatus CParameterMgr::getSystemClassXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult)
+CParameterMgr::CCommandHandler::CommandStatus
+        CParameterMgr::setConfigurableDomainsWithSettingsXMLCommmandProcess(
+                const IRemoteCommand& remoteCommand, string& strResult)
+{
+    return importDomainsXml(remoteCommand.getArgument(0), true, false, strResult) ?
+            CCommandHandler::EDone : CCommandHandler::EFailed;
+}
+
+CParameterMgr::CCommandHandler::CommandStatus
+        CParameterMgr::getSystemClassXMLCommmandProcess(
+                const IRemoteCommand& remoteCommand, string& strResult)
 {
     (void)remoteCommand;
 
@@ -1801,8 +1827,8 @@
     return getConfigurableDomains()->split(strDomain, pConfigurableElement, strError);
 }
 
-// XML Import/Export
-bool CParameterMgr::importDomainsXml(const string& strFileName, bool bWithSettings, string& strError)
+bool CParameterMgr::importDomainsXml(const string& strXmlSource, bool bWithSettings,
+                                     bool bFromFile, string& strError)
 {
     // Check tuning mode
     if (!checkTuningModeOn(strError)) {
@@ -1811,7 +1837,7 @@
     }
 
     // check path is absolute
-    if (strFileName[0] != '/') {
+    if (bFromFile && strXmlSource[0] != '/') {
 
         strError = "Please provide absolute path";
 
@@ -1823,25 +1849,67 @@
     // Context
     CXmlDomainSerializingContext xmlDomainSerializingContext(strError, bWithSettings);
 
-    // Secltion criteria definition for rule creation
-    xmlDomainSerializingContext.setSelectionCriteriaDefinition(getConstSelectionCriteria()->getSelectionCriteriaDefinition());
+    // Selection criteria definition for rule creation
+    xmlDomainSerializingContext.setSelectionCriteriaDefinition(
+            getConstSelectionCriteria()->getSelectionCriteriaDefinition());
 
-    // Parse
-    if (!xmlParse(xmlDomainSerializingContext, pConfigurableDomains, strFileName, "", EParameterConfigurationLibrary, "SystemClassName")) {
+    // Init serializing context
+    xmlDomainSerializingContext.set(
+            _pElementLibrarySet->getElementLibrary(EParameterConfigurationLibrary),
+            "", _strSchemaFolderLocation);
 
-        return false;
+    // Get Schema file associated to root element
+    string strXmlSchemaFilePath = _strSchemaFolderLocation + "/" +
+                                  pConfigurableDomains->getKind() + ".xsd";
+
+    // Xml Source
+    CXmlDocSource* pSource;
+
+    if (bFromFile) {
+
+        // when importing from a file strXmlSource is the file name
+        pSource = new CXmlFileDocSource(strXmlSource, strXmlSchemaFilePath,
+                                        pConfigurableDomains->getKind(),
+                                        pConfigurableDomains->getName(), "SystemClassName");
+
+    } else {
+
+        // when importing from an xml string, strXmlSource contains the string
+        pSource = new CXmlStringDocSource(strXmlSource, strXmlSchemaFilePath,
+                                          pConfigurableDomains->getKind(),
+                                          pConfigurableDomains->getName(), "SystemClassName");
+
+    }
+    // Start clean
+    pConfigurableDomains->clean();
+
+    // Use a doc sink that instantiate Configurable Domains from the given doc source
+    CXmlMemoryDocSink memorySink(pConfigurableDomains);
+
+    bool bProcessSuccess = memorySink.process(*pSource, xmlDomainSerializingContext);
+
+    if (!bProcessSuccess) {
+
+        //Cleanup
+        pConfigurableDomains->clean();
+
+    } else {
+
+        // Validate domains after XML import
+        pConfigurableDomains->validate(_pMainParameterBlackboard);
+
     }
 
-    // Validate domains after XML import
-    pConfigurableDomains->validate(_pMainParameterBlackboard);
+    delete pSource;
 
-    return true;
+    return bProcessSuccess;
 }
 
-bool CParameterMgr::exportDomainsXml(const string& strFileName, bool bWithSettings, string& strError) const
+bool CParameterMgr::exportDomainsXml(string& strXmlDest, bool bWithSettings, bool bToFile,
+                                     string& strError) const
 {
     // check path is absolute
-    if (strFileName[0] != '/') {
+    if (bToFile && strXmlDest[0] != '/') {
 
         strError = "Please provide absolute path";
 
@@ -1852,7 +1920,8 @@
     const CConfigurableDomains* pConfigurableDomains = getConstConfigurableDomains();
 
     // Get Schema file associated to root element
-    string strXmlSchemaFilePath = _strSchemaFolderLocation + "/" + pConfigurableDomains->getKind() + ".xsd";
+    string strXmlSchemaFilePath = _strSchemaFolderLocation + "/" +
+                                  pConfigurableDomains->getKind() + ".xsd";
 
     // Context
     CXmlDomainSerializingContext xmlDomainSerializingContext(strError, bWithSettings);
@@ -1864,16 +1933,27 @@
     xmlDomainSerializingContext.setOutputRawFormat(_bOutputRawFormatIsHex);
 
     // Use a doc source by loading data from instantiated Configurable Domains
-    CXmlMemoryDocSource memorySource(pConfigurableDomains, pConfigurableDomains->getKind(), strXmlSchemaFilePath, "parameter-framework", getVersion());
+    CXmlMemoryDocSource memorySource(pConfigurableDomains, pConfigurableDomains->getKind(),
+                                     strXmlSchemaFilePath, "parameter-framework", getVersion());
 
-    // Use a doc sink to write the doc data in a file
-    CXmlFileDocSink fileSink(strFileName);
+    // Xml Sink
+    CXmlDocSink* pSink;
 
-    if (!fileSink.process(memorySource, xmlDomainSerializingContext)) {
-        return false;
+    if (bToFile) {
+
+        // Use a doc sink to write the doc data in a file
+        pSink = new CXmlFileDocSink(strXmlDest);
+
+    } else {
+
+        // Use a doc sink to write the doc data in a string
+        pSink = new CXmlStringDocSink(strXmlDest);
     }
 
-    return true;
+    bool bProcessSuccess = pSink->process(memorySource, xmlDomainSerializingContext);
+
+    delete pSink;
+    return bProcessSuccess;
 }
 
 // Binary Import/Export
@@ -2116,42 +2196,6 @@
     getSelectionCriteria()->resetModifiedStatus();
 }
 
-/// GUI commands functions
-bool CParameterMgr::getDomainsXMLString(string& strResult, bool bWithSettings)
-{
-
-    // Root element
-    const CConfigurableDomains* pConfigurableDomains = getConstConfigurableDomains();
-
-    // Get Schema file associated to root element
-    string strXmlSchemaFilePath = _strSchemaFolderLocation + "/" + pConfigurableDomains->getKind() + ".xsd";
-
-    string strError;
-
-    // Context
-    CXmlDomainSerializingContext xmlDomainSerializingContext(strError, bWithSettings);
-
-    // Value space
-    xmlDomainSerializingContext.setValueSpaceRaw(_bValueSpaceIsRaw);
-
-    // Output raw format
-    xmlDomainSerializingContext.setOutputRawFormat(_bOutputRawFormatIsHex);
-
-    // Use a doc source by loading data from instantiated Configurable Domains
-    CXmlMemoryDocSource memorySource(pConfigurableDomains, pConfigurableDomains->getKind(), strXmlSchemaFilePath, "parameter-framework", getVersion());
-
-    // Use a doc sink the write the doc data in a string
-    CXmlStringDocSink stringSink(strResult);
-
-    if (!stringSink.process(memorySource, xmlDomainSerializingContext)) {
-        strResult = strError;
-
-        return false;
-    }
-
-    return true;
-}
-
 bool CParameterMgr::getSystemClassXMLString(string& strResult)
 {
     // Root element
@@ -2167,10 +2211,13 @@
     // Use a doc sink that write the doc data in a string
     CXmlStringDocSink stringSink(strResult);
 
-    if (!stringSink.process(memorySource, xmlSerializingContext)) {
+    bool bProcessSuccess = stringSink.process(memorySource, xmlSerializingContext);
+
+    if (!bProcessSuccess) {
+
         strResult = strError;
-        return false;
+
     }
 
-    return true;
+    return bProcessSuccess;
 }
diff --git a/parameter/ParameterMgr.h b/parameter/ParameterMgr.h
index 0d7119d..47ba21f 100644
--- a/parameter/ParameterMgr.h
+++ b/parameter/ParameterMgr.h
@@ -159,16 +159,50 @@
     bool removeConfigurableElementFromDomain(const string& strDomain, const string& strConfigurableElementPath, string& strError);
     bool split(const string& strDomain, const string& strConfigurableElementPath, string& strError);
 
-    // XML Import/Export
-    bool importDomainsXml(const string& strFileName, bool bWithSettings, string& strError);
-    bool exportDomainsXml(const string& strFileName, bool bWithSettings, string& strError) const;
+    /**
+      * Method that imports Configurable Domains from an Xml source.
+      *
+      * @param[in] strXmlSource a string containing an xml description or a path to an xml file
+      * @param[in] bWithSettings a boolean that determines if the settings should be used in the
+      * xml description
+      * @param[in] bFromFile a boolean that determines if the source is an xml description in
+      * strXmlSource or contained in a file. In that case strXmlSource is just the file path.
+      * @param[out] strError is used as the error output
+      *
+      * @return false if any error occures
+      */
+    bool importDomainsXml(const string& strXmlSource, bool bWithSettings, bool bFromFile,
+                          string& strError);
+
+    /**
+      * Method that exports Configurable Domains to an Xml destination.
+      * If bToFile is false, the xml description from the xml document will be written
+      * in strXmlDest. Otherwise it will be written in a file located at the path in strXmlDest
+      *
+      * @param[in:out] strXmlDest a string containing an xml description or a path to an xml file
+      * @param[in] bWithSettings a boolean that determines if the settings should be used in the
+      * xml description
+      * @param[in] bToFile a boolean that determines if the destination is an xml description in
+      * strXmlDest or contained in a file. In that case strXmlDest is just the file path.
+      * @param[out] strError is used as the error output
+      *
+      * @return false if any error occures
+      */
+    bool exportDomainsXml(string& strXmlDest, bool bWithSettings, bool bToFile,
+                          string& strError) const;
 
     // Binary Import/Export
     bool importDomainsBinary(const string& strFileName, string& strError);
     bool exportDomainsBinary(const string& strFileName, string& strError);
 
-    // GUI command XML send
-    bool getDomainsXMLString(string& strResult, bool bWithSettings);
+    /**
+      * Method that creates an Xml description of the instanciated parameter structure contained
+      * in SystemClass.
+      *
+      * @param[out] strResult contains the xml description of SystemClass or the errors if any
+      *
+      * @return false if any error occures during the creation of the xml description
+      */
     bool getSystemClassXMLString(string& strResult);
 
     // Introspect
@@ -257,9 +291,42 @@
     CCommandHandler::CommandStatus importConfigurableDomainsWithSettingsFromXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult);
     CCommandHandler::CommandStatus exportSettingsCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult);
     CCommandHandler::CommandStatus importSettingsCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult);
-    /// GUI commands
-    CCommandHandler::CommandStatus getSystemClassXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult);
-    CCommandHandler::CommandStatus getDomainsXMLCommmandProcess(const IRemoteCommand& remoteCommand, string& strResult);
+
+    /**
+      * Command handler method for getConfigurableDomainWithSettings command.
+      *
+      * @param[in] remoteCommand contains the arguments of the received command.
+      * @param[out] strResult a string containing the result of the command
+      *
+      * @return CCommandHandler::ESucceeded if command succeeded or CCommandHandler::EFailed
+      * in the other case
+      */
+    CCommandHandler::CommandStatus getConfigurableDomainsWithSettingsXMLCommmandProcess(
+            const IRemoteCommand& remoteCommand, string& strResult);
+
+    /**
+      * Command handler method for setConfigurableDomainWithSettings command.
+      *
+      * @param[in] remoteCommand contains the arguments of the received command.
+      * @param[out] strResult a string containing the result of the command
+      *
+      * @return CCommandHandler::ESucceeded if command succeeded or CCommandHandler::EFailed
+      * in the other case
+      */
+    CCommandHandler::CommandStatus setConfigurableDomainsWithSettingsXMLCommmandProcess(
+            const IRemoteCommand& remoteCommand, string& strResult);
+
+    /**
+      * Command handler method for getSystemClass command.
+      *
+      * @param[in] remoteCommand contains the arguments of the received command.
+      * @param[out] strResult a string containing the result of the command
+      *
+      * @return CCommandHandler::ESucceeded if command succeeded or CCommandHandler::EFailed
+      * in the other case
+      */
+    CCommandHandler::CommandStatus getSystemClassXMLCommmandProcess(
+            const IRemoteCommand& remoteCommand, string& strResult);
 
     // Max command usage length, use for formatting
     void setMaxCommandUsageLength();