1*ca5ec200SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ca5ec200SAndrew Rist * distributed with this work for additional information 6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance 9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at 10*ca5ec200SAndrew Rist * 11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ca5ec200SAndrew Rist * 13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an 15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the 17*ca5ec200SAndrew Rist * specific language governing permissions and limitations 18*ca5ec200SAndrew Rist * under the License. 19*ca5ec200SAndrew Rist * 20*ca5ec200SAndrew Rist *************************************************************/ 21*ca5ec200SAndrew Rist 22*ca5ec200SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "oox/core/fastparser.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include "oox/core/fasttokenhandler.hxx" 27cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx" 28cdf0e10cSrcweir #include "oox/helper/helper.hxx" 29cdf0e10cSrcweir #include "oox/helper/storagebase.hxx" 30cdf0e10cSrcweir #include "oox/token/namespacemap.hxx" 31cdf0e10cSrcweir 32cdf0e10cSrcweir namespace oox { 33cdf0e10cSrcweir namespace core { 34cdf0e10cSrcweir 35cdf0e10cSrcweir // ============================================================================ 36cdf0e10cSrcweir 37cdf0e10cSrcweir using namespace ::com::sun::star::io; 38cdf0e10cSrcweir using namespace ::com::sun::star::lang; 39cdf0e10cSrcweir using namespace ::com::sun::star::uno; 40cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax; 41cdf0e10cSrcweir 42cdf0e10cSrcweir using ::rtl::OUString; 43cdf0e10cSrcweir 44cdf0e10cSrcweir // ============================================================================ 45cdf0e10cSrcweir 46cdf0e10cSrcweir namespace { 47cdf0e10cSrcweir 48cdf0e10cSrcweir class InputStreamCloseGuard 49cdf0e10cSrcweir { 50cdf0e10cSrcweir public: 51cdf0e10cSrcweir explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ); 52cdf0e10cSrcweir ~InputStreamCloseGuard(); 53cdf0e10cSrcweir private: 54cdf0e10cSrcweir Reference< XInputStream > mxInStream; 55cdf0e10cSrcweir bool mbCloseStream; 56cdf0e10cSrcweir }; 57cdf0e10cSrcweir 58cdf0e10cSrcweir InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) : 59cdf0e10cSrcweir mxInStream( rxInStream ), 60cdf0e10cSrcweir mbCloseStream( bCloseStream ) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir InputStreamCloseGuard::~InputStreamCloseGuard() 65cdf0e10cSrcweir { 66cdf0e10cSrcweir if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {} 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir } // namespace 70cdf0e10cSrcweir 71cdf0e10cSrcweir // ============================================================================ 72cdf0e10cSrcweir 73cdf0e10cSrcweir FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) : 74cdf0e10cSrcweir mrNamespaceMap( StaticNamespaceMap::get() ) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir // create a fast parser instance 77cdf0e10cSrcweir Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW ); 78cdf0e10cSrcweir mxParser.set( xFactory->createInstanceWithContext( CREATE_OUSTRING( "com.sun.star.xml.sax.FastParser" ), rxContext ), UNO_QUERY_THROW ); 79cdf0e10cSrcweir 80cdf0e10cSrcweir // create the fast token handler based on the OOXML token list 81cdf0e10cSrcweir mxParser->setTokenHandler( new FastTokenHandler ); 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir FastParser::~FastParser() 85cdf0e10cSrcweir { 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException ) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir if( !mxParser.is() ) 91cdf0e10cSrcweir throw RuntimeException(); 92cdf0e10cSrcweir 93cdf0e10cSrcweir const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap, nNamespaceId ); 94cdf0e10cSrcweir if( !pNamespaceUrl ) 95cdf0e10cSrcweir throw IllegalArgumentException(); 96cdf0e10cSrcweir 97cdf0e10cSrcweir mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId ); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException ) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir if( !mxParser.is() ) 103cdf0e10cSrcweir throw RuntimeException(); 104cdf0e10cSrcweir mxParser->setFastDocumentHandler( rxDocHandler ); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir // guard closing the input stream also when exceptions are thrown 110cdf0e10cSrcweir InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream ); 111cdf0e10cSrcweir if( !mxParser.is() ) 112cdf0e10cSrcweir throw RuntimeException(); 113cdf0e10cSrcweir mxParser->parseStream( rInputSource ); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException ) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir InputSource aInputSource; 119cdf0e10cSrcweir aInputSource.sSystemId = rStreamName; 120cdf0e10cSrcweir aInputSource.aInputStream = rxInStream; 121cdf0e10cSrcweir parseStream( aInputSource, bCloseStream ); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException ) 125cdf0e10cSrcweir { 126cdf0e10cSrcweir parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream ); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir // ============================================================================ 130cdf0e10cSrcweir 131cdf0e10cSrcweir } // namespace core 132cdf0e10cSrcweir } // namespace oox 133