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

Generated by: LCOV version 1.10