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 <rtl/ref.hxx>
21 
22 #include <xmloff/xmlprmap.hxx>
23 #include <xmloff/xmlprhdl.hxx>
24 #include <xmloff/xmltypes.hxx>
25 #include <xmloff/xmltoken.hxx>
26 #include <xmloff/maptype.hxx>
27 #include <xmloff/prhdlfac.hxx>
28 #include <xmloff/xmlimp.hxx>
29 
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 
32 #include <vector>
33 
34 using namespace ::std;
35 
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::beans;
39 
40 namespace {
41 
54 struct XMLPropertySetMapperEntry_Impl
55 {
56  OUString sXMLAttributeName;
57  OUString sAPIPropertyName;
58  sal_Int32 nType;
59  sal_uInt16 nXMLNameSpace;
60  sal_Int16 nContextId;
61  SvtSaveOptions::ODFSaneDefaultVersion nEarliestODFVersionForExport;
62  bool bImportOnly;
63  const XMLPropertyHandler *pHdl;
64 
65  XMLPropertySetMapperEntry_Impl(
66  const XMLPropertyMapEntry& rMapEntry,
68 
69  sal_uInt32 GetPropType() const { return nType & XML_TYPE_PROP_MASK; }
70 };
71 
72 }
73 
74 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
75  const XMLPropertyMapEntry& rMapEntry,
77  sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
78  sAPIPropertyName( OUString(rMapEntry.msApiName, rMapEntry.nApiNameLength,
79  RTL_TEXTENCODING_ASCII_US ) ),
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->msApiName )
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->msApiName )
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 
159 sal_uInt32 XMLPropertySetMapper::GetEntryFlags( sal_Int32 nIndex ) const
160 {
161  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
162  return mpImpl->maMapEntries[nIndex].nType & ~MID_FLAG_MASK;
163 }
164 
165 sal_uInt32 XMLPropertySetMapper::GetEntryType( sal_Int32 nIndex ) const
166 {
167  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
168  sal_uInt32 nType = mpImpl->maMapEntries[nIndex].nType;
169  return nType;
170 }
171 
172 sal_uInt16 XMLPropertySetMapper::GetEntryNameSpace( sal_Int32 nIndex ) const
173 {
174  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
175  return mpImpl->maMapEntries[nIndex].nXMLNameSpace;
176 }
177 
178 const OUString& XMLPropertySetMapper::GetEntryXMLName( sal_Int32 nIndex ) const
179 {
180  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
181  return mpImpl->maMapEntries[nIndex].sXMLAttributeName;
182 }
183 
184 const OUString& XMLPropertySetMapper::GetEntryAPIName( sal_Int32 nIndex ) const
185 {
186  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
187  return mpImpl->maMapEntries[nIndex].sAPIPropertyName;
188 }
189 
190 sal_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) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
200  return mpImpl->maMapEntries[nIndex].nEarliestODFVersionForExport;
201 }
202 
204 {
205  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(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  const OUString& 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  const OUString& 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 
329 sal_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 
350 void 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 
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddMapperEntry(const rtl::Reference< XMLPropertySetMapper > &rMapper)
Definition: xmlprmap.cxx:139
sal_Int32 nIndex
sal_Int32 GetEntryIndex(sal_uInt16 nNamespace, const OUString &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_uInt32 GetEntryFlags(sal_Int32 nIndex) const
Returns the flags of an entry.
Definition: xmlprmap.cxx:159
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:77
sal_Int32 FindEntryIndex(const char *sApiName, sal_uInt16 nNameSpace, const OUString &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
sal_Int32 mnIndex
Definition: maptype.hxx:124
std::vector< rtl::Reference< XMLPropertyHandlerFactory > > maHdlFactories
Definition: xmlprmap.cxx:93
const OUString & GetEntryXMLName(sal_Int32 nIndex) const
Returns the 'local' XML-name of the entry.
Definition: xmlprmap.cxx:178
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
sal_Int32 GetEntryCount() const
Return number of entries in input-array.
Definition: xmlprmap.cxx:154
sal_Int32 mnType
const char * msApiName
Definition: maptype.hxx:33
Represents a property with its API-name, its XML-name and the type of its value.
Definition: maptype.hxx:31
constexpr size_t NMSP_SHIFT
Definition: xmlimp.hxx:90
bool mbImportOnly
Flag to specify whether entry is only used during import.
Definition: maptype.hxx:100
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...
const OUString & GetEntryAPIName(sal_Int32 nIndex) const
Returns the entry API name.
Definition: xmlprmap.cxx:184
sal_Int16 GetEntryContextId(sal_Int32 nIndex) const
returns the entry context id.
Definition: xmlprmap.cxx:190
std::unique_ptr< Impl > mpImpl
Definition: xmlprmap.hxx:40
static const OUString & getNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2020
bool importXML(const OUString &rStrImpValue, XMLPropertyState &rProperty, const SvXMLUnitConverter &rUnitConverter) const
Definition: xmlprmap.cxx:228
css::uno::Any maValue
Definition: maptype.hxx:125
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3378
#define MID_FLAG_MASK
Definition: xmltypes.hxx:36
virtual ~XMLPropertySetMapper() override
Definition: xmlprmap.cxx:135
sal_uInt16 GetEntryNameSpace(sal_Int32 nIndex) const
Returns the namespace-key of an entry.
Definition: xmlprmap.cxx:172
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
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
static const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nType)
Definition: txtprhdl.cxx:1218
OReadImagesDocumentHandler::Image_XML_Namespace nNamespace
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:122
QPRO_FUNC_TYPE nType
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...
Abstract base-class for different XML-types.
Definition: xmlprhdl.hxx:35
XMLPropertySetMapper(const XMLPropertySetMapper &)=delete
Impl(bool bForExport)
Definition: xmlprmap.cxx:97
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
std::vector< XMLPropertySetMapperEntry_Impl > maMapEntries
Definition: xmlprmap.cxx:92
sal_uInt32 GetEntryType(sal_Int32 nIndex) const
Returns the type of an entry.
Definition: xmlprmap.cxx:165
#define XML_TYPE_PROP_MASK
Definition: xmltypes.hxx:91
void RemoveEntry(sal_Int32 nIndex)
Remove an entry.
Definition: xmlprmap.cxx:350