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

Generated by: LCOV version 1.10