Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / oox / source / core / recordparser.cxx
blob5d4889584867d8ab4b9ecb8ad8112ed18a88ee5e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/core/recordparser.hxx>
22 #include <vector>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/xml/sax/SAXException.hpp>
26 #include <com/sun/star/xml/sax/XLocator.hpp>
27 #include <cppuhelper/implbase.hxx>
28 #include <osl/diagnose.h>
29 #include <oox/core/fragmenthandler.hxx>
31 namespace oox {
32 namespace core {
34 using namespace ::com::sun::star::io;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::xml::sax;
39 namespace prv {
41 class Locator : public ::cppu::WeakImplHelper< XLocator >
43 public:
44 explicit Locator( RecordParser* pParser ) : mpParser( pParser ) {}
46 void dispose();
47 /// @throws css::uno::RuntimeException
48 void checkDispose();
50 // com.sun.star.sax.XLocator interface
52 virtual sal_Int32 SAL_CALL getColumnNumber() override;
53 virtual sal_Int32 SAL_CALL getLineNumber() override;
54 virtual OUString SAL_CALL getPublicId() override;
55 virtual OUString SAL_CALL getSystemId() override;
57 private:
58 RecordParser* mpParser;
61 void Locator::dispose()
63 mpParser = nullptr;
66 void Locator::checkDispose()
68 if( !mpParser )
69 throw DisposedException();
72 sal_Int32 SAL_CALL Locator::getColumnNumber()
74 return -1;
77 sal_Int32 SAL_CALL Locator::getLineNumber()
79 return -1;
82 OUString SAL_CALL Locator::getPublicId()
84 checkDispose();
85 return OUString();
88 OUString SAL_CALL Locator::getSystemId()
90 checkDispose();
91 return mpParser->getInputSource().maSystemId;
94 class ContextStack
96 public:
97 explicit ContextStack( FragmentHandlerRef const & xHandler );
99 bool empty() const { return maStack.empty(); }
101 sal_Int32 getCurrentRecId() const;
102 bool hasCurrentEndRecId() const;
103 ContextHandlerRef getCurrentContext() const;
105 void pushContext( const RecordInfo& rRec, const ContextHandlerRef& rxContext );
106 void popContext();
108 private:
109 typedef ::std::pair< RecordInfo, ContextHandlerRef > ContextInfo;
110 typedef ::std::vector< ContextInfo > ContextInfoVec;
112 FragmentHandlerRef const mxHandler;
113 ContextInfoVec maStack;
116 ContextStack::ContextStack( FragmentHandlerRef const & xHandler ) :
117 mxHandler( xHandler )
121 sal_Int32 ContextStack::getCurrentRecId() const
123 return maStack.empty() ? -1 : maStack.back().first.mnStartRecId;
126 bool ContextStack::hasCurrentEndRecId() const
128 return !maStack.empty() && (maStack.back().first.mnEndRecId >= 0);
131 ContextHandlerRef ContextStack::getCurrentContext() const
133 if( !maStack.empty() )
134 return maStack.back().second;
135 return mxHandler.get();
138 void ContextStack::pushContext( const RecordInfo& rRecInfo, const ContextHandlerRef& rxContext )
140 OSL_ENSURE( (rRecInfo.mnEndRecId >= 0) || maStack.empty() || hasCurrentEndRecId(),
141 "ContextStack::pushContext - nested incomplete context record identifiers" );
142 maStack.emplace_back( rRecInfo, rxContext );
145 void ContextStack::popContext()
147 OSL_ENSURE( !maStack.empty(), "ContextStack::popContext - no context on stack" );
148 if( !maStack.empty() )
150 ContextInfo& rContextInfo = maStack.back();
151 if( rContextInfo.second.is() )
152 rContextInfo.second->endRecord( rContextInfo.first.mnStartRecId );
153 maStack.pop_back();
157 } // namespace prv
159 namespace {
161 /** Reads a byte from the passed stream, returns true on success. */
162 bool lclReadByte( sal_uInt8& ornByte, BinaryInputStream& rStrm )
164 return rStrm.readMemory( &ornByte, 1 ) == 1;
167 /** Reads a compressed signed 32-bit integer from the passed stream. */
168 bool lclReadCompressedInt( sal_Int32& ornValue, BinaryInputStream& rStrm )
170 ornValue = 0;
171 sal_uInt8 nByte;
172 if( !lclReadByte( nByte, rStrm ) ) return false;
173 ornValue = nByte & 0x7F;
174 if( (nByte & 0x80) == 0 ) return true;
175 if( !lclReadByte( nByte, rStrm ) ) return false;
176 ornValue |= sal_Int32( nByte & 0x7F ) << 7;
177 if( (nByte & 0x80) == 0 ) return true;
178 if( !lclReadByte( nByte, rStrm ) ) return false;
179 ornValue |= sal_Int32( nByte & 0x7F ) << 14;
180 if( (nByte & 0x80) == 0 ) return true;
181 if( !lclReadByte( nByte, rStrm ) ) return false;
182 ornValue |= sal_Int32( nByte & 0x7F ) << 21;
183 return true;
186 bool lclReadRecordHeader( sal_Int32& ornRecId, sal_Int32& ornRecSize, BinaryInputStream& rStrm )
188 return
189 lclReadCompressedInt( ornRecId, rStrm ) && (ornRecId >= 0) &&
190 lclReadCompressedInt( ornRecSize, rStrm ) && (ornRecSize >= 0);
193 bool lclReadNextRecord( sal_Int32& ornRecId, StreamDataSequence& orData, BinaryInputStream& rStrm )
195 sal_Int32 nRecSize = 0;
196 bool bValid = lclReadRecordHeader( ornRecId, nRecSize, rStrm );
197 if( bValid )
199 orData.realloc( nRecSize );
200 bValid = (nRecSize == 0) || (rStrm.readData( orData, nRecSize ) == nRecSize);
202 return bValid;
205 } // namespace
207 RecordParser::RecordParser()
209 mxLocator.set( new prv::Locator( this ) );
212 RecordParser::~RecordParser()
214 if( mxLocator.is() )
215 mxLocator->dispose();
218 void RecordParser::setFragmentHandler( const ::rtl::Reference< FragmentHandler >& rxHandler )
220 mxHandler = rxHandler;
222 // build record infos
223 maStartMap.clear();
224 maEndMap.clear();
225 const RecordInfo* pRecs = mxHandler.is() ? mxHandler->getRecordInfos() : nullptr;
226 OSL_ENSURE( pRecs, "RecordInfoProvider::RecordInfoProvider - missing record list" );
227 for( ; pRecs && pRecs->mnStartRecId >= 0; ++pRecs )
229 maStartMap[ pRecs->mnStartRecId ] = *pRecs;
230 if( pRecs->mnEndRecId >= 0 )
231 maEndMap[ pRecs->mnEndRecId ] = *pRecs;
235 void RecordParser::parseStream( const RecordInputSource& rInputSource )
237 maSource = rInputSource;
239 if( !maSource.mxInStream || maSource.mxInStream->isEof() )
240 throw IOException();
241 if( !mxHandler.is() )
242 throw SAXException();
244 // start the document
245 Reference< XLocator > xLocator( mxLocator.get() );
246 mxHandler->setDocumentLocator( xLocator );
247 mxHandler->startDocument();
249 // parse the stream
250 mxStack.reset( new prv::ContextStack( mxHandler ) );
251 sal_Int32 nRecId = 0;
252 StreamDataSequence aRecData;
253 while( lclReadNextRecord( nRecId, aRecData, *maSource.mxInStream ) )
255 // create record stream object from imported record data
256 SequenceInputStream aRecStrm( aRecData );
257 // try to leave a context, there may be other incomplete contexts on the stack
258 if( const RecordInfo* pEndRecInfo = getEndRecordInfo( nRecId ) )
260 // finalize contexts without record identifier for context end
261 while( !mxStack->empty() && !mxStack->hasCurrentEndRecId() )
262 mxStack->popContext();
263 // finalize the current context and pop context info from stack
264 OSL_ENSURE( mxStack->getCurrentRecId() == pEndRecInfo->mnStartRecId, "RecordParser::parseStream - context records mismatch" );
265 ContextHandlerRef xCurrContext = mxStack->getCurrentContext();
266 if( xCurrContext.is() )
268 // context end record may contain some data, handle it as simple record
269 aRecStrm.seekToStart();
270 xCurrContext->startRecord( nRecId, aRecStrm );
271 xCurrContext->endRecord( nRecId );
273 mxStack->popContext();
275 else
277 // end context with incomplete record id, if the same id comes again
278 if( (mxStack->getCurrentRecId() == nRecId) && !mxStack->hasCurrentEndRecId() )
279 mxStack->popContext();
280 // try to start a new context
281 ContextHandlerRef xCurrContext = mxStack->getCurrentContext();
282 if( xCurrContext.is() )
284 aRecStrm.seekToStart();
285 xCurrContext = xCurrContext->createRecordContext( nRecId, aRecStrm );
287 // track all context identifiers on the stack (do not push simple records)
288 const RecordInfo* pStartRecInfo = getStartRecordInfo( nRecId );
289 if( pStartRecInfo )
290 mxStack->pushContext( *pStartRecInfo, xCurrContext );
291 // import the record
292 if( xCurrContext.is() )
294 // import the record
295 aRecStrm.seekToStart();
296 xCurrContext->startRecord( nRecId, aRecStrm );
297 // end simple records (context records are finished in ContextStack::popContext)
298 if( !pStartRecInfo )
299 xCurrContext->endRecord( nRecId );
303 // close remaining contexts (missing context end records or stream error)
304 while( !mxStack->empty() )
305 mxStack->popContext();
306 mxStack.reset();
308 // finish document
309 mxHandler->endDocument();
311 maSource = RecordInputSource();
314 const RecordInfo* RecordParser::getStartRecordInfo( sal_Int32 nRecId ) const
316 RecordInfoMap::const_iterator aIt = maStartMap.find( nRecId );
317 return (aIt == maStartMap.end()) ? nullptr : &aIt->second;
320 const RecordInfo* RecordParser::getEndRecordInfo( sal_Int32 nRecId ) const
322 RecordInfoMap::const_iterator aIt = maEndMap.find( nRecId );
323 return (aIt == maEndMap.end()) ? nullptr : &aIt->second;
326 } // namespace core
327 } // namespace oox
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */