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 "XMLPropertyBackpatcher.hxx"
25 : #include <xmloff/txtimp.hxx>
26 :
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;
32 :
33 :
34 : template<class A>
35 20 : XMLPropertyBackpatcher<A>::XMLPropertyBackpatcher(
36 : const OUString& sPropName)
37 : : sPropertyName(sPropName)
38 : , bDefaultHandling(false)
39 : , bPreserveProperty(false)
40 20 : , sPreservePropertyName()
41 : {
42 20 : }
43 :
44 :
45 : template<class A>
46 20 : XMLPropertyBackpatcher<A>::~XMLPropertyBackpatcher()
47 : {
48 20 : SetDefault();
49 20 : }
50 :
51 :
52 : template<class A>
53 37 : void XMLPropertyBackpatcher<A>::ResolveId(
54 : const OUString& sName,
55 : A aValue)
56 : {
57 : // insert ID into ID map
58 37 : aIDMap[sName] = aValue;
59 :
60 : // backpatch old references, if backpatch list exists
61 37 : if (aBackpatchListMap.count(sName))
62 : {
63 : // aah, we have a backpatch list!
64 : BackpatchListType* pList =
65 0 : static_cast<BackpatchListType*>(aBackpatchListMap[sName]);
66 :
67 : // a) remove list from list map
68 0 : aBackpatchListMap.erase(sName);
69 :
70 : // b) for every item, set SequenceNumber
71 : // (and preserve Property, if appropriate)
72 0 : Any aAny;
73 0 : aAny <<= aValue;
74 0 : if (bPreserveProperty)
75 : {
76 : // preserve version
77 0 : for(BackpatchListType::iterator aIter = pList->begin();
78 0 : aIter != pList->end();
79 : ++aIter)
80 : {
81 0 : Reference<XPropertySet> xProp = (*aIter);
82 0 : Any aPres = xProp->getPropertyValue(sPreservePropertyName);
83 0 : xProp->setPropertyValue(sPropertyName, aAny);
84 0 : xProp->setPropertyValue(sPreservePropertyName, aPres);
85 : }
86 : }
87 : else
88 : {
89 : // without preserve
90 0 : for(BackpatchListType::iterator aIter = pList->begin();
91 0 : aIter != pList->end();
92 : ++aIter)
93 : {
94 0 : (*aIter)->setPropertyValue(sPropertyName, aAny);
95 : }
96 : }
97 :
98 : // c) delete list
99 0 : delete pList;
100 : }
101 : // else: no backpatch list -> then we're finished
102 37 : }
103 :
104 : template<class A>
105 4 : void XMLPropertyBackpatcher<A>::SetProperty(
106 : const Reference<XPropertySet> & xPropSet,
107 : const OUString& sName)
108 : {
109 4 : Reference<XPropertySet> xNonConstPropSet(xPropSet);
110 4 : SetProperty(xNonConstPropSet, sName);
111 4 : }
112 :
113 : template<class A>
114 4 : void XMLPropertyBackpatcher<A>::SetProperty(
115 : Reference<XPropertySet> & xPropSet,
116 : const OUString& sName)
117 : {
118 4 : if (aIDMap.count(sName))
119 : {
120 : // we know this ID -> set property
121 0 : Any aAny;
122 0 : aAny <<= aIDMap[sName];
123 0 : xPropSet->setPropertyValue(sPropertyName, aAny);
124 : }
125 : else
126 : {
127 : // ID unknown -> into backpatch list for later fixup
128 4 : if (! aBackpatchListMap.count(sName))
129 : {
130 : // create backpatch list for this name
131 2 : BackpatchListType* pTmp = new BackpatchListType() ;
132 2 : aBackpatchListMap[sName] = static_cast<void*>(pTmp);
133 : }
134 :
135 : // insert footnote
136 4 : static_cast<BackpatchListType*>(aBackpatchListMap[sName])->push_back(xPropSet);
137 : }
138 4 : }
139 :
140 : template<class A>
141 20 : void XMLPropertyBackpatcher<A>::SetDefault()
142 : {
143 20 : if (bDefaultHandling)
144 : {
145 : // not implemented yet
146 : }
147 20 : }
148 :
149 : // force instantiation of templates
150 : template class XMLPropertyBackpatcher<sal_Int16>;
151 : template class XMLPropertyBackpatcher<OUString>;
152 :
153 3626 : struct XMLTextImportHelper::BackpatcherImpl
154 : {
155 : /// backpatcher for references to footnotes and endnotes
156 : ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
157 : m_pFootnoteBackpatcher;
158 :
159 : /// backpatchers for references to sequences
160 : ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
161 : m_pSequenceIdBackpatcher;
162 :
163 : ::std::unique_ptr< XMLPropertyBackpatcher< OUString> >
164 : m_pSequenceNameBackpatcher;
165 : };
166 :
167 : std::shared_ptr<XMLTextImportHelper::BackpatcherImpl>
168 1813 : XMLTextImportHelper::MakeBackpatcherImpl()
169 : {
170 : // n.b.: the shared_ptr stores the dtor!
171 1813 : return std::shared_ptr<BackpatcherImpl>(new BackpatcherImpl);
172 : }
173 :
174 17 : static OUString GetSequenceNumber()
175 : {
176 17 : return OUString("SequenceNumber");
177 : }
178 :
179 :
180 : // XMLTextImportHelper
181 :
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.
186 :
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.
191 :
192 :
193 23 : XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetFootnoteBP()
194 : {
195 23 : if (!m_xBackpatcherImpl->m_pFootnoteBackpatcher.get())
196 : {
197 14 : m_xBackpatcherImpl->m_pFootnoteBackpatcher.reset(
198 28 : new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
199 : }
200 23 : return *m_xBackpatcherImpl->m_pFootnoteBackpatcher;
201 : }
202 :
203 9 : XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetSequenceIdBP()
204 : {
205 9 : if (!m_xBackpatcherImpl->m_pSequenceIdBackpatcher.get())
206 : {
207 3 : m_xBackpatcherImpl->m_pSequenceIdBackpatcher.reset(
208 6 : new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
209 : }
210 9 : return *m_xBackpatcherImpl->m_pSequenceIdBackpatcher;
211 : }
212 :
213 9 : XMLPropertyBackpatcher<OUString>& XMLTextImportHelper::GetSequenceNameBP()
214 : {
215 : static const char s_SourceName[] = "SourceName";
216 9 : if (!m_xBackpatcherImpl->m_pSequenceNameBackpatcher.get())
217 : {
218 3 : m_xBackpatcherImpl->m_pSequenceNameBackpatcher.reset(
219 6 : new XMLPropertyBackpatcher<OUString>(s_SourceName));
220 : }
221 9 : return *m_xBackpatcherImpl->m_pSequenceNameBackpatcher;
222 : }
223 :
224 23 : void XMLTextImportHelper::InsertFootnoteID(
225 : const OUString& sXMLId,
226 : sal_Int16 nAPIId)
227 : {
228 23 : GetFootnoteBP().ResolveId(sXMLId, nAPIId);
229 23 : }
230 :
231 0 : void XMLTextImportHelper::ProcessFootnoteReference(
232 : const OUString& sXMLId,
233 : const Reference<XPropertySet> & xPropSet)
234 : {
235 0 : GetFootnoteBP().SetProperty(xPropSet, sXMLId);
236 0 : }
237 :
238 7 : void XMLTextImportHelper::InsertSequenceID(
239 : const OUString& sXMLId,
240 : const OUString& sName,
241 : sal_Int16 nAPIId)
242 : {
243 7 : GetSequenceIdBP().ResolveId(sXMLId, nAPIId);
244 7 : GetSequenceNameBP().ResolveId(sXMLId, sName);
245 7 : }
246 :
247 2 : void XMLTextImportHelper::ProcessSequenceReference(
248 : const OUString& sXMLId,
249 : const Reference<XPropertySet> & xPropSet)
250 : {
251 2 : GetSequenceIdBP().SetProperty(xPropSet, sXMLId);
252 2 : GetSequenceNameBP().SetProperty(xPropSet, sXMLId);
253 2 : }
254 :
255 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|