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-17 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         148 : void ScMyStyleNumberFormats::AddStyleNumberFormat(const rtl::OUString& rStyleName, const sal_Int32 nNumberFormat)
      28             : {
      29         148 :     aSet.insert(ScMyStyleNumberFormat(rStyleName, nNumberFormat));
      30         148 : }
      31             : 
      32         232 : sal_Int32 ScMyStyleNumberFormats::GetStyleNumberFormat(const rtl::OUString& rStyleName)
      33             : {
      34         232 :     ScMyStyleNumberFormat aStyleNumberFormat(rStyleName);
      35         232 :     ScMyStyleNumberFormatSet::iterator aItr(aSet.find(aStyleNumberFormat));
      36         232 :     if (aItr == aSet.end())
      37          76 :         return -1;
      38             :     else
      39         156 :         return aItr->nNumberFormat;
      40             : }
      41             : 
      42        9102 : ScMyStyleRanges::ScMyStyleRanges() :
      43        9102 :     pCurrencyList(NULL)
      44             : {
      45        9102 : }
      46             : 
      47       27306 : ScMyStyleRanges::~ScMyStyleRanges()
      48             : {
      49        9102 :     delete pCurrencyList;
      50       18204 : }
      51             : 
      52        5836 : void ScMyStyleRanges::AddRange(const ScRange& rRange,
      53             :     const rtl::OUString* /*pStyleName*/, const sal_Int16 nType,
      54             :     ScXMLImport& /*rImport*/, const sal_uInt32 /*nMaxRanges*/)
      55             : {
      56        5836 :     switch (nType)
      57             :     {
      58             :         case util::NumberFormat::NUMBER:
      59             :         {
      60        2572 :             if (!mpNumberList)
      61         160 :                 mpNumberList.reset(new ScSimpleRangeList);
      62        2572 :             mpNumberList->addRange(rRange);
      63             :         }
      64        2572 :         break;
      65             :         case util::NumberFormat::TEXT:
      66             :         {
      67        3172 :             if (!mpTextList)
      68         350 :                 mpTextList.reset(new ScSimpleRangeList);
      69        3172 :             mpTextList->addRange(rRange);
      70             :         }
      71        3172 :         break;
      72             :         case util::NumberFormat::TIME:
      73             :         {
      74           2 :             if (!mpTimeList)
      75           2 :                 mpTimeList.reset(new ScSimpleRangeList);
      76           2 :             mpTimeList->addRange(rRange);
      77             :         }
      78           2 :         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           4 :             if (!mpPercentList)
      89           4 :                 mpPercentList.reset(new ScSimpleRangeList);
      90           4 :             mpPercentList->addRange(rRange);
      91             :         }
      92           4 :         break;
      93             :         case util::NumberFormat::LOGICAL:
      94             :         {
      95          82 :             if (!mpLogicalList)
      96          14 :                 mpLogicalList.reset(new ScSimpleRangeList);
      97          82 :             mpLogicalList->addRange(rRange);
      98             :         }
      99          82 :         break;
     100             :         case util::NumberFormat::UNDEFINED:
     101             :         {
     102           4 :             if (!mpUndefinedList)
     103           2 :                 mpUndefinedList.reset(new ScSimpleRangeList);
     104           4 :             mpUndefinedList->addRange(rRange);
     105             :         }
     106           4 :         break;
     107             :         default:
     108             :         {
     109             :             OSL_FAIL("wrong type");
     110             :         }
     111           0 :         break;
     112             :     }
     113        5836 : }
     114             : 
     115           2 : void ScMyStyleRanges::AddCurrencyRange(const ScRange& rRange,
     116             :     const rtl::OUString* /*pStyleName*/, const rtl::OUString* pCurrency,
     117             :     ScXMLImport& /*rImport*/, const sal_uInt32 /*nMaxRanges*/)
     118             : {
     119           2 :     if (!pCurrencyList)
     120           2 :         pCurrencyList = new ScMyCurrencyStylesSet();
     121           2 :     ScMyCurrencyStyle aStyle;
     122           2 :     if (pCurrency)
     123           2 :         aStyle.sCurrency = *pCurrency;
     124           2 :     ScMyCurrencyStylesSet::iterator aItr(pCurrencyList->find(aStyle));
     125           2 :     if (aItr == pCurrencyList->end())
     126             :     {
     127           2 :         std::pair<ScMyCurrencyStylesSet::iterator, bool> aPair(pCurrencyList->insert(aStyle));
     128           2 :         if (aPair.second)
     129             :         {
     130           2 :             aItr = aPair.first;
     131           2 :             aItr->mpRanges->addRange(rRange);
     132             :         }
     133             :     }
     134             :     else
     135           0 :         aItr->mpRanges->addRange(rRange);
     136           2 : }
     137             : 
     138        3426 : void ScMyStyleRanges::InsertCol(const sal_Int32 nCol, const sal_Int32 nTab, ScDocument* /*pDoc*/)
     139             : {
     140        3426 :     if (mpTextList)
     141          58 :         mpTextList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     142        3426 :     if (mpNumberList)
     143          44 :         mpNumberList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     144        3426 :     if (mpTimeList)
     145           0 :         mpTimeList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     146        3426 :     if (mpDateTimeList)
     147           0 :         mpDateTimeList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     148        3426 :     if (mpPercentList)
     149           0 :         mpPercentList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     150        3426 :     if (mpLogicalList)
     151           0 :         mpLogicalList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     152        3426 :     if (mpUndefinedList)
     153           0 :         mpUndefinedList->insertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
     154             : 
     155        3426 :     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        3426 : }
     166             : 
     167         534 : 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         534 :     list<ScRange>::const_iterator itr = rRanges.begin(), itrEnd = rRanges.end();
     172        1746 :     for (; itr != itrEnd; ++itr)
     173        1212 :         rImport.SetStyleToRange(*itr, pStyleName, nCellType, pCurrency);
     174         534 : }
     175             : 
     176         614 : void ScMyStyleRanges::SetStylesToRanges(const rtl::OUString* pStyleName, ScXMLImport& rImport)
     177             : {
     178         614 :     if (mpNumberList)
     179             :     {
     180         160 :         list<ScRange> aList;
     181         160 :         mpNumberList->getRangeList(aList);
     182         160 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::NUMBER, NULL, rImport);
     183         160 :         mpNumberList->clear();
     184             :     }
     185         614 :     if (mpTextList)
     186             :     {
     187         350 :         list<ScRange> aList;
     188         350 :         mpTextList->getRangeList(aList);
     189         350 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::TEXT, NULL, rImport);
     190         350 :         mpTextList->clear();
     191             :     }
     192         614 :     if (mpTimeList)
     193             :     {
     194           2 :         list<ScRange> aList;
     195           2 :         mpTimeList->getRangeList(aList);
     196           2 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::TIME, NULL, rImport);
     197           2 :         mpTimeList->clear();
     198             :     }
     199         614 :     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         614 :     if (mpPercentList)
     207             :     {
     208           4 :         list<ScRange> aList;
     209           4 :         mpPercentList->getRangeList(aList);
     210           4 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::PERCENT, NULL, rImport);
     211           4 :         mpPercentList->clear();
     212             :     }
     213         614 :     if (mpLogicalList)
     214             :     {
     215          14 :         list<ScRange> aList;
     216          14 :         mpLogicalList->getRangeList(aList);
     217          14 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::LOGICAL, NULL, rImport);
     218          14 :         mpLogicalList->clear();
     219             :     }
     220         614 :     if (mpUndefinedList)
     221             :     {
     222           2 :         list<ScRange> aList;
     223           2 :         mpUndefinedList->getRangeList(aList);
     224           2 :         SetStylesToRanges(aList, pStyleName, util::NumberFormat::UNDEFINED, NULL, rImport);
     225           2 :         mpUndefinedList->clear();
     226             :     }
     227         614 :     if (pCurrencyList)
     228             :     {
     229           2 :         ScMyCurrencyStylesSet::iterator aItr(pCurrencyList->begin());
     230           2 :         ScMyCurrencyStylesSet::iterator aEndItr(pCurrencyList->end());
     231           6 :         while (aItr != aEndItr)
     232             :         {
     233           2 :             list<ScRange> aList;
     234           2 :             aItr->mpRanges->getRangeList(aList);
     235           2 :             SetStylesToRanges(aList, pStyleName, util::NumberFormat::CURRENCY, &aItr->sCurrency, rImport);
     236           2 :             ++aItr;
     237           2 :         }
     238             :     }
     239         614 : }
     240             : 
     241             : //----------------------------------------------------------------------------
     242             : 
     243         264 : ScMyStylesImportHelper::ScMyStylesImportHelper(ScXMLImport& rTempImport)
     244             :     :
     245         264 :     aRowDefaultStyle(aCellStyles.end()),
     246             :     rImport(rTempImport),
     247             :     pStyleName(NULL),
     248             :     pPrevStyleName(NULL),
     249             :     pCurrency(NULL),
     250             :     pPrevCurrency(NULL),
     251             :     nMaxRanges(0),
     252         528 :     bPrevRangeAdded(true)
     253             : {
     254         264 : }
     255             : 
     256         528 : ScMyStylesImportHelper::~ScMyStylesImportHelper()
     257             : {
     258         264 :     delete pPrevStyleName;
     259         264 :     delete pPrevCurrency;
     260         264 :     delete pStyleName;
     261         264 :     delete pCurrency;
     262         264 : }
     263             : 
     264        5974 : void ScMyStylesImportHelper::ResetAttributes()
     265             : {
     266        5974 :     delete pPrevStyleName;
     267        5974 :     delete pPrevCurrency;
     268        5974 :     pPrevStyleName = pStyleName;
     269        5974 :     pPrevCurrency = pCurrency;
     270        5974 :     nPrevCellType = nCellType;
     271        5974 :     pStyleName = NULL;
     272        5974 :     pCurrency = NULL;
     273        5974 :     nCellType = 0;
     274        5974 : }
     275             : 
     276        9102 : ScMyStylesSet::iterator ScMyStylesImportHelper::GetIterator(const rtl::OUString* pStyleNameP)
     277             : {
     278        9102 :     ScMyStyle aStyle;
     279        9102 :     if (pStyleNameP)
     280        9102 :         aStyle.sStyleName = *pStyleNameP;
     281             :     else
     282             :     {
     283             :         OSL_FAIL("here is no stylename given");
     284             :     }
     285        9102 :     ScMyStylesSet::iterator aItr(aCellStyles.find(aStyle));
     286        9102 :     if (aItr == aCellStyles.end())
     287             :     {
     288         614 :         std::pair<ScMyStylesSet::iterator, bool> aPair(aCellStyles.insert(aStyle));
     289         614 :         if (aPair.second)
     290         614 :             aItr = aPair.first;
     291             :         else
     292             :         {
     293             :             OSL_FAIL("not possible to insert style");
     294           0 :             return aCellStyles.end();
     295             :         }
     296             :     }
     297        9102 :     return aItr;
     298             : }
     299             : 
     300        5418 : void ScMyStylesImportHelper::AddDefaultRange(const ScRange& rRange)
     301             : {
     302             :     OSL_ENSURE(aRowDefaultStyle != aCellStyles.end(), "no row default style");
     303        5418 :     if (aRowDefaultStyle->sStyleName.isEmpty())
     304             :     {
     305        5418 :         SCCOL nStartCol(rRange.aStart.Col());
     306        5418 :         SCCOL nEndCol(rRange.aEnd.Col());
     307        5418 :         if (aColDefaultStyles.size() > sal::static_int_cast<sal_uInt32>(nStartCol))
     308             :         {
     309        5418 :             ScMyStylesSet::iterator aPrevItr(aColDefaultStyles[nStartCol]);
     310             :             OSL_ENSURE(aColDefaultStyles.size() > sal::static_int_cast<sal_uInt32>(nEndCol), "to much columns");
     311       11138 :             for (SCCOL i = nStartCol + 1; (i <= nEndCol) && (i < sal::static_int_cast<SCCOL>(aColDefaultStyles.size())); ++i)
     312             :             {
     313        5720 :                 if (aPrevItr != aColDefaultStyles[i])
     314             :                 {
     315             :                     OSL_ENSURE(aPrevItr != aCellStyles.end(), "no column default style");
     316          22 :                     ScRange aRange(rRange);
     317          22 :                     aRange.aStart.SetCol(nStartCol);
     318          22 :                     aRange.aEnd.SetCol(i - 1);
     319          22 :                     delete pPrevStyleName;
     320          22 :                     pPrevStyleName = new rtl::OUString(aPrevItr->sStyleName);
     321          22 :                     AddSingleRange(aRange);
     322          22 :                     nStartCol = i;
     323          22 :                     aPrevItr = aColDefaultStyles[i];
     324             :                 }
     325             :             }
     326        5418 :             if (aPrevItr != aCellStyles.end())
     327             :             {
     328        5418 :                 ScRange aRange(rRange);
     329        5418 :                 aRange.aStart.SetCol(nStartCol);
     330        5418 :                 delete pPrevStyleName;
     331        5418 :                 pPrevStyleName = new rtl::OUString(aPrevItr->sStyleName);
     332        5418 :                 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        5418 : }
     351             : 
     352        5838 : void ScMyStylesImportHelper::AddSingleRange(const ScRange& rRange)
     353             : {
     354        5838 :     if (nMaxRanges == 0)
     355         158 :         nMaxRanges = aColDefaultStyles.size();
     356        5838 :     ScMyStylesSet::iterator aItr(GetIterator(pPrevStyleName));
     357        5838 :     if (aItr != aCellStyles.end())
     358             :     {
     359        5838 :         if (nPrevCellType != util::NumberFormat::CURRENCY)
     360        5836 :             aItr->xRanges->AddRange(rRange, pPrevStyleName, nPrevCellType,
     361       11672 :                 rImport, nMaxRanges);
     362             :         else
     363           2 :             aItr->xRanges->AddCurrencyRange(rRange, pPrevStyleName, pPrevCurrency,
     364           4 :                 rImport, nMaxRanges);
     365             :     }
     366        5838 : }
     367             : 
     368        5816 : void ScMyStylesImportHelper::AddRange()
     369             : {
     370        5816 :     if (pPrevStyleName && !pPrevStyleName->isEmpty())
     371         398 :         AddSingleRange(aPrevRange);
     372             :     else
     373        5418 :         AddDefaultRange(aPrevRange);
     374        5816 :     ResetAttributes();
     375        5816 : }
     376             : 
     377         244 : 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         244 :     ScMyStylesSet::iterator aItr(GetIterator(&sStyleName));
     382             :     OSL_ENSURE(aItr != aCellStyles.end(), "no column default style");
     383         244 :     aColDefaultStyles.reserve(aColDefaultStyles.size() + nRepeat);
     384        2472 :     for (sal_Int32 i = 0; i < nRepeat; ++i)
     385        2228 :         aColDefaultStyles.push_back(aItr);
     386         244 : }
     387             : 
     388        3020 : void ScMyStylesImportHelper::SetRowStyle(const rtl::OUString& sStyleName)
     389             : {
     390        3020 :     aRowDefaultStyle = GetIterator(&sStyleName);
     391        3020 : }
     392             : 
     393        7508 : void ScMyStylesImportHelper::SetAttributes(rtl::OUString* pStyleNameP,
     394             :     rtl::OUString* pCurrencyP, const sal_Int16 nCellTypeP)
     395             : {
     396        7508 :     delete this->pStyleName;
     397        7508 :     delete this->pCurrency;
     398        7508 :     this->pStyleName = pStyleNameP;
     399        7508 :     this->pCurrency = pCurrencyP;
     400        7508 :     this->nCellType = nCellTypeP;
     401        7508 : }
     402             : 
     403        7508 : void ScMyStylesImportHelper::AddRange(const ScRange& rRange)
     404             : {
     405        7508 :     if (!bPrevRangeAdded)
     406             :     {
     407        7350 :         bool bAddRange(false);
     408       11962 :         if (nCellType == nPrevCellType &&
     409        2490 :             IsEqual(pStyleName, pPrevStyleName) &&
     410        2122 :             IsEqual(pCurrency, pPrevCurrency))
     411             :         {
     412        2122 :             if (rRange.aStart.Row() == aPrevRange.aStart.Row())
     413             :             {
     414        1640 :                 if (rRange.aEnd.Row() == aPrevRange.aEnd.Row())
     415             :                 {
     416             :                     OSL_ENSURE(aPrevRange.aEnd.Col() + 1 == rRange.aStart.Col(), "something wents wrong");
     417        1640 :                     aPrevRange.aEnd.SetCol(rRange.aEnd.Col());
     418             :                 }
     419             :                 else
     420           0 :                     bAddRange = true;
     421             :             }
     422             :             else
     423             :             {
     424         874 :                 if (rRange.aStart.Col() == aPrevRange.aStart.Col() &&
     425         392 :                     rRange.aEnd.Col() == aPrevRange.aEnd.Col())
     426             :                 {
     427             :                     OSL_ENSURE(aPrevRange.aEnd.Row() + 1 == rRange.aStart.Row(), "something wents wrong");
     428          52 :                     aPrevRange.aEnd.SetRow(rRange.aEnd.Row());
     429             :                 }
     430             :                 else
     431         430 :                     bAddRange = true;
     432             :             }
     433             :         }
     434             :         else
     435        5228 :             bAddRange = true;
     436        7350 :         if (bAddRange)
     437             :         {
     438        5658 :             AddRange();
     439        5658 :             aPrevRange = rRange;
     440             :         }
     441             :     }
     442             :     else
     443             :     {
     444         158 :         aPrevRange = rRange;
     445         158 :         ResetAttributes();
     446         158 :         bPrevRangeAdded = false;
     447             :     }
     448        7508 : }
     449             : 
     450        6700 : void ScMyStylesImportHelper::AddCell(const ScAddress& rAddress)
     451             : {
     452        6700 :     ScRange aScRange( rAddress, rAddress );
     453        6700 :     AddRange(aScRange);
     454        6700 : }
     455             : 
     456        1668 : void ScMyStylesImportHelper::InsertCol(const sal_Int32 nCol, const sal_Int32 nTab, ScDocument* pDoc)
     457             : {
     458        1668 :     ScXMLImport::MutexGuard aGuard(rImport);
     459        1668 :     ScMyStylesSet::iterator aItr(aCellStyles.begin());
     460        1668 :     ScMyStylesSet::iterator aEndItr(aCellStyles.end());
     461        6762 :     while (aItr != aEndItr)
     462             :     {
     463        3426 :         aItr->xRanges->InsertCol(nCol, nTab, pDoc);
     464        3426 :         ++aItr;
     465        1668 :     }
     466        1668 : }
     467             : 
     468         158 : void ScMyStylesImportHelper::EndTable()
     469             : {
     470         158 :     if (!bPrevRangeAdded)
     471             :     {
     472         158 :         AddRange();
     473         158 :         bPrevRangeAdded = true;
     474             :     }
     475         158 :     nMaxRanges = 0;
     476         158 : }
     477             : 
     478         158 : void ScMyStylesImportHelper::SetStylesToRanges()
     479             : {
     480         158 :     ScMyStylesSet::iterator aItr(aCellStyles.begin());
     481         158 :     ScMyStylesSet::iterator aEndItr(aCellStyles.end());
     482         930 :     while (aItr != aEndItr)
     483             :     {
     484         614 :         aItr->xRanges->SetStylesToRanges(&aItr->sStyleName, rImport);
     485         614 :         ++aItr;
     486             :     }
     487         158 :     aColDefaultStyles.clear();
     488         158 :     aCellStyles.clear();
     489         158 :     nMaxRanges = 0;
     490         158 : }
     491             : 
     492             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10