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