LCOV - code coverage report
Current view: top level - oox/source/core - recordparser.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 112 136 82.4 %
Date: 2015-06-13 12:38:46 Functions: 22 28 78.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11