From 1fd0c19880f48fd5d98dfce81dfed21147b4dfe3 Mon Sep 17 00:00:00 2001 From: "Eduardo Ramos Testillano (eramedu)" Date: Sat, 2 May 2020 21:30:42 +0200 Subject: [PATCH] Normalize xml processing Centralize indentation spaces for xml representation with define macro 'ANNA_XML_INDENTATION_SPACES' which is 3. Add new working mode for xml Compiler: Sort. This one will sort attribute names within nodes. This eases normalization to match regexps based on xml diameter messages representation. As this mode is based on bit mask, must be power of 2 and the next free is 8. --- include/anna/core/util/defines.hpp | 3 +++ include/anna/xml/Compiler.hpp | 5 ++--- include/anna/xml/Node.hpp | 5 +++++ source/xml/Compiler.cpp | 16 ++++++++++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/include/anna/core/util/defines.hpp b/include/anna/core/util/defines.hpp index 7e56b7d..25a7e03 100644 --- a/include/anna/core/util/defines.hpp +++ b/include/anna/core/util/defines.hpp @@ -20,6 +20,9 @@ // For cstd0x compatibility we will use stdint.h types instead of std:: ones on cstdint: //#include when C++11 available +// For xml representations (xml::Compiler and json::functions::json2xml) +#define ANNA_XML_INDENTATION_SPACES 3 + // Decoding helpers #define DECODE2BYTES_INDX_VALUETYPE(buffer,indx,value_type) ((((value_type)buffer[indx] << 8) & 0xFF00) + ((value_type)buffer[indx+1] & 0x00FF)) diff --git a/include/anna/xml/Compiler.hpp b/include/anna/xml/Compiler.hpp index 581ba38..43e93ce 100644 --- a/include/anna/xml/Compiler.hpp +++ b/include/anna/xml/Compiler.hpp @@ -13,8 +13,6 @@ #include #include -// Three-space tabulator for xml string presentation (node indent) -#define ANNA_XML_COMPILER_TAB " " namespace anna { @@ -37,7 +35,8 @@ public: Visual = 0, /**< Separa cada uno de los componentes con espacios y retornos de carro */ Compact = 1, /**< Los componentes no tienen separacion entre si. Se puede usar para optimizar envios de mensajes */ ShowNullAttribute = 2, /**< Visualiza el valor nulo de un atributo como ="" */ - NoNamespaces = 4 /**; friend class XPath; friend class Decompressor; + friend class Compiler; }; } diff --git a/source/xml/Compiler.cpp b/source/xml/Compiler.cpp index 483e86e..1cbc286 100644 --- a/source/xml/Compiler.cpp +++ b/source/xml/Compiler.cpp @@ -7,6 +7,7 @@ #include +#include #include #include @@ -16,6 +17,10 @@ #include #include +#include +#include + + using namespace anna; using namespace anna::xml; @@ -133,7 +138,7 @@ throw(RuntimeException) { const Namespace* ns; for(int i = 0; i < level; i ++) - result += ANNA_XML_COMPILER_TAB; + result += std::string(ANNA_XML_INDENTATION_SPACES, ' '); result += '<'; writeFullName(node, result, flags); @@ -150,6 +155,13 @@ throw(RuntimeException) { } } + // Sort node attributes (this is a compiler used for on-demand representation, this sorting is not permanent in the object which uses it): + if(flags & Mode::Sort) { + Node *nc_node = const_cast(node); + std::sort(nc_node->attribute_begin(), nc_node->attribute_end(), + [](Attribute *a, Attribute *b) { return (std::strcmp(a->getName(), b->getName()) < 0); }); // sort alphabetically by attribute name + } + for(Node::const_attribute_iterator ii = node->attribute_begin(), maxii = node->attribute_end(); ii != maxii; ii ++) { attribute = Node::attribute(ii); result += ' '; @@ -203,7 +215,7 @@ throw(RuntimeException) { void Compiler::close(const Node* node, Result& result, const int level, const int flags) throw(RuntimeException) { for(int i = 0; i < level; i ++) - result += ANNA_XML_COMPILER_TAB; + result += std::string(ANNA_XML_INDENTATION_SPACES, ' '); result += "