From ab6fbbdfed3b85fdd6f4ea2aafd272602699dde7 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 13 Oct 2014 21:42:08 +0200 Subject: [PATCH] [DEV] add parsing of &"'<> spacial value in generic text. --- exml/Element.cpp | 16 +++++++++++----- exml/Element.h | 2 +- exml/Text.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- exml/Text.h | 1 + 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/exml/Element.cpp b/exml/Element.cpp index a925910..6e5a89d 100644 --- a/exml/Element.cpp +++ b/exml/Element.cpp @@ -137,11 +137,18 @@ void exml::Element::append(exml::Node* _node) { } std::string exml::Element::getText() { - // TODO : add more capabilities ... std::string res; - for (size_t iii=0; iiiiGenerate(res, 0); + if (m_listSub.size() == 1) { + if (m_listSub[0]->getType() == typeText) { + res = m_listSub[0]->getValue(); + } else { + m_listSub[0]->iGenerate(res, 0); + } + } else { + for (size_t iii=0; iiiiGenerate(res, 0); + } } } return res; @@ -309,7 +316,6 @@ bool exml::Element::subParse(const std::string& _data, int32_t& _pos, bool _case CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("End file with ' invalide XML"); return false; } - continue; } if(_data[iii+white+1] == '/') { diff --git a/exml/Element.h b/exml/Element.h index d1b927f..348899f 100644 --- a/exml/Element.h +++ b/exml/Element.h @@ -77,7 +77,7 @@ namespace exml { const Element* getNamed(const std::string& _name) const; /** * @brief get the internal data of the element (if the element has some sub node thay are converted in xml string == > like this it is not needed to use - * @return the curent data string. + * @return the curent data string. if Only one text node, then we get the parssed data (no & ...) if more than one node, then we transform &,",',<,> in xml normal text... */ std::string getText(); protected: diff --git a/exml/Text.cpp b/exml/Text.cpp index acd0a19..900978a 100644 --- a/exml/Text.cpp +++ b/exml/Text.cpp @@ -9,11 +9,50 @@ #include #include #include +#include #undef __class__ #define __class__ "Text" +// transform the Text with : +// "<" == "<" +// ">" == ">" +// "&" == "&" +// "'" == "'" +// """ == """ +static std::string replaceSpecialChar(const std::string& _inval) { + std::string out; + static std::regex regexLT("<"); + static std::regex regexGT(">"); + static std::regex regexAPOS("'"); + static std::regex regexQUOT("""); + static std::regex regexAMP("&"); + + out = std::regex_replace(_inval, regexLT, "<"); + out = std::regex_replace(out, regexGT, ">"); + out = std::regex_replace(out, regexAPOS, "'"); + out = std::regex_replace(out, regexQUOT, "\""); + out = std::regex_replace(out, regexAMP, "&"); + //EXML_ERROR("plop "<< _inval << " => " << out); + return out; +} +static std::string replaceSpecialCharOut(const std::string& _inval) { + std::string out; + static std::regex regexLT("<"); + static std::regex regexGT(">;"); + static std::regex regexAMP("&"); + static std::regex regexAPOS("'"); + static std::regex regexQUOT("\""); + + out = std::regex_replace(_inval, regexAMP, "&"); + out = std::regex_replace(out, regexQUOT, """); + out = std::regex_replace(out, regexAPOS, "'"); + out = std::regex_replace(out, regexGT, ">"); + out = std::regex_replace(out, regexLT, "<"); + return out; +} + static bool isWhiteChar(char32_t _val) { if( _val == ' ' || _val == '\t' @@ -25,7 +64,7 @@ static bool isWhiteChar(char32_t _val) { } bool exml::Text::iGenerate(std::string& _data, int32_t _indent) const { - _data += m_value; + _data += replaceSpecialCharOut(m_value); return true; } @@ -65,6 +104,7 @@ bool exml::Text::iParse(const std::string& _data, int32_t& _pos, bool _caseSensi m_value = std::string(_data, _pos, newEnd-(_pos)); EXML_VERBOSE(" find text '" << m_value << "'"); _pos = iii-1; + m_value = replaceSpecialChar(m_value); return true; } } @@ -73,6 +113,11 @@ bool exml::Text::iParse(const std::string& _data, int32_t& _pos, bool _caseSensi return false; } +bool exml::TextCDATA::iGenerate(std::string& _data, int32_t _indent) const { + _data += ""; + return true; +} + bool exml::TextCDATA::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { EXML_VERBOSE("start parse : 'text::CDATA'"); m_pos = _filePos; diff --git a/exml/Text.h b/exml/Text.h index 1a88aba..3e89ae9 100644 --- a/exml/Text.h +++ b/exml/Text.h @@ -58,6 +58,7 @@ namespace exml { virtual ~TextCDATA() { }; public: // herited function: virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); + virtual bool iGenerate(std::string& _data, int32_t _indent) const; }; };