1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/xml/sax/XLocator.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <osl/diagnose.h>
27 #include <oox/core/fragmenthandler.hxx>
32 using namespace ::com::sun::star::io
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::xml::sax
;
39 class Locator
: public ::cppu::WeakImplHelper
< XLocator
>
42 explicit Locator( RecordParser
* pParser
) : mpParser( pParser
) {}
45 /// @throws css::uno::RuntimeException
48 // com.sun.star.sax.XLocator interface
50 virtual sal_Int32 SAL_CALL
getColumnNumber() override
;
51 virtual sal_Int32 SAL_CALL
getLineNumber() override
;
52 virtual OUString SAL_CALL
getPublicId() override
;
53 virtual OUString SAL_CALL
getSystemId() override
;
56 RecordParser
* mpParser
;
59 void Locator::dispose()
64 void Locator::checkDispose()
67 throw DisposedException();
70 sal_Int32 SAL_CALL
Locator::getColumnNumber()
75 sal_Int32 SAL_CALL
Locator::getLineNumber()
80 OUString SAL_CALL
Locator::getPublicId()
86 OUString SAL_CALL
Locator::getSystemId()
89 return mpParser
->getInputSource().maSystemId
;
95 explicit ContextStack( FragmentHandlerRef
const & xHandler
);
97 bool empty() const { return maStack
.empty(); }
99 sal_Int32
getCurrentRecId() const;
100 bool hasCurrentEndRecId() const;
101 ContextHandlerRef
getCurrentContext() const;
103 void pushContext( const RecordInfo
& rRec
, const ContextHandlerRef
& rxContext
);
107 typedef ::std::pair
< RecordInfo
, ContextHandlerRef
> ContextInfo
;
108 typedef ::std::vector
< ContextInfo
> ContextInfoVec
;
110 FragmentHandlerRef
const mxHandler
;
111 ContextInfoVec maStack
;
114 ContextStack::ContextStack( FragmentHandlerRef
const & xHandler
) :
115 mxHandler( xHandler
)
119 sal_Int32
ContextStack::getCurrentRecId() const
121 return maStack
.empty() ? -1 : maStack
.back().first
.mnStartRecId
;
124 bool ContextStack::hasCurrentEndRecId() const
126 return !maStack
.empty() && (maStack
.back().first
.mnEndRecId
>= 0);
129 ContextHandlerRef
ContextStack::getCurrentContext() const
131 if( !maStack
.empty() )
132 return maStack
.back().second
;
133 return mxHandler
.get();
136 void ContextStack::pushContext( const RecordInfo
& rRecInfo
, const ContextHandlerRef
& rxContext
)
138 OSL_ENSURE( (rRecInfo
.mnEndRecId
>= 0) || maStack
.empty() || hasCurrentEndRecId(),
139 "ContextStack::pushContext - nested incomplete context record identifiers" );
140 maStack
.emplace_back( rRecInfo
, rxContext
);
143 void ContextStack::popContext()
145 OSL_ENSURE( !maStack
.empty(), "ContextStack::popContext - no context on stack" );
146 if( !maStack
.empty() )
148 ContextInfo
& rContextInfo
= maStack
.back();
149 if( rContextInfo
.second
.is() )
150 rContextInfo
.second
->endRecord( rContextInfo
.first
.mnStartRecId
);
159 /** Reads a byte from the passed stream, returns true on success. */
160 bool lclReadByte( sal_uInt8
& ornByte
, BinaryInputStream
& rStrm
)
162 return rStrm
.readMemory( &ornByte
, 1 ) == 1;
165 /** Reads a compressed signed 32-bit integer from the passed stream. */
166 bool lclReadCompressedInt( sal_Int32
& ornValue
, BinaryInputStream
& rStrm
)
170 if( !lclReadByte( nByte
, rStrm
) ) return false;
171 ornValue
= nByte
& 0x7F;
172 if( (nByte
& 0x80) == 0 ) return true;
173 if( !lclReadByte( nByte
, rStrm
) ) return false;
174 ornValue
|= sal_Int32( nByte
& 0x7F ) << 7;
175 if( (nByte
& 0x80) == 0 ) return true;
176 if( !lclReadByte( nByte
, rStrm
) ) return false;
177 ornValue
|= sal_Int32( nByte
& 0x7F ) << 14;
178 if( (nByte
& 0x80) == 0 ) return true;
179 if( !lclReadByte( nByte
, rStrm
) ) return false;
180 ornValue
|= sal_Int32( nByte
& 0x7F ) << 21;
184 bool lclReadRecordHeader( sal_Int32
& ornRecId
, sal_Int32
& ornRecSize
, BinaryInputStream
& rStrm
)
187 lclReadCompressedInt( ornRecId
, rStrm
) && (ornRecId
>= 0) &&
188 lclReadCompressedInt( ornRecSize
, rStrm
) && (ornRecSize
>= 0);
191 bool lclReadNextRecord( sal_Int32
& ornRecId
, StreamDataSequence
& orData
, BinaryInputStream
& rStrm
)
193 sal_Int32 nRecSize
= 0;
194 bool bValid
= lclReadRecordHeader( ornRecId
, nRecSize
, rStrm
);
197 orData
.realloc( nRecSize
);
198 bValid
= (nRecSize
== 0) || (rStrm
.readData( orData
, nRecSize
) == nRecSize
);
205 RecordParser::RecordParser()
207 mxLocator
.set( new prv::Locator( this ) );
210 RecordParser::~RecordParser()
213 mxLocator
->dispose();
216 void RecordParser::setFragmentHandler( const ::rtl::Reference
< FragmentHandler
>& rxHandler
)
218 mxHandler
= rxHandler
;
220 // build record infos
223 const RecordInfo
* pRecs
= mxHandler
.is() ? mxHandler
->getRecordInfos() : nullptr;
224 OSL_ENSURE( pRecs
, "RecordInfoProvider::RecordInfoProvider - missing record list" );
225 for( ; pRecs
&& pRecs
->mnStartRecId
>= 0; ++pRecs
)
227 maStartMap
[ pRecs
->mnStartRecId
] = *pRecs
;
228 if( pRecs
->mnEndRecId
>= 0 )
229 maEndMap
[ pRecs
->mnEndRecId
] = *pRecs
;
233 void RecordParser::parseStream( const RecordInputSource
& rInputSource
)
235 maSource
= rInputSource
;
237 if( !maSource
.mxInStream
|| maSource
.mxInStream
->isEof() )
239 if( !mxHandler
.is() )
240 throw SAXException();
242 // start the document
243 Reference
< XLocator
> xLocator( mxLocator
.get() );
244 mxHandler
->setDocumentLocator( xLocator
);
245 mxHandler
->startDocument();
248 mxStack
.reset( new prv::ContextStack( mxHandler
) );
249 sal_Int32 nRecId
= 0;
250 StreamDataSequence aRecData
;
251 while( lclReadNextRecord( nRecId
, aRecData
, *maSource
.mxInStream
) )
253 // create record stream object from imported record data
254 SequenceInputStream
aRecStrm( aRecData
);
255 // try to leave a context, there may be other incomplete contexts on the stack
256 if( const RecordInfo
* pEndRecInfo
= getEndRecordInfo( nRecId
) )
258 // finalize contexts without record identifier for context end
259 while( !mxStack
->empty() && !mxStack
->hasCurrentEndRecId() )
260 mxStack
->popContext();
261 // finalize the current context and pop context info from stack
262 OSL_ENSURE( mxStack
->getCurrentRecId() == pEndRecInfo
->mnStartRecId
, "RecordParser::parseStream - context records mismatch" );
263 ContextHandlerRef xCurrContext
= mxStack
->getCurrentContext();
264 if( xCurrContext
.is() )
266 // context end record may contain some data, handle it as simple record
267 aRecStrm
.seekToStart();
268 xCurrContext
->startRecord( nRecId
, aRecStrm
);
269 xCurrContext
->endRecord( nRecId
);
271 mxStack
->popContext();
275 // end context with incomplete record id, if the same id comes again
276 if( (mxStack
->getCurrentRecId() == nRecId
) && !mxStack
->hasCurrentEndRecId() )
277 mxStack
->popContext();
278 // try to start a new context
279 ContextHandlerRef xCurrContext
= mxStack
->getCurrentContext();
280 if( xCurrContext
.is() )
282 aRecStrm
.seekToStart();
283 xCurrContext
= xCurrContext
->createRecordContext( nRecId
, aRecStrm
);
285 // track all context identifiers on the stack (do not push simple records)
286 const RecordInfo
* pStartRecInfo
= getStartRecordInfo( nRecId
);
288 mxStack
->pushContext( *pStartRecInfo
, xCurrContext
);
290 if( xCurrContext
.is() )
293 aRecStrm
.seekToStart();
294 xCurrContext
->startRecord( nRecId
, aRecStrm
);
295 // end simple records (context records are finished in ContextStack::popContext)
297 xCurrContext
->endRecord( nRecId
);
301 // close remaining contexts (missing context end records or stream error)
302 while( !mxStack
->empty() )
303 mxStack
->popContext();
307 mxHandler
->endDocument();
309 maSource
= RecordInputSource();
312 const RecordInfo
* RecordParser::getStartRecordInfo( sal_Int32 nRecId
) const
314 RecordInfoMap::const_iterator aIt
= maStartMap
.find( nRecId
);
315 return (aIt
== maStartMap
.end()) ? nullptr : &aIt
->second
;
318 const RecordInfo
* RecordParser::getEndRecordInfo( sal_Int32 nRecId
) const
320 RecordInfoMap::const_iterator aIt
= maEndMap
.find( nRecId
);
321 return (aIt
== maEndMap
.end()) ? nullptr : &aIt
->second
;
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */