LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/filter/xml - XMLStylesImportHelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 261 269 97.0 %
Date: 2013-07-09 Functions: 25 25 100.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 "XMLStylesImportHelper.hxx"
      21             : #include "xmlimprt.hxx"
      22             : #include <com/sun/star/util/NumberFormat.hpp>
      23             : 
      24             : using namespace com::sun::star;
      25             : using ::std::list;
      26             : 
      27         134 : void ScMyStyleNumberFormats::AddStyleNumberFormat(const OUString& rStyleName, const sal_Int32 nNumberFormat)
      28             : {
      29         134 :     aSet.insert(ScMyStyleNumberFormat(rStyleName, nNumberFormat));
      30         134 : }
      31             : 
      32         240 : sal_Int32 ScMyStyleNumberFormats::GetStyleNumberFormat(const OUString& rStyleName)
      33             : {
      34         240 :     ScMyStyleNumberFormat aStyleNumberFormat(rStyleName);
      35         240 :     ScMyStyleNumberFormatSet::iterator aItr(aSet.find(aStyleNumberFormat));
      36         240 :     if (aItr == aSet.end())
      37          80 :         return -1;
      38             :     else
      39         160 :         return aItr->nNumberFormat;
      40             : }
      41             : 
      42        6767 : ScMyStyleRanges::ScMyStyleRanges() :
      43        6767 :     pCurrencyList(NULL)
      44             : {
      45        6767 : }
      46             : 
      47       20301 : ScMyStyleRanges::~ScMyStyleRanges()
      48             : {
      49        6767 :     delete pCurrencyList;
      50       13534 : }
      51             : 
      52        4511 : void ScMyStyleRanges::AddRange(const ScRange& rRange, const sal_Int16 nType)
      53             : {
      54        4511 :     switch (nType)
      55             :     {
      56             :         case util::NumberFormat::NUMBER:
      57             :         {
      58        1996 :             if (!mpNumberList)
      59         167 :                 mpNumberList.reset(new ScSimpleRangeList);
      60        1996 :             mpNumberList->addRange(rRange);
      61             :         }
      62        1996 :         break;
      63             :         case util::NumberFormat::TEXT:
      64             :         {
      65        2456 :             if (!mpTextList)
      66         264 :                 mpTextList.reset(new ScSimpleRangeList);
      67        2456 :             mpTextList->addRange(rRange);
      68             :         }
      69        2456 :         break;
      70             :         case util::NumberFormat::TIME:
      71             :         {
      72           3 :             if (!mpTimeList)
      73           3 :                 mpTimeList.reset(new ScSimpleRangeList);
      74           3 :             mpTimeList->addRange(rRange);
      75             :         }
      76           3 :         break;
      77             :         case util::NumberFormat::DATETIME:
      78             :         {
      79           2 :             if (!mpDateTimeList)
      80           2 :                 mpDateTimeList.reset(new ScSimpleRangeList);
      81           2 :             mpDateTimeList->addRange(rRange);
      82             :         }
      83           2 :         break;
      84             :         case util::NumberFormat::PERCENT:
      85             :         {
      86           6 :             if (!mpPercentList)
      87           5 :                 mpPercentList.reset(new ScSimpleRangeList);
      88           6 :             mpPercentList->addRange(rRange);
      89             :         }
      90           6 :         break;
      91             :         case util::NumberFormat::LOGICAL:
      92             :         {
      93          43 :             if (!mpLogicalList)
      94           9 :                 mpLogicalList.reset(new ScSimpleRangeList);
      95          43 :             mpLogicalList->addRange(rRange);
      96             :         }
      97          43 :         break;
      98             :         case util::NumberFormat::UNDEFINED:
      99             :         {
     100           5 :             if (!mpUndefinedList)
     101           4 :                 mpUndefinedList.reset(new ScSimpleRangeList);
     102           5 :             mpUndefinedList->addRange(rRange);
     103             :         }
     104           5 :         break;
     105             :         default:
     106             :         {
     107             :             OSL_FAIL("wrong type");
     108             :         }
     109           0 :         break;
     110             :     }
     111        4511 : }
     112             : 
     113           3 : void ScMyStyleRanges::AddCurrencyRange(const ScRange& rRange, const OUString* pCurrency)
     114             : {
     115           3 :     if (!pCurrencyList)
     116           3 :         pCurrencyList = new ScMyCurrencyStylesSet();
     117           3 :     ScMyCurrencyStyle aStyle;
     118           3 :     if (pCurrency)
     119           3 :         aStyle.sCurrency = *pCurrency;
     120           3 :     ScMyCurrencyStylesSet::iterator aItr(pCurrencyList->find(aStyle));
     121           3 :     if (aItr == pCurrencyList->end())
     122             :     {
     123           3 :         std::pair<ScMyCurrencyStylesSet::iterator, bool> aPair(pCurrencyList->insert(aStyle));
     124           3 :         if (aPair.second)
     125             :         {
     126           3 :             aItr = aPair.first;
     127           3 :             aItr->mpRanges->addRange(rRange);
     128             :         }
     129             :     }
     130             :     else
     131           0 :         aItr->mpRanges->addRange(rRange);
     132           3 : }
     133             : 
     134        2308 : void ScMyStyleRanges::InsertCol(const sal_Int32 nCol, const sal_Int32 nTab, ScDocument* /*pDoc*/)
     135             : {
     136        2308 :     if (mpTextList)
     137          49 :         mpTextList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     138        2308 :     if (mpNumberList)
     139          67 :         mpNumberList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     140        2308 :     if (mpTimeList)
     141           5 :         mpTimeList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     142        2308 :     if (mpDateTimeList)
     143           6 :         mpDateTimeList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     144        2308 :     if (mpPercentList)
     145           8 :         mpPercentList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     146        2308 :     if (mpLogicalList)
     147           2 :         mpLogicalList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     148        2308 :     if (mpUndefinedList)
     149           0 :         mpUndefinedList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     150             : 
     151        2308 :     if (pCurrencyList)
     152             :     {
     153           7 :         ScMyCurrencyStylesSet::iterator aItr(pCurrencyList->begin());
     154           7 :         ScMyCurrencyStylesSet::iterator aEndItr(pCurrencyList->end());
     155          21 :         while (aItr != aEndItr)
     156             :         {
     157           7 :             aItr->mpRanges->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     158           7 :             ++aItr;
     159             :         }
     160             :     }
     161        2308 : }
     162             : 
     163         457 : void ScMyStyleRanges::SetStylesToRanges(const list<ScRange>& rRanges,
     164             :     const OUString* pStyleName, const sal_Int16 nCellType,
     165             :     const OUString* pCurrency, ScXMLImport& rImport)
     166             : {
     167         457 :     list<ScRange>::const_iterator itr = rRanges.begin(), itrEnd = rRanges.end();
     168        1911 :     for (; itr != itrEnd; ++itr)
     169        1454 :         rImport.SetStyleToRange(*itr, pStyleName, nCellType, pCurrency);
     170         457 : }
     171             : 
     172         550 : void ScMyStyleRanges::SetStylesToRanges(const OUString* pStyleName, ScXMLImport& rImport)
     173             : {
     174         550 :     if (mpNumberList)
     175             :     {
     176         167 :         list<ScRange> aList;
     177         167 :         mpNumberList->getRangeList(aList);
     178         167 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::NUMBER, NULL, rImport);
     179         167 :         mpNumberList->clear();
     180             :     }
     181         550 :     if (mpTextList)
     182             :     {
     183         264 :         list<ScRange> aList;
     184         264 :         mpTextList->getRangeList(aList);
     185         264 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::TEXT, NULL, rImport);
     186         264 :         mpTextList->clear();
     187             :     }
     188         550 :     if (mpTimeList)
     189             :     {
     190           3 :         list<ScRange> aList;
     191           3 :         mpTimeList->getRangeList(aList);
     192           3 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::TIME, NULL, rImport);
     193           3 :         mpTimeList->clear();
     194             :     }
     195         550 :     if (mpDateTimeList)
     196             :     {
     197           2 :         list<ScRange> aList;
     198           2 :         mpDateTimeList->getRangeList(aList);
     199           2 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::DATETIME, NULL, rImport);
     200           2 :         mpDateTimeList->clear();
     201             :     }
     202         550 :     if (mpPercentList)
     203             :     {
     204           5 :         list<ScRange> aList;
     205           5 :         mpPercentList->getRangeList(aList);
     206           5 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::PERCENT, NULL, rImport);
     207           5 :         mpPercentList->clear();
     208             :     }
     209         550 :     if (mpLogicalList)
     210             :     {
     211           9 :         list<ScRange> aList;
     212           9 :         mpLogicalList->getRangeList(aList);
     213           9 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::LOGICAL, NULL, rImport);
     214           9 :         mpLogicalList->clear();
     215             :     }
     216         550 :     if (mpUndefinedList)
     217             :     {
     218           4 :         list<ScRange> aList;
     219           4 :         mpUndefinedList->getRangeList(aList);
     220           4 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::UNDEFINED, NULL, rImport);
     221           4 :         mpUndefinedList->clear();
     222             :     }
     223         550 :     if (pCurrencyList)
     224             :     {
     225           3 :         ScMyCurrencyStylesSet::iterator aItr(pCurrencyList->begin());
     226           3 :         ScMyCurrencyStylesSet::iterator aEndItr(pCurrencyList->end());
     227           9 :         while (aItr != aEndItr)
     228             :         {
     229           3 :             list<ScRange> aList;
     230           3 :             aItr->mpRanges->getRangeList(aList);
     231           3 :             SetStylesToRanges(aList, pStyleName, util::NumberFormat::CURRENCY, &aItr->sCurrency, rImport);
     232           3 :             ++aItr;
     233           3 :         }
     234             :     }
     235         550 : }
     236             : 
     237             : //----------------------------------------------------------------------------
     238             : 
     239         302 : ScMyStylesImportHelper::ScMyStylesImportHelper(ScXMLImport& rTempImport)
     240             :     :
     241         302 :     aRowDefaultStyle(aCellStyles.end()),
     242             :     rImport(rTempImport),
     243             :     pStyleName(NULL),
     244             :     pPrevStyleName(NULL),
     245             :     pCurrency(NULL),
     246             :     pPrevCurrency(NULL),
     247         604 :     bPrevRangeAdded(true)
     248             : {
     249         302 : }
     250             : 
     251         604 : ScMyStylesImportHelper::~ScMyStylesImportHelper()
     252             : {
     253         302 :     delete pPrevStyleName;
     254         302 :     delete pPrevCurrency;
     255         302 :     delete pStyleName;
     256         302 :     delete pCurrency;
     257         302 : }
     258             : 
     259        4657 : void ScMyStylesImportHelper::ResetAttributes()
     260             : {
     261        4657 :     delete pPrevStyleName;
     262        4657 :     delete pPrevCurrency;
     263        4657 :     pPrevStyleName = pStyleName;
     264        4657 :     pPrevCurrency = pCurrency;
     265        4657 :     nPrevCellType = nCellType;
     266        4657 :     pStyleName = NULL;
     267        4657 :     pCurrency = NULL;
     268        4657 :     nCellType = 0;
     269        4657 : }
     270             : 
     271        6767 : ScMyStylesSet::iterator ScMyStylesImportHelper::GetIterator(const OUString* pStyleNameP)
     272             : {
     273        6767 :     ScMyStyle aStyle;
     274        6767 :     if (pStyleNameP)
     275        6767 :         aStyle.sStyleName = *pStyleNameP;
     276             :     else
     277             :     {
     278             :         OSL_FAIL("here is no stylename given");
     279             :     }
     280        6767 :     ScMyStylesSet::iterator aItr(aCellStyles.find(aStyle));
     281        6767 :     if (aItr == aCellStyles.end())
     282             :     {
     283         550 :         std::pair<ScMyStylesSet::iterator, bool> aPair(aCellStyles.insert(aStyle));
     284         550 :         if (aPair.second)
     285         550 :             aItr = aPair.first;
     286             :         else
     287             :         {
     288             :             OSL_FAIL("not possible to insert style");
     289           0 :             return aCellStyles.end();
     290             :         }
     291             :     }
     292        6767 :     return aItr;
     293             : }
     294             : 
     295        3722 : void ScMyStylesImportHelper::AddDefaultRange(const ScRange& rRange)
     296             : {
     297             :     OSL_ENSURE(aRowDefaultStyle != aCellStyles.end(), "no row default style");
     298        3722 :     if (aRowDefaultStyle->sStyleName.isEmpty())
     299             :     {
     300        3722 :         SCCOL nStartCol(rRange.aStart.Col());
     301        3722 :         SCCOL nEndCol(rRange.aEnd.Col());
     302        3722 :         if (aColDefaultStyles.size() > sal::static_int_cast<sal_uInt32>(nStartCol))
     303             :         {
     304        3722 :             ScMyStylesSet::iterator aPrevItr(aColDefaultStyles[nStartCol]);
     305             :             OSL_ENSURE(aColDefaultStyles.size() > sal::static_int_cast<sal_uInt32>(nEndCol), "to much columns");
     306       17221 :             for (SCCOL i = nStartCol + 1; (i <= nEndCol) && (i < sal::static_int_cast<SCCOL>(aColDefaultStyles.size())); ++i)
     307             :             {
     308       13499 :                 if (aPrevItr != aColDefaultStyles[i])
     309             :                 {
     310             :                     OSL_ENSURE(aPrevItr != aCellStyles.end(), "no column default style");
     311          25 :                     ScRange aRange(rRange);
     312          25 :                     aRange.aStart.SetCol(nStartCol);
     313          25 :                     aRange.aEnd.SetCol(i - 1);
     314          25 :                     delete pPrevStyleName;
     315          25 :                     pPrevStyleName = new OUString(aPrevItr->sStyleName);
     316          25 :                     AddSingleRange(aRange);
     317          25 :                     nStartCol = i;
     318          25 :                     aPrevItr = aColDefaultStyles[i];
     319             :                 }
     320             :             }
     321        3722 :             if (aPrevItr != aCellStyles.end())
     322             :             {
     323        3722 :                 ScRange aRange(rRange);
     324        3722 :                 aRange.aStart.SetCol(nStartCol);
     325        3722 :                 delete pPrevStyleName;
     326        3722 :                 pPrevStyleName = new OUString(aPrevItr->sStyleName);
     327        3722 :                 AddSingleRange(aRange);
     328             :             }
     329             :             else
     330             :             {
     331             :                 OSL_FAIL("no column default style");
     332             :             }
     333             :         }
     334             :         else
     335             :         {
     336             :             OSL_FAIL("too much columns");
     337             :         }
     338             :     }
     339             :     else
     340             :     {
     341           0 :         delete pPrevStyleName;
     342           0 :         pPrevStyleName = new OUString(aRowDefaultStyle->sStyleName);
     343           0 :         AddSingleRange(rRange);
     344             :     }
     345        3722 : }
     346             : 
     347        4514 : void ScMyStylesImportHelper::AddSingleRange(const ScRange& rRange)
     348             : {
     349        4514 :     ScMyStylesSet::iterator aItr(GetIterator(pPrevStyleName));
     350        4514 :     if (aItr != aCellStyles.end())
     351             :     {
     352        4514 :         if (nPrevCellType != util::NumberFormat::CURRENCY)
     353        4511 :             aItr->xRanges->AddRange(rRange, nPrevCellType);
     354             :         else
     355           3 :             aItr->xRanges->AddCurrencyRange(rRange, pPrevCurrency);
     356             :     }
     357        4514 : }
     358             : 
     359        4489 : void ScMyStylesImportHelper::AddRange()
     360             : {
     361        4489 :     if (pPrevStyleName && !pPrevStyleName->isEmpty())
     362         767 :         AddSingleRange(aPrevRange);
     363             :     else
     364        3722 :         AddDefaultRange(aPrevRange);
     365        4489 :     ResetAttributes();
     366        4489 : }
     367             : 
     368         259 : void ScMyStylesImportHelper::AddColumnStyle(const OUString& sStyleName, const sal_Int32 nColumn, const sal_Int32 nRepeat)
     369             : {
     370             :     (void)nColumn;  // avoid warning in product version
     371             :     OSL_ENSURE(static_cast<sal_uInt32>(nColumn) == aColDefaultStyles.size(), "some columns are absent");
     372         259 :     ScMyStylesSet::iterator aItr(GetIterator(&sStyleName));
     373             :     OSL_ENSURE(aItr != aCellStyles.end(), "no column default style");
     374         259 :     aColDefaultStyles.reserve(aColDefaultStyles.size() + nRepeat);
     375        6758 :     for (sal_Int32 i = 0; i < nRepeat; ++i)
     376        6499 :         aColDefaultStyles.push_back(aItr);
     377         259 : }
     378             : 
     379        1994 : void ScMyStylesImportHelper::SetRowStyle(const OUString& sStyleName)
     380             : {
     381        1994 :     aRowDefaultStyle = GetIterator(&sStyleName);
     382        1994 : }
     383             : 
     384        5652 : void ScMyStylesImportHelper::SetAttributes(OUString* pStyleNameP,
     385             :     OUString* pCurrencyP, const sal_Int16 nCellTypeP)
     386             : {
     387        5652 :     delete this->pStyleName;
     388        5652 :     delete this->pCurrency;
     389        5652 :     this->pStyleName = pStyleNameP;
     390        5652 :     this->pCurrency = pCurrencyP;
     391        5652 :     this->nCellType = nCellTypeP;
     392        5652 : }
     393             : 
     394        5652 : void ScMyStylesImportHelper::AddRange(const ScRange& rRange)
     395             : {
     396        5652 :     if (!bPrevRangeAdded)
     397             :     {
     398        5484 :         bool bAddRange(false);
     399       12700 :         if (nCellType == nPrevCellType &&
     400        6976 :             IsEqual(pStyleName, pPrevStyleName) &&
     401        1492 :             IsEqual(pCurrency, pPrevCurrency))
     402             :         {
     403        1492 :             if (rRange.aStart.Row() == aPrevRange.aStart.Row())
     404             :             {
     405        1011 :                 if (rRange.aEnd.Row() == aPrevRange.aEnd.Row())
     406             :                 {
     407             :                     OSL_ENSURE(aPrevRange.aEnd.Col() + 1 == rRange.aStart.Col(), "something wents wrong");
     408        1011 :                     aPrevRange.aEnd.SetCol(rRange.aEnd.Col());
     409             :                 }
     410             :                 else
     411           0 :                     bAddRange = true;
     412             :             }
     413             :             else
     414             :             {
     415         907 :                 if (rRange.aStart.Col() == aPrevRange.aStart.Col() &&
     416         426 :                     rRange.aEnd.Col() == aPrevRange.aEnd.Col())
     417             :                 {
     418             :                     OSL_ENSURE(aPrevRange.aEnd.Row() + 1 == rRange.aStart.Row(), "something wents wrong");
     419         152 :                     aPrevRange.aEnd.SetRow(rRange.aEnd.Row());
     420             :                 }
     421             :                 else
     422         329 :                     bAddRange = true;
     423             :             }
     424             :         }
     425             :         else
     426        3992 :             bAddRange = true;
     427        5484 :         if (bAddRange)
     428             :         {
     429        4321 :             AddRange();
     430        4321 :             aPrevRange = rRange;
     431             :         }
     432             :     }
     433             :     else
     434             :     {
     435         168 :         aPrevRange = rRange;
     436         168 :         ResetAttributes();
     437         168 :         bPrevRangeAdded = false;
     438             :     }
     439        5652 : }
     440             : 
     441        5073 : void ScMyStylesImportHelper::AddCell(const ScAddress& rAddress)
     442             : {
     443        5073 :     ScRange aScRange( rAddress, rAddress );
     444        5073 :     AddRange(aScRange);
     445        5073 : }
     446             : 
     447        1054 : void ScMyStylesImportHelper::InsertCol(const sal_Int32 nCol, const sal_Int32 nTab, ScDocument* pDoc)
     448             : {
     449        1054 :     ScXMLImport::MutexGuard aGuard(rImport);
     450        1054 :     ScMyStylesSet::iterator aItr(aCellStyles.begin());
     451        1054 :     ScMyStylesSet::iterator aEndItr(aCellStyles.end());
     452        4416 :     while (aItr != aEndItr)
     453             :     {
     454        2308 :         aItr->xRanges->InsertCol(nCol, nTab, pDoc);
     455        2308 :         ++aItr;
     456        1054 :     }
     457        1054 : }
     458             : 
     459         168 : void ScMyStylesImportHelper::EndTable()
     460             : {
     461         168 :     if (!bPrevRangeAdded)
     462             :     {
     463         168 :         AddRange();
     464         168 :         bPrevRangeAdded = true;
     465             :     }
     466         168 : }
     467             : 
     468         168 : void ScMyStylesImportHelper::SetStylesToRanges()
     469             : {
     470         168 :     ScMyStylesSet::iterator aItr(aCellStyles.begin());
     471         168 :     ScMyStylesSet::iterator aEndItr(aCellStyles.end());
     472         886 :     while (aItr != aEndItr)
     473             :     {
     474         550 :         aItr->xRanges->SetStylesToRanges(&aItr->sStyleName, rImport);
     475         550 :         ++aItr;
     476             :     }
     477         168 :     aColDefaultStyles.clear();
     478         168 :     aCellStyles.clear();
     479         168 : }
     480             : 
     481             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10