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

Generated by: LCOV version 1.10