merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / text / XMLPropertyBackpatcher.cxx
bloba8c52313a909ca2b80d70424b7931bd4c456bc06
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLPropertyBackpatcher.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/uno/Reference.h>
36 #ifndef _RTL_USTRING
37 #include <rtl/ustring.hxx>
38 #endif
39 #include <tools/debug.hxx>
40 #include "XMLPropertyBackpatcher.hxx"
41 #include <xmloff/txtimp.hxx> // XMLTextImportHelper partially implemented here
44 using ::rtl::OUString;
45 using ::std::vector;
46 using ::std::map;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Any;
49 using ::com::sun::star::beans::XPropertySet;
52 template<class A>
53 XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
54 const ::rtl::OUString& sPropName)
55 : sPropertyName(sPropName)
56 , bDefaultHandling(sal_False)
57 , bPreserveProperty(sal_False)
58 , sPreservePropertyName()
62 template<class A>
63 XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
64 const OUString& sPropName,
65 const OUString& sPreserveName,
66 sal_Bool bDefault,
67 A aDef)
68 : sPropertyName(sPropName)
69 , bDefaultHandling(bDefault)
70 , bPreserveProperty(sPreserveName.getLength()>0)
71 , sPreservePropertyName(sPreserveName)
72 , aDefault(aDef)
76 template<class A>
77 XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
78 const sal_Char* pPropName)
79 : bDefaultHandling(sal_False)
80 , bPreserveProperty(sal_False)
82 DBG_ASSERT(pPropName != NULL, "need property name");
83 sPropertyName = OUString::createFromAscii(pPropName);
86 template<class A>
87 XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
88 const sal_Char* pPropName,
89 const sal_Char* pPreservePropName,
90 sal_Bool bDefault,
91 A aDef)
92 : bDefaultHandling(bDefault)
93 , bPreserveProperty(pPreservePropName != NULL)
94 , aDefault(aDef)
96 DBG_ASSERT(pPropName != NULL, "need property name");
97 sPropertyName = OUString::createFromAscii(pPropName);
98 if (pPreservePropName != NULL)
100 sPreservePropertyName = OUString::createFromAscii(pPreservePropName);
104 template<class A>
105 XMLPropertyBackpatcher<A>::~XMLPropertyBackpatcher()
107 SetDefault();
111 template<class A>
112 void XMLPropertyBackpatcher<A>::ResolveId(
113 const OUString& sName,
114 A aValue)
116 // insert ID into ID map
117 aIDMap[sName] = aValue;
119 // backpatch old references, if backpatch list exists
120 if (aBackpatchListMap.count(sName))
122 // aah, we have a backpatch list!
123 BackpatchListType* pList =
124 (BackpatchListType*)aBackpatchListMap[sName];
126 // a) remove list from list map
127 aBackpatchListMap.erase(sName);
129 // b) for every item, set SequenceNumber
130 // (and preserve Property, if appropriate)
131 Any aAny;
132 aAny <<= aValue;
133 if (bPreserveProperty)
135 // preserve version
136 for(BackpatchListType::iterator aIter = pList->begin();
137 aIter != pList->end();
138 aIter++)
140 Reference<XPropertySet> xProp = (*aIter);
141 Any aPres = xProp->getPropertyValue(sPreservePropertyName);
142 xProp->setPropertyValue(sPropertyName, aAny);
143 xProp->setPropertyValue(sPreservePropertyName, aPres);
146 else
148 // without preserve
149 for(BackpatchListType::iterator aIter = pList->begin();
150 aIter != pList->end();
151 aIter++)
153 (*aIter)->setPropertyValue(sPropertyName, aAny);
157 // c) delete list
158 delete pList;
160 // else: no backpatch list -> then we're finished
163 template<class A>
164 void XMLPropertyBackpatcher<A>::SetProperty(
165 const Reference<XPropertySet> & xPropSet,
166 const OUString& sName)
168 Reference<XPropertySet> xNonConstPropSet(xPropSet);
169 SetProperty(xNonConstPropSet, sName);
172 template<class A>
173 void XMLPropertyBackpatcher<A>::SetProperty(
174 Reference<XPropertySet> & xPropSet,
175 const OUString& sName)
177 if (aIDMap.count(sName))
179 // we know this ID -> set property
180 Any aAny;
181 aAny <<= aIDMap[sName];
182 xPropSet->setPropertyValue(sPropertyName, aAny);
184 else
186 // ID unknown -> into backpatch list for later fixup
187 if (! aBackpatchListMap.count(sName))
189 // create backpatch list for this name
190 BackpatchListType* pTmp = new BackpatchListType() ;
191 aBackpatchListMap[sName] = (void*)pTmp;
194 // insert footnote
195 ((BackpatchListType*)aBackpatchListMap[sName])->push_back(xPropSet);
199 template<class A>
200 void XMLPropertyBackpatcher<A>::SetDefault()
202 if (bDefaultHandling)
204 // not implemented yet
208 // force instantiation of templates
209 template class XMLPropertyBackpatcher<sal_Int16>;
210 template class XMLPropertyBackpatcher<OUString>;
215 // XMLTextImportHelper
217 // Code from XMLTextImportHelper using the XMLPropertyBackpatcher is
218 // implemented here. The reason is that in the unxsols2 environment,
219 // all templates are instatiated as file local (switch
220 // -instances=static), and thus are not accessible from the outside.
222 // The previous solution was to force additional instantiation of
223 // XMLPropertyBackpatcher in txtimp.cxx. This solution combines all
224 // usage of the XMLPropertyBackpatcher in XMLPropertyBackpatcher.cxx
225 // instead.
228 XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetFootnoteBP()
230 if (NULL == pFootnoteBackpatcher)
232 pFootnoteBackpatcher =
233 new XMLPropertyBackpatcher<sal_Int16>(sSequenceNumber);
235 return *pFootnoteBackpatcher;
238 XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetSequenceIdBP()
240 if (NULL == pSequenceIdBackpatcher)
242 pSequenceIdBackpatcher =
243 new XMLPropertyBackpatcher<sal_Int16>(sSequenceNumber);
245 return *pSequenceIdBackpatcher;
248 XMLPropertyBackpatcher<OUString>& XMLTextImportHelper::GetSequenceNameBP()
250 if (NULL == pSequenceNameBackpatcher)
252 pSequenceNameBackpatcher =
253 new XMLPropertyBackpatcher<OUString>(sSourceName);
255 return *pSequenceNameBackpatcher;
258 void XMLTextImportHelper::InsertFootnoteID(
259 const OUString& sXMLId,
260 sal_Int16 nAPIId)
262 GetFootnoteBP().ResolveId(sXMLId, nAPIId);
265 void XMLTextImportHelper::ProcessFootnoteReference(
266 const OUString& sXMLId,
267 const Reference<XPropertySet> & xPropSet)
269 GetFootnoteBP().SetProperty(xPropSet, sXMLId);
272 void XMLTextImportHelper::InsertSequenceID(
273 const OUString& sXMLId,
274 const OUString& sName,
275 sal_Int16 nAPIId)
277 GetSequenceIdBP().ResolveId(sXMLId, nAPIId);
278 GetSequenceNameBP().ResolveId(sXMLId, sName);
281 void XMLTextImportHelper::ProcessSequenceReference(
282 const OUString& sXMLId,
283 const Reference<XPropertySet> & xPropSet)
285 GetSequenceIdBP().SetProperty(xPropSet, sXMLId);
286 GetSequenceNameBP().SetProperty(xPropSet, sXMLId);
289 void XMLTextImportHelper::_FinitBackpatcher()
291 delete pFootnoteBackpatcher;
292 delete pSequenceIdBackpatcher;
293 delete pSequenceNameBackpatcher;