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

Generated by: LCOV version 1.10