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