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 555 : InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
55 : mxInStream( rxInStream ),
56 555 : mbCloseStream( bCloseStream )
57 : {
58 555 : }
59 :
60 1110 : InputStreamCloseGuard::~InputStreamCloseGuard()
61 : {
62 555 : if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
63 555 : }
64 :
65 : } // namespace
66 :
67 : // ============================================================================
68 :
69 264 : FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
70 264 : mrNamespaceMap( StaticNamespaceMap::get() )
71 : {
72 : // create a fast parser instance
73 264 : Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW );
74 264 : mxParser.set( xFactory->createInstanceWithContext( CREATE_OUSTRING( "com.sun.star.xml.sax.FastParser" ), rxContext ), UNO_QUERY_THROW );
75 :
76 : // create the fast tokenhandler
77 264 : mxTokenHandler.set( new FastTokenHandler );
78 :
79 : // create the fast token handler based on the OOXML token list
80 264 : mxParser->setTokenHandler( mxTokenHandler );
81 264 : }
82 :
83 264 : FastParser::~FastParser()
84 : {
85 264 : }
86 :
87 2223 : void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
88 : {
89 2223 : if( !mxParser.is() )
90 0 : throw RuntimeException();
91 :
92 2223 : const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap, nNamespaceId );
93 2223 : if( !pNamespaceUrl )
94 0 : throw IllegalArgumentException();
95 :
96 2223 : mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
97 2223 : }
98 :
99 354 : void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
100 : {
101 354 : if( !mxParser.is() )
102 0 : throw RuntimeException();
103 354 : mxParser->setFastDocumentHandler( rxDocHandler );
104 354 : }
105 :
106 555 : 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 555 : InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
110 555 : if( !mxParser.is() )
111 0 : throw RuntimeException();
112 558 : mxParser->parseStream( rInputSource );
113 552 : }
114 :
115 343 : void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
116 : {
117 343 : InputSource aInputSource;
118 343 : aInputSource.sSystemId = rStreamName;
119 343 : aInputSource.aInputStream = rxInStream;
120 346 : parseStream( aInputSource, bCloseStream );
121 340 : }
122 :
123 191 : void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
124 : {
125 194 : parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
126 188 : }
127 :
128 6 : OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
129 : {
130 6 : if( !mxParser.is() )
131 0 : throw RuntimeException();
132 6 : return mxParser->getNamespaceURL( rPrefix );
133 : }
134 :
135 6 : sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
136 : {
137 165 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.begin(), aEnd = mrNamespaceMap.end(); aIt != aEnd; ++aIt )
138 162 : if( rUrl == aIt->second )
139 3 : return aIt->first;
140 3 : return 0;
141 : }
142 :
143 : // ============================================================================
144 :
145 : } // namespace core
146 51 : } // namespace oox
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|