diff options
author | Mohammed Abdul Azeem <azeemmysore@gmail.com> | 2016-05-26 02:05:13 +0530 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2016-05-28 09:46:52 +0000 |
commit | 8128d597224b55965c4f1bba3d824f193a55041b (patch) | |
tree | ea27a1aac6903a855526944219dbd4afbebca63f /sax/qa/cppunit | |
parent | ab0189433c1593c3c3ccf6a947aa7ba84e806d91 (diff) |
sax2/ unit tests.
Test xml files are added, string is built for each file
and namespaces are handled.
Change-Id: I0ab799ca5c9de7311ccca2a6033a96e02598064f
Reviewed-on: https://gerrit.libreoffice.org/25468
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'sax/qa/cppunit')
-rw-r--r-- | sax/qa/cppunit/xmlimport.cxx | 114 |
1 files changed, 94 insertions, 20 deletions
diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index 204c45d18fd7..0ba32c15578a 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -32,12 +32,16 @@ #include <osl/file.hxx> #include <unotools/ucbstreamhelper.hxx> #include <unotools/streamwrap.hxx> +#include <string> +#include <stack> +#include <deque> namespace { using namespace css; using namespace css::uno; using namespace css::io; +using namespace std; Reference< XInputStream > createStreamFromFile ( const OUString filePath) @@ -56,11 +60,17 @@ Reference< XInputStream > createStreamFromFile ( class TestDocumentHandler : public cppu::WeakImplHelper< xml::sax::XDocumentHandler > { private: - // OUString m_aStr; + OUString m_aStr; + deque< pair<OUString,OUString> > m_aNamespaceStack; + stack<sal_uInt16> m_aCountStack; + + OUString canonicalform(const OUString &sName, const OUString &sValue, bool isElement); + OUString getNamespace(const OUString &sName); public: TestDocumentHandler() {} - //const OUString& getString() { return m_aStr; } + OUString getString() { return m_aStr; } + // XDocumentHandler virtual void SAL_CALL startDocument() throw (xml::sax::SAXException, RuntimeException, std::exception) override; virtual void SAL_CALL endDocument() throw (xml::sax::SAXException, RuntimeException, std::exception) override; @@ -72,10 +82,53 @@ public: virtual void SAL_CALL setDocumentLocator( const Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; }; +OUString TestDocumentHandler::canonicalform(const OUString &sName, const OUString &sValue, bool isElement) +{ + sal_Int16 nIndex = sName.indexOf(":"); + if ( !isElement && sName.match( "xmlns" ) ) + { + m_aCountStack.top() += 1; + if ( nIndex < 0 ) + m_aNamespaceStack.push_back( make_pair( OUString( "default" ), sValue ) ); + else + m_aNamespaceStack.push_back( make_pair( sName.copy( nIndex + 1 ), sValue ) ); + } + else + { + if ( nIndex >= 0 ) + { + OUString sNamespace = getNamespace( sName.copy( 0, nIndex ) ); + return sNamespace + sName.copy(nIndex); + } + else + { + OUString sDefaultns = getNamespace( "default" ); + if ( !isElement || sDefaultns.isEmpty() ) + return sName; + else + return sDefaultns + ":" + sName; + } + } + return OUString(""); +} + +OUString TestDocumentHandler::getNamespace(const OUString &sName) +{ + for (sal_Int16 i = m_aNamespaceStack.size() - 1; i>=0; i--) + { + pair<OUString, OUString> aPair = m_aNamespaceStack.at(i); + if (aPair.first == sName) + return aPair.second; + } + return OUString(""); +} + void SAL_CALL TestDocumentHandler::startDocument() throw(xml::sax::SAXException, RuntimeException, std::exception) { - // m_aStr.clear(); + m_aStr.clear(); + m_aNamespaceStack.emplace_back( make_pair( OUString( "default" ), OUString() ) ); + m_aCountStack.emplace(0); } @@ -84,30 +137,45 @@ void SAL_CALL TestDocumentHandler::endDocument() { } -void SAL_CALL TestDocumentHandler::startElement( const OUString& /*aName*/, const Reference< xml::sax::XAttributeList >& /*xAttribs*/ ) +void SAL_CALL TestDocumentHandler::startElement( const OUString& aName, const Reference< xml::sax::XAttributeList >& xAttribs ) throw( xml::sax::SAXException, RuntimeException, std::exception ) { - // m_aStr = m_aStr + "<" + aName + "> "; + OUString sAttributes; + m_aCountStack.push(0); + sal_uInt16 len = xAttribs->getLength(); + for (sal_uInt16 i=0; i<len; i++) + { + OUString sAttrValue = xAttribs->getValueByIndex(i); + OUString sAttrName = canonicalform(xAttribs->getNameByIndex(i), sAttrValue, false); + if (!sAttrName.isEmpty()) + sAttributes = sAttrName + sAttrValue; + } + m_aStr = m_aStr + canonicalform(aName, "", true) + sAttributes; } -void SAL_CALL TestDocumentHandler::endElement( const OUString& /*aName*/ ) +void SAL_CALL TestDocumentHandler::endElement( const OUString& aName ) throw( xml::sax::SAXException, RuntimeException, std::exception ) { - // m_aStr = m_aStr + "</" + aName + ">\n"; + m_aStr = m_aStr + canonicalform(aName, "", true); + sal_uInt16 nPopQty = m_aCountStack.top(); + for (sal_uInt16 i=0; i<nPopQty; i++) + m_aNamespaceStack.pop_back(); + m_aCountStack.pop(); } -void SAL_CALL TestDocumentHandler::characters( const OUString& /*aChars*/ ) +void SAL_CALL TestDocumentHandler::characters( const OUString& aChars ) throw(xml::sax::SAXException, RuntimeException, std::exception) { - // m_aStr = m_aStr + aChars + " "; + m_aStr = m_aStr + aChars; } -void SAL_CALL TestDocumentHandler::ignorableWhitespace( const OUString& /*aWhitespaces*/ ) +void SAL_CALL TestDocumentHandler::ignorableWhitespace( const OUString& aWhitespaces ) throw(xml::sax::SAXException, RuntimeException, std::exception) { + m_aStr = m_aStr + aWhitespaces; } @@ -161,16 +229,22 @@ void XMLImportTest::tearDown() void XMLImportTest::parse() { - OUString fileName = "note.xml"; - Reference< XInputStream > rIS = createStreamFromFile( m_sDirPath + fileName ); - xml::sax::InputSource source; - source.aInputStream = rIS; - source.sSystemId = "internal"; - m_xParser->parseStream(source); - // OUString aStr = m_xDocumentHandler->getString(); - // OString o = OUStringToOString( aStr, RTL_TEXTENCODING_ASCII_US ); - // CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false ); - CPPUNIT_ASSERT(true); + OUString fileNames[] = {"simple.xml", "defaultns.xml", "inlinens.xml", + "multiplens.xml", "multiplepfx.xml", + "nstoattributes.xml", "nestedns.xml"}; + + for (sal_uInt16 i = 0; i < sizeof( fileNames ) / sizeof( string ); i++) + { + Reference< XInputStream > rIS = createStreamFromFile( m_sDirPath + fileNames[i] ); + xml::sax::InputSource source; + source.aInputStream = rIS; + source.sSystemId = "internal"; + m_xParser->parseStream(source); + // OUString aStr = m_xDocumentHandler->getString(); + // OString o = OUStringToOString( aStr, RTL_TEXTENCODING_ASCII_US ); + // CPPUNIT_ASSERT_MESSAGE( std::string(o.pData->buffer), false ); + CPPUNIT_ASSERT(true); + } } CPPUNIT_TEST_SUITE_REGISTRATION( XMLImportTest ); |