LCOV - code coverage report
Current view: top level - libreoffice/oox/source/core - contexthandler2.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 84 115 73.0 %
Date: 2012-12-27 Functions: 25 38 65.8 %
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/contexthandler2.hxx"
      21             : #include <rtl/ustrbuf.hxx>
      22             : 
      23             : namespace oox {
      24             : namespace core {
      25             : 
      26             : // ============================================================================
      27             : 
      28             : using namespace ::com::sun::star::uno;
      29             : using namespace ::com::sun::star::xml::sax;
      30             : 
      31             : using ::rtl::OUString;
      32             : using ::rtl::OUStringBuffer;
      33             : 
      34             : // ============================================================================
      35             : 
      36             : /** Information about a processed element. */
      37        4286 : struct ElementInfo
      38             : {
      39             :     OUStringBuffer      maChars;            /// Collected element characters.
      40             :     sal_Int32           mnElement;          /// The element identifier.
      41             :     bool                mbTrimSpaces;       /// True = trims leading/trailing spaces from text data.
      42             : 
      43        3254 :     inline explicit     ElementInfo() : mnElement( XML_TOKEN_INVALID ), mbTrimSpaces( false ) {}
      44             :                         ElementInfo( sal_Int32 nElement ) : mnElement( nElement ), mbTrimSpaces(false) {}
      45             : };
      46             : 
      47             : // ============================================================================
      48             : 
      49         109 : ContextHandler2Helper::ContextHandler2Helper( bool bEnableTrimSpace ) :
      50         109 :     mxContextStack( new ContextStack ),
      51             :     mnRootStackSize( 0 ),
      52         218 :     mbEnableTrimSpace( bEnableTrimSpace )
      53             : {
      54         109 :     pushElementInfo( XML_ROOT_CONTEXT );
      55         109 : }
      56             : 
      57         662 : ContextHandler2Helper::ContextHandler2Helper( const ContextHandler2Helper& rParent ) :
      58             :     mxContextStack( rParent.mxContextStack ),
      59         662 :     mnRootStackSize( rParent.mxContextStack->size() ),
      60        1324 :     mbEnableTrimSpace( rParent.mbEnableTrimSpace )
      61             : {
      62         662 : }
      63             : 
      64         771 : ContextHandler2Helper::~ContextHandler2Helper()
      65             : {
      66         771 : }
      67             : 
      68           0 : sal_Int32 ContextHandler2Helper::getCurrentElementWithMce() const
      69             : {
      70           0 :     return mxContextStack->empty() ? XML_ROOT_CONTEXT : mxContextStack->back().mnElement;
      71             : }
      72             : 
      73        7123 : sal_Int32 ContextHandler2Helper::getCurrentElement() const
      74             : {
      75       21411 :     for ( ContextStack::reverse_iterator It = mxContextStack->rbegin();
      76       14274 :           It != mxContextStack->rend(); ++It )
      77        7137 :         if( getNamespace( It->mnElement ) != NMSP_mce )
      78        7123 :             return It->mnElement;
      79           0 :     return XML_ROOT_CONTEXT;
      80             : }
      81             : 
      82          89 : sal_Int32 ContextHandler2Helper::getParentElement( sal_Int32 nCountBack ) const
      83             : {
      84          89 :     if( (nCountBack < 0) || (mxContextStack->size() < static_cast< size_t >( nCountBack )) )
      85           0 :         return XML_TOKEN_INVALID;
      86          89 :     return (mxContextStack->size() == static_cast< size_t >( nCountBack )) ?
      87          89 :         XML_ROOT_CONTEXT : (*mxContextStack)[ mxContextStack->size() - nCountBack - 1 ].mnElement;
      88             : }
      89             : 
      90         194 : bool ContextHandler2Helper::isRootElement() const
      91             : {
      92         194 :     return mxContextStack->size() == mnRootStackSize + 1;
      93             : }
      94             : 
      95        4283 : Reference< XFastContextHandler > ContextHandler2Helper::implCreateChildContext(
      96             :         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
      97             : {
      98             :     // #i76091# process collected characters (calls onCharacters() if needed)
      99        4283 :     processCollectedChars();
     100        4283 :     ContextHandlerRef xContext = onCreateContext( nElement, AttributeList( rxAttribs ) );
     101        4283 :     return Reference< XFastContextHandler >( xContext.get() );
     102             : }
     103             : 
     104        3145 : void ContextHandler2Helper::implStartElement( sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
     105             : {
     106        3145 :     AttributeList aAttribs( rxAttribs );
     107        3145 :     pushElementInfo( nElement ).mbTrimSpaces = aAttribs.getToken( XML_TOKEN( space ), XML_TOKEN_INVALID ) != XML_preserve;
     108        3145 :     onStartElement( aAttribs );
     109        3145 : }
     110             : 
     111        1587 : void ContextHandler2Helper::implCharacters( const OUString& rChars )
     112             : {
     113             :     // #i76091# collect characters until new element starts or this element ends
     114        1587 :     if( !mxContextStack->empty() )
     115        1587 :         mxContextStack->back().maChars.append(rChars);
     116        1587 : }
     117             : 
     118        3145 : void ContextHandler2Helper::implEndElement( sal_Int32 nElement )
     119             : {
     120             :     (void)nElement;     // prevent "unused parameter" warning in product build
     121             :     OSL_ENSURE( getCurrentElementWithMce() == nElement, "ContextHandler2Helper::implEndElement - context stack broken" );
     122        3145 :     if( !mxContextStack->empty() )
     123             :     {
     124             :         // #i76091# process collected characters (calls onCharacters() if needed)
     125        3145 :         processCollectedChars();
     126        3145 :         onEndElement();
     127        3145 :         popElementInfo();
     128             :     }
     129        3145 : }
     130             : 
     131           0 : ContextHandlerRef ContextHandler2Helper::implCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
     132             : {
     133           0 :     return onCreateRecordContext( nRecId, rStrm );
     134             : }
     135             : 
     136           0 : void ContextHandler2Helper::implStartRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
     137             : {
     138           0 :     pushElementInfo( nRecId );
     139           0 :     onStartRecord( rStrm );
     140           0 : }
     141             : 
     142           0 : void ContextHandler2Helper::implEndRecord( sal_Int32 nRecId )
     143             : {
     144             :     (void)nRecId;   // prevent "unused parameter" warning in product build
     145             :     OSL_ENSURE( getCurrentElementWithMce() == nRecId, "ContextHandler2Helper::implEndRecord - context stack broken" );
     146           0 :     if( !mxContextStack->empty() )
     147             :     {
     148           0 :         onEndRecord();
     149           0 :         popElementInfo();
     150             :     }
     151           0 : }
     152             : 
     153        3254 : ElementInfo& ContextHandler2Helper::pushElementInfo( sal_Int32 nElement )
     154             : {
     155        3254 :     mxContextStack->resize( mxContextStack->size() + 1 );
     156        3254 :     ElementInfo& rInfo = mxContextStack->back();
     157        3254 :     rInfo.mnElement = nElement;
     158        3254 :     return rInfo;
     159             : }
     160             : 
     161        3145 : void ContextHandler2Helper::popElementInfo()
     162             : {
     163             :     OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::popElementInfo - context stack broken" );
     164        3145 :     if( !mxContextStack->empty() )
     165        3145 :         mxContextStack->pop_back();
     166        3145 : }
     167             : 
     168        7428 : void ContextHandler2Helper::processCollectedChars()
     169             : {
     170             :     OSL_ENSURE( !mxContextStack->empty(), "ContextHandler2Helper::processCollectedChars - no context info" );
     171        7428 :     ElementInfo& rInfo = mxContextStack->back();
     172        7428 :     if( rInfo.maChars.getLength() > 0 )
     173             :     {
     174        1139 :         OUString aChars = rInfo.maChars.makeStringAndClear();
     175        1139 :         if( mbEnableTrimSpace && rInfo.mbTrimSpaces )
     176         867 :             aChars = aChars.trim();
     177        1139 :         if( !aChars.isEmpty() )
     178        1138 :             onCharacters( aChars );
     179             :     }
     180        7428 : }
     181             : 
     182             : // ============================================================================
     183             : 
     184         596 : ContextHandler2::ContextHandler2( ContextHandler2Helper& rParent ) :
     185         596 :     ContextHandler( dynamic_cast< ContextHandler& >( rParent ) ),
     186         596 :     ContextHandler2Helper( rParent )
     187             : {
     188         596 : }
     189             : 
     190         640 : ContextHandler2::~ContextHandler2()
     191             : {
     192         640 : }
     193             : 
     194             : // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
     195             : 
     196        2597 : Reference< XFastContextHandler > SAL_CALL ContextHandler2::createFastChildContext(
     197             :         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException )
     198             : {
     199        2597 :     return implCreateChildContext( nElement, rxAttribs );
     200             : }
     201             : 
     202        2544 : void SAL_CALL ContextHandler2::startFastElement(
     203             :         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw( SAXException, RuntimeException )
     204             : {
     205        2544 :     implStartElement( nElement, rxAttribs );
     206        2544 : }
     207             : 
     208        1272 : void SAL_CALL ContextHandler2::characters( const OUString& rChars ) throw( SAXException, RuntimeException )
     209             : {
     210        1272 :     implCharacters( rChars );
     211        1272 : }
     212             : 
     213        2544 : void SAL_CALL ContextHandler2::endFastElement( sal_Int32 nElement ) throw( SAXException, RuntimeException )
     214             : {
     215        2544 :     implEndElement( nElement );
     216        2544 : }
     217             : 
     218             : // oox.core.RecordContext interface -------------------------------------------
     219             : 
     220           0 : ContextHandlerRef ContextHandler2::createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
     221             : {
     222           0 :     return implCreateRecordContext( nRecId, rStrm );
     223             : }
     224             : 
     225           0 : void ContextHandler2::startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
     226             : {
     227           0 :     implStartRecord( nRecId, rStrm );
     228           0 : }
     229             : 
     230           0 : void ContextHandler2::endRecord( sal_Int32 nRecId )
     231             : {
     232           0 :     implEndRecord( nRecId );
     233           0 : }
     234             : 
     235             : // oox.core.ContextHandler2Helper interface -----------------------------------
     236             : 
     237           0 : ContextHandlerRef ContextHandler2::onCreateContext( sal_Int32, const AttributeList& )
     238             : {
     239           0 :     return 0;
     240             : }
     241             : 
     242        2065 : void ContextHandler2::onStartElement( const AttributeList& )
     243             : {
     244        2065 : }
     245             : 
     246         102 : void ContextHandler2::onCharacters( const OUString& )
     247             : {
     248         102 : }
     249             : 
     250         712 : void ContextHandler2::onEndElement()
     251             : {
     252         712 : }
     253             : 
     254           0 : ContextHandlerRef ContextHandler2::onCreateRecordContext( sal_Int32, SequenceInputStream& )
     255             : {
     256           0 :     return 0;
     257             : }
     258             : 
     259           0 : void ContextHandler2::onStartRecord( SequenceInputStream& )
     260             : {
     261           0 : }
     262             : 
     263           0 : void ContextHandler2::onEndRecord()
     264             : {
     265           0 : }
     266             : 
     267             : // ============================================================================
     268             : 
     269             : } // namespace core
     270             : } // namespace oox
     271             : 
     272             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10