diff options
author | Michael Stahl <mstahl@redhat.com> | 2013-06-05 16:48:37 +0200 |
---|---|---|
committer | Michael Stahl <mstahl@redhat.com> | 2013-06-05 16:55:51 +0200 |
commit | 96ee9884b78dce54223e83943a7c5c832ae78759 (patch) | |
tree | bb0f5cc4096dd1626427a2f885d70801803648e6 /sax | |
parent | 350534aba8f82d57782ec22fa4f6b776509beb2d (diff) |
sax: SaxWriter: add well-formedness assertions
In an --enable-dbgutil build, assert on the following XML
well-formedness violations:
* Element Type Match (-> start/end tag mismatch)
* Unique Att Spec (-> duplicate attributes)
Change-Id: I1e32a9fd096463a418a197fcdb3174df79f7a785
Diffstat (limited to 'sax')
-rw-r--r-- | sax/source/expatwrap/saxwriter.cxx | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/sax/source/expatwrap/saxwriter.cxx b/sax/source/expatwrap/saxwriter.cxx index 20660eb150d8..f49b8c2bc1fb 100644 --- a/sax/source/expatwrap/saxwriter.cxx +++ b/sax/source/expatwrap/saxwriter.cxx @@ -16,8 +16,12 @@ * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ + #include <string.h> +#include <stack> +#include <set> + #include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/util/XCloneable.hpp> #include <com/sun/star/xml/sax/XParser.hpp> @@ -72,6 +76,12 @@ enum SaxInvalidCharacterError class SaxWriterHelper { +#ifdef DBG_UTIL +public: + ::std::stack<OUString> m_DebugStartedElements; +#endif + +private: Reference< XOutputStream > m_out; Sequence < sal_Int8 > m_Sequence; sal_Int8* mp_Sequence; @@ -80,7 +90,6 @@ class SaxWriterHelper sal_uInt32 nCurrentPos; sal_Bool m_bStartElementFinished; - inline sal_uInt32 writeSequence() throw( SAXException ); // use only if to insert the bytes more space in the sequence is needed and @@ -541,6 +550,12 @@ inline void SaxWriterHelper::startDocument() throw( SAXException ) inline SaxInvalidCharacterError SaxWriterHelper::startElement(const OUString& rName, const Reference< XAttributeList >& xAttribs) throw( SAXException ) { FinishStartElement(); + +#ifdef DBG_UTIL + m_DebugStartedElements.push(rName); + ::std::set<OUString> DebugAttributes; +#endif + mp_Sequence[nCurrentPos] = '<'; nCurrentPos++; if (nCurrentPos == SEQUENCESIZE) @@ -558,7 +573,13 @@ inline SaxInvalidCharacterError SaxWriterHelper::startElement(const OUString& rN if (nCurrentPos == SEQUENCESIZE) nCurrentPos = writeSequence(); - if (!writeString(xAttribs->getNameByIndex( i ), sal_False, sal_False)) + OUString const& rAttrName(xAttribs->getNameByIndex(i)); +#ifdef DBG_UTIL + // Well-formedness constraint: Unique Att Spec + assert(DebugAttributes.find(rAttrName) == DebugAttributes.end()); + DebugAttributes.insert(rAttrName); +#endif + if (!writeString(rAttrName, sal_False, sal_False)) eRet = SAX_ERROR; mp_Sequence[nCurrentPos] = '='; @@ -608,6 +629,7 @@ inline sal_Bool SaxWriterHelper::FinishEmptyElement() throw( SAXException ) inline sal_Bool SaxWriterHelper::endElement(const OUString& rName) throw( SAXException ) { FinishStartElement(); + mp_Sequence[nCurrentPos] = '<'; nCurrentPos++; if (nCurrentPos == SEQUENCESIZE) @@ -1160,6 +1182,14 @@ void SAXWriter::endElement(const OUString& aName) throw (SAXException, Runtime } sal_Bool bRet(sal_True); + // check here because Helper's endElement is not always called +#ifdef DBG_UTIL + assert(!mp_SaxWriterHelper->m_DebugStartedElements.empty()); + // Well-formedness constraint: Element Type Match + assert(aName == mp_SaxWriterHelper->m_DebugStartedElements.top()); + mp_SaxWriterHelper->m_DebugStartedElements.pop(); +#endif + if( mp_SaxWriterHelper->FinishEmptyElement() ) m_bForceLineBreak = sal_False; else |