From: Eduardo Ramos Testillano (eramedu) Date: Tue, 28 Apr 2020 09:21:48 +0000 (+0200) Subject: Improve xml parsing on diameter codec X-Git-Url: https://git.teslayout.com/public/public/public/?a=commitdiff_plain;h=57040efdece4ea1f4487608de3d1afbb9d1378c8;p=anna.git Improve xml parsing on diameter codec 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. --- diff --git a/include/anna/diameter/codec/Message.hpp b/include/anna/diameter/codec/Message.hpp index dd0b13a..1cc8960 100644 --- a/include/anna/diameter/codec/Message.hpp +++ b/include/anna/diameter/codec/Message.hpp @@ -556,6 +556,17 @@ public: */ 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 diff --git a/include/anna/diameter/codec/functions.hpp b/include/anna/diameter/codec/functions.hpp index acafa64..43dfa92 100644 --- a/include/anna/diameter/codec/functions.hpp +++ b/include/anna/diameter/codec/functions.hpp @@ -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. */ - 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); }; diff --git a/source/diameter/codec/Message.cpp b/source/diameter/codec/Message.cpp index 0ea876b..cdbd7bc 100644 --- a/source/diameter/codec/Message.cpp +++ b/source/diameter/codec/Message.cpp @@ -770,6 +770,16 @@ void Message::loadXML(const std::string &xmlPathFile) throw(anna::RuntimeExcepti 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() //------------------------------------------------------------------------------ diff --git a/source/diameter/codec/functions.cpp b/source/diameter/codec/functions.cpp index 93ee8e3..fe0ee03 100644 --- a/source/diameter/codec/functions.cpp +++ b/source/diameter/codec/functions.cpp @@ -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()); - 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); + ); }