Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "oox/core/fastparser.hxx"
21 : :
22 : : #include "oox/core/fasttokenhandler.hxx"
23 : : #include "oox/helper/containerhelper.hxx"
24 : : #include "oox/helper/helper.hxx"
25 : : #include "oox/helper/storagebase.hxx"
26 : : #include "oox/token/namespacemap.hxx"
27 : :
28 : : namespace oox {
29 : : namespace core {
30 : :
31 : : // ============================================================================
32 : :
33 : : using namespace ::com::sun::star::io;
34 : : using namespace ::com::sun::star::lang;
35 : : using namespace ::com::sun::star::uno;
36 : : using namespace ::com::sun::star::xml::sax;
37 : :
38 : : using ::rtl::OUString;
39 : :
40 : : // ============================================================================
41 : :
42 : : namespace {
43 : :
44 : : class InputStreamCloseGuard
45 : : {
46 : : public:
47 : : explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
48 : : ~InputStreamCloseGuard();
49 : : private:
50 : : Reference< XInputStream > mxInStream;
51 : : bool mbCloseStream;
52 : : };
53 : :
54 : 1164 : InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
55 : : mxInStream( rxInStream ),
56 : 1164 : mbCloseStream( bCloseStream )
57 : : {
58 : 1164 : }
59 : :
60 : 1164 : InputStreamCloseGuard::~InputStreamCloseGuard()
61 : : {
62 [ + + ][ + + ]: 1164 : if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
[ + + ][ + - ]
[ + - # # ]
63 [ # # ]: 1164 : }
64 : :
65 : : } // namespace
66 : :
67 : : // ============================================================================
68 : :
69 : 548 : FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
70 [ + - ]: 548 : mrNamespaceMap( StaticNamespaceMap::get() )
71 : : {
72 : : // create a fast parser instance
73 [ + - ][ + - ]: 548 : Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW );
[ + - ]
74 [ + - ][ + - ]: 548 : mxParser.set( xFactory->createInstanceWithContext( CREATE_OUSTRING( "com.sun.star.xml.sax.FastParser" ), rxContext ), UNO_QUERY_THROW );
[ + - ][ + - ]
75 : :
76 : : // create the fast tokenhandler
77 [ + - ][ + - ]: 548 : mxTokenHandler.set( new FastTokenHandler );
[ + - ]
78 : :
79 : : // create the fast token handler based on the OOXML token list
80 [ + - ][ + - ]: 548 : mxParser->setTokenHandler( mxTokenHandler );
81 : 548 : }
82 : :
83 : 548 : FastParser::~FastParser()
84 : : {
85 [ - + ]: 548 : }
86 : :
87 : 4503 : void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
88 : : {
89 [ - + ]: 4503 : if( !mxParser.is() )
90 [ # # ]: 0 : throw RuntimeException();
91 : :
92 : 4503 : const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap, nNamespaceId );
93 [ - + ]: 4503 : if( !pNamespaceUrl )
94 [ # # ]: 0 : throw IllegalArgumentException();
95 : :
96 : 4503 : mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
97 : 4503 : }
98 : :
99 : 758 : void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
100 : : {
101 [ - + ]: 758 : if( !mxParser.is() )
102 [ # # ]: 0 : throw RuntimeException();
103 : 758 : mxParser->setFastDocumentHandler( rxDocHandler );
104 : 758 : }
105 : :
106 : 1164 : void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
107 : : {
108 : : // guard closing the input stream also when exceptions are thrown
109 [ + - ]: 1164 : InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
110 [ - + ]: 1164 : if( !mxParser.is() )
111 [ # # ]: 0 : throw RuntimeException();
112 [ + - ][ + + ]: 1164 : mxParser->parseStream( rInputSource );
[ + - ]
113 : 1145 : }
114 : :
115 : 734 : void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
116 : : {
117 [ + - ]: 734 : InputSource aInputSource;
118 : 734 : aInputSource.sSystemId = rStreamName;
119 [ + - ]: 734 : aInputSource.aInputStream = rxInStream;
120 [ + + ][ + - ]: 734 : parseStream( aInputSource, bCloseStream );
121 : 715 : }
122 : :
123 : 401 : void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
124 : : {
125 [ + + ]: 401 : parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
126 : 382 : }
127 : :
128 : 12 : OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
129 : : {
130 [ - + ]: 12 : if( !mxParser.is() )
131 [ # # ]: 0 : throw RuntimeException();
132 : 12 : return mxParser->getNamespaceURL( rPrefix );
133 : : }
134 : :
135 : 12 : sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
136 : : {
137 [ + + ]: 279 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.begin(), aEnd = mrNamespaceMap.end(); aIt != aEnd; ++aIt )
138 [ + + ]: 276 : if( rUrl == aIt->second )
139 : 9 : return aIt->first;
140 : 12 : return 0;
141 : : }
142 : :
143 : : // ============================================================================
144 : :
145 : : } // namespace core
146 [ + - ][ + - ]: 285 : } // namespace oox
147 : :
148 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|