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 "oox/core/recordparser.hxx"
21 :
22 : #include <vector>
23 : #include <com/sun/star/lang/DisposedException.hpp>
24 : #include <com/sun/star/xml/sax/XLocator.hpp>
25 : #include <cppuhelper/implbase1.hxx>
26 : #include "oox/core/fragmenthandler.hxx"
27 :
28 : namespace oox {
29 : namespace core {
30 :
31 : // ============================================================================
32 :
33 : using namespace ::com::sun::star::io;
34 : using namespace ::com::sun::star::lang;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::xml::sax;
37 :
38 : using ::rtl::OUString;
39 :
40 : // ============================================================================
41 :
42 : namespace prv {
43 :
44 0 : class Locator : public ::cppu::WeakImplHelper1< XLocator >
45 : {
46 : public:
47 0 : inline explicit Locator( RecordParser* pParser ) : mpParser( pParser ) {}
48 :
49 : void dispose();
50 : void checkDispose() throw( RuntimeException );
51 :
52 : // com.sun.star.sax.XLocator interface
53 :
54 : virtual sal_Int32 SAL_CALL getColumnNumber() throw( RuntimeException );
55 : virtual sal_Int32 SAL_CALL getLineNumber() throw( RuntimeException );
56 : virtual OUString SAL_CALL getPublicId() throw( RuntimeException );
57 : virtual OUString SAL_CALL getSystemId() throw( RuntimeException );
58 :
59 : private:
60 : RecordParser* mpParser;
61 : };
62 :
63 : // ----------------------------------------------------------------------------
64 :
65 0 : void Locator::dispose()
66 : {
67 0 : mpParser = 0;
68 0 : }
69 :
70 0 : void Locator::checkDispose() throw( RuntimeException )
71 : {
72 0 : if( !mpParser )
73 0 : throw DisposedException();
74 0 : }
75 :
76 0 : sal_Int32 SAL_CALL Locator::getColumnNumber() throw( RuntimeException )
77 : {
78 0 : return -1;
79 : }
80 :
81 0 : sal_Int32 SAL_CALL Locator::getLineNumber() throw( RuntimeException )
82 : {
83 0 : return -1;
84 : }
85 :
86 0 : OUString SAL_CALL Locator::getPublicId() throw( RuntimeException )
87 : {
88 0 : checkDispose();
89 0 : return mpParser->getInputSource().maPublicId;
90 : }
91 :
92 0 : OUString SAL_CALL Locator::getSystemId() throw( RuntimeException )
93 : {
94 0 : checkDispose();
95 0 : return mpParser->getInputSource().maSystemId;
96 : }
97 :
98 : // ============================================================================
99 :
100 0 : class ContextStack
101 : {
102 : public:
103 : explicit ContextStack( FragmentHandlerRef xHandler );
104 :
105 0 : inline bool empty() const { return maStack.empty(); }
106 :
107 : sal_Int32 getCurrentRecId() const;
108 : bool hasCurrentEndRecId() const;
109 : ContextHandlerRef getCurrentContext() const;
110 :
111 : void pushContext( const RecordInfo& rRec, const ContextHandlerRef& rxContext );
112 : void popContext();
113 :
114 : private:
115 : typedef ::std::pair< RecordInfo, ContextHandlerRef > ContextInfo;
116 : typedef ::std::vector< ContextInfo > ContextInfoVec;
117 :
118 : FragmentHandlerRef mxHandler;
119 : ContextInfoVec maStack;
120 : };
121 :
122 : // ----------------------------------------------------------------------------
123 :
124 0 : ContextStack::ContextStack( FragmentHandlerRef xHandler ) :
125 0 : mxHandler( xHandler )
126 : {
127 0 : }
128 :
129 0 : sal_Int32 ContextStack::getCurrentRecId() const
130 : {
131 0 : return maStack.empty() ? -1 : maStack.back().first.mnStartRecId;
132 : }
133 :
134 0 : bool ContextStack::hasCurrentEndRecId() const
135 : {
136 0 : return !maStack.empty() && (maStack.back().first.mnEndRecId >= 0);
137 : }
138 :
139 0 : ContextHandlerRef ContextStack::getCurrentContext() const
140 : {
141 0 : if( !maStack.empty() )
142 0 : return maStack.back().second;
143 0 : return mxHandler.get();
144 : }
145 :
146 0 : void ContextStack::pushContext( const RecordInfo& rRecInfo, const ContextHandlerRef& rxContext )
147 : {
148 : OSL_ENSURE( (rRecInfo.mnEndRecId >= 0) || maStack.empty() || hasCurrentEndRecId(),
149 : "ContextStack::pushContext - nested incomplete context record identifiers" );
150 0 : maStack.push_back( ContextInfo( rRecInfo, rxContext ) );
151 0 : }
152 :
153 0 : void ContextStack::popContext()
154 : {
155 : OSL_ENSURE( !maStack.empty(), "ContextStack::popContext - no context on stack" );
156 0 : if( !maStack.empty() )
157 : {
158 0 : ContextInfo& rContextInfo = maStack.back();
159 0 : if( rContextInfo.second.is() )
160 0 : rContextInfo.second->endRecord( rContextInfo.first.mnStartRecId );
161 0 : maStack.pop_back();
162 : }
163 0 : }
164 :
165 : } // namespace prv
166 :
167 : // ============================================================================
168 :
169 : namespace {
170 :
171 : /** Reads a byte from the passed stream, returns true on success. */
172 0 : inline bool lclReadByte( sal_uInt8& ornByte, BinaryInputStream& rStrm )
173 : {
174 0 : return rStrm.readMemory( &ornByte, 1 ) == 1;
175 : }
176 :
177 : /** Reads a compressed signed 32-bit integer from the passed stream. */
178 0 : bool lclReadCompressedInt( sal_Int32& ornValue, BinaryInputStream& rStrm )
179 : {
180 0 : ornValue = 0;
181 : sal_uInt8 nByte;
182 0 : if( !lclReadByte( nByte, rStrm ) ) return false;
183 0 : ornValue = nByte & 0x7F;
184 0 : if( (nByte & 0x80) == 0 ) return true;
185 0 : if( !lclReadByte( nByte, rStrm ) ) return false;
186 0 : ornValue |= sal_Int32( nByte & 0x7F ) << 7;
187 0 : if( (nByte & 0x80) == 0 ) return true;
188 0 : if( !lclReadByte( nByte, rStrm ) ) return false;
189 0 : ornValue |= sal_Int32( nByte & 0x7F ) << 14;
190 0 : if( (nByte & 0x80) == 0 ) return true;
191 0 : if( !lclReadByte( nByte, rStrm ) ) return false;
192 0 : ornValue |= sal_Int32( nByte & 0x7F ) << 21;
193 0 : return true;
194 : }
195 :
196 0 : bool lclReadRecordHeader( sal_Int32& ornRecId, sal_Int32& ornRecSize, BinaryInputStream& rStrm )
197 : {
198 : return
199 0 : lclReadCompressedInt( ornRecId, rStrm ) && (ornRecId >= 0) &&
200 0 : lclReadCompressedInt( ornRecSize, rStrm ) && (ornRecSize >= 0);
201 : }
202 :
203 0 : bool lclReadNextRecord( sal_Int32& ornRecId, StreamDataSequence& orData, BinaryInputStream& rStrm )
204 : {
205 0 : sal_Int32 nRecSize = 0;
206 0 : bool bValid = lclReadRecordHeader( ornRecId, nRecSize, rStrm );
207 0 : if( bValid )
208 : {
209 0 : orData.realloc( nRecSize );
210 0 : bValid = (nRecSize == 0) || (rStrm.readData( orData, nRecSize ) == nRecSize);
211 : }
212 0 : return bValid;
213 : }
214 :
215 : } // namespace
216 :
217 : // ============================================================================
218 :
219 0 : RecordParser::RecordParser()
220 : {
221 0 : mxLocator.set( new prv::Locator( this ) );
222 0 : }
223 :
224 0 : RecordParser::~RecordParser()
225 : {
226 0 : if( mxLocator.is() )
227 0 : mxLocator->dispose();
228 0 : }
229 :
230 0 : void RecordParser::setFragmentHandler( const ::rtl::Reference< FragmentHandler >& rxHandler )
231 : {
232 0 : mxHandler = rxHandler;
233 :
234 : // build record infos
235 0 : maStartMap.clear();
236 0 : maEndMap.clear();
237 0 : const RecordInfo* pRecs = mxHandler.is() ? mxHandler->getRecordInfos() : 0;
238 : OSL_ENSURE( pRecs, "RecordInfoProvider::RecordInfoProvider - missing record list" );
239 0 : for( ; pRecs && pRecs->mnStartRecId >= 0; ++pRecs )
240 : {
241 0 : maStartMap[ pRecs->mnStartRecId ] = *pRecs;
242 0 : if( pRecs->mnEndRecId >= 0 )
243 0 : maEndMap[ pRecs->mnEndRecId ] = *pRecs;
244 : }
245 0 : }
246 :
247 0 : void RecordParser::parseStream( const RecordInputSource& rInputSource ) throw( SAXException, IOException, RuntimeException )
248 : {
249 0 : maSource = rInputSource;
250 :
251 0 : if( !maSource.mxInStream || maSource.mxInStream->isEof() )
252 0 : throw IOException();
253 0 : if( !mxHandler.is() )
254 0 : throw SAXException();
255 :
256 : // start the document
257 0 : Reference< XLocator > xLocator( mxLocator.get() );
258 0 : mxHandler->setDocumentLocator( xLocator );
259 0 : mxHandler->startDocument();
260 :
261 : // parse the stream
262 0 : mxStack.reset( new prv::ContextStack( mxHandler ) );
263 0 : sal_Int32 nRecId = 0;
264 0 : StreamDataSequence aRecData;
265 0 : while( lclReadNextRecord( nRecId, aRecData, *maSource.mxInStream ) )
266 : {
267 : // create record stream object from imported record data
268 0 : SequenceInputStream aRecStrm( aRecData );
269 : // try to leave a context, there may be other incomplete contexts on the stack
270 0 : if( const RecordInfo* pEndRecInfo = getEndRecordInfo( nRecId ) )
271 : {
272 : // finalize contexts without record identifier for context end
273 0 : while( !mxStack->empty() && !mxStack->hasCurrentEndRecId() )
274 0 : mxStack->popContext();
275 : // finalize the current context and pop context info from stack
276 : OSL_ENSURE( mxStack->getCurrentRecId() == pEndRecInfo->mnStartRecId, "RecordParser::parseStream - context records mismatch" );
277 : (void)pEndRecInfo; // suppress compiler warning for unused variable
278 0 : ContextHandlerRef xCurrContext = mxStack->getCurrentContext();
279 0 : if( xCurrContext.is() )
280 : {
281 : // context end record may contain some data, handle it as simple record
282 0 : aRecStrm.seekToStart();
283 0 : xCurrContext->startRecord( nRecId, aRecStrm );
284 0 : xCurrContext->endRecord( nRecId );
285 : }
286 0 : mxStack->popContext();
287 : }
288 : else
289 : {
290 : // end context with incomplete record id, if the same id comes again
291 0 : if( (mxStack->getCurrentRecId() == nRecId) && !mxStack->hasCurrentEndRecId() )
292 0 : mxStack->popContext();
293 : // try to start a new context
294 0 : ContextHandlerRef xCurrContext = mxStack->getCurrentContext();
295 0 : if( xCurrContext.is() )
296 : {
297 0 : aRecStrm.seekToStart();
298 0 : xCurrContext = xCurrContext->createRecordContext( nRecId, aRecStrm );
299 : }
300 : // track all context identifiers on the stack (do not push simple records)
301 0 : const RecordInfo* pStartRecInfo = getStartRecordInfo( nRecId );
302 0 : if( pStartRecInfo )
303 0 : mxStack->pushContext( *pStartRecInfo, xCurrContext );
304 : // import the record
305 0 : if( xCurrContext.is() )
306 : {
307 : // import the record
308 0 : aRecStrm.seekToStart();
309 0 : xCurrContext->startRecord( nRecId, aRecStrm );
310 : // end simple records (context records are finished in ContextStack::popContext)
311 0 : if( !pStartRecInfo )
312 0 : xCurrContext->endRecord( nRecId );
313 0 : }
314 : }
315 0 : }
316 : // close remaining contexts (missing context end records or stream error)
317 0 : while( !mxStack->empty() )
318 0 : mxStack->popContext();
319 0 : mxStack.reset();
320 :
321 : // finish document
322 0 : mxHandler->endDocument();
323 :
324 0 : maSource = RecordInputSource();
325 0 : }
326 :
327 0 : const RecordInfo* RecordParser::getStartRecordInfo( sal_Int32 nRecId ) const
328 : {
329 0 : RecordInfoMap::const_iterator aIt = maStartMap.find( nRecId );
330 0 : return (aIt == maStartMap.end()) ? 0 : &aIt->second;
331 : }
332 :
333 0 : const RecordInfo* RecordParser::getEndRecordInfo( sal_Int32 nRecId ) const
334 : {
335 0 : RecordInfoMap::const_iterator aIt = maEndMap.find( nRecId );
336 0 : return (aIt == maEndMap.end()) ? 0 : &aIt->second;
337 : }
338 :
339 : // ============================================================================
340 :
341 : } // namespace core
342 : } // namespace oox
343 :
344 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|