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 <sal/config.h>
21 :
22 : #include <com/sun/star/xml/sax/FastParser.hpp>
23 : #include "oox/core/fastparser.hxx"
24 :
25 : #include "oox/core/fasttokenhandler.hxx"
26 : #include "oox/helper/containerhelper.hxx"
27 : #include "oox/helper/helper.hxx"
28 : #include "oox/helper/storagebase.hxx"
29 : #include "oox/token/namespacemap.hxx"
30 :
31 : #include "sax/fastparser.hxx"
32 :
33 : namespace oox {
34 : namespace core {
35 :
36 : using namespace ::com::sun::star::io;
37 : using namespace ::com::sun::star::lang;
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star::xml::sax;
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 18368 : InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
54 : mxInStream( rxInStream ),
55 18368 : mbCloseStream( bCloseStream )
56 : {
57 18368 : }
58 :
59 36736 : InputStreamCloseGuard::~InputStreamCloseGuard()
60 : {
61 18368 : if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
62 18368 : }
63 :
64 : } // namespace
65 :
66 16324 : FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
67 16324 : mrNamespaceMap( StaticNamespaceMap::get() ),
68 32648 : mpParser(NULL)
69 : {
70 : // create a fast parser instance
71 16324 : mxParser = css::xml::sax::FastParser::create(rxContext);
72 16324 : mpParser = dynamic_cast<sax_fastparser::FastSaxParser*>(mxParser.get());
73 :
74 : // create the fast tokenhandler
75 16324 : mxTokenHandler.set( new FastTokenHandler );
76 :
77 : // create the fast token handler based on the OOXML token list
78 16324 : mxParser->setTokenHandler( mxTokenHandler );
79 16324 : }
80 :
81 16604 : FastParser::~FastParser()
82 : {
83 16604 : }
84 :
85 240624 : void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
86 : {
87 240624 : if( !mxParser.is() )
88 0 : throw RuntimeException();
89 :
90 : // add handling for OOXML strict here
91 240624 : const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
92 240624 : if( !pNamespaceUrl )
93 0 : throw IllegalArgumentException();
94 :
95 240624 : mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
96 :
97 : //also register the OOXML strict namespaces for the same id
98 240624 : const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
99 240624 : if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
100 : {
101 93372 : mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
102 : }
103 240624 : }
104 :
105 14240 : void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
106 : {
107 14240 : if( !mxParser.is() )
108 0 : throw RuntimeException();
109 14240 : mxParser->setFastDocumentHandler( rxDocHandler );
110 14240 : }
111 :
112 18368 : void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
113 : {
114 : // guard closing the input stream also when exceptions are thrown
115 18368 : InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
116 18368 : if( !mxParser.is() )
117 0 : throw RuntimeException();
118 18728 : mxParser->parseStream( rInputSource );
119 18008 : }
120 :
121 11308 : void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
122 : {
123 11308 : InputSource aInputSource;
124 11308 : aInputSource.sSystemId = rStreamName;
125 11308 : aInputSource.aInputStream = rxInStream;
126 11668 : parseStream( aInputSource, bCloseStream );
127 10948 : }
128 :
129 5960 : void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
130 : {
131 6320 : parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
132 5600 : }
133 :
134 0 : OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
135 : {
136 0 : if( !mxParser.is() )
137 0 : throw RuntimeException();
138 0 : return mxParser->getNamespaceURL( rPrefix );
139 : }
140 :
141 138 : bool FastParser::hasNamespaceURL( const OUString& rPrefix ) const
142 : {
143 138 : if (!mxParser.is())
144 0 : throw RuntimeException();
145 :
146 138 : if (!mpParser)
147 0 : return false;
148 :
149 138 : return mpParser->hasNamespaceURL(rPrefix);
150 : }
151 :
152 0 : sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
153 : {
154 0 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maTransitionalNamespaceMap.begin(),
155 0 : aEnd = mrNamespaceMap.maTransitionalNamespaceMap.end(); aIt != aEnd; ++aIt )
156 0 : if( rUrl == aIt->second )
157 0 : return aIt->first;
158 :
159 0 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maStrictNamespaceMap.begin(),
160 0 : aEnd = mrNamespaceMap.maStrictNamespaceMap.end(); aIt != aEnd; ++aIt )
161 0 : if( rUrl == aIt->second )
162 0 : return aIt->first;
163 :
164 0 : return 0;
165 : }
166 :
167 : } // namespace core
168 408 : } // namespace oox
169 :
170 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|