LibreOffice Module xmloff (master) 1
xmlprmap.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 <o3tl/safeint.hxx>
21#include <rtl/ref.hxx>
22
23#include <xmloff/xmlprmap.hxx>
24#include <xmloff/xmlprhdl.hxx>
25#include <xmloff/xmltypes.hxx>
26#include <xmloff/xmltoken.hxx>
27#include <xmloff/maptype.hxx>
28#include <xmloff/prhdlfac.hxx>
29#include <xmloff/xmlimp.hxx>
30
31#include <com/sun/star/beans/XPropertySet.hpp>
32
33#include <vector>
34
35using namespace ::std;
36
37using namespace ::com::sun::star::uno;
38using namespace ::com::sun::star::beans;
40
41namespace {
42
55struct XMLPropertySetMapperEntry_Impl
56{
57 OUString sXMLAttributeName;
58 OUString sAPIPropertyName;
59 sal_Int32 nType;
60 sal_uInt16 nXMLNameSpace;
61 sal_Int16 nContextId;
62 SvtSaveOptions::ODFSaneDefaultVersion nEarliestODFVersionForExport;
63 bool bImportOnly;
64 const XMLPropertyHandler *pHdl;
65
66 XMLPropertySetMapperEntry_Impl(
67 const XMLPropertyMapEntry& rMapEntry,
69
70 sal_uInt32 GetPropType() const { return nType & XML_TYPE_PROP_MASK; }
71};
72
73}
74
75XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
76 const XMLPropertyMapEntry& rMapEntry,
78 sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
79 sAPIPropertyName( rMapEntry.getApiName() ),
80 nType( rMapEntry.mnType ),
81 nXMLNameSpace( rMapEntry.mnNameSpace ),
82 nContextId( rMapEntry.mnContextId ),
83 nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
84 bImportOnly( rMapEntry.mbImportOnly),
85 pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
86{
87 assert(pHdl);
88}
89
91{
92 std::vector<XMLPropertySetMapperEntry_Impl> maMapEntries;
93 std::vector<rtl::Reference <XMLPropertyHandlerFactory> > maHdlFactories;
94
96
97 explicit Impl( bool bForExport ) : mbOnlyExportMappings(bForExport) {}
98};
99
100// Ctor
102 const XMLPropertyMapEntry* pEntries, const rtl::Reference<XMLPropertyHandlerFactory>& rFactory,
103 bool bForExport ) :
104 mpImpl(new Impl(bForExport))
105{
106 mpImpl->maHdlFactories.push_back(rFactory);
107 if( !pEntries )
108 return;
109
110 const XMLPropertyMapEntry* pIter = pEntries;
111
112 if (mpImpl->mbOnlyExportMappings)
113 {
114 while( !pIter->IsEnd() )
115 {
116 if (!pIter->mbImportOnly)
117 {
118 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
119 mpImpl->maMapEntries.push_back( aEntry );
120 }
121 ++pIter;
122 }
123 }
124 else
125 {
126 while( !pIter->IsEnd() )
127 {
128 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
129 mpImpl->maMapEntries.push_back( aEntry );
130 ++pIter;
131 }
132 }
133}
134
136{
137}
138
141{
142 for( const auto& rHdlFactory : rMapper->mpImpl->maHdlFactories )
143 {
144 mpImpl->maHdlFactories.push_back(rHdlFactory);
145 }
146
147 for( const auto& rMapEntry : rMapper->mpImpl->maMapEntries )
148 {
149 if (!mpImpl->mbOnlyExportMappings || !rMapEntry.bImportOnly)
150 mpImpl->maMapEntries.push_back( rMapEntry );
151 }
152}
153
155{
156 return mpImpl->maMapEntries.size();
157}
158
159sal_uInt32 XMLPropertySetMapper::GetEntryFlags( sal_Int32 nIndex ) const
160{
161 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
162 return mpImpl->maMapEntries[nIndex].nType & ~MID_FLAG_MASK;
163}
164
165sal_uInt32 XMLPropertySetMapper::GetEntryType( sal_Int32 nIndex ) const
166{
167 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
168 sal_uInt32 nType = mpImpl->maMapEntries[nIndex].nType;
169 return nType;
170}
171
172sal_uInt16 XMLPropertySetMapper::GetEntryNameSpace( sal_Int32 nIndex ) const
173{
174 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
175 return mpImpl->maMapEntries[nIndex].nXMLNameSpace;
176}
177
178const OUString& XMLPropertySetMapper::GetEntryXMLName( sal_Int32 nIndex ) const
179{
180 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
181 return mpImpl->maMapEntries[nIndex].sXMLAttributeName;
182}
183
184const OUString& XMLPropertySetMapper::GetEntryAPIName( sal_Int32 nIndex ) const
185{
186 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
187 return mpImpl->maMapEntries[nIndex].sAPIPropertyName;
188}
189
190sal_Int16 XMLPropertySetMapper::GetEntryContextId( sal_Int32 nIndex ) const
191{
192 assert((-1 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
193 return nIndex == -1 ? 0 : mpImpl->maMapEntries[nIndex].nContextId;
194}
195
198{
199 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
200 return mpImpl->maMapEntries[nIndex].nEarliestODFVersionForExport;
201}
202
204{
205 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
206 return mpImpl->maMapEntries[nIndex].pHdl;
207}
208
209// Export a Property
211 OUString& rStrExpValue,
212 const XMLPropertyState& rProperty,
213 const SvXMLUnitConverter& rUnitConverter ) const
214{
215 bool bRet = false;
216
217 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
218
219 assert(pHdl);
220 if( pHdl )
221 bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
222 rUnitConverter );
223
224 return bRet;
225}
226
227// Import a Property
229 const OUString& rStrImpValue,
230 XMLPropertyState& rProperty,
231 const SvXMLUnitConverter& rUnitConverter ) const
232{
233 bool bRet = false;
234
235 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
236
237 if( pHdl )
238 bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
239 rUnitConverter );
240
241 return bRet;
242}
243
244// Search for the given name and the namespace in the list and return
245// the index of the entry
246// If there is no matching entry the method returns -1
248 sal_uInt16 nNamespace,
249 std::u16string_view rStrName,
250 sal_uInt32 nPropType,
251 sal_Int32 nStartAt /* = -1 */ ) const
252{
253 sal_Int32 nEntries = GetEntryCount();
254 sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
255
256 if ( nEntries && nIndex < nEntries )
257 {
258 do
259 {
260 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
261 if( (!nPropType || nPropType == rEntry.GetPropType()) &&
262 rEntry.nXMLNameSpace == nNamespace &&
263 rStrName == rEntry.sXMLAttributeName )
264 return nIndex;
265 else
266 nIndex++;
267
268 } while( nIndex<nEntries );
269 }
270
271 return -1;
272}
273
274// Search for the given name and the namespace in the list and return
275// the index of the entry
276// If there is no matching entry the method returns -1
278 sal_Int32 nElement,
279 sal_uInt32 nPropType,
280 sal_Int32 nStartAt /* = -1 */ ) const
281{
282 sal_Int32 nEntries = GetEntryCount();
283 sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
284
285 if ( nEntries && nIndex < nEntries )
286 {
287 sal_uInt16 nNamespace = (nElement >> NMSP_SHIFT) - 1;
288 const OUString& rStrName = SvXMLImport::getNameFromToken(nElement);
289 do
290 {
291 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
292 if( (!nPropType || nPropType == rEntry.GetPropType()) &&
293 rEntry.nXMLNameSpace == nNamespace &&
294 rStrName == rEntry.sXMLAttributeName )
295 return nIndex;
296 else
297 nIndex++;
298
299 } while( nIndex<nEntries );
300 }
301
302 return -1;
303}
304
307 const char* sApiName,
308 sal_uInt16 nNameSpace,
309 std::u16string_view sXMLName ) const
310{
311 sal_Int32 nIndex = 0;
312 sal_Int32 nEntries = GetEntryCount();
313
314 do
315 {
316 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
317 if( rEntry.nXMLNameSpace == nNameSpace &&
318 rEntry.sXMLAttributeName == sXMLName &&
319 rEntry.sAPIPropertyName.equalsAscii( sApiName ) )
320 return nIndex;
321 else
322 nIndex++;
323
324 } while( nIndex < nEntries );
325
326 return -1;
327}
328
329sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
330{
331 const sal_Int32 nEntries = GetEntryCount();
332
333 if ( nEntries )
334 {
335 sal_Int32 nIndex = 0;
336 do
337 {
338 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
339 if( rEntry.nContextId == nContextId )
340 return nIndex;
341 else
342 nIndex++;
343
344 } while( nIndex < nEntries );
345 }
346
347 return -1;
348}
349
350void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
351{
352 const sal_Int32 nEntries = GetEntryCount();
353 if( nIndex>=nEntries || nIndex<0 )
354 return;
355 vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = mpImpl->maMapEntries.begin();
356 std::advance(aEIter, nIndex);
357 mpImpl->maMapEntries.erase( aEIter );
358}
359
361{
362 for (const XMLPropertySetMapperEntry_Impl& rMapEntry : mpImpl->maMapEntries)
363 rNames.insert(rMapEntry.sAPIPropertyName);
364}
365
366/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:83
Abstract base-class for different XML-types.
Definition: xmlprhdl.hxx:36
virtual bool exportXML(OUString &rStrExpValue, const css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const =0
Exports the given value according to the XML-data-type corresponding to the derived class.
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const =0
Imports the given value according to the XML-data-type corresponding to the derived class.
std::unique_ptr< Impl > mpImpl
Definition: xmlprmap.hxx:41
void AddMapperEntry(const rtl::Reference< XMLPropertySetMapper > &rMapper)
Definition: xmlprmap.cxx:139
const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nIndex) const
Retrieves a PropertyHandler for that property which placed at nIndex in the XMLPropertyMapEntry-array...
Definition: xmlprmap.cxx:203
sal_Int32 FindEntryIndex(const char *sApiName, sal_uInt16 nNameSpace, std::u16string_view sXMLName) const
searches for an entry that matches the given api name, namespace and local name or -1 if nothing foun...
Definition: xmlprmap.cxx:306
SvtSaveOptions::ODFSaneDefaultVersion GetEarliestODFVersionForExport(sal_Int32 nIndex) const
returns the earliest ODF version for which this property should be exported as standard ODF element,...
Definition: xmlprmap.cxx:197
sal_uInt32 GetEntryType(sal_Int32 nIndex) const
Returns the type of an entry.
Definition: xmlprmap.cxx:165
sal_Int16 GetEntryContextId(sal_Int32 nIndex) const
returns the entry context id.
Definition: xmlprmap.cxx:190
bool importXML(const OUString &rStrImpValue, XMLPropertyState &rProperty, const SvXMLUnitConverter &rUnitConverter) const
Definition: xmlprmap.cxx:228
const OUString & GetEntryAPIName(sal_Int32 nIndex) const
Returns the entry API name.
Definition: xmlprmap.cxx:184
sal_Int32 GetEntryCount() const
Return number of entries in input-array.
Definition: xmlprmap.cxx:154
sal_uInt32 GetEntryFlags(sal_Int32 nIndex) const
Returns the flags of an entry.
Definition: xmlprmap.cxx:159
void GetEntryAPINames(o3tl::sorted_vector< OUString > &rNames) const
Definition: xmlprmap.cxx:360
const OUString & GetEntryXMLName(sal_Int32 nIndex) const
Returns the 'local' XML-name of the entry.
Definition: xmlprmap.cxx:178
sal_Int32 GetEntryIndex(sal_uInt16 nNamespace, std::u16string_view rStrName, sal_uInt32 nPropType, sal_Int32 nStartAt=-1) const
Returns the index of an entry with the given XML-name and namespace If there is no matching entry the...
Definition: xmlprmap.cxx:247
sal_uInt16 GetEntryNameSpace(sal_Int32 nIndex) const
Returns the namespace-key of an entry.
Definition: xmlprmap.cxx:172
void RemoveEntry(sal_Int32 nIndex)
Remove an entry.
Definition: xmlprmap.cxx:350
XMLPropertySetMapper(const XMLPropertySetMapper &)=delete
virtual ~XMLPropertySetMapper() override
Definition: xmlprmap.cxx:135
bool exportXML(OUString &rStrExpValue, const XMLPropertyState &rProperty, const SvXMLUnitConverter &rUnitConverter) const
import/export This methods calls the respective im/export-method of the respective PropertyHandler.
Definition: xmlprmap.cxx:210
std::pair< const_iterator, bool > insert(Value &&x)
sal_Int32 nIndex
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3527
QPRO_FUNC_TYPE nType
sal_Int32 mnType
OReadStatusBarDocumentHandler::StatusBar_XML_Namespace nNamespace
Represents a property with its API-name, its XML-name and the type of its value.
Definition: maptype.hxx:33
bool mbImportOnly
Flag to specify whether entry is only used during import.
Definition: maptype.hxx:100
bool IsEnd() const
Definition: maptype.hxx:132
std::vector< XMLPropertySetMapperEntry_Impl > maMapEntries
Definition: xmlprmap.cxx:92
Impl(bool bForExport)
Definition: xmlprmap.cxx:97
std::vector< rtl::Reference< XMLPropertyHandlerFactory > > maHdlFactories
Definition: xmlprmap.cxx:93
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:140
css::uno::Any maValue
Definition: maptype.hxx:142
sal_Int32 mnIndex
Definition: maptype.hxx:141
static const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nType)
Definition: txtprhdl.cxx:1240
constexpr size_t NMSP_SHIFT
Definition: xmlimp.hxx:94
#define XML_TYPE_PROP_MASK
Definition: xmltypes.hxx:91
#define MID_FLAG_MASK
Definition: xmltypes.hxx:36