1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx" 30*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 31*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <rtl/ustring.hxx> 34*cdf0e10cSrcweir #include <tools/debug.hxx> 35*cdf0e10cSrcweir #include "XMLPropertyBackpatcher.hxx" 36*cdf0e10cSrcweir #include <xmloff/txtimp.hxx> // XMLTextImportHelper partially implemented here 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir using ::rtl::OUString; 40*cdf0e10cSrcweir using ::std::vector; 41*cdf0e10cSrcweir using ::std::map; 42*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 43*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 44*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir template<class A> 48*cdf0e10cSrcweir XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher( 49*cdf0e10cSrcweir const ::rtl::OUString& sPropName) 50*cdf0e10cSrcweir : sPropertyName(sPropName) 51*cdf0e10cSrcweir , bDefaultHandling(sal_False) 52*cdf0e10cSrcweir , bPreserveProperty(sal_False) 53*cdf0e10cSrcweir , sPreservePropertyName() 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir } 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir template<class A> 58*cdf0e10cSrcweir XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher( 59*cdf0e10cSrcweir const OUString& sPropName, 60*cdf0e10cSrcweir const OUString& sPreserveName, 61*cdf0e10cSrcweir sal_Bool bDefault, 62*cdf0e10cSrcweir A aDef) 63*cdf0e10cSrcweir : sPropertyName(sPropName) 64*cdf0e10cSrcweir , bDefaultHandling(bDefault) 65*cdf0e10cSrcweir , bPreserveProperty(sPreserveName.getLength()>0) 66*cdf0e10cSrcweir , sPreservePropertyName(sPreserveName) 67*cdf0e10cSrcweir , aDefault(aDef) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir template<class A> 72*cdf0e10cSrcweir XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher( 73*cdf0e10cSrcweir const sal_Char* pPropName) 74*cdf0e10cSrcweir : bDefaultHandling(sal_False) 75*cdf0e10cSrcweir , bPreserveProperty(sal_False) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir DBG_ASSERT(pPropName != NULL, "need property name"); 78*cdf0e10cSrcweir sPropertyName = OUString::createFromAscii(pPropName); 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir template<class A> 82*cdf0e10cSrcweir XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher( 83*cdf0e10cSrcweir const sal_Char* pPropName, 84*cdf0e10cSrcweir const sal_Char* pPreservePropName, 85*cdf0e10cSrcweir sal_Bool bDefault, 86*cdf0e10cSrcweir A aDef) 87*cdf0e10cSrcweir : bDefaultHandling(bDefault) 88*cdf0e10cSrcweir , bPreserveProperty(pPreservePropName != NULL) 89*cdf0e10cSrcweir , aDefault(aDef) 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir DBG_ASSERT(pPropName != NULL, "need property name"); 92*cdf0e10cSrcweir sPropertyName = OUString::createFromAscii(pPropName); 93*cdf0e10cSrcweir if (pPreservePropName != NULL) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir sPreservePropertyName = OUString::createFromAscii(pPreservePropName); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir template<class A> 100*cdf0e10cSrcweir XMLPropertyBackpatcher<A>::~XMLPropertyBackpatcher() 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir SetDefault(); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir template<class A> 107*cdf0e10cSrcweir void XMLPropertyBackpatcher<A>::ResolveId( 108*cdf0e10cSrcweir const OUString& sName, 109*cdf0e10cSrcweir A aValue) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir // insert ID into ID map 112*cdf0e10cSrcweir aIDMap[sName] = aValue; 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir // backpatch old references, if backpatch list exists 115*cdf0e10cSrcweir if (aBackpatchListMap.count(sName)) 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir // aah, we have a backpatch list! 118*cdf0e10cSrcweir BackpatchListType* pList = 119*cdf0e10cSrcweir (BackpatchListType*)aBackpatchListMap[sName]; 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir // a) remove list from list map 122*cdf0e10cSrcweir aBackpatchListMap.erase(sName); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir // b) for every item, set SequenceNumber 125*cdf0e10cSrcweir // (and preserve Property, if appropriate) 126*cdf0e10cSrcweir Any aAny; 127*cdf0e10cSrcweir aAny <<= aValue; 128*cdf0e10cSrcweir if (bPreserveProperty) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir // preserve version 131*cdf0e10cSrcweir for(BackpatchListType::iterator aIter = pList->begin(); 132*cdf0e10cSrcweir aIter != pList->end(); 133*cdf0e10cSrcweir aIter++) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir Reference<XPropertySet> xProp = (*aIter); 136*cdf0e10cSrcweir Any aPres = xProp->getPropertyValue(sPreservePropertyName); 137*cdf0e10cSrcweir xProp->setPropertyValue(sPropertyName, aAny); 138*cdf0e10cSrcweir xProp->setPropertyValue(sPreservePropertyName, aPres); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir else 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir // without preserve 144*cdf0e10cSrcweir for(BackpatchListType::iterator aIter = pList->begin(); 145*cdf0e10cSrcweir aIter != pList->end(); 146*cdf0e10cSrcweir aIter++) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir (*aIter)->setPropertyValue(sPropertyName, aAny); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // c) delete list 153*cdf0e10cSrcweir delete pList; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir // else: no backpatch list -> then we're finished 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir template<class A> 159*cdf0e10cSrcweir void XMLPropertyBackpatcher<A>::SetProperty( 160*cdf0e10cSrcweir const Reference<XPropertySet> & xPropSet, 161*cdf0e10cSrcweir const OUString& sName) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir Reference<XPropertySet> xNonConstPropSet(xPropSet); 164*cdf0e10cSrcweir SetProperty(xNonConstPropSet, sName); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir template<class A> 168*cdf0e10cSrcweir void XMLPropertyBackpatcher<A>::SetProperty( 169*cdf0e10cSrcweir Reference<XPropertySet> & xPropSet, 170*cdf0e10cSrcweir const OUString& sName) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir if (aIDMap.count(sName)) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir // we know this ID -> set property 175*cdf0e10cSrcweir Any aAny; 176*cdf0e10cSrcweir aAny <<= aIDMap[sName]; 177*cdf0e10cSrcweir xPropSet->setPropertyValue(sPropertyName, aAny); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir else 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir // ID unknown -> into backpatch list for later fixup 182*cdf0e10cSrcweir if (! aBackpatchListMap.count(sName)) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir // create backpatch list for this name 185*cdf0e10cSrcweir BackpatchListType* pTmp = new BackpatchListType() ; 186*cdf0e10cSrcweir aBackpatchListMap[sName] = (void*)pTmp; 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir // insert footnote 190*cdf0e10cSrcweir ((BackpatchListType*)aBackpatchListMap[sName])->push_back(xPropSet); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir template<class A> 195*cdf0e10cSrcweir void XMLPropertyBackpatcher<A>::SetDefault() 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir if (bDefaultHandling) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir // not implemented yet 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir // force instantiation of templates 204*cdf0e10cSrcweir template class XMLPropertyBackpatcher<sal_Int16>; 205*cdf0e10cSrcweir template class XMLPropertyBackpatcher<OUString>; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir struct SAL_DLLPRIVATE XMLTextImportHelper::BackpatcherImpl 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir /// backpatcher for references to footnotes and endnotes 210*cdf0e10cSrcweir ::std::auto_ptr< XMLPropertyBackpatcher<sal_Int16> > 211*cdf0e10cSrcweir m_pFootnoteBackpatcher; 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir /// backpatchers for references to sequences 214*cdf0e10cSrcweir ::std::auto_ptr< XMLPropertyBackpatcher<sal_Int16> > 215*cdf0e10cSrcweir m_pSequenceIdBackpatcher; 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir ::std::auto_ptr< XMLPropertyBackpatcher< ::rtl::OUString> > 218*cdf0e10cSrcweir m_pSequenceNameBackpatcher; 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir }; 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir ::boost::shared_ptr<XMLTextImportHelper::BackpatcherImpl> 223*cdf0e10cSrcweir XMLTextImportHelper::MakeBackpatcherImpl() 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir // n.b.: the shared_ptr stores the dtor! 226*cdf0e10cSrcweir return ::boost::shared_ptr<BackpatcherImpl>(new BackpatcherImpl); 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir static ::rtl::OUString const& GetSequenceNumber() 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir static ::rtl::OUString s_SequenceNumber( 232*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("SequenceNumber")); 233*cdf0e10cSrcweir return s_SequenceNumber; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir // 237*cdf0e10cSrcweir // XMLTextImportHelper 238*cdf0e10cSrcweir // 239*cdf0e10cSrcweir // Code from XMLTextImportHelper using the XMLPropertyBackpatcher is 240*cdf0e10cSrcweir // implemented here. The reason is that in the unxsols2 environment, 241*cdf0e10cSrcweir // all templates are instatiated as file local (switch 242*cdf0e10cSrcweir // -instances=static), and thus are not accessible from the outside. 243*cdf0e10cSrcweir // 244*cdf0e10cSrcweir // The previous solution was to force additional instantiation of 245*cdf0e10cSrcweir // XMLPropertyBackpatcher in txtimp.cxx. This solution combines all 246*cdf0e10cSrcweir // usage of the XMLPropertyBackpatcher in XMLPropertyBackpatcher.cxx 247*cdf0e10cSrcweir // instead. 248*cdf0e10cSrcweir // 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetFootnoteBP() 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir if (!m_pBackpatcherImpl->m_pFootnoteBackpatcher.get()) 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir m_pBackpatcherImpl->m_pFootnoteBackpatcher.reset( 255*cdf0e10cSrcweir new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber())); 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir return *m_pBackpatcherImpl->m_pFootnoteBackpatcher; 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetSequenceIdBP() 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir if (!m_pBackpatcherImpl->m_pSequenceIdBackpatcher.get()) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir m_pBackpatcherImpl->m_pSequenceIdBackpatcher.reset( 265*cdf0e10cSrcweir new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber())); 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir return *m_pBackpatcherImpl->m_pSequenceIdBackpatcher; 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir XMLPropertyBackpatcher<OUString>& XMLTextImportHelper::GetSequenceNameBP() 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir static ::rtl::OUString s_SourceName( 273*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("SourceName")); 274*cdf0e10cSrcweir if (!m_pBackpatcherImpl->m_pSequenceNameBackpatcher.get()) 275*cdf0e10cSrcweir { 276*cdf0e10cSrcweir m_pBackpatcherImpl->m_pSequenceNameBackpatcher.reset( 277*cdf0e10cSrcweir new XMLPropertyBackpatcher<OUString>(s_SourceName)); 278*cdf0e10cSrcweir } 279*cdf0e10cSrcweir return *m_pBackpatcherImpl->m_pSequenceNameBackpatcher; 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir void XMLTextImportHelper::InsertFootnoteID( 283*cdf0e10cSrcweir const OUString& sXMLId, 284*cdf0e10cSrcweir sal_Int16 nAPIId) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir GetFootnoteBP().ResolveId(sXMLId, nAPIId); 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir void XMLTextImportHelper::ProcessFootnoteReference( 290*cdf0e10cSrcweir const OUString& sXMLId, 291*cdf0e10cSrcweir const Reference<XPropertySet> & xPropSet) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir GetFootnoteBP().SetProperty(xPropSet, sXMLId); 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir void XMLTextImportHelper::InsertSequenceID( 297*cdf0e10cSrcweir const OUString& sXMLId, 298*cdf0e10cSrcweir const OUString& sName, 299*cdf0e10cSrcweir sal_Int16 nAPIId) 300*cdf0e10cSrcweir { 301*cdf0e10cSrcweir GetSequenceIdBP().ResolveId(sXMLId, nAPIId); 302*cdf0e10cSrcweir GetSequenceNameBP().ResolveId(sXMLId, sName); 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir void XMLTextImportHelper::ProcessSequenceReference( 306*cdf0e10cSrcweir const OUString& sXMLId, 307*cdf0e10cSrcweir const Reference<XPropertySet> & xPropSet) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir GetSequenceIdBP().SetProperty(xPropSet, sXMLId); 310*cdf0e10cSrcweir GetSequenceNameBP().SetProperty(xPropSet, sXMLId); 311*cdf0e10cSrcweir } 312*cdf0e10cSrcweir 313