LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / xmloff / source / text / XMLPropertyBackpatcher.cxx
bloba2e4ac3a6fc0ab20fc9138f3ed94a57c88c2d11b
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 <memory>
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"
26 #include <xmloff/txtimp.hxx>
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)
42 template<class A>
43 XMLPropertyBackpatcher<A>::~XMLPropertyBackpatcher()
48 template<class A>
49 void XMLPropertyBackpatcher<A>::ResolveId(
50 const OUString& sName,
51 A aValue)
53 // insert ID into ID map
54 aIDMap[sName] = aValue;
56 // backpatch old references, if backpatch list exists
57 auto it = aBackpatchListMap.find(sName);
58 if (it == aBackpatchListMap.end())
59 return;
61 // aah, we have a backpatch list!
62 std::unique_ptr<BackpatchListType> pList = std::move(it->second);
64 // a) remove list from list map
65 aBackpatchListMap.erase(it);
67 // b) for every item, set SequenceNumber
68 // (and preserve Property, if appropriate)
69 Any aAny;
70 aAny <<= aValue;
71 for(const auto& rBackpatch : *pList)
73 rBackpatch->setPropertyValue(sPropertyName, aAny);
75 // else: no backpatch list -> then we're finished
78 template<class A>
79 void XMLPropertyBackpatcher<A>::SetProperty(
80 const Reference<XPropertySet> & xPropSet,
81 const OUString& sName)
83 if (aIDMap.count(sName))
85 // we know this ID -> set property
86 xPropSet->setPropertyValue(sPropertyName, css::uno::Any(aIDMap[sName]));
88 else
90 // ID unknown -> into backpatch list for later fixup
91 if (! aBackpatchListMap.count(sName))
93 // create backpatch list for this name
94 aBackpatchListMap.emplace(sName, new BackpatchListType);
97 // insert footnote
98 aBackpatchListMap[sName]->push_back(xPropSet);
102 // force instantiation of templates
103 template class XMLPropertyBackpatcher<sal_Int16>;
104 template class XMLPropertyBackpatcher<OUString>;
106 struct XMLTextImportHelper::BackpatcherImpl
108 /// backpatcher for references to footnotes and endnotes
109 ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
110 m_pFootnoteBackpatcher;
112 /// backpatchers for references to sequences
113 ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
114 m_pSequenceIdBackpatcher;
116 ::std::unique_ptr< XMLPropertyBackpatcher< OUString> >
117 m_pSequenceNameBackpatcher;
120 std::shared_ptr<XMLTextImportHelper::BackpatcherImpl>
121 XMLTextImportHelper::MakeBackpatcherImpl()
123 // n.b.: the shared_ptr stores the dtor!
124 return std::make_shared<BackpatcherImpl>();
127 static OUString GetSequenceNumber()
129 return "SequenceNumber";
133 // XMLTextImportHelper
135 // Code from XMLTextImportHelper using the XMLPropertyBackpatcher is
136 // implemented here. The reason is that in the unxsols2 environment,
137 // all templates are instantiated as file local (switch
138 // -instances=static), and thus are not accessible from the outside.
140 // The previous solution was to force additional instantiation of
141 // XMLPropertyBackpatcher in txtimp.cxx. This solution combines all
142 // usage of the XMLPropertyBackpatcher in XMLPropertyBackpatcher.cxx
143 // instead.
146 XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetFootnoteBP()
148 if (!m_xBackpatcherImpl->m_pFootnoteBackpatcher)
150 m_xBackpatcherImpl->m_pFootnoteBackpatcher.reset(
151 new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
153 return *m_xBackpatcherImpl->m_pFootnoteBackpatcher;
156 XMLPropertyBackpatcher<sal_Int16>& XMLTextImportHelper::GetSequenceIdBP()
158 if (!m_xBackpatcherImpl->m_pSequenceIdBackpatcher)
160 m_xBackpatcherImpl->m_pSequenceIdBackpatcher.reset(
161 new XMLPropertyBackpatcher<sal_Int16>(GetSequenceNumber()));
163 return *m_xBackpatcherImpl->m_pSequenceIdBackpatcher;
166 XMLPropertyBackpatcher<OUString>& XMLTextImportHelper::GetSequenceNameBP()
168 if (!m_xBackpatcherImpl->m_pSequenceNameBackpatcher)
170 m_xBackpatcherImpl->m_pSequenceNameBackpatcher.reset(
171 new XMLPropertyBackpatcher<OUString>("SourceName"));
173 return *m_xBackpatcherImpl->m_pSequenceNameBackpatcher;
176 void XMLTextImportHelper::InsertFootnoteID(
177 const OUString& sXMLId,
178 sal_Int16 nAPIId)
180 GetFootnoteBP().ResolveId(sXMLId, nAPIId);
183 void XMLTextImportHelper::ProcessFootnoteReference(
184 const OUString& sXMLId,
185 const Reference<XPropertySet> & xPropSet)
187 GetFootnoteBP().SetProperty(xPropSet, sXMLId);
190 void XMLTextImportHelper::InsertSequenceID(
191 const OUString& sXMLId,
192 const OUString& sName,
193 sal_Int16 nAPIId)
195 GetSequenceIdBP().ResolveId(sXMLId, nAPIId);
196 GetSequenceNameBP().ResolveId(sXMLId, sName);
199 void XMLTextImportHelper::ProcessSequenceReference(
200 const OUString& sXMLId,
201 const Reference<XPropertySet> & xPropSet)
203 GetSequenceIdBP().SetProperty(xPropSet, sXMLId);
204 GetSequenceNameBP().SetProperty(xPropSet, sXMLId);
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */