From fdd1c64821a81d653ddc911e35149ff969e2f198 Mon Sep 17 00:00:00 2001 From: Mohammed Abdul Azeem Date: Wed, 6 Jul 2016 20:44:39 +0530 Subject: GSOC: Adapt XLegacyFastParser to function like XParser. Made XFastParser to pass namespace prefix instead of URI for Unknown attributes and elements, Namespace handler is provided to resolve those. Test for XFastParser unknown elements is removed, since testing XLegacyFastParser indirectly tests that also. Change-Id: Ia41ff5d3d4c07cef0ca23ba858bfb2a94b91b1f5 Reviewed-on: https://gerrit.libreoffice.org/26982 Tested-by: Jenkins Reviewed-by: Michael Meeks --- sax/source/fastparser/legacyfastparser.cxx | 88 ++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) (limited to 'sax/source/fastparser/legacyfastparser.cxx') diff --git a/sax/source/fastparser/legacyfastparser.cxx b/sax/source/fastparser/legacyfastparser.cxx index 94929a67d344..ab673441f914 100644 --- a/sax/source/fastparser/legacyfastparser.cxx +++ b/sax/source/fastparser/legacyfastparser.cxx @@ -21,11 +21,14 @@ #include #include #include +#include #include #include #include #include #include +#include +#include using namespace std; using namespace ::cppu; @@ -37,11 +40,73 @@ using namespace io; namespace { +class NamespaceHandler : public WeakImplHelper< XFastNamespaceHandler > +{ +private: + struct NamespaceDefine + { + OUString m_aPrefix; + OUString m_aNamespaceURI; + + NamespaceDefine( const OUString& rPrefix, const OUString& rNamespaceURI ) : m_aPrefix( rPrefix ), m_aNamespaceURI( rNamespaceURI ) {} + }; + vector< unique_ptr< NamespaceDefine > > m_aNamespaceDefines; + +public: + NamespaceHandler(); + void addNSDeclAttributes( rtl::Reference < comphelper::AttributeList >& rAttrList ); + + //XFastNamespaceHandler + virtual void SAL_CALL registerNamespace( const OUString& rNamespacePrefix, const OUString& rNamespaceURI ) + throw (RuntimeException, exception) override; + virtual OUString SAL_CALL getNamespaceURI( const OUString& rNamespacePrefix ) + throw (RuntimeException, exception) override; +}; + +NamespaceHandler::NamespaceHandler() +{ +} + +void NamespaceHandler::addNSDeclAttributes( rtl::Reference < comphelper::AttributeList >& rAttrList ) +{ + for(const auto& aNamespaceDefine : m_aNamespaceDefines) + { + OUString& rPrefix = aNamespaceDefine.get()->m_aPrefix; + OUString& rNamespaceURI = aNamespaceDefine.get()->m_aNamespaceURI; + OUString sDecl; + if ( rPrefix.isEmpty() ) + sDecl = "xmlns"; + else + sDecl = "xmlns:" + rPrefix; + rAttrList->AddAttribute( sDecl, "CDATA", rNamespaceURI ); + } + m_aNamespaceDefines.clear(); +} -class SaxLegacyFastParser : public WeakImplHelper< XServiceInfo, XParser > +void NamespaceHandler::registerNamespace( const OUString& rNamespacePrefix, const OUString& rNamespaceURI ) + throw (RuntimeException, exception) { + m_aNamespaceDefines.push_back( o3tl::make_unique( + rNamespacePrefix, rNamespaceURI) ); +} + +OUString NamespaceHandler::getNamespaceURI( const OUString&/* rNamespacePrefix */ ) + throw (RuntimeException, exception) +{ + return OUString(); +} + +class SaxLegacyFastParser : public WeakImplHelper< XInitialization, XServiceInfo, XParser > +{ +private: + rtl::Reference< NamespaceHandler > m_aNamespaceHandler; public: SaxLegacyFastParser(); + +// css::lang::XInitialization: + virtual void SAL_CALL initialize(css::uno::Sequence const& rArguments) + throw (RuntimeException, Exception, exception) override; + // The SAX-Parser-Interface virtual void SAL_CALL parseStream( const InputSource& structSource) throw ( SAXException, IOException, RuntimeException, exception) override; @@ -86,9 +151,9 @@ class CallbackDocumentHandler : public WeakImplHelper< XFastDocumentHandler > { private: Reference< XDocumentHandler > m_xDocumentHandler; + rtl::Reference< NamespaceHandler > m_aNamespaceHandler; public: - CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler ) - { m_xDocumentHandler.set( xDocumentHandler ); } + CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler, rtl::Reference< NamespaceHandler > const & rNamespaceHandler ); // XFastDocumentHandler virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override; @@ -106,6 +171,12 @@ public: }; +CallbackDocumentHandler::CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler, rtl::Reference< NamespaceHandler > const & rNamespaceHandler ) +{ + m_xDocumentHandler.set( xDocumentHandler ); + m_aNamespaceHandler.set( rNamespaceHandler.get() ); +} + void SAL_CALL CallbackDocumentHandler::startDocument() throw (SAXException, RuntimeException, exception) { @@ -139,6 +210,7 @@ void SAL_CALL CallbackDocumentHandler::startUnknownElement( const OUString& Name { OUString elementName; rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList; + m_aNamespaceHandler->addNSDeclAttributes( rAttrList ); if ( !Namespace.isEmpty() ) elementName = Namespace + ":" + Name; else @@ -200,11 +272,17 @@ void SAL_CALL CallbackDocumentHandler::characters( const OUString& aChars ) m_xDocumentHandler->characters( aChars ); } -SaxLegacyFastParser::SaxLegacyFastParser( ) +SaxLegacyFastParser::SaxLegacyFastParser( ) : m_aNamespaceHandler( new NamespaceHandler ) { m_xParser = FastParser::create( ::comphelper::getProcessComponentContext() ); m_xParser->setTokenHandler( new CallbackTokenHandler() ); + m_xParser->setNamespaceHandler( m_aNamespaceHandler.get() ); +} + +void SAL_CALL SaxLegacyFastParser::initialize(Sequence< Any > const&/* rArguments */) + throw (RuntimeException, Exception, exception) +{ } void SaxLegacyFastParser::parseStream( const InputSource& structSource ) @@ -212,7 +290,7 @@ void SaxLegacyFastParser::parseStream( const InputSource& structSource ) IOException, RuntimeException, exception) { - m_xParser->setFastDocumentHandler( new CallbackDocumentHandler( m_xDocumentHandler.get() ) ); + m_xParser->setFastDocumentHandler( new CallbackDocumentHandler( m_xDocumentHandler.get(), m_aNamespaceHandler.get() ) ); m_xParser->parseStream( structSource ); } -- cgit