LibreOffice Module xmloff (master)  1
XMLPropertyBackpatcher.cxx
Go to the documentation of this file.
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 <memory>
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/uno/Reference.h>
23 
24 #include <rtl/ustring.hxx>
26 #include <xmloff/txtimp.hxx>
27 
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>
36  const OUString& sPropName)
37 : sPropertyName(sPropName)
38 {
39 }
40 
41 
42 template<class A>
44 {
45 }
46 
47 
48 template<class A>
50  const OUString& sName,
51  A aValue)
52 {
53  // insert ID into ID map
54  aIDMap[sName] = aValue;
55 
56  // backpatch old references, if backpatch list exists
57  auto it = aBackpatchListMap.find(sName);
58  if (it == aBackpatchListMap.end())
59  return;
60 
61  // aah, we have a backpatch list!
62  std::unique_ptr<BackpatchListType> pList = std::move(it->second);
63 
64  // a) remove list from list map
65  aBackpatchListMap.erase(it);
66 
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)
72  {
73  rBackpatch->setPropertyValue(sPropertyName, aAny);
74  }
75  // else: no backpatch list -> then we're finished
76 }
77 
78 template<class A>
80  const Reference<XPropertySet> & xPropSet,
81  const OUString& sName)
82 {
83  if (aIDMap.count(sName))
84  {
85  // we know this ID -> set property
86  xPropSet->setPropertyValue(sPropertyName, css::uno::Any(aIDMap[sName]));
87  }
88  else
89  {
90  // ID unknown -> into backpatch list for later fixup
91  if (! aBackpatchListMap.count(sName))
92  {
93  // create backpatch list for this name
94  aBackpatchListMap.emplace(sName, new BackpatchListType);
95  }
96 
97  // insert footnote
98  aBackpatchListMap[sName]->push_back(xPropSet);
99  }
100 }
101 
102 // force instantiation of templates
103 template class XMLPropertyBackpatcher<sal_Int16>;
104 template class XMLPropertyBackpatcher<OUString>;
105 
107 {
109  ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
111 
113  ::std::unique_ptr< XMLPropertyBackpatcher<sal_Int16> >
115 
116  ::std::unique_ptr< XMLPropertyBackpatcher< OUString> >
118 };
119 
120 std::shared_ptr<XMLTextImportHelper::BackpatcherImpl>
122 {
123  // n.b.: the shared_ptr stores the dtor!
124  return std::make_shared<BackpatcherImpl>();
125 }
126 
127 static OUString GetSequenceNumber()
128 {
129  return "SequenceNumber";
130 }
131 
132 
133 // XMLTextImportHelper
134 
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.
139 
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.
144 
145 
147 {
148  if (!m_xBackpatcherImpl->m_pFootnoteBackpatcher)
149  {
150  m_xBackpatcherImpl->m_pFootnoteBackpatcher.reset(
152  }
153  return *m_xBackpatcherImpl->m_pFootnoteBackpatcher;
154 }
155 
157 {
158  if (!m_xBackpatcherImpl->m_pSequenceIdBackpatcher)
159  {
160  m_xBackpatcherImpl->m_pSequenceIdBackpatcher.reset(
162  }
163  return *m_xBackpatcherImpl->m_pSequenceIdBackpatcher;
164 }
165 
167 {
168  if (!m_xBackpatcherImpl->m_pSequenceNameBackpatcher)
169  {
170  m_xBackpatcherImpl->m_pSequenceNameBackpatcher.reset(
171  new XMLPropertyBackpatcher<OUString>("SourceName"));
172  }
173  return *m_xBackpatcherImpl->m_pSequenceNameBackpatcher;
174 }
175 
177  const OUString& sXMLId,
178  sal_Int16 nAPIId)
179 {
180  GetFootnoteBP().ResolveId(sXMLId, nAPIId);
181 }
182 
184  const OUString& sXMLId,
185  const Reference<XPropertySet> & xPropSet)
186 {
187  GetFootnoteBP().SetProperty(xPropSet, sXMLId);
188 }
189 
191  const OUString& sXMLId,
192  const OUString& sName,
193  sal_Int16 nAPIId)
194 {
195  GetSequenceIdBP().ResolveId(sXMLId, nAPIId);
196  GetSequenceNameBP().ResolveId(sXMLId, sName);
197 }
198 
200  const OUString& sXMLId,
201  const Reference<XPropertySet> & xPropSet)
202 {
203  GetSequenceIdBP().SetProperty(xPropSet, sXMLId);
204  GetSequenceNameBP().SetProperty(xPropSet, sXMLId);
205 }
206 
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XMLPropertyBackpatcher< OUString > & GetSequenceNameBP()
This class maintains an OUString->sal_Int16 mapping for cases in which an XPropertySet needs to be fi...
::std::vector< css::uno::Reference< css::beans::XPropertySet > > BackpatchListType
backpatch list type
XMLPropertyBackpatcher< sal_Int16 > & GetSequenceIdBP()
void ProcessSequenceReference(const OUString &sXMLId, const css::uno::Reference< css::beans::XPropertySet > &xPropSet)
set sequence ID or insert into backpatch list
static std::shared_ptr< BackpatcherImpl > MakeBackpatcherImpl()
std::shared_ptr< BackpatcherImpl > m_xBackpatcherImpl
Definition: txtimp.hxx:373
XMLPropertyBackpatcher(const OUString &sPropertyName)
XMLPropertyBackpatcher< sal_Int16 > & GetFootnoteBP()
void ResolveId(const OUString &sName, A aValue)
resolve a known ID.
::std::unique_ptr< XMLPropertyBackpatcher< sal_Int16 > > m_pFootnoteBackpatcher
backpatcher for references to footnotes and endnotes
::std::unique_ptr< XMLPropertyBackpatcher< sal_Int16 > > m_pSequenceIdBackpatcher
backpatchers for references to sequences
void ProcessFootnoteReference(const OUString &sXMLId, const css::uno::Reference< css::beans::XPropertySet > &xPropSet)
set the proper footnote reference ID, or put into backpatch list if ID is unknown ...
static OUString GetSequenceNumber()
void InsertSequenceID(const OUString &sXMLId, const OUString &sName, sal_Int16 nAPIId)
insert new sequence ID Also fixup open references from backpatch list to this ID. ...
void SetProperty(const css::uno::Reference< css::beans::XPropertySet > &xPropSet, const OUString &sName)
Set property with the proper value for this name.
::std::unique_ptr< XMLPropertyBackpatcher< OUString > > m_pSequenceNameBackpatcher
void InsertFootnoteID(const OUString &sXMLId, sal_Int16 nAPIId)
insert new footnote ID.