diff options
author | Mohammed Abdul Azeem <azeemmysore@gmail.com> | 2016-06-13 23:30:09 +0530 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2016-06-17 10:04:58 +0000 |
commit | 551a5066fcc8000850d34408d8c1bcdd5b599db3 (patch) | |
tree | 3db4333c535b828e61147d4a7c34bc02d27590d6 | |
parent | 4561119a8bab986df25a5ce2a544aa96394cbd5d (diff) |
GSoC: Mapping legacy sax parser and XFastParser
This implements legacy parser interface using XFastParser, and
unit test is also added.
Change-Id: Ia2eb7d517d80a3f7ec0cf26ffa2e5747ad22b186
Reviewed-on: https://gerrit.libreoffice.org/26229
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Tested-by: Michael Meeks <michael.meeks@collabora.com>
-rw-r--r-- | sax/Library_expwrap.mk | 1 | ||||
-rw-r--r-- | sax/qa/cppunit/xmlimport.cxx | 53 | ||||
-rw-r--r-- | sax/source/expatwrap/expwrap.component | 4 | ||||
-rw-r--r-- | sax/source/fastparser/legacyfastparser.cxx | 275 |
4 files changed, 325 insertions, 8 deletions
diff --git a/sax/Library_expwrap.mk b/sax/Library_expwrap.mk index e8e049ab1efd..6b38278982e9 100644 --- a/sax/Library_expwrap.mk +++ b/sax/Library_expwrap.mk @@ -44,6 +44,7 @@ $(eval $(call gb_Library_add_exception_objects,expwrap,\ sax/source/expatwrap/saxwriter \ sax/source/expatwrap/xml2utf \ sax/source/fastparser/fastparser \ + sax/source/fastparser/legacyfastparser \ )) # vim: set noet sw=4 ts=4: diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index c8bd21740b79..fe4af600022f 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -83,6 +83,7 @@ private: public: TestDocumentHandler() {} const OUString& getString() { return m_aStr; } + void setString( const OUString aStr ) { m_aStr = aStr; } // XDocumentHandler virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override; @@ -319,15 +320,43 @@ public: } }; +class TestLegacyDocumentHandler : public TestDocumentHandler +{ +public: + virtual void SAL_CALL startElement( const OUString& aName, const Reference< XAttributeList >& xAttribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endElement( const OUString& aName ) throw (SAXException, RuntimeException, exception) override; +}; + +void SAL_CALL TestLegacyDocumentHandler::startElement( const OUString& aName, const Reference< XAttributeList >& xAttribs ) + throw( SAXException, RuntimeException, exception ) +{ + setString( getString() + aName ); + sal_uInt16 len = xAttribs->getLength(); + for (sal_uInt16 i = 0; i < len; i++) + { + OUString sAttrName = xAttribs->getNameByIndex(i); + OUString sAttrValue = xAttribs->getValueByIndex(i); + setString( getString() + sAttrName + sAttrValue ); + } +} + + +void SAL_CALL TestLegacyDocumentHandler::endElement( const OUString& aName ) + throw( SAXException, RuntimeException, exception ) +{ + setString( getString() + aName ); +} class XMLImportTest : public test::BootstrapFixture { private: OUString m_sDirPath; - rtl::Reference< TestDocumentHandler > m_xDocumentHandler; - rtl::Reference< TestFastDocumentHandler > m_xFastDocumentHandler; + Reference< TestDocumentHandler > m_xDocumentHandler; + Reference< TestFastDocumentHandler > m_xFastDocumentHandler; Reference< XParser > m_xParser; Reference< XFastParser > m_xFastParser; + Reference< XParser > m_xLegacyFastParser; + Reference< TestLegacyDocumentHandler > m_xLegacyDocumentHandler; Reference< XFastTokenHandler > m_xFastTokenHandler; public: @@ -345,16 +374,19 @@ public: void XMLImportTest::setUp() { test::BootstrapFixture::setUp(); + Reference< XComponentContext > xContext = comphelper::getProcessComponentContext(); m_xDocumentHandler.set( new TestDocumentHandler() ); m_xFastDocumentHandler.set( new TestFastDocumentHandler() ); + m_xLegacyDocumentHandler.set( new TestLegacyDocumentHandler() ); m_xFastTokenHandler.set( new TestTokenHandler() ); - m_xParser = Parser::create( - ::comphelper::getProcessComponentContext() ); - m_xFastParser = FastParser::create( - ::comphelper::getProcessComponentContext() ); - m_xParser->setDocumentHandler( m_xDocumentHandler.get() ); - m_xFastParser->setFastDocumentHandler( m_xFastDocumentHandler.get() ); + m_xParser = Parser::create( xContext ); + m_xFastParser = FastParser::create( xContext ); + m_xParser->setDocumentHandler( m_xDocumentHandler ); + m_xFastParser->setFastDocumentHandler( m_xFastDocumentHandler ); m_xFastParser->setTokenHandler( m_xFastTokenHandler ); + m_xLegacyFastParser.set( xContext->getServiceManager()->createInstanceWithContext + ( "com.sun.star.xml.sax.LegacyFastParser", xContext ), UNO_QUERY ); + m_xLegacyFastParser->setDocumentHandler( m_xLegacyDocumentHandler ); m_sDirPath = m_directories.getPathFromSrc( "/sax/qa/data/" ); } @@ -382,7 +414,12 @@ void XMLImportTest::parse() m_xFastParser->parseStream(source); const OUString& rFastParserStr = m_xFastDocumentHandler->getString(); + source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] ); + m_xLegacyFastParser->parseStream(source); + const OUString& rLegacyFastParserStr = m_xLegacyDocumentHandler->getString(); + CPPUNIT_ASSERT_EQUAL( rParserStr, rFastParserStr ); + CPPUNIT_ASSERT_EQUAL( rFastParserStr, rLegacyFastParserStr ); // OString o = OUStringToOString( Str, RTL_TEXTENCODING_ASCII_US ); // CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false ); } diff --git a/sax/source/expatwrap/expwrap.component b/sax/source/expatwrap/expwrap.component index 612819865a80..1f72eccf3145 100644 --- a/sax/source/expatwrap/expwrap.component +++ b/sax/source/expatwrap/expwrap.component @@ -31,4 +31,8 @@ constructor="com_sun_star_comp_extensions_xml_sax_FastParser_get_implementation"> <service name="com.sun.star.xml.sax.FastParser"/> </implementation> + <implementation name="com.sun.star.comp.extensions.xml.sax.LegacyFastParser" + constructor="com_sun_star_comp_extensions_xml_sax_LegacyFastParser_get_implementation"> + <service name="com.sun.star.xml.sax.LegacyFastParser"/> + </implementation> </component> diff --git a/sax/source/fastparser/legacyfastparser.cxx b/sax/source/fastparser/legacyfastparser.cxx new file mode 100644 index 000000000000..8210ab0c8be6 --- /dev/null +++ b/sax/source/fastparser/legacyfastparser.cxx @@ -0,0 +1,275 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/xml/sax/XParser.hpp> +#include <com/sun/star/xml/sax/FastParser.hpp> +#include <com/sun/star/xml/sax/FastToken.hpp> +#include <comphelper/attributelist.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <comphelper/processfactory.hxx> +#include <rtl/ref.hxx> +#include <sax/fastparser.hxx> + +using namespace std; +using namespace ::cppu; +using namespace css; +using namespace uno; +using namespace lang; +using namespace xml::sax; +using namespace io; + +namespace { + + +class SaxLegacyFastParser : public WeakImplHelper< XServiceInfo, XParser > +{ +public: + SaxLegacyFastParser(); +// The SAX-Parser-Interface + virtual void SAL_CALL parseStream( const InputSource& structSource) + throw ( SAXException, IOException, RuntimeException, exception) override; + virtual void SAL_CALL setDocumentHandler(const Reference< XDocumentHandler > & xHandler) + throw (RuntimeException, exception) override; + virtual void SAL_CALL setErrorHandler(const Reference< XErrorHandler > & xHandler) + throw (RuntimeException, exception) override; + virtual void SAL_CALL setDTDHandler(const Reference < XDTDHandler > & xHandler) + throw (RuntimeException, exception) override; + virtual void SAL_CALL setEntityResolver(const Reference< XEntityResolver >& xResolver) + throw (RuntimeException, exception) override; + virtual void SAL_CALL setLocale( const Locale &locale ) + throw (RuntimeException, exception) override; + +// XServiceInfo + OUString SAL_CALL getImplementationName() throw (exception) override; + Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (exception) override; + sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (exception) override; + +private: + Reference< XFastParser > m_xParser; + Reference< XDocumentHandler > m_xDocumentHandler; + +}; + +class CallbackTokenHandler : public cppu::WeakImplHelper< XFastTokenHandler > +{ +public: + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const Sequence<sal_Int8>& ) + throw (RuntimeException, exception) override + { + return FastToken::DONTKNOW; + } + virtual Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 ) + throw (RuntimeException, exception) override + { + return Sequence<sal_Int8>(); + } +}; + +class CallbackDocumentHandler : public WeakImplHelper< XFastDocumentHandler > +{ +private: + Reference< XDocumentHandler > m_xDocumentHandler; +public: + CallbackDocumentHandler( Reference< XDocumentHandler > xDocumentHandler ) + { m_xDocumentHandler.set( xDocumentHandler ); } + + // XFastDocumentHandler + virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) throw (SAXException, RuntimeException, exception) override; + + // XFastContextHandler + virtual void SAL_CALL startFastElement( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endFastElement( sal_Int32 Element ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) throw (SAXException, RuntimeException, exception) override; + virtual Reference< XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual Reference< XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL characters( const OUString& aChars ) throw (SAXException, RuntimeException, exception) override; + +}; + +void SAL_CALL CallbackDocumentHandler::startDocument() + throw (SAXException, RuntimeException, exception) +{ + if ( m_xDocumentHandler.is() ) + m_xDocumentHandler->startDocument(); +} + +void SAL_CALL CallbackDocumentHandler::endDocument() + throw (SAXException, RuntimeException, exception) +{ + if ( m_xDocumentHandler.is() ) + m_xDocumentHandler->endDocument(); +} + +void SAL_CALL CallbackDocumentHandler::setDocumentLocator( const Reference< XLocator >& xLocator ) + throw (SAXException, RuntimeException, exception) +{ + if ( m_xDocumentHandler.is() ) + m_xDocumentHandler->setDocumentLocator( xLocator ); +} + +void SAL_CALL CallbackDocumentHandler::startFastElement( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ ) + throw (SAXException, RuntimeException, exception) +{ +} + +void SAL_CALL CallbackDocumentHandler::startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) + throw (SAXException, RuntimeException, exception) +{ + if ( m_xDocumentHandler.is() ) + { + OUString elementName; + rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList; + if ( !Namespace.isEmpty() ) + elementName = Namespace + ":" + Name; + else + elementName = Name; + + Sequence< xml::Attribute > unknownAttribs = Attribs->getUnknownAttributes(); + sal_uInt16 len = unknownAttribs.getLength(); + for (sal_uInt16 i = 0; i < len; i++) + { + OUString& rAttrValue = unknownAttribs[i].Value; + OUString sAttrName = unknownAttribs[i].Name; + OUString& rAttrNamespaceURL = unknownAttribs[i].NamespaceURL; + if ( !rAttrNamespaceURL.isEmpty() ) + sAttrName = rAttrNamespaceURL + ":" + sAttrName; + + rAttrList->AddAttribute( sAttrName, "CDATA", rAttrValue ); + } + m_xDocumentHandler->startElement( elementName, rAttrList.get() ); + } +} + +void SAL_CALL CallbackDocumentHandler::endFastElement( sal_Int32/* nElement */) + throw (SAXException, RuntimeException, exception) +{ +} + + +void SAL_CALL CallbackDocumentHandler::endUnknownElement( const OUString& Namespace, const OUString& Name ) + throw (SAXException, RuntimeException, exception) +{ + if ( m_xDocumentHandler.is() ) + { + OUString elementName; + if ( !Namespace.isEmpty() ) + elementName = Namespace + ":" + Name; + else + elementName = Name; + m_xDocumentHandler->endElement( elementName ); + } +} + +Reference< XFastContextHandler > SAL_CALL CallbackDocumentHandler::createFastChildContext( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ ) + throw (SAXException, RuntimeException, exception) +{ + return this; +} + + +Reference< XFastContextHandler > SAL_CALL CallbackDocumentHandler::createUnknownChildContext( const OUString&/* Namespace */, const OUString&/* Name */, const Reference< XFastAttributeList >&/* Attribs */ ) + throw (SAXException, RuntimeException, exception) +{ + return this; +} + +void SAL_CALL CallbackDocumentHandler::characters( const OUString& aChars ) + throw (SAXException, RuntimeException, exception) +{ + if ( m_xDocumentHandler.is() ) + m_xDocumentHandler->characters( aChars ); +} + +SaxLegacyFastParser::SaxLegacyFastParser( ) +{ + m_xParser = FastParser::create( + ::comphelper::getProcessComponentContext() ); + m_xParser->setTokenHandler( new CallbackTokenHandler() ); +} + +void SaxLegacyFastParser::parseStream( const InputSource& structSource ) + throw ( SAXException, + IOException, + RuntimeException, exception) +{ + m_xParser->setFastDocumentHandler( new CallbackDocumentHandler( m_xDocumentHandler.get() ) ); + m_xParser->parseStream( structSource ); +} + +void SaxLegacyFastParser::setDocumentHandler( const Reference< XDocumentHandler > & xHandler ) + throw (RuntimeException, exception) +{ + m_xDocumentHandler = xHandler; +} + +void SaxLegacyFastParser::setErrorHandler( const Reference< XErrorHandler > & xHandler ) + throw (RuntimeException, exception) +{ + m_xParser->setErrorHandler( xHandler ); +} + +void SaxLegacyFastParser::setDTDHandler( const Reference < XDTDHandler > &/* xHandler */ ) + throw (RuntimeException, exception) +{ + +} + +void SaxLegacyFastParser::setEntityResolver( const Reference< XEntityResolver >& xResolver ) + throw (RuntimeException, exception) +{ + m_xParser->setEntityResolver( xResolver ); +} + +void SaxLegacyFastParser::setLocale( const Locale &locale ) + throw (RuntimeException, exception) +{ + m_xParser->setLocale( locale ); +} + +OUString SaxLegacyFastParser::getImplementationName() throw (exception) +{ + return OUString("com.sun.star.comp.extensions.xml.sax.LegacyFastParser"); +} + +sal_Bool SaxLegacyFastParser::supportsService(const OUString& ServiceName) throw (exception) +{ + return cppu::supportsService(this, ServiceName); +} + +Sequence< OUString > SaxLegacyFastParser::getSupportedServiceNames() throw (exception) +{ + Sequence<OUString> seq { "com.sun.star.xml.sax.LegacyFastParser" }; + return seq; +} + +} //namespace + +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL +com_sun_star_comp_extensions_xml_sax_LegacyFastParser_get_implementation( + css::uno::XComponentContext *, + css::uno::Sequence<css::uno::Any> const &) +{ + return cppu::acquire(new SaxLegacyFastParser); +} + + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |