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 :
39 : // ============================================================================
40 :
41 : namespace {
42 :
43 : class InputStreamCloseGuard
44 : {
45 : public:
46 : explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
47 : ~InputStreamCloseGuard();
48 : private:
49 : Reference< XInputStream > mxInStream;
50 : bool mbCloseStream;
51 : };
52 :
53 1055 : InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
54 : mxInStream( rxInStream ),
55 1055 : mbCloseStream( bCloseStream )
56 : {
57 1055 : }
58 :
59 2110 : InputStreamCloseGuard::~InputStreamCloseGuard()
60 : {
61 1055 : if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
62 1055 : }
63 :
64 : } // namespace
65 :
66 : // ============================================================================
67 :
68 553 : FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
69 553 : mrNamespaceMap( StaticNamespaceMap::get() )
70 : {
71 : // create a fast parser instance
72 553 : Reference< XMultiComponentFactory > xFactory( rxContext->getServiceManager(), UNO_SET_THROW );
73 553 : mxParser.set( xFactory->createInstanceWithContext( "com.sun.star.xml.sax.FastParser", rxContext ), UNO_QUERY_THROW );
74 :
75 : // create the fast tokenhandler
76 553 : mxTokenHandler.set( new FastTokenHandler );
77 :
78 : // create the fast token handler based on the OOXML token list
79 553 : mxParser->setTokenHandler( mxTokenHandler );
80 553 : }
81 :
82 553 : FastParser::~FastParser()
83 : {
84 553 : }
85 :
86 4605 : void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
87 : {
88 4605 : if( !mxParser.is() )
89 0 : throw RuntimeException();
90 :
91 4605 : const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap, nNamespaceId );
92 4605 : if( !pNamespaceUrl )
93 0 : throw IllegalArgumentException();
94 :
95 4605 : mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
96 4605 : }
97 :
98 704 : void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
99 : {
100 704 : if( !mxParser.is() )
101 0 : throw RuntimeException();
102 704 : mxParser->setFastDocumentHandler( rxDocHandler );
103 704 : }
104 :
105 1055 : void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
106 : {
107 : // guard closing the input stream also when exceptions are thrown
108 1055 : InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
109 1055 : if( !mxParser.is() )
110 0 : throw RuntimeException();
111 1127 : mxParser->parseStream( rInputSource );
112 983 : }
113 :
114 684 : void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
115 : {
116 684 : InputSource aInputSource;
117 684 : aInputSource.sSystemId = rStreamName;
118 684 : aInputSource.aInputStream = rxInStream;
119 756 : parseStream( aInputSource, bCloseStream );
120 612 : }
121 :
122 400 : void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
123 : {
124 472 : parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
125 328 : }
126 :
127 6 : OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
128 : {
129 6 : if( !mxParser.is() )
130 0 : throw RuntimeException();
131 6 : return mxParser->getNamespaceURL( rPrefix );
132 : }
133 :
134 6 : sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
135 : {
136 169 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.begin(), aEnd = mrNamespaceMap.end(); aIt != aEnd; ++aIt )
137 166 : if( rUrl == aIt->second )
138 3 : return aIt->first;
139 3 : return 0;
140 : }
141 :
142 : // ============================================================================
143 :
144 : } // namespace core
145 141 : } // namespace oox
146 :
147 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|