LCOV - code coverage report
Current view: top level - xmloff/source/text - txtlists.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 129 208 62.0 %
Date: 2014-04-11 Functions: 16 24 66.7 %
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             : 
      21             : #include <txtlists.hxx>
      22             : 
      23             : #include <tools/debug.hxx>
      24             : #include <tools/date.hxx>
      25             : #include <tools/time.hxx>
      26             : 
      27             : #include <xmloff/txtimp.hxx>
      28             : #include <xmloff/xmlimp.hxx>
      29             : #include <xmloff/xmlnumi.hxx>
      30             : 
      31             : #include <com/sun/star/style/XStyle.hpp>
      32             : #include <com/sun/star/beans/XPropertySet.hpp>
      33             : #include "XMLTextListItemContext.hxx"
      34             : #include "XMLTextListBlockContext.hxx"
      35             : #include "txtparai.hxx"
      36             : 
      37             : 
      38             : using namespace ::com::sun::star;
      39             : 
      40             : 
      41        1596 : XMLTextListsHelper::XMLTextListsHelper()
      42             :    :  mpProcessedLists( 0 ),
      43             :       msLastProcessedListId(),
      44             :       msListStyleOfLastProcessedList(),
      45             :       // Inconsistent behavior regarding lists (#i92811#)
      46             :       mpMapListIdToListStyleDefaultListId( 0 ),
      47             :       mpContinuingLists( 0 ),
      48        1596 :       mpListStack( 0 )
      49             : {
      50        1596 : }
      51             : 
      52        3192 : XMLTextListsHelper::~XMLTextListsHelper()
      53             : {
      54        1596 :     if ( mpProcessedLists )
      55             :     {
      56         495 :         mpProcessedLists->clear();
      57         495 :         delete mpProcessedLists;
      58             :     }
      59             :     // Inconsistent behavior regarding lists (#i92811#)#
      60        1596 :     if ( mpMapListIdToListStyleDefaultListId )
      61             :     {
      62          10 :         mpMapListIdToListStyleDefaultListId->clear();
      63          10 :         delete mpMapListIdToListStyleDefaultListId;
      64             :     }
      65        1596 :     if ( mpContinuingLists )
      66             :     {
      67           0 :         mpContinuingLists->clear();
      68           0 :         delete mpContinuingLists;
      69             :     }
      70        1596 :     if ( mpListStack )
      71             :     {
      72           0 :         mpListStack->clear();
      73           0 :         delete mpListStack;
      74             :     }
      75        1596 : }
      76             : 
      77         438 : void XMLTextListsHelper::PushListContext(
      78             :     XMLTextListBlockContext *i_pListBlock)
      79             : {
      80             : //    fprintf(stderr, "PushListContext\n");
      81             :     mListStack.push(::boost::make_tuple(i_pListBlock,
      82             :         static_cast<XMLTextListItemContext*>(0),
      83         438 :         static_cast<XMLNumberedParaContext*>(0)));
      84         438 : }
      85             : 
      86           0 : void XMLTextListsHelper::PushListContext(
      87             :     XMLNumberedParaContext *i_pNumberedParagraph)
      88             : {
      89             : //    fprintf(stderr, "PushListContext(NP)\n");
      90             :     mListStack.push(::boost::make_tuple(
      91             :         static_cast<XMLTextListBlockContext*>(0),
      92           0 :         static_cast<XMLTextListItemContext*>(0), i_pNumberedParagraph));
      93           0 : }
      94             : 
      95         436 : void XMLTextListsHelper::PopListContext()
      96             : {
      97             :     OSL_ENSURE(mListStack.size(),
      98             :         "internal error: PopListContext: mListStack empty");
      99             : //    fprintf(stderr, "PopListContext\n");
     100         436 :     if ( !mListStack.empty())
     101         436 :         mListStack.pop();
     102         436 : }
     103             : 
     104        3792 : void XMLTextListsHelper::ListContextTop(
     105             :     XMLTextListBlockContext*& o_pListBlockContext,
     106             :     XMLTextListItemContext*& o_pListItemContext,
     107             :     XMLNumberedParaContext*& o_pNumberedParagraphContext )
     108             : {
     109        3792 :     if ( !mListStack.empty() ) {
     110             :         o_pListBlockContext =
     111         580 :             static_cast<XMLTextListBlockContext*>(&mListStack.top().get<0>());
     112             :         o_pListItemContext  =
     113         580 :             static_cast<XMLTextListItemContext *>(&mListStack.top().get<1>());
     114             :         o_pNumberedParagraphContext =
     115         580 :             static_cast<XMLNumberedParaContext *>(&mListStack.top().get<2>());
     116             :     }
     117        3792 : }
     118             : 
     119         390 : void XMLTextListsHelper::SetListItem( XMLTextListItemContext *i_pListItem )
     120             : {
     121             :     // may be cleared by ListBlockContext for upper list...
     122             :     if (i_pListItem) {
     123             :         OSL_ENSURE(mListStack.size(),
     124             :             "internal error: SetListItem: mListStack empty");
     125             :         OSL_ENSURE(mListStack.top().get<0>(),
     126             :             "internal error: SetListItem: mListStack has no ListBlock");
     127             :         OSL_ENSURE(!mListStack.top().get<1>(),
     128             :             "error: SetListItem: list item already exists");
     129             :     }
     130         390 :     if ( !mListStack.empty() ) {
     131         368 :         mListStack.top().get<1>() = i_pListItem;
     132             :     }
     133         390 : }
     134             : 
     135             : // Handling for parameter <sListStyleDefaultListId> (#i92811#)
     136         527 : void XMLTextListsHelper::KeepListAsProcessed( const OUString& sListId,
     137             :                                               const OUString& sListStyleName,
     138             :                                               const OUString& sContinueListId,
     139             :                                               const OUString& sListStyleDefaultListId )
     140             : {
     141         527 :     if ( IsListProcessed( sListId ) )
     142             :     {
     143             :         DBG_ASSERT( false,
     144             :                     "<XMLTextListsHelper::KeepListAsProcessed(..)> - list id already added" );
     145         527 :         return;
     146             :     }
     147             : 
     148         527 :     if ( mpProcessedLists == 0 )
     149             :     {
     150         495 :         mpProcessedLists = new tMapForLists();
     151             :     }
     152             : 
     153             :     ::std::pair< OUString, OUString >
     154         527 :                                 aListData( sListStyleName, sContinueListId );
     155         527 :     (*mpProcessedLists)[ sListId ] = aListData;
     156             : 
     157         527 :     msLastProcessedListId = sListId;
     158         527 :     msListStyleOfLastProcessedList = sListStyleName;
     159             : 
     160             :     // Inconsistent behavior regarding lists (#i92811#)
     161         527 :     if ( !sListStyleDefaultListId.isEmpty())
     162             :     {
     163          26 :         if ( mpMapListIdToListStyleDefaultListId == 0 )
     164             :         {
     165          10 :             mpMapListIdToListStyleDefaultListId = new tMapForLists();
     166             :         }
     167             : 
     168          26 :         if ( mpMapListIdToListStyleDefaultListId->find( sListStyleName ) ==
     169          26 :                                 mpMapListIdToListStyleDefaultListId->end() )
     170             :         {
     171             :             ::std::pair< OUString, OUString >
     172          19 :                                 aListIdMapData( sListId, sListStyleDefaultListId );
     173          19 :             (*mpMapListIdToListStyleDefaultListId)[ sListStyleName ] =
     174          19 :                                                                 aListIdMapData;
     175             :         }
     176         527 :     }
     177             : }
     178             : 
     179         565 : sal_Bool XMLTextListsHelper::IsListProcessed( const OUString& sListId ) const
     180             : {
     181         565 :     if ( mpProcessedLists == 0 )
     182             :     {
     183         497 :         return sal_False;
     184             :     }
     185             : 
     186          68 :     return mpProcessedLists->find( sListId ) != mpProcessedLists->end();
     187             : }
     188             : 
     189          97 : OUString XMLTextListsHelper::GetListStyleOfProcessedList(
     190             :                                             const OUString& sListId ) const
     191             : {
     192          97 :     if ( mpProcessedLists != 0 )
     193             :     {
     194          97 :         tMapForLists::const_iterator aIter = mpProcessedLists->find( sListId );
     195          97 :         if ( aIter != mpProcessedLists->end() )
     196             :         {
     197          97 :             return (*aIter).second.first;
     198             :         }
     199             :     }
     200             : 
     201           0 :     return OUString();
     202             : }
     203             : 
     204           5 : OUString XMLTextListsHelper::GetContinueListIdOfProcessedList(
     205             :                                             const OUString& sListId ) const
     206             : {
     207           5 :     if ( mpProcessedLists != 0 )
     208             :     {
     209           5 :         tMapForLists::const_iterator aIter = mpProcessedLists->find( sListId );
     210           5 :         if ( aIter != mpProcessedLists->end() )
     211             :         {
     212           5 :             return (*aIter).second.second;
     213             :         }
     214             :     }
     215             : 
     216           0 :     return OUString();
     217             : }
     218             : 
     219           3 : const OUString& XMLTextListsHelper::GetLastProcessedListId() const
     220             : {
     221           3 :     return msLastProcessedListId;
     222             : }
     223             : 
     224           3 : const OUString& XMLTextListsHelper::GetListStyleOfLastProcessedList() const
     225             : {
     226           3 :     return msListStyleOfLastProcessedList;
     227             : }
     228             : 
     229          21 : OUString XMLTextListsHelper::GenerateNewListId() const
     230             : {
     231          21 :     static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
     232          21 :     OUString sTmpStr( "list" );
     233             : 
     234          21 :     if (bHack)
     235             :     {
     236             :         static sal_Int64 nIdCounter = SAL_CONST_INT64(5000000000);
     237           0 :         sTmpStr += OUString::number(nIdCounter++);
     238             :     }
     239             :     else
     240             :     {
     241             :         // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
     242          21 :         sal_Int64 n = Time( Time::SYSTEM ).GetTime();
     243          21 :         n += Date( Date::SYSTEM ).GetDate();
     244          21 :         n += rand();
     245             :         // Value of xml:id in element <text:list> has to be a valid ID type (#i92478#)
     246          21 :         sTmpStr += OUString::number( n );
     247             :     }
     248             : 
     249          21 :     OUString sNewListId( sTmpStr );
     250          21 :     if ( mpProcessedLists != 0 )
     251             :     {
     252          19 :         long nHitCount = 0;
     253          38 :         while ( mpProcessedLists->find( sNewListId ) != mpProcessedLists->end() )
     254             :         {
     255           0 :             ++nHitCount;
     256           0 :             sNewListId = sTmpStr;
     257           0 :             sNewListId += OUString::number( nHitCount );
     258             :         }
     259             :     }
     260             : 
     261          21 :     return sNewListId;
     262             : }
     263             : 
     264             : // Provide list id for a certain list block for import (#i92811#)
     265         108 : OUString XMLTextListsHelper::GetListIdForListBlock( XMLTextListBlockContext& rListBlock )
     266             : {
     267         108 :     OUString sListBlockListId( rListBlock.GetContinueListId() );
     268         108 :     if ( sListBlockListId.isEmpty() )
     269             :     {
     270          89 :         sListBlockListId = rListBlock.GetListId();
     271             :     }
     272             : 
     273         108 :     if ( mpMapListIdToListStyleDefaultListId != 0 )
     274             :     {
     275          97 :         if ( !sListBlockListId.isEmpty() )
     276             :         {
     277             :             const OUString sListStyleName =
     278          97 :                                 GetListStyleOfProcessedList( sListBlockListId );
     279             : 
     280             :             tMapForLists::const_iterator aIter =
     281          97 :                     mpMapListIdToListStyleDefaultListId->find( sListStyleName );
     282          97 :             if ( aIter != mpMapListIdToListStyleDefaultListId->end() )
     283             :             {
     284          97 :                 if ( (*aIter).second.first == sListBlockListId )
     285             :                 {
     286          90 :                     sListBlockListId = (*aIter).second.second;
     287             :                 }
     288          97 :             }
     289             :         }
     290             :     }
     291             : 
     292         108 :     return sListBlockListId;
     293             : }
     294             : 
     295           0 : void XMLTextListsHelper::StoreLastContinuingList( const OUString& sListId,
     296             :                                                   const OUString& sContinuingListId )
     297             : {
     298           0 :     if ( mpContinuingLists == 0 )
     299             :     {
     300           0 :         mpContinuingLists = new tMapForContinuingLists();
     301             :     }
     302             : 
     303           0 :     (*mpContinuingLists)[ sListId ] = sContinuingListId;
     304           0 : }
     305             : 
     306           0 : OUString XMLTextListsHelper::GetLastContinuingListId(
     307             :                                                 const OUString& sListId ) const
     308             : {
     309           0 :     if ( mpContinuingLists != 0)
     310             :     {
     311             :         tMapForContinuingLists::const_iterator aIter =
     312           0 :                                                 mpContinuingLists->find( sListId );
     313           0 :         if ( aIter != mpContinuingLists->end() )
     314             :         {
     315           0 :             return (*aIter).second;
     316             :         }
     317             :     }
     318             : 
     319           0 :     return sListId;
     320             : }
     321             : 
     322           0 : void XMLTextListsHelper::PushListOnStack( const OUString& sListId,
     323             :                                           const OUString& sListStyleName )
     324             : {
     325           0 :     if ( mpListStack == 0 )
     326             :     {
     327           0 :         mpListStack = new tStackForLists();
     328             :     }
     329             :     ::std::pair< OUString, OUString >
     330           0 :                                 aListData( sListId, sListStyleName );
     331           0 :     mpListStack->push_back( aListData );
     332           0 : }
     333           0 : void XMLTextListsHelper::PopListFromStack()
     334             : {
     335           0 :     if ( mpListStack != 0 &&
     336           0 :          mpListStack->size() > 0 )
     337             :     {
     338           0 :         mpListStack->pop_back();
     339             :     }
     340           0 : }
     341             : 
     342           0 : sal_Bool XMLTextListsHelper::EqualsToTopListStyleOnStack( const OUString& sListId ) const
     343             : {
     344           0 :     return mpListStack != 0
     345           0 :            ? sListId == mpListStack->back().second
     346           0 :            : sal_False;
     347             : }
     348             : 
     349             : OUString
     350           0 : XMLTextListsHelper::GetNumberedParagraphListId(
     351             :     const sal_uInt16 i_Level,
     352             :     const OUString& i_StyleName)
     353             : {
     354           0 :     if (i_StyleName.isEmpty()) {
     355             :         OSL_FAIL("invalid numbered-paragraph: no style-name");
     356             :     }
     357           0 :     if (!i_StyleName.isEmpty()
     358           0 :         && (i_Level < mLastNumberedParagraphs.size())
     359           0 :         && (mLastNumberedParagraphs[i_Level].first == i_StyleName) )
     360             :     {
     361             :         OSL_ENSURE(!mLastNumberedParagraphs[i_Level].second.isEmpty(),
     362             :             "internal error: numbered-paragraph style-name but no list-id?");
     363           0 :         return mLastNumberedParagraphs[i_Level].second;
     364             :     } else {
     365           0 :         return GenerateNewListId();
     366             :     }
     367             : }
     368             : 
     369             : static void
     370          64 : ClampLevel(uno::Reference<container::XIndexReplace> const& i_xNumRules,
     371             :     sal_Int16 & io_rLevel)
     372             : {
     373             :     OSL_ENSURE(i_xNumRules.is(), "internal error: ClampLevel: NumRules null");
     374          64 :     if (i_xNumRules.is()) {
     375          64 :         const sal_Int32 nLevelCount( i_xNumRules->getCount() );
     376          64 :         if ( io_rLevel >= nLevelCount ) {
     377           0 :             io_rLevel = sal::static_int_cast< sal_Int16 >(nLevelCount-1);
     378             :         }
     379             :     }
     380          64 : }
     381             : 
     382             : uno::Reference<container::XIndexReplace>
     383           0 : XMLTextListsHelper::EnsureNumberedParagraph(
     384             :     SvXMLImport & i_rImport,
     385             :     const OUString& i_ListId,
     386             :     sal_Int16 & io_rLevel, const OUString& i_StyleName)
     387             : {
     388             :     OSL_ENSURE(!i_ListId.isEmpty(), "invalid ListId");
     389             :     OSL_ENSURE(io_rLevel >= 0, "invalid Level");
     390           0 :     NumParaList_t & rNPList( mNPLists[i_ListId] );
     391           0 :     const OUString none; // default
     392           0 :     if ( rNPList.empty() ) {
     393             :         // create default list style for top level
     394           0 :         sal_Int16 lev(0);
     395             :         rNPList.push_back(::std::make_pair(none,
     396           0 :             MakeNumRule(i_rImport, 0, none, none, lev) ));
     397             :     }
     398             :     // create num rule first because this might clamp the level...
     399           0 :     uno::Reference<container::XIndexReplace> xNumRules;
     400           0 :     if ((0 == io_rLevel) || rNPList.empty() || !i_StyleName.isEmpty()) {
     401             :         // no parent to inherit from, or explicit style given => new numrules!
     402             :         // index of parent: level - 1, but maybe that does not exist
     403             :         const size_t parent( std::min(static_cast<size_t>(io_rLevel),
     404           0 :             rNPList.size()) - 1 );
     405           0 :         xNumRules = MakeNumRule(i_rImport,
     406           0 :             io_rLevel > 0 ? rNPList[parent].second : 0,
     407           0 :             io_rLevel > 0 ? rNPList[parent].first  : none,
     408           0 :             i_StyleName, io_rLevel);
     409             :     } else {
     410             :         // no style given, but has a parent => reuse parent numrules!
     411           0 :         ClampLevel(rNPList.back().second, io_rLevel);
     412             :     }
     413           0 :     if (static_cast<sal_uInt16>(io_rLevel) + 1U > rNPList.size()) {
     414             :         // new level: need to enlarge
     415           0 :         for (size_t i = rNPList.size();
     416           0 :                 i < static_cast<size_t>(io_rLevel); ++i)
     417             :         {
     418           0 :             NumParaList_t::value_type const rule(rNPList.back());
     419           0 :             rNPList.push_back(rule);
     420           0 :         }
     421           0 :         NumParaList_t::value_type const rule(rNPList.back());
     422           0 :         rNPList.push_back(xNumRules.is()
     423             :             ? ::std::make_pair(i_StyleName, xNumRules)
     424           0 :             : rule);
     425             :     } else {
     426             :         // old level: no need to enlarge; possibly shrink
     427           0 :         if (xNumRules.is()) {
     428           0 :             rNPList[io_rLevel] = std::make_pair(i_StyleName, xNumRules);
     429             :         }
     430           0 :         if (static_cast<sal_uInt16>(io_rLevel) + 1U < rNPList.size()) {
     431           0 :             rNPList.erase(rNPList.begin() + io_rLevel + 1, rNPList.end());
     432             :         }
     433             :     }
     434             :     // remember the list id
     435           0 :     if (mLastNumberedParagraphs.size() <= static_cast<size_t>(io_rLevel)) {
     436           0 :         mLastNumberedParagraphs.resize(io_rLevel+1);
     437             :     }
     438           0 :     mLastNumberedParagraphs[io_rLevel] = std::make_pair(i_StyleName, i_ListId);
     439           0 :     return rNPList.back().second;
     440             : }
     441             : 
     442             : /** extracted from the XMLTextListBlockContext constructor */
     443             : uno::Reference<container::XIndexReplace>
     444          64 : XMLTextListsHelper::MakeNumRule(
     445             :     SvXMLImport & i_rImport,
     446             :     const uno::Reference<container::XIndexReplace>& i_rNumRule,
     447             :     const OUString& i_ParentStyleName,
     448             :     const OUString& i_StyleName,
     449             :     sal_Int16 & io_rLevel,
     450             :     sal_Bool* o_pRestartNumbering,
     451             :     sal_Bool* io_pSetDefaults)
     452             : {
     453          64 :     static OUString s_NumberingRules( "NumberingRules");
     454          64 :     uno::Reference<container::XIndexReplace> xNumRules(i_rNumRule);
     455          64 :     if ( !i_StyleName.isEmpty() && i_StyleName != i_ParentStyleName )
     456             :     {
     457             :         const OUString sDisplayStyleName(
     458             :             i_rImport.GetStyleDisplayName( XML_STYLE_FAMILY_TEXT_LIST,
     459          30 :                                              i_StyleName) );
     460             :         const uno::Reference < container::XNameContainer >& rNumStyles(
     461          30 :                             i_rImport.GetTextImport()->GetNumberingStyles() );
     462          30 :         if( rNumStyles.is() && rNumStyles->hasByName( sDisplayStyleName ) )
     463             :         {
     464           4 :             uno::Reference < style::XStyle > xStyle;
     465           8 :             uno::Any any = rNumStyles->getByName( sDisplayStyleName );
     466           4 :             any >>= xStyle;
     467             : 
     468             :             uno::Reference< beans::XPropertySet > xPropSet( xStyle,
     469           8 :                 uno::UNO_QUERY );
     470           4 :             any = xPropSet->getPropertyValue(s_NumberingRules);
     471           8 :             any >>= xNumRules;
     472             :         }
     473             :         else
     474             :         {
     475             :             const SvxXMLListStyleContext *pListStyle(
     476          26 :                 i_rImport.GetTextImport()->FindAutoListStyle( i_StyleName ) );
     477          26 :             if( pListStyle )
     478             :             {
     479          26 :                 xNumRules = pListStyle->GetNumRules();
     480          26 :                 if( !xNumRules.is() )
     481             :                 {
     482          15 :                     pListStyle->CreateAndInsertAuto();
     483          15 :                     xNumRules = pListStyle->GetNumRules();
     484             :                 }
     485             :             }
     486          30 :         }
     487             :     }
     488             : 
     489          64 :     sal_Bool bSetDefaults(io_pSetDefaults ? *io_pSetDefaults : sal_False);
     490          64 :     if ( !xNumRules.is() )
     491             :     {
     492             :         // If no style name has been specified for this style and for any
     493             :         // parent or if no num rule with the specified name exists,
     494             :         // create a new one.
     495             : 
     496           8 :         xNumRules =
     497           8 :             SvxXMLListStyleContext::CreateNumRule( i_rImport.GetModel() );
     498             :         DBG_ASSERT( xNumRules.is(), "got no numbering rule" );
     499           4 :         if ( !xNumRules.is() )
     500           0 :             return xNumRules;
     501             : 
     502             :         // Because it is a new num rule, numbering must not be restarted.
     503           4 :         if (o_pRestartNumbering) *o_pRestartNumbering = sal_False;
     504           4 :         bSetDefaults = sal_True;
     505           4 :         if (io_pSetDefaults) *io_pSetDefaults = bSetDefaults;
     506             :     }
     507             : 
     508          64 :     ClampLevel(xNumRules, io_rLevel);
     509             : 
     510          64 :     if ( bSetDefaults )
     511             :     {
     512             :         // Because there is no list style sheet for this style, a default
     513             :         // format must be set for any level of this num rule.
     514             :         SvxXMLListStyleContext::SetDefaultStyle( xNumRules, io_rLevel,
     515           4 :             false );
     516             :     }
     517             : 
     518          64 :     return xNumRules;
     519             : }
     520             : 
     521             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10