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 .
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/uno/Reference.h>
24 #include <rtl/ustring.hxx>
25 #include "XMLPropertyBackpatcher.hxx"
27 #include <xmloff/txtimp.hxx>
30 using ::com::sun::star::uno::Reference
;
31 using ::com::sun::star::uno::Any
;
32 using ::com::sun::star::beans::XPropertySet
;
36 XMLPropertyBackpatcher
<A
>::XMLPropertyBackpatcher(
38 : sPropertyName(std::move(sPropName
))
44 XMLPropertyBackpatcher
<A
>::~XMLPropertyBackpatcher()
50 void XMLPropertyBackpatcher
<A
>::ResolveId(
51 const OUString
& sName
,
54 // insert ID into ID map
55 aIDMap
[sName
] = aValue
;
57 // backpatch old references, if backpatch list exists
58 auto it
= aBackpatchListMap
.find(sName
);
59 if (it
== aBackpatchListMap
.end())
62 // aah, we have a backpatch list!
63 std::unique_ptr
<BackpatchListType
> pList
= std::move(it
->second
);
65 // a) remove list from list map
66 aBackpatchListMap
.erase(it
);
68 // b) for every item, set SequenceNumber
69 // (and preserve Property, if appropriate)
72 for(const auto& rBackpatch
: *pList
)
74 rBackpatch
->setPropertyValue(sPropertyName
, aAny
);
76 // else: no backpatch list -> then we're finished
80 void XMLPropertyBackpatcher
<A
>::SetProperty(
81 const Reference
<XPropertySet
> & xPropSet
,
82 const OUString
& sName
)
84 if (aIDMap
.count(sName
))
86 // we know this ID -> set property
87 xPropSet
->setPropertyValue(sPropertyName
, css::uno::Any(aIDMap
[sName
]));
91 // ID unknown -> into backpatch list for later fixup
92 if (! aBackpatchListMap
.count(sName
))
94 // create backpatch list for this name
95 aBackpatchListMap
.emplace(sName
, new BackpatchListType
);
99 aBackpatchListMap
[sName
]->push_back(xPropSet
);
103 // force instantiation of templates
104 template class XMLPropertyBackpatcher
<sal_Int16
>;
105 template class XMLPropertyBackpatcher
<OUString
>;
107 struct XMLTextImportHelper::BackpatcherImpl
109 /// backpatcher for references to footnotes and endnotes
110 ::std::unique_ptr
< XMLPropertyBackpatcher
<sal_Int16
> >
111 m_pFootnoteBackpatcher
;
113 /// backpatchers for references to sequences
114 ::std::unique_ptr
< XMLPropertyBackpatcher
<sal_Int16
> >
115 m_pSequenceIdBackpatcher
;
117 ::std::unique_ptr
< XMLPropertyBackpatcher
< OUString
> >
118 m_pSequenceNameBackpatcher
;
121 std::shared_ptr
<XMLTextImportHelper::BackpatcherImpl
>
122 XMLTextImportHelper::MakeBackpatcherImpl()
124 // n.b.: the shared_ptr stores the dtor!
125 return std::make_shared
<BackpatcherImpl
>();
128 static OUString
GetSequenceNumber()
130 return u
"SequenceNumber"_ustr
;
134 // XMLTextImportHelper
136 // Code from XMLTextImportHelper using the XMLPropertyBackpatcher is
137 // implemented here. The reason is that in the unxsols2 environment,
138 // all templates are instantiated as file local (switch
139 // -instances=static), and thus are not accessible from the outside.
141 // The previous solution was to force additional instantiation of
142 // XMLPropertyBackpatcher in txtimp.cxx. This solution combines all
143 // usage of the XMLPropertyBackpatcher in XMLPropertyBackpatcher.cxx
147 XMLPropertyBackpatcher
<sal_Int16
>& XMLTextImportHelper::GetFootnoteBP()
149 if (!m_xBackpatcherImpl
->m_pFootnoteBackpatcher
)
151 m_xBackpatcherImpl
->m_pFootnoteBackpatcher
.reset(
152 new XMLPropertyBackpatcher
<sal_Int16
>(GetSequenceNumber()));
154 return *m_xBackpatcherImpl
->m_pFootnoteBackpatcher
;
157 XMLPropertyBackpatcher
<sal_Int16
>& XMLTextImportHelper::GetSequenceIdBP()
159 if (!m_xBackpatcherImpl
->m_pSequenceIdBackpatcher
)
161 m_xBackpatcherImpl
->m_pSequenceIdBackpatcher
.reset(
162 new XMLPropertyBackpatcher
<sal_Int16
>(GetSequenceNumber()));
164 return *m_xBackpatcherImpl
->m_pSequenceIdBackpatcher
;
167 XMLPropertyBackpatcher
<OUString
>& XMLTextImportHelper::GetSequenceNameBP()
169 if (!m_xBackpatcherImpl
->m_pSequenceNameBackpatcher
)
171 m_xBackpatcherImpl
->m_pSequenceNameBackpatcher
.reset(
172 new XMLPropertyBackpatcher
<OUString
>(u
"SourceName"_ustr
));
174 return *m_xBackpatcherImpl
->m_pSequenceNameBackpatcher
;
177 void XMLTextImportHelper::InsertFootnoteID(
178 const OUString
& sXMLId
,
181 GetFootnoteBP().ResolveId(sXMLId
, nAPIId
);
184 void XMLTextImportHelper::ProcessFootnoteReference(
185 const OUString
& sXMLId
,
186 const Reference
<XPropertySet
> & xPropSet
)
188 GetFootnoteBP().SetProperty(xPropSet
, sXMLId
);
191 void XMLTextImportHelper::InsertSequenceID(
192 const OUString
& sXMLId
,
193 const OUString
& sName
,
196 GetSequenceIdBP().ResolveId(sXMLId
, nAPIId
);
197 GetSequenceNameBP().ResolveId(sXMLId
, sName
);
200 void XMLTextImportHelper::ProcessSequenceReference(
201 const OUString
& sXMLId
,
202 const Reference
<XPropertySet
> & xPropSet
)
204 GetSequenceIdBP().SetProperty(xPropSet
, sXMLId
);
205 GetSequenceNameBP().SetProperty(xPropSet
, sXMLId
);
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */