1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 using ::com::sun::star::uno::Reference
;
30 using ::com::sun::star::uno::Any
;
31 using ::com::sun::star::beans::XPropertySet
;
35 XMLPropertyBackpatcher
<A
>::XMLPropertyBackpatcher(
36 const OUString
& sPropName
)
37 : sPropertyName(sPropName
)
38 , bDefaultHandling(false)
39 , bPreserveProperty(false)
40 , sPreservePropertyName()
46 XMLPropertyBackpatcher
<A
>::~XMLPropertyBackpatcher()
53 void XMLPropertyBackpatcher
<A
>::ResolveId(
54 const OUString
& sName
,
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)
74 if (bPreserveProperty
)
77 for(BackpatchListType::iterator aIter
= pList
->begin();
78 aIter
!= pList
->end();
81 Reference
<XPropertySet
> xProp
= (*aIter
);
82 Any aPres
= xProp
->getPropertyValue(sPreservePropertyName
);
83 xProp
->setPropertyValue(sPropertyName
, aAny
);
84 xProp
->setPropertyValue(sPreservePropertyName
, aPres
);
90 for(BackpatchListType::iterator aIter
= pList
->begin();
91 aIter
!= pList
->end();
94 (*aIter
)->setPropertyValue(sPropertyName
, aAny
);
101 // else: no backpatch list -> then we're finished
105 void XMLPropertyBackpatcher
<A
>::SetProperty(
106 const Reference
<XPropertySet
> & xPropSet
,
107 const OUString
& sName
)
109 Reference
<XPropertySet
> xNonConstPropSet(xPropSet
);
110 SetProperty(xNonConstPropSet
, sName
);
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
122 aAny
<<= aIDMap
[sName
];
123 xPropSet
->setPropertyValue(sPropertyName
, aAny
);
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
;
136 static_cast<BackpatchListType
*>(aBackpatchListMap
[sName
])->push_back(xPropSet
);
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
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
,
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
,
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: */