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