From 532b2f48185c9ee3f389f1a3fbdfffcf113c15c0 Mon Sep 17 00:00:00 2001 From: Kohei Yoshida Date: Mon, 2 Dec 2013 19:20:59 -0500 Subject: Add a means to check if a namespace exists. Useful when we just need to check if the stream has a certain namespace defined. Calling getNamespaceURL() may throw SAXException in such case. Change-Id: Ib2b7b202492390158270d87bab95d1793c9d8a70 --- oox/Library_oox.mk | 1 + oox/source/core/fastparser.cxx | 17 ++++++++++++++++- oox/source/core/fragmenthandler2.cxx | 6 ++++++ oox/source/core/xmlfilterbase.cxx | 5 +++++ 4 files changed, 28 insertions(+), 1 deletion(-) (limited to 'oox') diff --git a/oox/Library_oox.mk b/oox/Library_oox.mk index b7e763c6f575..0a4fc04e0360 100644 --- a/oox/Library_oox.mk +++ b/oox/Library_oox.mk @@ -40,6 +40,7 @@ $(eval $(call gb_Library_use_libraries,oox,\ cppuhelper \ editeng \ drawinglayer \ + fastsax \ msfilter \ sal \ i18nlangtag \ diff --git a/oox/source/core/fastparser.cxx b/oox/source/core/fastparser.cxx index 03fd60a7481e..70e12a896098 100644 --- a/oox/source/core/fastparser.cxx +++ b/oox/source/core/fastparser.cxx @@ -25,6 +25,8 @@ #include "oox/helper/storagebase.hxx" #include "oox/token/namespacemap.hxx" +#include "sax/fastparser.hxx" + namespace oox { namespace core { @@ -66,11 +68,13 @@ InputStreamCloseGuard::~InputStreamCloseGuard() // ============================================================================ FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) : - mrNamespaceMap( StaticNamespaceMap::get() ) + mrNamespaceMap( StaticNamespaceMap::get() ), + mpParser(NULL) { // create a fast parser instance Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW ); mxParser.set( xFactory->createInstanceWithContext( "com.sun.star.xml.sax.FastParser", rxContext ), UNO_QUERY_THROW ); + mpParser = dynamic_cast(mxParser.get()); // create the fast tokenhandler mxTokenHandler.set( new FastTokenHandler ); @@ -131,6 +135,17 @@ OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalAr return mxParser->getNamespaceURL( rPrefix ); } +bool FastParser::hasNamespaceURL( const OUString& rPrefix ) const +{ + if (!mxParser.is()) + throw RuntimeException(); + + if (!mpParser) + return false; + + return mpParser->hasNamespaceURL(rPrefix); +} + sal_Int32 FastParser::getNamespaceId( const OUString& rUrl ) { for( NamespaceMap::const_iterator aIt = mrNamespaceMap.begin(), aEnd = mrNamespaceMap.end(); aIt != aEnd; ++aIt ) diff --git a/oox/source/core/fragmenthandler2.cxx b/oox/source/core/fragmenthandler2.cxx index 668eb7fbee49..8133b7382133 100644 --- a/oox/source/core/fragmenthandler2.cxx +++ b/oox/source/core/fragmenthandler2.cxx @@ -67,6 +67,12 @@ bool FragmentHandler2::prepareMceContext( sal_Int32 nElement, const AttributeLis case MCE_TOKEN( Choice ): { OUString aRequires = rAttribs.getString( ( XML_Requires ), OUString("none") ); + if (!getFilter().hasNamespaceURL(aRequires)) + // Check to see if we have this namespace defined first, + // because calling getNamespaceURL() would throw if the + // namespace doesn't exist. + return false; + aRequires = getFilter().getNamespaceURL( aRequires ); if( getFilter().getNamespaceId( aRequires ) > 0 && !aMceState.empty() && aMceState.back() == MCE_STARTED ) aMceState.back() = MCE_FOUND_CHOICE; diff --git a/oox/source/core/xmlfilterbase.cxx b/oox/source/core/xmlfilterbase.cxx index 30f384898ce9..ea2cc07f86f0 100644 --- a/oox/source/core/xmlfilterbase.cxx +++ b/oox/source/core/xmlfilterbase.cxx @@ -300,6 +300,11 @@ OUString XmlFilterBase::getNamespaceURL( const OUString& rPrefix ) return mxImpl->maFastParser.getNamespaceURL( rPrefix ); } +bool XmlFilterBase::hasNamespaceURL( const OUString& rPrefix ) const +{ + return mxImpl->maFastParser.hasNamespaceURL(rPrefix); +} + sal_Int32 XmlFilterBase::getNamespaceId( const OUString& rUrl ) { return mxImpl->maFastParser.getNamespaceId( rUrl ); -- cgit v1.2.3