Improve xml parsing on diameter codec
authorEduardo Ramos Testillano (eramedu) <eduardo.ramos.testillano@ericsson.com>
Tue, 28 Apr 2020 09:21:48 +0000 (11:21 +0200)
committerEduardo Ramos Testillano (eramedu) <eduardo.ramos.testillano@ericsson.com>
Tue, 28 Apr 2020 09:21:48 +0000 (11:21 +0200)
loadXML is renamed to loadXMLFile to be more explicit.
Added loadXMLString to load an xml representation.
This will allow to interpret directly json values as base64 xml
encodings.

include/anna/diameter/codec/Message.hpp
include/anna/diameter/codec/functions.hpp
source/diameter/codec/Message.cpp
source/diameter/codec/functions.cpp

index dd0b13a..1cc8960 100644 (file)
@@ -556,6 +556,17 @@ public:
    */
   void loadXML(const std::string &xmlPathFile) throw(anna::RuntimeException);
 
    */
   void loadXML(const std::string &xmlPathFile) throw(anna::RuntimeException);
 
+  /**
+   * Interpret a xml string in order to create a diameter message
+   * You could apply this multiple times over the same object. A basic cleanup is done respecting the codec engine.
+   *
+   * @see functions::messageXmlDocumentFromXmlString
+   * @see fromXML
+   *
+   * @param xmlString xml representation of the diameter message
+   */
+  void loadXMLString(const std::string &xmlString) throw(anna::RuntimeException);
+
 
   // getters
 
 
   // getters
 
index acafa64..43dfa92 100644 (file)
@@ -318,7 +318,7 @@ struct functions {
      For example, you could load the document to be decoded over a codec Message by mean #Message::fromXML (using
      the xml document #getRootNode) during document lifetime. After that, it could be destroyed.
    */
      For example, you could load the document to be decoded over a codec Message by mean #Message::fromXML (using
      the xml document #getRootNode) during document lifetime. After that, it could be destroyed.
    */
-  static void messageXmlDocumentFromXmlString(anna::xml::DocumentFile &xmlDocument, const std::string &xmlString) throw(anna::RuntimeException);
+  static void messageXmlDocumentFromXmlString(anna::xml::DocumentMemory &xmlDocument, const std::string &xmlString) throw(anna::RuntimeException);
 };
 
 
 };
 
 
index 0ea876b..cdbd7bc 100644 (file)
@@ -770,6 +770,16 @@ void Message::loadXML(const std::string &xmlPathFile) throw(anna::RuntimeExcepti
   fromXML(xmlDocument.getRootNode());
 }
 
   fromXML(xmlDocument.getRootNode());
 }
 
+//------------------------------------------------------------------------------
+//----------------------------------------------------- Message::loadXMLString()
+//------------------------------------------------------------------------------
+void Message::loadXMLString(const std::string &xmlString) throw(anna::RuntimeException) {
+
+  anna::xml::DocumentMemory xmlDocument;
+  anna::diameter::codec::functions::messageXmlDocumentFromXmlString(xmlDocument, xmlString);
+  fromXML(xmlDocument.getRootNode());
+}
+
 //------------------------------------------------------------------------------
 //----------------------------------------------------------- Message::fromXML()
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 //----------------------------------------------------------- Message::fromXML()
 //------------------------------------------------------------------------------
index 93ee8e3..fe0ee03 100644 (file)
@@ -336,9 +336,15 @@ void anna::diameter::codec::functions::messageXmlDocumentFromXmlFile(anna::xml::
   );
 }
 
   );
 }
 
-void anna::diameter::codec::functions::messageXmlDocumentFromXmlString(anna::xml::DocumentFile &xmlDocument, const std::string &xmlString) throw(anna::RuntimeException) {
+void anna::diameter::codec::functions::messageXmlDocumentFromXmlString(anna::xml::DocumentMemory &xmlDocument, const std::string &xmlString) throw(anna::RuntimeException) {
   LOGDEBUG(anna::Logger::debug("Parsing diameter message from xml string representation into xml document", ANNA_FILE_LOCATION));
   xmlDocument.initialize(xmlString.c_str());
   LOGDEBUG(anna::Logger::debug("Parsing diameter message from xml string representation into xml document", ANNA_FILE_LOCATION));
   xmlDocument.initialize(xmlString.c_str());
-  xmlDocument.parse(MessageDTDMemory); // Parsing: fail here if xml violates dtd
-  LOGDEBUG(anna::Logger::debug("Parsing OK from XML string representation", ANNA_FILE_LOCATION));
+  const anna::xml::Node *rootNode = xmlDocument.parse(MessageDTDMemory); // Parsing: fail here if xml violates dtd
+  LOGDEBUG(
+      std::string trace = "Parsing OK from XML string representation '";
+  trace += xmlString;
+  trace += "':\n";
+  trace += anna::xml::Compiler().apply(rootNode);
+  anna::Logger::debug(trace, ANNA_FILE_LOCATION);
+  );
 }
 }