Kaydet (Commit) fdd1c648 authored tarafından Mohammed Abdul Azeem's avatar Mohammed Abdul Azeem Kaydeden (comit) Michael Meeks

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/26982Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMichael Meeks <michael.meeks@collabora.com>
üst 563dd59c
...@@ -61,6 +61,7 @@ public: ...@@ -61,6 +61,7 @@ public:
virtual void SAL_CALL setErrorHandler( const css::uno::Reference< css::xml::sax::XErrorHandler >& Handler ) throw (css::uno::RuntimeException, std::exception) override; virtual void SAL_CALL setErrorHandler( const css::uno::Reference< css::xml::sax::XErrorHandler >& Handler ) throw (css::uno::RuntimeException, std::exception) override;
virtual void SAL_CALL setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) throw (css::uno::RuntimeException, std::exception) override; virtual void SAL_CALL setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) throw (css::uno::RuntimeException, std::exception) override;
virtual void SAL_CALL setLocale( const css::lang::Locale& rLocale ) throw (css::uno::RuntimeException, std::exception) override; virtual void SAL_CALL setLocale( const css::lang::Locale& rLocale ) throw (css::uno::RuntimeException, std::exception) override;
virtual void SAL_CALL setNamespaceHandler( const css::uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler) throw (css::uno::RuntimeException, std::exception) override;
// XServiceInfo // XServiceInfo
virtual OUString SAL_CALL getImplementationName( ) throw (css::uno::RuntimeException, std::exception) override; virtual OUString SAL_CALL getImplementationName( ) throw (css::uno::RuntimeException, std::exception) override;
......
...@@ -4330,6 +4330,7 @@ $(eval $(call gb_UnoApi_add_idlfiles,offapi,com/sun/star/xml/sax,\ ...@@ -4330,6 +4330,7 @@ $(eval $(call gb_UnoApi_add_idlfiles,offapi,com/sun/star/xml/sax,\
XEntityResolver \ XEntityResolver \
XErrorHandler \ XErrorHandler \
XExtendedDocumentHandler \ XExtendedDocumentHandler \
XFastNamespaceHandler \
XFastAttributeList \ XFastAttributeList \
XFastContextHandler \ XFastContextHandler \
XFastDocumentHandler \ XFastDocumentHandler \
......
/* -*- 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 .
*/
#ifndef __com_sun_star_xml_sax_XFastNamespaceHandler_idl__
#define __com_sun_star_xml_sax_XFastNamespaceHandler_idl__
#include <com/sun/star/uno/XInterface.idl>
module com { module sun { module star { module xml { module sax {
interface XFastNamespaceHandler: com::sun::star::uno::XInterface
{
void registerNamespace( [in] string NamespacePrefix, [in] string NamespaceURI );
string getNamespaceURI( [in] string NamespacePrefix );
};
}; }; }; }; };
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
#include <com/sun/star/lang/IllegalArgumentException.idl> #include <com/sun/star/lang/IllegalArgumentException.idl>
#include <com/sum/star/xml/sax/XFastNamespaceHandler.idl>
module com { module sun { module star { module xml { module sax { module com { module sun { module star { module xml { module sax {
...@@ -152,6 +154,8 @@ interface XFastParser: com::sun::star::uno::XInterface ...@@ -152,6 +154,8 @@ interface XFastParser: com::sun::star::uno::XInterface
*/ */
string getNamespaceURL( [in] string prefix ) string getNamespaceURL( [in] string prefix )
raises( com::sun::star::lang::IllegalArgumentException ); raises( com::sun::star::lang::IllegalArgumentException );
void setNamespaceHandler( [in] XFastNamespaceHandler Handler);
}; };
......
...@@ -204,149 +204,6 @@ void SAL_CALL TestDocumentHandler::setDocumentLocator( const Reference< XLocator ...@@ -204,149 +204,6 @@ void SAL_CALL TestDocumentHandler::setDocumentLocator( const Reference< XLocator
{ {
} }
class TestFastDocumentHandler : public cppu::WeakImplHelper< XFastDocumentHandler >
{
private:
OUString m_aStr;
public:
const OUString& getString() { return m_aStr; }
// 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 TestFastDocumentHandler::startDocument()
throw (SAXException, RuntimeException, exception)
{
m_aStr.clear();
}
void SAL_CALL TestFastDocumentHandler::endDocument()
throw (SAXException, RuntimeException, exception)
{
}
void SAL_CALL TestFastDocumentHandler::setDocumentLocator( const Reference< XLocator >&/* xLocator */ )
throw (SAXException, RuntimeException, exception)
{
}
void SAL_CALL TestFastDocumentHandler::startFastElement( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ )
throw (SAXException, RuntimeException, exception)
{
}
void SAL_CALL TestFastDocumentHandler::startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs )
throw (SAXException, RuntimeException, exception)
{
if ( !Namespace.isEmpty() )
m_aStr = m_aStr + Namespace + ":" + Name;
else
m_aStr = m_aStr + 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& rAttrName = unknownAttribs[i].Name;
OUString& rAttrNamespaceURL = unknownAttribs[i].NamespaceURL;
if ( !rAttrNamespaceURL.isEmpty() )
m_aStr = m_aStr + rAttrNamespaceURL + ":" + rAttrName + rAttrValue;
else
m_aStr = m_aStr + rAttrName + rAttrValue;
}
}
void SAL_CALL TestFastDocumentHandler::endFastElement( sal_Int32/* nElement */)
throw (SAXException, RuntimeException, exception)
{
}
void SAL_CALL TestFastDocumentHandler::endUnknownElement( const OUString& Namespace, const OUString& Name )
throw (SAXException, RuntimeException, exception)
{
if ( !Namespace.isEmpty() )
m_aStr = m_aStr + Namespace + ":" + Name;
else
m_aStr = m_aStr + Name;
}
Reference< XFastContextHandler > SAL_CALL TestFastDocumentHandler::createFastChildContext( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ )
throw (SAXException, RuntimeException, exception)
{
return this;
}
Reference< XFastContextHandler > SAL_CALL TestFastDocumentHandler::createUnknownChildContext( const OUString&/* Namespace */, const OUString&/* Name */, const Reference< XFastAttributeList >&/* Attribs */ )
throw (SAXException, RuntimeException, exception)
{
return this;
}
void SAL_CALL TestFastDocumentHandler::characters( const OUString& aChars )
throw (SAXException, RuntimeException, exception)
{
m_aStr = m_aStr + aChars;
}
class TestTokenHandler : 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
{
CPPUNIT_ASSERT_MESSAGE( "getUTF8Identifier: unexpected call", false );
return Sequence<sal_Int8>();
}
};
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 NSDocumentHandler : public cppu::WeakImplHelper< XDocumentHandler > class NSDocumentHandler : public cppu::WeakImplHelper< XDocumentHandler >
{ {
public: public:
...@@ -411,12 +268,8 @@ class XMLImportTest : public test::BootstrapFixture ...@@ -411,12 +268,8 @@ class XMLImportTest : public test::BootstrapFixture
private: private:
OUString m_sDirPath; OUString m_sDirPath;
rtl::Reference< TestDocumentHandler > m_xDocumentHandler; rtl::Reference< TestDocumentHandler > m_xDocumentHandler;
rtl::Reference< TestFastDocumentHandler > m_xFastDocumentHandler;
Reference< XParser > m_xParser; Reference< XParser > m_xParser;
Reference< XFastParser > m_xFastParser;
Reference< XParser > m_xLegacyFastParser; Reference< XParser > m_xLegacyFastParser;
rtl::Reference< TestLegacyDocumentHandler > m_xLegacyDocumentHandler;
Reference< XFastTokenHandler > m_xFastTokenHandler;
public: public:
virtual void setUp() override; virtual void setUp() override;
...@@ -437,17 +290,11 @@ void XMLImportTest::setUp() ...@@ -437,17 +290,11 @@ void XMLImportTest::setUp()
test::BootstrapFixture::setUp(); test::BootstrapFixture::setUp();
Reference< XComponentContext > xContext = comphelper::getProcessComponentContext(); Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
m_xDocumentHandler.set( new TestDocumentHandler() ); m_xDocumentHandler.set( new TestDocumentHandler() );
m_xFastDocumentHandler.set( new TestFastDocumentHandler() );
m_xLegacyDocumentHandler.set( new TestLegacyDocumentHandler() );
m_xFastTokenHandler.set( new TestTokenHandler() );
m_xParser = Parser::create( xContext ); m_xParser = Parser::create( xContext );
m_xFastParser = FastParser::create( xContext );
m_xParser->setDocumentHandler( m_xDocumentHandler.get() ); m_xParser->setDocumentHandler( m_xDocumentHandler.get() );
m_xFastParser->setFastDocumentHandler( m_xFastDocumentHandler.get() );
m_xFastParser->setTokenHandler( m_xFastTokenHandler );
m_xLegacyFastParser.set( xContext->getServiceManager()->createInstanceWithContext m_xLegacyFastParser.set( xContext->getServiceManager()->createInstanceWithContext
( "com.sun.star.xml.sax.LegacyFastParser", xContext ), UNO_QUERY ); ( "com.sun.star.xml.sax.LegacyFastParser", xContext ), UNO_QUERY );
m_xLegacyFastParser->setDocumentHandler( m_xLegacyDocumentHandler.get() ); m_xLegacyFastParser->setDocumentHandler( m_xDocumentHandler.get() );
m_sDirPath = m_directories.getPathFromSrc( "/sax/qa/data/" ); m_sDirPath = m_directories.getPathFromSrc( "/sax/qa/data/" );
} }
...@@ -462,7 +309,7 @@ void XMLImportTest::parse() ...@@ -462,7 +309,7 @@ void XMLImportTest::parse()
"multiplens.xml", "multiplepfx.xml", "multiplens.xml", "multiplepfx.xml",
"nstoattributes.xml", "nestedns.xml"}; "nstoattributes.xml", "nestedns.xml"};
for (sal_uInt16 i = 0; i < sizeof( fileNames ) / sizeof( string ); i++) for (sal_uInt16 i = 0; i < sizeof( fileNames ) / sizeof( OUString ); i++)
{ {
InputSource source; InputSource source;
source.sSystemId = "internal"; source.sSystemId = "internal";
...@@ -471,16 +318,11 @@ void XMLImportTest::parse() ...@@ -471,16 +318,11 @@ void XMLImportTest::parse()
m_xParser->parseStream(source); m_xParser->parseStream(source);
const OUString& rParserStr = m_xDocumentHandler->getString(); const OUString& rParserStr = m_xDocumentHandler->getString();
source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] );
m_xFastParser->parseStream(source);
const OUString& rFastParserStr = m_xFastDocumentHandler->getString();
source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] ); source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] );
m_xLegacyFastParser->parseStream(source); m_xLegacyFastParser->parseStream(source);
const OUString& rLegacyFastParserStr = m_xLegacyDocumentHandler->getString(); const OUString& rLegacyFastParserStr = m_xDocumentHandler->getString();
CPPUNIT_ASSERT_EQUAL( rParserStr, rFastParserStr ); CPPUNIT_ASSERT_EQUAL( rParserStr, rLegacyFastParserStr );
CPPUNIT_ASSERT_EQUAL( rFastParserStr, rLegacyFastParserStr );
// OString o = OUStringToOString( Str, RTL_TEXTENCODING_ASCII_US ); // OString o = OUStringToOString( Str, RTL_TEXTENCODING_ASCII_US );
// CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false ); // CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false );
} }
...@@ -496,6 +338,10 @@ void XMLImportTest::testIllegalNamespaceUse() ...@@ -496,6 +338,10 @@ void XMLImportTest::testIllegalNamespaceUse()
source.aInputStream = createStreamFromFile( m_sDirPath + "multiplepfx.xml" ); source.aInputStream = createStreamFromFile( m_sDirPath + "multiplepfx.xml" );
m_xParser->parseStream(source); m_xParser->parseStream(source);
m_xLegacyFastParser->setDocumentHandler( m_xNSDocumentHandler.get() );
source.aInputStream = createStreamFromFile( m_sDirPath + "multiplepfx.xml" );
m_xLegacyFastParser->parseStream(source);
} }
CPPUNIT_TEST_SUITE_REGISTRATION( XMLImportTest ); CPPUNIT_TEST_SUITE_REGISTRATION( XMLImportTest );
......
...@@ -119,6 +119,7 @@ struct ParserData ...@@ -119,6 +119,7 @@ struct ParserData
FastTokenHandlerBase* mpTokenHandler; FastTokenHandlerBase* mpTokenHandler;
css::uno::Reference< css::xml::sax::XErrorHandler > mxErrorHandler; css::uno::Reference< css::xml::sax::XErrorHandler > mxErrorHandler;
css::uno::Reference< css::xml::sax::XEntityResolver > mxEntityResolver; css::uno::Reference< css::xml::sax::XEntityResolver > mxEntityResolver;
css::uno::Reference< css::xml::sax::XFastNamespaceHandler >mxNamespaceHandler;
css::lang::Locale maLocale; css::lang::Locale maLocale;
ParserData(); ParserData();
...@@ -210,6 +211,7 @@ public: ...@@ -210,6 +211,7 @@ public:
OUString getNamespaceURL( const OUString& rPrefix ) throw(css::lang::IllegalArgumentException, css::uno::RuntimeException); OUString getNamespaceURL( const OUString& rPrefix ) throw(css::lang::IllegalArgumentException, css::uno::RuntimeException);
void setErrorHandler( const css::uno::Reference< css::xml::sax::XErrorHandler >& Handler ) throw (css::uno::RuntimeException); void setErrorHandler( const css::uno::Reference< css::xml::sax::XErrorHandler >& Handler ) throw (css::uno::RuntimeException);
void setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) throw (css::uno::RuntimeException); void setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) throw (css::uno::RuntimeException);
void setNamespaceHandler( const css::uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler) throw (css::uno::RuntimeException);
void setLocale( const css::lang::Locale& rLocale ) throw (css::uno::RuntimeException); void setLocale( const css::lang::Locale& rLocale ) throw (css::uno::RuntimeException);
// called by the C callbacks of the expat parser // called by the C callbacks of the expat parser
...@@ -863,6 +865,11 @@ void FastSaxParserImpl::setLocale( const lang::Locale & Locale ) throw (RuntimeE ...@@ -863,6 +865,11 @@ void FastSaxParserImpl::setLocale( const lang::Locale & Locale ) throw (RuntimeE
maData.maLocale = Locale; maData.maLocale = Locale;
} }
void FastSaxParserImpl::setNamespaceHandler( const Reference< XFastNamespaceHandler >& Handler ) throw (RuntimeException)
{
maData.mxNamespaceHandler = Handler;
}
void FastSaxParserImpl::deleteUsedEvents() void FastSaxParserImpl::deleteUsedEvents()
{ {
Entity& rEntity = getEntity(); Entity& rEntity = getEntity();
...@@ -1033,10 +1040,11 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm ...@@ -1033,10 +1040,11 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm
new FastAttributeList( rEntity.mxTokenHandler, new FastAttributeList( rEntity.mxTokenHandler,
rEntity.mpTokenHandler ) ); rEntity.mpTokenHandler ) );
OUString sNamespace;
sal_Int32 nNamespaceToken = FastToken::DONTKNOW; sal_Int32 nNamespaceToken = FastToken::DONTKNOW;
if (!rEntity.maNamespaceStack.empty()) if (!rEntity.maNamespaceStack.empty())
{ {
rEvent.msNamespace = rEntity.maNamespaceStack.top().msName; sNamespace = rEntity.maNamespaceStack.top().msName;
nNamespaceToken = rEntity.maNamespaceStack.top().mnToken; nNamespaceToken = rEntity.maNamespaceStack.top().mnToken;
} }
...@@ -1054,12 +1062,17 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm ...@@ -1054,12 +1062,17 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm
{ {
DefineNamespace( OString( XML_CAST( namespaces[ i ] )), DefineNamespace( OString( XML_CAST( namespaces[ i ] )),
OUString( XML_CAST( namespaces[ i + 1 ] ), strlen( XML_CAST( namespaces[ i + 1 ] )), RTL_TEXTENCODING_UTF8 )); OUString( XML_CAST( namespaces[ i + 1 ] ), strlen( XML_CAST( namespaces[ i + 1 ] )), RTL_TEXTENCODING_UTF8 ));
if( rEntity.mxNamespaceHandler.is() )
rEntity.mxNamespaceHandler->registerNamespace( OUString( XML_CAST( namespaces[ i ] ),strlen( XML_CAST( namespaces[ i ] )), RTL_TEXTENCODING_UTF8 ),
OUString( XML_CAST( namespaces[ i + 1 ] ), strlen( XML_CAST( namespaces[ i + 1 ] )), RTL_TEXTENCODING_UTF8 ));
} }
else else
{ {
// default namespace // default namespace
rEvent.msNamespace = OUString( XML_CAST( namespaces[ i + 1 ] ), strlen( XML_CAST( namespaces[ i + 1 ] )), RTL_TEXTENCODING_UTF8 ); sNamespace = OUString( XML_CAST( namespaces[ i + 1 ] ), strlen( XML_CAST( namespaces[ i + 1 ] )), RTL_TEXTENCODING_UTF8 );
nNamespaceToken = GetNamespaceToken( rEvent.msNamespace ); nNamespaceToken = GetNamespaceToken( sNamespace );
if( rEntity.mxNamespaceHandler.is() )
rEntity.mxNamespaceHandler->registerNamespace("", OUString( XML_CAST( namespaces[ i + 1 ] ), strlen( XML_CAST( namespaces[ i + 1 ] )), RTL_TEXTENCODING_UTF8 ) );
} }
} }
...@@ -1072,7 +1085,7 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm ...@@ -1072,7 +1085,7 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm
if( nAttributeToken != FastToken::DONTKNOW ) if( nAttributeToken != FastToken::DONTKNOW )
rEvent.mxAttributes->add( nAttributeToken, XML_CAST( attributes[ i + 3 ] ), attributes[ i + 4 ] - attributes[ i + 3 ] ); rEvent.mxAttributes->add( nAttributeToken, XML_CAST( attributes[ i + 3 ] ), attributes[ i + 4 ] - attributes[ i + 3 ] );
else else
rEvent.mxAttributes->addUnknown( OUString( XML_CAST( attributes[ i + 2 ] ), strlen( XML_CAST( attributes[ i + 2 ] )), RTL_TEXTENCODING_UTF8 ), rEvent.mxAttributes->addUnknown( OUString( XML_CAST( attributes[ i + 1 ] ), strlen( XML_CAST( attributes[ i + 1 ] )), RTL_TEXTENCODING_UTF8 ),
OString( XML_CAST( attributes[ i ] )), OString( XML_CAST( attributes[ i + 3 ] ), attributes[ i + 4 ] - attributes[ i + 3 ] )); OString( XML_CAST( attributes[ i ] )), OString( XML_CAST( attributes[ i + 3 ] ), attributes[ i + 4 ] - attributes[ i + 3 ] ));
} }
else else
...@@ -1088,7 +1101,7 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm ...@@ -1088,7 +1101,7 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm
if( prefix != nullptr ) if( prefix != nullptr )
rEvent.mnElementToken = GetTokenWithPrefix( prefix, strlen( XML_CAST( prefix )), localName, strlen( XML_CAST( localName ))); rEvent.mnElementToken = GetTokenWithPrefix( prefix, strlen( XML_CAST( prefix )), localName, strlen( XML_CAST( localName )));
else if( !rEvent.msNamespace.isEmpty() ) else if( !sNamespace.isEmpty() )
rEvent.mnElementToken = GetTokenWithContextNamespace( nNamespaceToken, localName, strlen( XML_CAST( localName ))); rEvent.mnElementToken = GetTokenWithContextNamespace( nNamespaceToken, localName, strlen( XML_CAST( localName )));
else else
rEvent.mnElementToken = GetToken( localName, strlen( XML_CAST( localName ))); rEvent.mnElementToken = GetToken( localName, strlen( XML_CAST( localName )));
...@@ -1097,15 +1110,18 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm ...@@ -1097,15 +1110,18 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm
{ {
if( prefix != nullptr ) if( prefix != nullptr )
{ {
rEvent.msNamespace = OUString( XML_CAST( URI ), strlen( XML_CAST( URI )), RTL_TEXTENCODING_UTF8 ); sNamespace = OUString( XML_CAST( URI ), strlen( XML_CAST( URI )), RTL_TEXTENCODING_UTF8 );
nNamespaceToken = GetNamespaceToken( rEvent.msNamespace ); nNamespaceToken = GetNamespaceToken( sNamespace );
rEvent.msNamespace = OUString( XML_CAST( prefix ), strlen( XML_CAST( prefix )), RTL_TEXTENCODING_UTF8 );
} }
else
rEvent.msNamespace.clear();
rEvent.msElementName = OUString( XML_CAST( localName ), strlen( XML_CAST( localName )), RTL_TEXTENCODING_UTF8 ); rEvent.msElementName = OUString( XML_CAST( localName ), strlen( XML_CAST( localName )), RTL_TEXTENCODING_UTF8 );
} }
else // token is always preferred. else // token is always preferred.
rEvent.msElementName.clear(); rEvent.msElementName.clear();
rEntity.maNamespaceStack.push( NameWithToken(rEvent.msNamespace, nNamespaceToken) ); rEntity.maNamespaceStack.push( NameWithToken(sNamespace, nNamespaceToken) );
if (rEntity.mbEnableThreads) if (rEntity.mbEnableThreads)
produce(); produce();
else else
...@@ -1314,6 +1330,12 @@ void FastSaxParser::setLocale( const lang::Locale& rLocale ) ...@@ -1314,6 +1330,12 @@ void FastSaxParser::setLocale( const lang::Locale& rLocale )
mpImpl->setLocale(rLocale); mpImpl->setLocale(rLocale);
} }
void FastSaxParser::setNamespaceHandler( const uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler)
throw (uno::RuntimeException, std::exception)
{
mpImpl->setNamespaceHandler(Handler);
}
OUString FastSaxParser::getImplementationName() OUString FastSaxParser::getImplementationName()
throw (uno::RuntimeException, std::exception) throw (uno::RuntimeException, std::exception)
{ {
......
...@@ -21,11 +21,14 @@ ...@@ -21,11 +21,14 @@
#include <com/sun/star/xml/sax/XParser.hpp> #include <com/sun/star/xml/sax/XParser.hpp>
#include <com/sun/star/xml/sax/FastParser.hpp> #include <com/sun/star/xml/sax/FastParser.hpp>
#include <com/sun/star/xml/sax/FastToken.hpp> #include <com/sun/star/xml/sax/FastToken.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <comphelper/attributelist.hxx> #include <comphelper/attributelist.hxx>
#include <cppuhelper/supportsservice.hxx> #include <cppuhelper/supportsservice.hxx>
#include <comphelper/processfactory.hxx> #include <comphelper/processfactory.hxx>
#include <rtl/ref.hxx> #include <rtl/ref.hxx>
#include <sax/fastparser.hxx> #include <sax/fastparser.hxx>
#include <vector>
#include <o3tl/make_unique.hxx>
using namespace std; using namespace std;
using namespace ::cppu; using namespace ::cppu;
...@@ -37,11 +40,73 @@ using namespace io; ...@@ -37,11 +40,73 @@ using namespace io;
namespace { 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<NamespaceDefine>(
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: public:
SaxLegacyFastParser(); SaxLegacyFastParser();
// css::lang::XInitialization:
virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const& rArguments)
throw (RuntimeException, Exception, exception) override;
// The SAX-Parser-Interface // The SAX-Parser-Interface
virtual void SAL_CALL parseStream( const InputSource& structSource) virtual void SAL_CALL parseStream( const InputSource& structSource)
throw ( SAXException, IOException, RuntimeException, exception) override; throw ( SAXException, IOException, RuntimeException, exception) override;
...@@ -86,9 +151,9 @@ class CallbackDocumentHandler : public WeakImplHelper< XFastDocumentHandler > ...@@ -86,9 +151,9 @@ class CallbackDocumentHandler : public WeakImplHelper< XFastDocumentHandler >
{ {
private: private:
Reference< XDocumentHandler > m_xDocumentHandler; Reference< XDocumentHandler > m_xDocumentHandler;
rtl::Reference< NamespaceHandler > m_aNamespaceHandler;
public: public:
CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler ) CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler, rtl::Reference< NamespaceHandler > const & rNamespaceHandler );
{ m_xDocumentHandler.set( xDocumentHandler ); }
// XFastDocumentHandler // XFastDocumentHandler
virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override; virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override;
...@@ -106,6 +171,12 @@ public: ...@@ -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() void SAL_CALL CallbackDocumentHandler::startDocument()
throw (SAXException, RuntimeException, exception) throw (SAXException, RuntimeException, exception)
{ {
...@@ -139,6 +210,7 @@ void SAL_CALL CallbackDocumentHandler::startUnknownElement( const OUString& Name ...@@ -139,6 +210,7 @@ void SAL_CALL CallbackDocumentHandler::startUnknownElement( const OUString& Name
{ {
OUString elementName; OUString elementName;
rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList; rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList;
m_aNamespaceHandler->addNSDeclAttributes( rAttrList );
if ( !Namespace.isEmpty() ) if ( !Namespace.isEmpty() )
elementName = Namespace + ":" + Name; elementName = Namespace + ":" + Name;
else else
...@@ -200,11 +272,17 @@ void SAL_CALL CallbackDocumentHandler::characters( const OUString& aChars ) ...@@ -200,11 +272,17 @@ void SAL_CALL CallbackDocumentHandler::characters( const OUString& aChars )
m_xDocumentHandler->characters( aChars ); m_xDocumentHandler->characters( aChars );
} }
SaxLegacyFastParser::SaxLegacyFastParser( ) SaxLegacyFastParser::SaxLegacyFastParser( ) : m_aNamespaceHandler( new NamespaceHandler )
{ {
m_xParser = FastParser::create( m_xParser = FastParser::create(
::comphelper::getProcessComponentContext() ); ::comphelper::getProcessComponentContext() );
m_xParser->setTokenHandler( new CallbackTokenHandler() ); 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 ) void SaxLegacyFastParser::parseStream( const InputSource& structSource )
...@@ -212,7 +290,7 @@ void SaxLegacyFastParser::parseStream( const InputSource& structSource ) ...@@ -212,7 +290,7 @@ void SaxLegacyFastParser::parseStream( const InputSource& structSource )
IOException, IOException,
RuntimeException, exception) 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 ); m_xParser->parseStream( structSource );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment