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

Generated by: LCOV version 1.10