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

GSOC - Handling namespace declaration missing case:

initialization parameter to FastParser will turn off
the namespace declaration missing exception. Test cases
have also been given to verify the same.

Change-Id: I4c3e02c7ad92d50e279f895ced53c78fc8f49b91
Reviewed-on: https://gerrit.libreoffice.org/27278Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMichael Meeks <michael.meeks@collabora.com>
Tested-by: 's avatarMichael Meeks <michael.meeks@collabora.com>
üst 495cfa27
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
#include <com/sun/star/xml/sax/XFastParser.hpp> #include <com/sun/star/xml/sax/XFastParser.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/lang/XServiceInfo.hpp>
#include <cppuhelper/implbase2.hxx> #include <cppuhelper/weak.hxx>
#include <cppuhelper/implbase.hxx>
#include <com/sun/star/lang/XInitialization.hpp>
#include <sax/fastsaxdllapi.h> #include <sax/fastsaxdllapi.h>
#include <memory> #include <memory>
...@@ -42,7 +44,8 @@ class FastSaxParserImpl; ...@@ -42,7 +44,8 @@ class FastSaxParserImpl;
// This class implements the external Parser interface // This class implements the external Parser interface
class FASTSAX_DLLPUBLIC FastSaxParser class FASTSAX_DLLPUBLIC FastSaxParser
: public ::cppu::WeakImplHelper2< : public ::cppu::WeakImplHelper<
css::lang::XInitialization,
css::xml::sax::XFastParser, css::xml::sax::XFastParser,
css::lang::XServiceInfo > css::lang::XServiceInfo >
{ {
...@@ -52,6 +55,9 @@ public: ...@@ -52,6 +55,9 @@ public:
FastSaxParser(); FastSaxParser();
virtual ~FastSaxParser(); virtual ~FastSaxParser();
// css::lang::XInitialization:
virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const& rArguments) throw (css::uno::RuntimeException, css::uno::Exception, std::exception) override;
// XFastParser // XFastParser
virtual void SAL_CALL parseStream( const css::xml::sax::InputSource& aInputSource ) throw (css::xml::sax::SAXException, css::io::IOException, css::uno::RuntimeException, std::exception) override; virtual void SAL_CALL parseStream( const css::xml::sax::InputSource& aInputSource ) throw (css::xml::sax::SAXException, css::io::IOException, css::uno::RuntimeException, std::exception) override;
virtual void SAL_CALL setFastDocumentHandler( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& Handler ) throw (css::uno::RuntimeException, std::exception) override; virtual void SAL_CALL setFastDocumentHandler( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& Handler ) throw (css::uno::RuntimeException, std::exception) override;
......
...@@ -278,10 +278,12 @@ public: ...@@ -278,10 +278,12 @@ public:
XMLImportTest() : BootstrapFixture(true, false) {} XMLImportTest() : BootstrapFixture(true, false) {}
void parse(); void parse();
void testMissingNamespaceDeclaration();
void testIllegalNamespaceUse(); void testIllegalNamespaceUse();
CPPUNIT_TEST_SUITE( XMLImportTest ); CPPUNIT_TEST_SUITE( XMLImportTest );
CPPUNIT_TEST( parse ); CPPUNIT_TEST( parse );
CPPUNIT_TEST( testMissingNamespaceDeclaration );
CPPUNIT_TEST( testIllegalNamespaceUse ); CPPUNIT_TEST( testIllegalNamespaceUse );
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
}; };
...@@ -317,11 +319,11 @@ void XMLImportTest::parse() ...@@ -317,11 +319,11 @@ void XMLImportTest::parse()
source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] ); source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] );
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] ); source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] );
m_xLegacyFastParser->parseStream(source); m_xLegacyFastParser->parseStream(source);
const OUString& rLegacyFastParserStr = m_xDocumentHandler->getString(); const OUString rLegacyFastParserStr = m_xDocumentHandler->getString();
CPPUNIT_ASSERT_EQUAL( rParserStr, rLegacyFastParserStr ); CPPUNIT_ASSERT_EQUAL( rParserStr, rLegacyFastParserStr );
// OString o = OUStringToOString( Str, RTL_TEXTENCODING_ASCII_US ); // OString o = OUStringToOString( Str, RTL_TEXTENCODING_ASCII_US );
...@@ -329,6 +331,33 @@ void XMLImportTest::parse() ...@@ -329,6 +331,33 @@ void XMLImportTest::parse()
} }
} }
void XMLImportTest::testMissingNamespaceDeclaration()
{
OUString fileNames[] = { "manifestwithnsdecl.xml", "manifestwithoutnsdecl.xml" };
uno::Reference<lang::XInitialization> const xInit(m_xLegacyFastParser,
uno::UNO_QUERY_THROW);
uno::Sequence<uno::Any> args(1);
args[0] <<= OUString("IgnoreMissingNSDecl");
xInit->initialize( args );
for (sal_uInt16 i = 0; i < sizeof( fileNames ) / sizeof( OUString ); i++)
{
InputSource source;
source.sSystemId = "internal";
source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] );
m_xParser->parseStream(source);
const OUString rParserStr = m_xDocumentHandler->getString();
source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] );
m_xLegacyFastParser->parseStream(source);
const OUString rLegacyFastParserStr = m_xDocumentHandler->getString();
CPPUNIT_ASSERT_EQUAL( rParserStr, rLegacyFastParserStr );
}
}
void XMLImportTest::testIllegalNamespaceUse() void XMLImportTest::testIllegalNamespaceUse()
{ {
rtl::Reference< NSDocumentHandler > m_xNSDocumentHandler; rtl::Reference< NSDocumentHandler > m_xNSDocumentHandler;
......
<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.text"/>
<manifest:file-entry manifest:full-path="Thumbnails/thumbnail.png" manifest:media-type="image/png"/>
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="Configurations2/accelerator/current.xml" manifest:media-type=""/>
<manifest:file-entry manifest:full-path="Configurations2/" manifest:media-type="application/vnd.sun.xml.ui.configuration"/>
<manifest:file-entry manifest:full-path="manifest.rdf" manifest:media-type="application/rdf+xml"/>
</manifest:manifest>
<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest>
<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.text"/>
<manifest:file-entry manifest:full-path="Thumbnails/thumbnail.png" manifest:media-type="image/png"/>
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="Configurations2/accelerator/current.xml" manifest:media-type=""/>
<manifest:file-entry manifest:full-path="Configurations2/" manifest:media-type="application/vnd.sun.xml.ui.configuration"/>
<manifest:file-entry manifest:full-path="manifest.rdf" manifest:media-type="application/rdf+xml"/>
</manifest:manifest>
...@@ -233,6 +233,7 @@ public: ...@@ -233,6 +233,7 @@ public:
Entity& getEntity() { return *mpTop; } Entity& getEntity() { return *mpTop; }
void parse(); void parse();
void produce( bool bForceFlush = false ); void produce( bool bForceFlush = false );
bool m_bIgnoreMissingNSDecl;
private: private:
bool consume(EventList *); bool consume(EventList *);
...@@ -619,6 +620,7 @@ FastSaxParserImpl::FastSaxParserImpl( FastSaxParser* ) : ...@@ -619,6 +620,7 @@ FastSaxParserImpl::FastSaxParserImpl( FastSaxParser* ) :
#if 0 #if 0
mpFront(pFront), mpFront(pFront),
#endif #endif
m_bIgnoreMissingNSDecl(false),
mpTop(nullptr) mpTop(nullptr)
{ {
mxDocumentLocator.set( new FastLocatorImpl( this ) ); mxDocumentLocator.set( new FastLocatorImpl( this ) );
...@@ -668,7 +670,7 @@ sal_Int32 FastSaxParserImpl::GetTokenWithPrefix( const xmlChar* pPrefix, int nPr ...@@ -668,7 +670,7 @@ sal_Int32 FastSaxParserImpl::GetTokenWithPrefix( const xmlChar* pPrefix, int nPr
break; break;
} }
if( !nNamespace ) if( !nNamespace && !m_bIgnoreMissingNSDecl )
throw SAXException("No namespace defined for " + OUString(XML_CAST(pPrefix), throw SAXException("No namespace defined for " + OUString(XML_CAST(pPrefix),
nPrefixLen, RTL_TEXTENCODING_UTF8), Reference< XInterface >(), Any()); nPrefixLen, RTL_TEXTENCODING_UTF8), Reference< XInterface >(), Any());
} }
...@@ -1129,7 +1131,8 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm ...@@ -1129,7 +1131,8 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm
{ {
if( prefix != nullptr ) if( prefix != nullptr )
{ {
sNamespace = OUString( XML_CAST( URI ), strlen( XML_CAST( URI )), RTL_TEXTENCODING_UTF8 ); if ( !m_bIgnoreMissingNSDecl || URI != nullptr )
sNamespace = OUString( XML_CAST( URI ), strlen( XML_CAST( URI )), RTL_TEXTENCODING_UTF8 );
nNamespaceToken = GetNamespaceToken( sNamespace ); nNamespaceToken = GetNamespaceToken( sNamespace );
rEvent.msNamespace = OUString( XML_CAST( prefix ), strlen( XML_CAST( prefix )), RTL_TEXTENCODING_UTF8 ); rEvent.msNamespace = OUString( XML_CAST( prefix ), strlen( XML_CAST( prefix )), RTL_TEXTENCODING_UTF8 );
} }
...@@ -1300,6 +1303,24 @@ FastSaxParser::~FastSaxParser() ...@@ -1300,6 +1303,24 @@ FastSaxParser::~FastSaxParser()
{ {
} }
void SAL_CALL
FastSaxParser::initialize(css::uno::Sequence< css::uno::Any > const& rArguments)
throw (css::uno::RuntimeException, css::uno::Exception, std::exception)
{
if (rArguments.getLength())
{
OUString str;
if ( ( rArguments[0] >>= str ) && "IgnoreMissingNSDecl" == str )
mpImpl->m_bIgnoreMissingNSDecl = true;
else if ( str == "DoSmeplease" )
{
//just ignore as this is already immune to billon laughs
}
else
throw IllegalArgumentException();
}
}
void FastSaxParser::parseStream( const xml::sax::InputSource& aInputSource ) void FastSaxParser::parseStream( const xml::sax::InputSource& aInputSource )
throw (xml::sax::SAXException, io::IOException, throw (xml::sax::SAXException, io::IOException,
uno::RuntimeException, std::exception) uno::RuntimeException, std::exception)
......
...@@ -280,9 +280,12 @@ SaxLegacyFastParser::SaxLegacyFastParser( ) : m_aNamespaceHandler( new Namespace ...@@ -280,9 +280,12 @@ SaxLegacyFastParser::SaxLegacyFastParser( ) : m_aNamespaceHandler( new Namespace
m_xParser->setNamespaceHandler( m_aNamespaceHandler.get() ); m_xParser->setNamespaceHandler( m_aNamespaceHandler.get() );
} }
void SAL_CALL SaxLegacyFastParser::initialize(Sequence< Any > const&/* rArguments */) void SAL_CALL SaxLegacyFastParser::initialize(Sequence< Any > const& rArguments )
throw (RuntimeException, Exception, exception) throw (RuntimeException, Exception, exception)
{ {
uno::Reference<lang::XInitialization> const xInit(m_xParser,
uno::UNO_QUERY_THROW);
xInit->initialize( rArguments );
} }
void SaxLegacyFastParser::parseStream( const InputSource& structSource ) void SaxLegacyFastParser::parseStream( const InputSource& 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