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 :
37 :
38 : using namespace ::com::sun::star::io;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::xml::sax;
42 :
43 :
44 :
45 :
46 : namespace {
47 :
48 : class InputStreamCloseGuard
49 : {
50 : public:
51 : explicit InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream );
52 : ~InputStreamCloseGuard();
53 : private:
54 : Reference< XInputStream > mxInStream;
55 : bool mbCloseStream;
56 : };
57 :
58 0 : InputStreamCloseGuard::InputStreamCloseGuard( const Reference< XInputStream >& rxInStream, bool bCloseStream ) :
59 : mxInStream( rxInStream ),
60 0 : mbCloseStream( bCloseStream )
61 : {
62 0 : }
63 :
64 0 : InputStreamCloseGuard::~InputStreamCloseGuard()
65 : {
66 0 : if( mxInStream.is() && mbCloseStream ) try { mxInStream->closeInput(); } catch( Exception& ) {}
67 0 : }
68 :
69 : } // namespace
70 :
71 :
72 :
73 0 : FastParser::FastParser( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
74 0 : mrNamespaceMap( StaticNamespaceMap::get() ),
75 0 : mpParser(NULL)
76 : {
77 : // create a fast parser instance
78 0 : mxParser = css::xml::sax::FastParser::create(rxContext);
79 0 : mpParser = dynamic_cast<sax_fastparser::FastSaxParser*>(mxParser.get());
80 :
81 : // create the fast tokenhandler
82 0 : mxTokenHandler.set( new FastTokenHandler );
83 :
84 : // create the fast token handler based on the OOXML token list
85 0 : mxParser->setTokenHandler( mxTokenHandler );
86 0 : }
87 :
88 0 : FastParser::~FastParser()
89 : {
90 0 : }
91 :
92 0 : void FastParser::registerNamespace( sal_Int32 nNamespaceId ) throw( IllegalArgumentException, RuntimeException )
93 : {
94 0 : if( !mxParser.is() )
95 0 : throw RuntimeException();
96 :
97 : // add handling for OOXML strict here
98 0 : const OUString* pNamespaceUrl = ContainerHelper::getMapElement( mrNamespaceMap.maTransitionalNamespaceMap, nNamespaceId );
99 0 : if( !pNamespaceUrl )
100 0 : throw IllegalArgumentException();
101 :
102 0 : mxParser->registerNamespace( *pNamespaceUrl, nNamespaceId );
103 :
104 : //also register the OOXML strict namespaces for the same id
105 0 : const OUString* pNamespaceStrictUrl = ContainerHelper::getMapElement( mrNamespaceMap.maStrictNamespaceMap, nNamespaceId );
106 0 : if(pNamespaceStrictUrl && (*pNamespaceUrl != *pNamespaceStrictUrl))
107 : {
108 0 : mxParser->registerNamespace( *pNamespaceStrictUrl, nNamespaceId );
109 : }
110 0 : }
111 :
112 0 : void FastParser::setDocumentHandler( const Reference< XFastDocumentHandler >& rxDocHandler ) throw( RuntimeException )
113 : {
114 0 : if( !mxParser.is() )
115 0 : throw RuntimeException();
116 0 : mxParser->setFastDocumentHandler( rxDocHandler );
117 0 : }
118 :
119 0 : void FastParser::parseStream( const InputSource& rInputSource, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
120 : {
121 : // guard closing the input stream also when exceptions are thrown
122 0 : InputStreamCloseGuard aGuard( rInputSource.aInputStream, bCloseStream );
123 0 : if( !mxParser.is() )
124 0 : throw RuntimeException();
125 0 : mxParser->parseStream( rInputSource );
126 0 : }
127 :
128 0 : void FastParser::parseStream( const Reference< XInputStream >& rxInStream, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
129 : {
130 0 : InputSource aInputSource;
131 0 : aInputSource.sSystemId = rStreamName;
132 0 : aInputSource.aInputStream = rxInStream;
133 0 : parseStream( aInputSource, bCloseStream );
134 0 : }
135 :
136 0 : void FastParser::parseStream( StorageBase& rStorage, const OUString& rStreamName, bool bCloseStream ) throw( SAXException, IOException, RuntimeException )
137 : {
138 0 : parseStream( rStorage.openInputStream( rStreamName ), rStreamName, bCloseStream );
139 0 : }
140 :
141 0 : OUString FastParser::getNamespaceURL( const OUString& rPrefix ) throw( IllegalArgumentException, RuntimeException )
142 : {
143 0 : if( !mxParser.is() )
144 0 : throw RuntimeException();
145 0 : return mxParser->getNamespaceURL( rPrefix );
146 : }
147 :
148 0 : bool FastParser::hasNamespaceURL( const OUString& rPrefix ) const
149 : {
150 0 : if (!mxParser.is())
151 0 : throw RuntimeException();
152 :
153 0 : if (!mpParser)
154 0 : return false;
155 :
156 0 : return mpParser->hasNamespaceURL(rPrefix);
157 : }
158 :
159 0 : sal_Int32 FastParser::getNamespaceId( const OUString& rUrl )
160 : {
161 0 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maTransitionalNamespaceMap.begin(),
162 0 : aEnd = mrNamespaceMap.maTransitionalNamespaceMap.end(); aIt != aEnd; ++aIt )
163 0 : if( rUrl == aIt->second )
164 0 : return aIt->first;
165 :
166 0 : for( NamespaceMap::const_iterator aIt = mrNamespaceMap.maStrictNamespaceMap.begin(),
167 0 : aEnd = mrNamespaceMap.maStrictNamespaceMap.end(); aIt != aEnd; ++aIt )
168 0 : if( rUrl == aIt->second )
169 0 : return aIt->first;
170 :
171 0 : return 0;
172 : }
173 :
174 :
175 :
176 : } // namespace core
177 0 : } // namespace oox
178 :
179 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|