1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include "oox/core/fastparser.hxx" 29 30 #include "oox/core/fasttokenhandler.hxx" 31 #include "oox/helper/containerhelper.hxx" 32 #include "oox/helper/helper.hxx" 33 #include "oox/helper/storagebase.hxx" 34 #include "oox/token/namespacemap.hxx" 35 36 namespace oox { 37 namespace core { 38 39 // ============================================================================ 40 41 using namespace ::com::sun::star::io; 42 using namespace ::com::sun::star::lang; 43 using namespace ::com::sun::star::uno; 44 using namespace ::com::sun::star::xml::sax; 45 46 using ::rtl::OUString; 47 48 // ============================================================================ 49 50 namespace { 51 52 class InputStreamCloseGuard 53 { 54 public: 55 explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ); 56 ~InputStreamCloseGuard(); 57 private: 58 Reference< XInputStream > mxInStream; 59 bool mbCloseStream; 60 }; 61 62 InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) : 63 mxInStream( rxInStream ), 64 mbCloseStream( bCloseStream ) 65 { 66 } 67 68 InputStreamCloseGuard::~InputStreamCloseGuard() 69 { 70 if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {} 71 } 72 73 } // namespace 74 75 // ============================================================================ 76 77 FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) : 78 mrNamespaceMap( StaticNamespaceMap::get() ) 79 { 80 // create a fast parser instance 81 Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW ); 82 mxParser.set( xFactory->createInstanceWithContext( CREATE_OUSTRING( "com.sun.star.xml.sax.FastParser" ), rxContext ), UNO_QUERY_THROW ); 83 84 // create the fast token handler based on the OOXML token list 85 mxParser->setTokenHandler( new FastTokenHandler ); 86 } 87 88 FastParser::~FastParser() 89 { 90 } 91 92 void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException ) 93 { 94 if( !mxParser.is() ) 95 throw RuntimeException(); 96 97 const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap, nNamespaceId ); 98 if( !pNamespaceUrl ) 99 throw IllegalArgumentException(); 100 101 mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId ); 102 } 103 104 void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException ) 105 { 106 if( !mxParser.is() ) 107 throw RuntimeException(); 108 mxParser->setFastDocumentHandler( rxDocHandler ); 109 } 110 111 void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException ) 112 { 113 // guard closing the input stream also when exceptions are thrown 114 InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream ); 115 if( !mxParser.is() ) 116 throw RuntimeException(); 117 mxParser->parseStream( rInputSource ); 118 } 119 120 void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException ) 121 { 122 InputSource aInputSource; 123 aInputSource.sSystemId = rStreamName; 124 aInputSource.aInputStream = rxInStream; 125 parseStream( aInputSource, bCloseStream ); 126 } 127 128 void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException ) 129 { 130 parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream ); 131 } 132 133 // ============================================================================ 134 135 } // namespace core 136 } // namespace oox 137