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

Generated by: LCOV version 1.11