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

Generated by: LCOV version 1.10