From 9efa390989ab5a5d6c4dc815f9926ab2f7719c0c Mon Sep 17 00:00:00 2001 From: marian Date: Fri, 6 Jan 2012 10:05:05 +0000 Subject: [PATCH] Fixed bug: always produce the same xml text regardless the way data is pushed into sax parser (sequentially or full). Added method to finalize incomplete xml text after pushing all data to sax parser. Added debug. git-svn-id: http://yate.null.ro/svn/yate/trunk@4798 acf43c95-373e-0410-b603-e72c3f656dc1 --- libs/yxml/XML.cpp | 51 +++++++++++++++++++++++++++++++++------------ libs/yxml/yatexml.h | 15 +++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/libs/yxml/XML.cpp b/libs/yxml/XML.cpp index ba654be0..a69bad32 100644 --- a/libs/yxml/XML.cpp +++ b/libs/yxml/XML.cpp @@ -167,21 +167,15 @@ bool XmlSaxParser::parse(const char* text) auxData << m_buf.substr(0,len); } if (auxData.c_str()) { // We have an end of tag or another child is riseing - resetError(); - unEscape(auxData); - gotText(auxData); - resetParsed(); - if (error()) + if (!processText(auxData)) return false; m_buf = m_buf.substr(len); len = 0; auxData = ""; } - skipBlanks(); - len = 0; - if (!m_buf.at(len + 1)) + char auxCar = m_buf.at(1); + if (!auxCar) return setError(Incomplete); - char auxCar = m_buf.at(len + 1); if (auxCar == '?') { m_buf = m_buf.substr(2); if (!parseInstruction()) @@ -206,10 +200,14 @@ bool XmlSaxParser::parse(const char* text) if (!parseElement()) return false; } - if (!completed()) { - // We have an element that is not complete - auxData << m_buf; - m_parsed.assign(auxData); + // Incomplete text + if ((unparsed() == None || unparsed() == Text) && (auxData || m_buf)) { + if (!auxData) + m_parsed.assign(m_buf); + else { + auxData << m_buf; + m_parsed.assign(auxData); + } m_buf = ""; setUnparsed(Text); return setError(Incomplete); @@ -220,9 +218,19 @@ bool XmlSaxParser::parse(const char* text) } m_buf = ""; resetParsed(); + setUnparsed(None); return true; } +// Process incomplete text +bool XmlSaxParser::completeText() +{ + if (!completed() || unparsed() != Text || error() != Incomplete) + return error() == NoError; + String tmp = m_parsed; + return processText(tmp); +} + // Parse an unfinished xml object bool XmlSaxParser::auxParse() { @@ -583,6 +591,7 @@ bool XmlSaxParser::parseComment() // Parse an element form the main buffer bool XmlSaxParser::parseElement() { + XDebug(this,DebugAll,"XmlSaxParser::parseElement() buf len=%u [%p]",m_buf.length(),this); if (!m_buf.c_str()) { setUnparsed(Element); return setError(Incomplete); @@ -1064,6 +1073,22 @@ bool XmlSaxParser::processElement(NamedList& list, bool empty) return false; } +// Calls gotText() and reset parsed on success +bool XmlSaxParser::processText(String& text) +{ + resetError(); + unEscape(text); + if (!error()) + gotText(text); + else + setUnparsed(Text); + if (!error()) { + resetParsed(); + setUnparsed(None); + } + return error() == NoError; +} + /* * XmlDomPareser diff --git a/libs/yxml/yatexml.h b/libs/yxml/yatexml.h index 2a335b94..22bdf05a 100644 --- a/libs/yxml/yatexml.h +++ b/libs/yxml/yatexml.h @@ -153,6 +153,14 @@ public: */ bool parse(const char* data); + /** + * Process incomplete text if the parser is completed. + * This method should be called to complete text after + * all data was pushed into the parser + * @return True if all data was successfully parsed + */ + bool completeText(); + /** * Get the error code found while parsing * @return Error code @@ -460,6 +468,13 @@ protected: */ bool processElement(NamedList& list, bool empty); + /** + * Unescape text, call gotText() and reset parsed on success + * @param text The text to process + * @return True if there is no error + */ + bool processText(String& text); + /** * The offset where the parser was stop */