From 18edd88edc0c45d9c3b8f6faa45bab481ec078f5 Mon Sep 17 00:00:00 2001 From: Mohammed Abdul Azeem Date: Wed, 3 Aug 2016 17:27:48 +0530 Subject: GSoC - Making legacyfastparser to use tokens: This tokenizes some elements, de-tokenize while consuming and emits elements through legacy interface. DummyTokenHandler is just to test the correctness. Change-Id: I1ea1e4d806ed4d426215f93b3f6b66a9776f6479 Reviewed-on: https://gerrit.libreoffice.org/27849 Tested-by: Jenkins Reviewed-by: Noel Grandin --- sax/qa/cppunit/xmlimport.cxx | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'sax/qa/cppunit') diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index f7fcd739d4c3..c50a6749fef3 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -264,6 +266,73 @@ void SAL_CALL NSDocumentHandler::startElement( const OUString& aName, const Refe CPPUNIT_ASSERT(false); } +class DummyTokenHandler : public cppu::WeakImplHelper< XFastTokenHandler >, + public sax_fastparser::FastTokenHandlerBase +{ +public: + const static OUString tokens[]; + const static OUString namespaceURIs[]; + const static OUString namespacePrefixes[]; + + // XFastTokenHandler + virtual Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 nToken ) + throw (css::uno::RuntimeException, std::exception) override; + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const css::uno::Sequence< sal_Int8 >& Identifier ) + throw (css::uno::RuntimeException, std::exception) override; + //FastTokenHandlerBase + virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const override; +}; + +const OUString DummyTokenHandler::tokens[] = { "Signature", "CanonicalizationMethod", "Algorithm", "Type", + "DigestMethod", "Reference", "document", + "spacing", "Player", "Height" }; + +const OUString DummyTokenHandler::namespaceURIs[] = { "http://www.w3.org/2000/09/xmldsig#", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main/", + "xyzsports.com/players/football/" }; + +const OUString DummyTokenHandler::namespacePrefixes[] = { "", "w", "Player" }; + +Sequence< sal_Int8 > DummyTokenHandler::getUTF8Identifier( sal_Int32 nToken ) + throw (uno::RuntimeException, std::exception) +{ + OString aUtf8Token; + if ( ( ( nToken & 0xffff0000 ) != 0 ) ) //namespace + { + sal_uInt32 nNamespaceToken = ( nToken >> 16 ) - 1; + if ( nNamespaceToken < sizeof( namespacePrefixes ) / sizeof( OUString ) ) + aUtf8Token = OUStringToOString( namespacePrefixes[ nNamespaceToken ], RTL_TEXTENCODING_UTF8 ); + } + else //element or attribute + { + sal_uInt32 nElementToken = nToken & 0xffff; + if ( nElementToken < sizeof( tokens ) / sizeof( OUString ) ) + aUtf8Token = OUStringToOString( tokens[ nElementToken ], RTL_TEXTENCODING_UTF8 ); + } + Sequence< sal_Int8 > aSeq = Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( + aUtf8Token.getStr() ), aUtf8Token.getLength() ); + return aSeq; +} + +sal_Int32 DummyTokenHandler::getTokenFromUTF8( const uno::Sequence< sal_Int8 >& rIdentifier ) + throw (uno::RuntimeException, std::exception) +{ + return getTokenDirect( reinterpret_cast< const char* >( + rIdentifier.getConstArray() ), rIdentifier.getLength() ); +} + +sal_Int32 DummyTokenHandler::getTokenDirect( const char* pToken, sal_Int32 nLength ) const +{ + OUString sToken( pToken, nLength, RTL_TEXTENCODING_UTF8 ); + for( sal_uInt16 i = 0; i < sizeof(tokens)/sizeof(OUString); i++ ) + { + if ( tokens[i] == sToken ) + return (sal_Int32)i; + } + return FastToken::DONTKNOW; +} + + class XMLImportTest : public test::BootstrapFixture { private: @@ -298,6 +367,26 @@ void XMLImportTest::setUp() m_xLegacyFastParser.set( xContext->getServiceManager()->createInstanceWithContext ( "com.sun.star.xml.sax.LegacyFastParser", xContext ), UNO_QUERY ); m_xLegacyFastParser->setDocumentHandler( m_xDocumentHandler.get() ); + + Reference< XFastTokenHandler > xTokenHandler; + xTokenHandler.set( new DummyTokenHandler() ); + uno::Reference const xInit(m_xLegacyFastParser, + uno::UNO_QUERY_THROW); + uno::Sequence args(1); + args[0] <<= xTokenHandler; + xInit->initialize( args ); + + sal_Int32 nNamespaceCount = sizeof( DummyTokenHandler::namespaceURIs ) / sizeof( OUString ); + uno::Sequence namespaceArgs( nNamespaceCount + 1 ); + namespaceArgs[0] <<= OUString( "registerNamespaces" ); + for (sal_Int32 i = 1; i <= nNamespaceCount; i++ ) + { + css::beans::Pair rPair; + rPair = css::beans::Pair( DummyTokenHandler::namespaceURIs[i - 1], i << 16 ); + namespaceArgs[i] <<= rPair; + } + xInit->initialize( namespaceArgs ); + m_sDirPath = m_directories.getPathFromSrc( "/sax/qa/data/" ); } -- cgit