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