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 ::com::sun::star::uno;
36using namespace ::com::sun::star::beans;
38
39namespace {
40
53struct XMLPropertySetMapperEntry_Impl
54{
55 OUString sXMLAttributeName;
56 OUString sAPIPropertyName;
57 sal_Int32 nType;
58 sal_uInt16 nXMLNameSpace;
59 sal_Int16 nContextId;
60 SvtSaveOptions::ODFSaneDefaultVersion nEarliestODFVersionForExport;
61 bool bImportOnly;
62 const XMLPropertyHandler *pHdl;
63
64 XMLPropertySetMapperEntry_Impl(
65 const XMLPropertyMapEntry& rMapEntry,
67
68 sal_uInt32 GetPropType() const { return nType & XML_TYPE_PROP_MASK; }
69};
70
71}
72
73XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
74 const XMLPropertyMapEntry& rMapEntry,
76 sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
77 sAPIPropertyName( rMapEntry.getApiName() ),
78 nType( rMapEntry.mnType ),
79 nXMLNameSpace( rMapEntry.mnNameSpace ),
80 nContextId( rMapEntry.mnContextId ),
81 nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
82 bImportOnly( rMapEntry.mbImportOnly),
83 pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
84{
85 assert(pHdl);
86}
87
89{
90 std::vector<XMLPropertySetMapperEntry_Impl> maMapEntries;
91 std::vector<rtl::Reference <XMLPropertyHandlerFactory> > maHdlFactories;
92
94
95 explicit Impl( bool bForExport ) : mbOnlyExportMappings(bForExport) {}
96};
97
98// Ctor
100 const XMLPropertyMapEntry* pEntries, const rtl::Reference<XMLPropertyHandlerFactory>& rFactory,
101 bool bForExport ) :
102 mpImpl(new Impl(bForExport))
103{
104 mpImpl->maHdlFactories.push_back(rFactory);
105 if( !pEntries )
106 return;
107
108 const XMLPropertyMapEntry* pIter = pEntries;
109
110 if (mpImpl->mbOnlyExportMappings)
111 {
112 while( !pIter->IsEnd() )
113 {
114 if (!pIter->mbImportOnly)
115 {
116 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
117 mpImpl->maMapEntries.push_back( aEntry );
118 }
119 ++pIter;
120 }
121 }
122 else
123 {
124 while( !pIter->IsEnd() )
125 {
126 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
127 mpImpl->maMapEntries.push_back( aEntry );
128 ++pIter;
129 }
130 }
131}
132
134{
135}
136
139{
140 for( const auto& rHdlFactory : rMapper->mpImpl->maHdlFactories )
141 {
142 mpImpl->maHdlFactories.push_back(rHdlFactory);
143 }
144
145 for( const auto& rMapEntry : rMapper->mpImpl->maMapEntries )
146 {
147 if (!mpImpl->mbOnlyExportMappings || !rMapEntry.bImportOnly)
148 mpImpl->maMapEntries.push_back( rMapEntry );
149 }
150}
151
153{
154 return mpImpl->maMapEntries.size();
155}
156
157sal_uInt32 XMLPropertySetMapper::GetEntryFlags( sal_Int32 nIndex ) const
158{
159 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
160 return mpImpl->maMapEntries[nIndex].nType & ~MID_FLAG_MASK;
161}
162
163sal_uInt32 XMLPropertySetMapper::GetEntryType( sal_Int32 nIndex ) const
164{
165 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
166 sal_uInt32 nType = mpImpl->maMapEntries[nIndex].nType;
167 return nType;
168}
169
170sal_uInt16 XMLPropertySetMapper::GetEntryNameSpace( sal_Int32 nIndex ) const
171{
172 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
173 return mpImpl->maMapEntries[nIndex].nXMLNameSpace;
174}
175
176const OUString& XMLPropertySetMapper::GetEntryXMLName( sal_Int32 nIndex ) const
177{
178 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
179 return mpImpl->maMapEntries[nIndex].sXMLAttributeName;
180}
181
182const OUString& XMLPropertySetMapper::GetEntryAPIName( sal_Int32 nIndex ) const
183{
184 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
185 return mpImpl->maMapEntries[nIndex].sAPIPropertyName;
186}
187
188sal_Int16 XMLPropertySetMapper::GetEntryContextId( sal_Int32 nIndex ) const
189{
190 assert((-1 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
191 return nIndex == -1 ? 0 : mpImpl->maMapEntries[nIndex].nContextId;
192}
193
196{
197 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
198 return mpImpl->maMapEntries[nIndex].nEarliestODFVersionForExport;
199}
200
202{
203 assert((0 <= nIndex) && (o3tl::make_unsigned(nIndex) < mpImpl->maMapEntries.size()));
204 return mpImpl->maMapEntries[nIndex].pHdl;
205}
206
207// Export a Property
209 OUString& rStrExpValue,
210 const XMLPropertyState& rProperty,
211 const SvXMLUnitConverter& rUnitConverter ) const
212{
213 bool bRet = false;
214
215 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
216
217 assert(pHdl);
218 if( pHdl )
219 bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
220 rUnitConverter );
221
222 return bRet;
223}
224
225// Import a Property
227 const OUString& rStrImpValue,
228 XMLPropertyState& rProperty,
229 const SvXMLUnitConverter& rUnitConverter ) const
230{
231 bool bRet = false;
232
233 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
234
235 if( pHdl )
236 bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
237 rUnitConverter );
238
239 return bRet;
240}
241
242// Search for the given name and the namespace in the list and return
243// the index of the entry
244// If there is no matching entry the method returns -1
246 sal_uInt16 nNamespace,
247 std::u16string_view rStrName,
248 sal_uInt32 nPropType,
249 sal_Int32 nStartAt /* = -1 */ ) const
250{
251 sal_Int32 nEntries = GetEntryCount();
252 sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
253
254 if ( nEntries && nIndex < nEntries )
255 {
256 do
257 {
258 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
259 if( (!nPropType || nPropType == rEntry.GetPropType()) &&
260 rEntry.nXMLNameSpace == nNamespace &&
261 rStrName == rEntry.sXMLAttributeName )
262 return nIndex;
263 else
264 nIndex++;
265
266 } while( nIndex<nEntries );
267 }
268
269 return -1;
270}
271
272// Search for the given name and the namespace in the list and return
273// the index of the entry
274// If there is no matching entry the method returns -1
276 sal_Int32 nElement,
277 sal_uInt32 nPropType,
278 sal_Int32 nStartAt /* = -1 */ ) const
279{
280 sal_Int32 nEntries = GetEntryCount();
281 sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
282
283 if ( nEntries && nIndex < nEntries )
284 {
285 sal_uInt16 nNamespace = (nElement >> NMSP_SHIFT) - 1;
286 const OUString& rStrName = SvXMLImport::getNameFromToken(nElement);
287 do
288 {
289 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
290 if( (!nPropType || nPropType == rEntry.GetPropType()) &&
291 rEntry.nXMLNameSpace == nNamespace &&
292 rStrName == rEntry.sXMLAttributeName )
293 return nIndex;
294 else
295 nIndex++;
296
297 } while( nIndex<nEntries );
298 }
299
300 return -1;
301}
302
305 const char* sApiName,
306 sal_uInt16 nNameSpace,
307 std::u16string_view sXMLName ) const
308{
309 sal_Int32 nIndex = 0;
310 sal_Int32 nEntries = GetEntryCount();
311
312 do
313 {
314 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
315 if( rEntry.nXMLNameSpace == nNameSpace &&
316 rEntry.sXMLAttributeName == sXMLName &&
317 rEntry.sAPIPropertyName.equalsAscii( sApiName ) )
318 return nIndex;
319 else
320 nIndex++;
321
322 } while( nIndex < nEntries );
323
324 return -1;
325}
326
327sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
328{
329 const sal_Int32 nEntries = GetEntryCount();
330
331 if ( nEntries )
332 {
333 sal_Int32 nIndex = 0;
334 do
335 {
336 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
337 if( rEntry.nContextId == nContextId )
338 return nIndex;
339 else
340 nIndex++;
341
342 } while( nIndex < nEntries );
343 }
344
345 return -1;
346}
347
348void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
349{
350 const sal_Int32 nEntries = GetEntryCount();
351 if( nIndex>=nEntries || nIndex<0 )
352 return;
353 std::vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = mpImpl->maMapEntries.begin();
354 std::advance(aEIter, nIndex);
355 mpImpl->maMapEntries.erase( aEIter );
356}
357
358/* 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:40
void AddMapperEntry(const rtl::Reference< XMLPropertySetMapper > &rMapper)
Definition: xmlprmap.cxx:137
const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nIndex) const
Retrieves a PropertyHandler for that property which placed at nIndex in the XMLPropertyMapEntry-array...
Definition: xmlprmap.cxx:201
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:304
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:195
sal_uInt32 GetEntryType(sal_Int32 nIndex) const
Returns the type of an entry.
Definition: xmlprmap.cxx:163
sal_Int16 GetEntryContextId(sal_Int32 nIndex) const
returns the entry context id.
Definition: xmlprmap.cxx:188
bool importXML(const OUString &rStrImpValue, XMLPropertyState &rProperty, const SvXMLUnitConverter &rUnitConverter) const
Definition: xmlprmap.cxx:226
const OUString & GetEntryAPIName(sal_Int32 nIndex) const
Returns the entry API name.
Definition: xmlprmap.cxx:182
sal_Int32 GetEntryCount() const
Return number of entries in input-array.
Definition: xmlprmap.cxx:152
sal_uInt32 GetEntryFlags(sal_Int32 nIndex) const
Returns the flags of an entry.
Definition: xmlprmap.cxx:157
const OUString & GetEntryXMLName(sal_Int32 nIndex) const
Returns the 'local' XML-name of the entry.
Definition: xmlprmap.cxx:176
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:245
sal_uInt16 GetEntryNameSpace(sal_Int32 nIndex) const
Returns the namespace-key of an entry.
Definition: xmlprmap.cxx:170
void RemoveEntry(sal_Int32 nIndex)
Remove an entry.
Definition: xmlprmap.cxx:348
XMLPropertySetMapper(const XMLPropertySetMapper &)=delete
virtual ~XMLPropertySetMapper() override
Definition: xmlprmap.cxx:133
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:208
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:3541
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:90
Impl(bool bForExport)
Definition: xmlprmap.cxx:95
std::vector< rtl::Reference< XMLPropertyHandlerFactory > > maHdlFactories
Definition: xmlprmap.cxx:91
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:1223
constexpr size_t NMSP_SHIFT
Definition: xmlimp.hxx:93
#define XML_TYPE_PROP_MASK
Definition: xmltypes.hxx:91
#define MID_FLAG_MASK
Definition: xmltypes.hxx:36