LCOV - code coverage report
Current view: top level - xmloff/source/text - XMLPropertyBackpatcher.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 42 78 53.8 %
Date: 2014-11-03 Functions: 17 23 73.9 %
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 <com/sun/star/beans/XPropertySet.hpp>
      21             : #include <com/sun/star/uno/Reference.h>
      22             : 
      23             : #include <rtl/ustring.hxx>
      24             : #include "XMLPropertyBackpatcher.hxx"
      25             : #include <xmloff/txtimp.hxx>
      26             : 
      27             : using ::std::vector;
      28             : using ::std::map;
      29             : using ::com::sun::star::uno::Reference;
      30             : using ::com::sun::star::uno::Any;
      31             : using ::com::sun::star::beans::XPropertySet;
      32             : 
      33             : 
      34             : template<class A>
      35          26 : XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
      36             :     const OUString& sPropName)
      37             : :   sPropertyName(sPropName)
      38             : ,   bDefaultHandling(false)
      39             : ,   bPreserveProperty(false)
      40          26 : ,   sPreservePropertyName()
      41             : {
      42          26 : }
      43             : 
      44             : 
      45             : template<class A>
      46          26 : XMLPropertyBackpatcher<A>::~XMLPropertyBackpatcher()
      47             : {
      48          26 :     SetDefault();
      49          26 : }
      50             : 
      51             : 
      52             : template<class A>
      53          44 : void XMLPropertyBackpatcher<A>::ResolveId(
      54             :     const OUString& sName,
      55             :     A aValue)
      56             : {
      57             :     // insert ID into ID map
      58          44 :     aIDMap[sName] = aValue;
      59             : 
      60             :     // backpatch old references, if backpatch list exists
      61          44 :     if (aBackpatchListMap.count(sName))
      62             :     {
      63             :         // aah, we have a backpatch list!
      64             :         BackpatchListType* pList =
      65           0 :             (BackpatchListType*)aBackpatchListMap[sName];
      66             : 
      67             :         // a) remove list from list map
      68           0 :         aBackpatchListMap.erase(sName);
      69             : 
      70             :         // b) for every item, set SequenceNumber
      71             :         //    (and preserve Property, if appropriate)
      72           0 :         Any aAny;
      73           0 :         aAny <<= aValue;
      74           0 :         if (bPreserveProperty)
      75             :         {
      76             :             // preserve version
      77           0 :             for(BackpatchListType::iterator aIter = pList->begin();
      78           0 :                 aIter != pList->end();
      79             :                 ++aIter)
      80             :             {
      81           0 :                 Reference<XPropertySet> xProp = (*aIter);
      82           0 :                 Any aPres = xProp->getPropertyValue(sPreservePropertyName);
      83           0 :                 xProp->setPropertyValue(sPropertyName, aAny);
      84           0 :                 xProp->setPropertyValue(sPreservePropertyName, aPres);
      85             :             }
      86             :         }
      87             :         else
      88             :         {
      89             :             // without preserve
      90           0 :             for(BackpatchListType::iterator aIter = pList->begin();
      91           0 :                 aIter != pList->end();
      92             :                 ++aIter)
      93             :             {
      94           0 :                 (*aIter)->setPropertyValue(sPropertyName, aAny);
      95             :             }
      96             :         }
      97             : 
      98             :         // c) delete list
      99           0 :         delete pList;
     100             :     }
     101             :     // else: no backpatch list -> then we're finished
     102          44 : }
     103             : 
     104             : template<class A>
     105           0 : void XMLPropertyBackpatcher<A>::SetProperty(
     106             :     const Reference<XPropertySet> & xPropSet,
     107             :     const OUString& sName)
     108             : {
     109           0 :     Reference<XPropertySet> xNonConstPropSet(xPropSet);
     110           0 :     SetProperty(xNonConstPropSet, sName);
     111           0 : }
     112             : 
     113             : template<class A>
     114           0 : void XMLPropertyBackpatcher<A>::SetProperty(
     115             :     Reference<XPropertySet> & xPropSet,
     116             :     const OUString& sName)
     117             : {
     118           0 :     if (aIDMap.count(sName))
     119             :     {
     120             :         // we know this ID -> set property
     121           0 :         Any aAny;
     122           0 :         aAny <<= aIDMap[sName];
     123           0 :         xPropSet->setPropertyValue(sPropertyName, aAny);
     124             :     }
     125             :     else
     126             :     {
     127             :         // ID unknown -> into backpatch list for later fixup
     128           0 :         if (! aBackpatchListMap.count(sName))
     129             :         {
     130             :             // create backpatch list for this name
     131           0 :             BackpatchListType* pTmp = new BackpatchListType() ;
     132           0 :             aBackpatchListMap[sName] = (void*)pTmp;
     133             :         }
     134             : 
     135             :         // insert footnote
     136           0 :         ((BackpatchListType*)aBackpatchListMap[sName])->push_back(xPropSet);
     137             :     }
     138           0 : }
     139             : 
     140             : template<class A>
     141          26 : void XMLPropertyBackpatcher<A>::SetDefault()
     142             : {
     143          26 :     if (bDefaultHandling)
     144             :     {
     145             :         // not implemented yet
     146             :     }
     147          26 : }
     148             : 
     149             : // force instantiation of templates
     150             : template class XMLPropertyBackpatcher<sal_Int16>;
     151             : template class XMLPropertyBackpatcher<OUString>;
     152             : 
     153        4744 : struct XMLTextImportHelper::BackpatcherImpl
     154             : {
     155             :     /// backpatcher for references to footnotes and endnotes
     156             :     ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
     157             :         m_pFootnoteBackpatcher;
     158             : 
     159             :     /// backpatchers for references to sequences
     160             :     ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
     161             :         m_pSequenceIdBackpatcher;
     162             : 
     163             :     ::std::unique_ptr< XMLPropertyBackpatcher< OUString> >
     164             :         m_pSequenceNameBackpatcher;
     165             : };
     166             : 
     167             : ::boost::shared_ptr<XMLTextImportHelper::BackpatcherImpl>
     168        2372 : XMLTextImportHelper::MakeBackpatcherImpl()
     169             : {
     170             :     // n.b.: the shared_ptr stores the dtor!
     171        2372 :     return ::boost::shared_ptr<BackpatcherImpl>(new BackpatcherImpl);
     172             : }
     173             : 
     174          24 : static OUString const& GetSequenceNumber()
     175             : {
     176          24 :     static OUString s_SequenceNumber("SequenceNumber");
     177          24 :     return s_SequenceNumber;
     178             : }
     179             : 
     180             : 
     181             : // XMLTextImportHelper
     182             : 
     183             : // Code from XMLTextImportHelper using the XMLPropertyBackpatcher is
     184             : // implemented here. The reason is that in the unxsols2 environment,
     185             : // all templates are instatiated as file local (switch
     186             : // -instances=static), and thus are not accessible from the outside.
     187             : 
     188             : // The previous solution was to force additional instantiation of
     189             : // XMLPropertyBackpatcher in txtimp.cxx. This solution combines all
     190             : // usage of the XMLPropertyBackpatcher in XMLPropertyBackpatcher.cxx
     191             : // instead.
     192             : 
     193             : 
     194          36 : XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetFootnoteBP()
     195             : {
     196          36 :     if (!m_pBackpatcherImpl->m_pFootnoteBackpatcher.get())
     197             :     {
     198          22 :         m_pBackpatcherImpl->m_pFootnoteBackpatcher.reset(
     199          44 :             new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
     200             :     }
     201          36 :     return *m_pBackpatcherImpl->m_pFootnoteBackpatcher;
     202             : }
     203             : 
     204           4 : XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetSequenceIdBP()
     205             : {
     206           4 :     if (!m_pBackpatcherImpl->m_pSequenceIdBackpatcher.get())
     207             :     {
     208           2 :         m_pBackpatcherImpl->m_pSequenceIdBackpatcher.reset(
     209           4 :             new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
     210             :     }
     211           4 :     return *m_pBackpatcherImpl->m_pSequenceIdBackpatcher;
     212             : }
     213             : 
     214           4 : XMLPropertyBackpatcher<OUString>& XMLTextImportHelper::GetSequenceNameBP()
     215             : {
     216           4 :     static OUString s_SourceName("SourceName");
     217           4 :     if (!m_pBackpatcherImpl->m_pSequenceNameBackpatcher.get())
     218             :     {
     219           2 :         m_pBackpatcherImpl->m_pSequenceNameBackpatcher.reset(
     220           4 :             new XMLPropertyBackpatcher<OUString>(s_SourceName));
     221             :     }
     222           4 :     return *m_pBackpatcherImpl->m_pSequenceNameBackpatcher;
     223             : }
     224             : 
     225          36 : void XMLTextImportHelper::InsertFootnoteID(
     226             :     const OUString& sXMLId,
     227             :     sal_Int16 nAPIId)
     228             : {
     229          36 :     GetFootnoteBP().ResolveId(sXMLId, nAPIId);
     230          36 : }
     231             : 
     232           0 : void XMLTextImportHelper::ProcessFootnoteReference(
     233             :     const OUString& sXMLId,
     234             :     const Reference<XPropertySet> & xPropSet)
     235             : {
     236           0 :     GetFootnoteBP().SetProperty(xPropSet, sXMLId);
     237           0 : }
     238             : 
     239           4 : void XMLTextImportHelper::InsertSequenceID(
     240             :     const OUString& sXMLId,
     241             :     const OUString& sName,
     242             :     sal_Int16 nAPIId)
     243             : {
     244           4 :     GetSequenceIdBP().ResolveId(sXMLId, nAPIId);
     245           4 :     GetSequenceNameBP().ResolveId(sXMLId, sName);
     246           4 : }
     247             : 
     248           0 : void XMLTextImportHelper::ProcessSequenceReference(
     249             :     const OUString& sXMLId,
     250             :     const Reference<XPropertySet> & xPropSet)
     251             : {
     252           0 :     GetSequenceIdBP().SetProperty(xPropSet, sXMLId);
     253           0 :     GetSequenceNameBP().SetProperty(xPropSet, sXMLId);
     254           0 : }
     255             : 
     256             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10