Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / core / recordparser.cxx
blob24ee5dc0e96c55cd6b5311f0a30999d08ad297eb
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 <utility>
23 #include <vector>
24 #include <com/sun/star/io/IOException.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <com/sun/star/xml/sax/SAXException.hpp>
27 #include <com/sun/star/xml/sax/XLocator.hpp>
28 #include <cppuhelper/implbase.hxx>
29 #include <osl/diagnose.h>
30 #include <oox/core/fragmenthandler.hxx>
32 namespace oox::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 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 mxHandler;
113 ContextInfoVec maStack;
116 ContextStack::ContextStack( FragmentHandlerRef xHandler ) :
117 mxHandler(std::move( 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;
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 oox::core::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 mxHandler->setDocumentLocator( mxLocator );
246 mxHandler->startDocument();
248 // parse the stream
249 mxStack.reset( new prv::ContextStack( mxHandler ) );
250 sal_Int32 nRecId = 0;
251 StreamDataSequence aRecData;
252 while( lclReadNextRecord( nRecId, aRecData, *maSource.mxInStream ) )
254 // create record stream object from imported record data
255 SequenceInputStream aRecStrm( aRecData );
256 // try to leave a context, there may be other incomplete contexts on the stack
257 if( const RecordInfo* pEndRecInfo = getEndRecordInfo( nRecId ) )
259 // finalize contexts without record identifier for context end
260 while( !mxStack->empty() && !mxStack->hasCurrentEndRecId() )
261 mxStack->popContext();
262 // finalize the current context and pop context info from stack
263 OSL_ENSURE( mxStack->getCurrentRecId() == pEndRecInfo->mnStartRecId, "RecordParser::parseStream - context records mismatch" );
264 ContextHandlerRef xCurrContext = mxStack->getCurrentContext();
265 if( xCurrContext.is() )
267 // context end record may contain some data, handle it as simple record
268 aRecStrm.seekToStart();
269 xCurrContext->startRecord( nRecId, aRecStrm );
270 xCurrContext->endRecord( nRecId );
272 mxStack->popContext();
274 else
276 // end context with incomplete record id, if the same id comes again
277 if( (mxStack->getCurrentRecId() == nRecId) && !mxStack->hasCurrentEndRecId() )
278 mxStack->popContext();
279 // try to start a new context
280 ContextHandlerRef xCurrContext = mxStack->getCurrentContext();
281 if( xCurrContext.is() )
283 aRecStrm.seekToStart();
284 xCurrContext = xCurrContext->createRecordContext( nRecId, aRecStrm );
286 // track all context identifiers on the stack (do not push simple records)
287 const RecordInfo* pStartRecInfo = getStartRecordInfo( nRecId );
288 if( pStartRecInfo )
289 mxStack->pushContext( *pStartRecInfo, xCurrContext );
290 // import the record
291 if( xCurrContext.is() )
293 // import the record
294 aRecStrm.seekToStart();
295 xCurrContext->startRecord( nRecId, aRecStrm );
296 // end simple records (context records are finished in ContextStack::popContext)
297 if( !pStartRecInfo )
298 xCurrContext->endRecord( nRecId );
302 // close remaining contexts (missing context end records or stream error)
303 while( !mxStack->empty() )
304 mxStack->popContext();
305 mxStack.reset();
307 // finish document
308 mxHandler->endDocument();
310 maSource = RecordInputSource();
313 const RecordInfo* RecordParser::getStartRecordInfo( sal_Int32 nRecId ) const
315 RecordInfoMap::const_iterator aIt = maStartMap.find( nRecId );
316 return (aIt == maStartMap.end()) ? nullptr : &aIt->second;
319 const RecordInfo* RecordParser::getEndRecordInfo( sal_Int32 nRecId ) const
321 RecordInfoMap::const_iterator aIt = maEndMap.find( nRecId );
322 return (aIt == maEndMap.end()) ? nullptr : &aIt->second;
325 } // namespace oox::core
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */