bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLPropertyBackpatcher.cxx
blob5844b0937e581e8dc0eba939e2cd24b57ec3b24b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/uno/Reference.h>
23 #include <rtl/ustring.hxx>
24 #include "XMLPropertyBackpatcher.hxx"
25 #include <xmloff/txtimp.hxx>
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;
34 template<class A>
35 XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
36 const OUString& sPropName)
37 : sPropertyName(sPropName)
38 , bDefaultHandling(false)
39 , bPreserveProperty(false)
40 , sPreservePropertyName()
45 template<class A>
46 XMLPropertyBackpatcher<A>::~XMLPropertyBackpatcher()
48 SetDefault();
52 template<class A>
53 void XMLPropertyBackpatcher<A>::ResolveId(
54 const OUString& sName,
55 A aValue)
57 // insert ID into ID map
58 aIDMap[sName] = aValue;
60 // backpatch old references, if backpatch list exists
61 if (aBackpatchListMap.count(sName))
63 // aah, we have a backpatch list!
64 BackpatchListType* pList =
65 static_cast<BackpatchListType*>(aBackpatchListMap[sName]);
67 // a) remove list from list map
68 aBackpatchListMap.erase(sName);
70 // b) for every item, set SequenceNumber
71 // (and preserve Property, if appropriate)
72 Any aAny;
73 aAny <<= aValue;
74 if (bPreserveProperty)
76 // preserve version
77 for(BackpatchListType::iterator aIter = pList->begin();
78 aIter != pList->end();
79 ++aIter)
81 Reference<XPropertySet> xProp = (*aIter);
82 Any aPres = xProp->getPropertyValue(sPreservePropertyName);
83 xProp->setPropertyValue(sPropertyName, aAny);
84 xProp->setPropertyValue(sPreservePropertyName, aPres);
87 else
89 // without preserve
90 for(BackpatchListType::iterator aIter = pList->begin();
91 aIter != pList->end();
92 ++aIter)
94 (*aIter)->setPropertyValue(sPropertyName, aAny);
98 // c) delete list
99 delete pList;
101 // else: no backpatch list -> then we're finished
104 template<class A>
105 void XMLPropertyBackpatcher<A>::SetProperty(
106 const Reference<XPropertySet> & xPropSet,
107 const OUString& sName)
109 Reference<XPropertySet> xNonConstPropSet(xPropSet);
110 SetProperty(xNonConstPropSet, sName);
113 template<class A>
114 void XMLPropertyBackpatcher<A>::SetProperty(
115 Reference<XPropertySet> & xPropSet,
116 const OUString& sName)
118 if (aIDMap.count(sName))
120 // we know this ID -> set property
121 Any aAny;
122 aAny <<= aIDMap[sName];
123 xPropSet->setPropertyValue(sPropertyName, aAny);
125 else
127 // ID unknown -> into backpatch list for later fixup
128 if (! aBackpatchListMap.count(sName))
130 // create backpatch list for this name
131 BackpatchListType* pTmp = new BackpatchListType() ;
132 aBackpatchListMap[sName] = (void*)pTmp;
135 // insert footnote
136 static_cast<BackpatchListType*>(aBackpatchListMap[sName])->push_back(xPropSet);
140 template<class A>
141 void XMLPropertyBackpatcher<A>::SetDefault()
143 if (bDefaultHandling)
145 // not implemented yet
149 // force instantiation of templates
150 template class XMLPropertyBackpatcher<sal_Int16>;
151 template class XMLPropertyBackpatcher<OUString>;
153 struct XMLTextImportHelper::BackpatcherImpl
155 /// backpatcher for references to footnotes and endnotes
156 ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
157 m_pFootnoteBackpatcher;
159 /// backpatchers for references to sequences
160 ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
161 m_pSequenceIdBackpatcher;
163 ::std::unique_ptr< XMLPropertyBackpatcher< OUString> >
164 m_pSequenceNameBackpatcher;
167 std::shared_ptr<XMLTextImportHelper::BackpatcherImpl>
168 XMLTextImportHelper::MakeBackpatcherImpl()
170 // n.b.: the shared_ptr stores the dtor!
171 return std::shared_ptr<BackpatcherImpl>(new BackpatcherImpl);
174 static OUString GetSequenceNumber()
176 return OUString("SequenceNumber");
180 // XMLTextImportHelper
182 // Code from XMLTextImportHelper using the XMLPropertyBackpatcher is
183 // implemented here. The reason is that in the unxsols2 environment,
184 // all templates are instatiated as file local (switch
185 // -instances=static), and thus are not accessible from the outside.
187 // The previous solution was to force additional instantiation of
188 // XMLPropertyBackpatcher in txtimp.cxx. This solution combines all
189 // usage of the XMLPropertyBackpatcher in XMLPropertyBackpatcher.cxx
190 // instead.
193 XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetFootnoteBP()
195 if (!m_xBackpatcherImpl->m_pFootnoteBackpatcher.get())
197 m_xBackpatcherImpl->m_pFootnoteBackpatcher.reset(
198 new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
200 return *m_xBackpatcherImpl->m_pFootnoteBackpatcher;
203 XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetSequenceIdBP()
205 if (!m_xBackpatcherImpl->m_pSequenceIdBackpatcher.get())
207 m_xBackpatcherImpl->m_pSequenceIdBackpatcher.reset(
208 new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
210 return *m_xBackpatcherImpl->m_pSequenceIdBackpatcher;
213 XMLPropertyBackpatcher<OUString>& XMLTextImportHelper::GetSequenceNameBP()
215 static const char s_SourceName[] = "SourceName";
216 if (!m_xBackpatcherImpl->m_pSequenceNameBackpatcher.get())
218 m_xBackpatcherImpl->m_pSequenceNameBackpatcher.reset(
219 new XMLPropertyBackpatcher<OUString>(s_SourceName));
221 return *m_xBackpatcherImpl->m_pSequenceNameBackpatcher;
224 void XMLTextImportHelper::InsertFootnoteID(
225 const OUString& sXMLId,
226 sal_Int16 nAPIId)
228 GetFootnoteBP().ResolveId(sXMLId, nAPIId);
231 void XMLTextImportHelper::ProcessFootnoteReference(
232 const OUString& sXMLId,
233 const Reference<XPropertySet> & xPropSet)
235 GetFootnoteBP().SetProperty(xPropSet, sXMLId);
238 void XMLTextImportHelper::InsertSequenceID(
239 const OUString& sXMLId,
240 const OUString& sName,
241 sal_Int16 nAPIId)
243 GetSequenceIdBP().ResolveId(sXMLId, nAPIId);
244 GetSequenceNameBP().ResolveId(sXMLId, sName);
247 void XMLTextImportHelper::ProcessSequenceReference(
248 const OUString& sXMLId,
249 const Reference<XPropertySet> & xPropSet)
251 GetSequenceIdBP().SetProperty(xPropSet, sXMLId);
252 GetSequenceNameBP().SetProperty(xPropSet, sXMLId);
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */