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 
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 
31 #include <vector>
32 
33 using namespace ::std;
34 
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::beans;
38 
39 namespace {
40 
53 struct 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 
73 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
74  const XMLPropertyMapEntry& rMapEntry,
76  sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
77  sAPIPropertyName( OUString(rMapEntry.msApiName, rMapEntry.nApiNameLength,
78  RTL_TEXTENCODING_ASCII_US ) ),
79  nType( rMapEntry.mnType ),
80  nXMLNameSpace( rMapEntry.mnNameSpace ),
81  nContextId( rMapEntry.mnContextId ),
82  nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
83  bImportOnly( rMapEntry.mbImportOnly),
84  pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
85 {
86  assert(pHdl);
87 }
88 
90 {
91  std::vector<XMLPropertySetMapperEntry_Impl> maMapEntries;
92  std::vector<rtl::Reference <XMLPropertyHandlerFactory> > maHdlFactories;
93 
95 
96  explicit Impl( bool bForExport ) : mbOnlyExportMappings(bForExport) {}
97 };
98 
99 // Ctor
101  const XMLPropertyMapEntry* pEntries, const rtl::Reference<XMLPropertyHandlerFactory>& rFactory,
102  bool bForExport ) :
103  mpImpl(new Impl(bForExport))
104 {
105  mpImpl->maHdlFactories.push_back(rFactory);
106  if( pEntries )
107  {
108  const XMLPropertyMapEntry* pIter = pEntries;
109 
110  if (mpImpl->mbOnlyExportMappings)
111  {
112  while( pIter->msApiName )
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->msApiName )
125  {
126  XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
127  mpImpl->maMapEntries.push_back( aEntry );
128  ++pIter;
129  }
130  }
131  }
132 }
133 
135 {
136 }
137 
140 {
141  for( const auto& rHdlFactory : rMapper->mpImpl->maHdlFactories )
142  {
143  mpImpl->maHdlFactories.push_back(rHdlFactory);
144  }
145 
146  for( const auto& rMapEntry : rMapper->mpImpl->maMapEntries )
147  {
148  if (!mpImpl->mbOnlyExportMappings || !rMapEntry.bImportOnly)
149  mpImpl->maMapEntries.push_back( rMapEntry );
150  }
151 }
152 
154 {
155  return mpImpl->maMapEntries.size();
156 }
157 
158 sal_uInt32 XMLPropertySetMapper::GetEntryFlags( sal_Int32 nIndex ) const
159 {
160  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
161  return mpImpl->maMapEntries[nIndex].nType & ~MID_FLAG_MASK;
162 }
163 
164 sal_uInt32 XMLPropertySetMapper::GetEntryType( sal_Int32 nIndex ) const
165 {
166  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
167  sal_uInt32 nType = mpImpl->maMapEntries[nIndex].nType;
168  return nType;
169 }
170 
171 sal_uInt16 XMLPropertySetMapper::GetEntryNameSpace( sal_Int32 nIndex ) const
172 {
173  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
174  return mpImpl->maMapEntries[nIndex].nXMLNameSpace;
175 }
176 
177 const OUString& XMLPropertySetMapper::GetEntryXMLName( sal_Int32 nIndex ) const
178 {
179  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
180  return mpImpl->maMapEntries[nIndex].sXMLAttributeName;
181 }
182 
183 const OUString& XMLPropertySetMapper::GetEntryAPIName( sal_Int32 nIndex ) const
184 {
185  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
186  return mpImpl->maMapEntries[nIndex].sAPIPropertyName;
187 }
188 
189 sal_Int16 XMLPropertySetMapper::GetEntryContextId( sal_Int32 nIndex ) const
190 {
191  assert((-1 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
192  return nIndex == -1 ? 0 : mpImpl->maMapEntries[nIndex].nContextId;
193 }
194 
197 {
198  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
199  return mpImpl->maMapEntries[nIndex].nEarliestODFVersionForExport;
200 }
201 
203 {
204  assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
205  return mpImpl->maMapEntries[nIndex].pHdl;
206 }
207 
208 // Export a Property
210  OUString& rStrExpValue,
211  const XMLPropertyState& rProperty,
212  const SvXMLUnitConverter& rUnitConverter ) const
213 {
214  bool bRet = false;
215 
216  const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
217 
218  assert(pHdl);
219  if( pHdl )
220  bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
221  rUnitConverter );
222 
223  return bRet;
224 }
225 
226 // Import a Property
228  const OUString& rStrImpValue,
229  XMLPropertyState& rProperty,
230  const SvXMLUnitConverter& rUnitConverter ) const
231 {
232  bool bRet = false;
233 
234  const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
235 
236  if( pHdl )
237  bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
238  rUnitConverter );
239 
240  return bRet;
241 }
242 
243 // Search for the given name and the namespace in the list and return
244 // the index of the entry
245 // If there is no matching entry the method returns -1
247  sal_uInt16 nNamespace,
248  const OUString& rStrName,
249  sal_uInt32 nPropType,
250  sal_Int32 nStartAt /* = -1 */ ) const
251 {
252  sal_Int32 nEntries = GetEntryCount();
253  sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
254 
255  if ( nEntries && nIndex < nEntries )
256  {
257  do
258  {
259  const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
260  if( (!nPropType || nPropType == rEntry.GetPropType()) &&
261  rEntry.nXMLNameSpace == nNamespace &&
262  rStrName == rEntry.sXMLAttributeName )
263  return nIndex;
264  else
265  nIndex++;
266 
267  } while( nIndex<nEntries );
268  }
269 
270  return -1;
271 }
272 
275  const char* sApiName,
276  sal_uInt16 nNameSpace,
277  const OUString& sXMLName ) const
278 {
279  sal_Int32 nIndex = 0;
280  sal_Int32 nEntries = GetEntryCount();
281 
282  do
283  {
284  const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
285  if( rEntry.nXMLNameSpace == nNameSpace &&
286  rEntry.sXMLAttributeName == sXMLName &&
287  rEntry.sAPIPropertyName.equalsAscii( sApiName ) )
288  return nIndex;
289  else
290  nIndex++;
291 
292  } while( nIndex < nEntries );
293 
294  return -1;
295 }
296 
297 sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
298 {
299  const sal_Int32 nEntries = GetEntryCount();
300 
301  if ( nEntries )
302  {
303  sal_Int32 nIndex = 0;
304  do
305  {
306  const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
307  if( rEntry.nContextId == nContextId )
308  return nIndex;
309  else
310  nIndex++;
311 
312  } while( nIndex < nEntries );
313  }
314 
315  return -1;
316 }
317 
318 void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
319 {
320  const sal_Int32 nEntries = GetEntryCount();
321  if( nIndex>=nEntries || nIndex<0 )
322  return;
323  vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = mpImpl->maMapEntries.begin();
324  std::advance(aEIter, nIndex);
325  mpImpl->maMapEntries.erase( aEIter );
326 }
327 
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddMapperEntry(const rtl::Reference< XMLPropertySetMapper > &rMapper)
Definition: xmlprmap.cxx:138
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:246
sal_uInt32 GetEntryFlags(sal_Int32 nIndex) const
Returns the flags of an entry.
Definition: xmlprmap.cxx:158
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:274
sal_Int32 mnIndex
Definition: maptype.hxx:124
std::vector< rtl::Reference< XMLPropertyHandlerFactory > > maHdlFactories
Definition: xmlprmap.cxx:92
const OUString & GetEntryXMLName(sal_Int32 nIndex) const
Returns the 'local' XML-name of the entry.
Definition: xmlprmap.cxx:177
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
sal_Int32 GetEntryCount() const
Return number of entries in input-array.
Definition: xmlprmap.cxx:153
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
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:183
sal_Int16 GetEntryContextId(sal_Int32 nIndex) const
returns the entry context id.
Definition: xmlprmap.cxx:189
std::unique_ptr< Impl > mpImpl
Definition: xmlprmap.hxx:40
bool importXML(const OUString &rStrImpValue, XMLPropertyState &rProperty, const SvXMLUnitConverter &rUnitConverter) const
Definition: xmlprmap.cxx:227
css::uno::Any maValue
Definition: maptype.hxx:125
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3370
#define MID_FLAG_MASK
Definition: xmltypes.hxx:36
virtual ~XMLPropertySetMapper() override
Definition: xmlprmap.cxx:134
sal_uInt16 GetEntryNameSpace(sal_Int32 nIndex) const
Returns the namespace-key of an entry.
Definition: xmlprmap.cxx:171
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:209
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:196
static const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nType)
Definition: txtprhdl.cxx:1218
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:96
const XMLPropertyHandler * GetPropertyHandler(sal_Int32 nIndex) const
Retrieves a PropertyHandler for that property which placed at nIndex in the XMLPropertyMapEntry-array...
Definition: xmlprmap.cxx:202
std::vector< XMLPropertySetMapperEntry_Impl > maMapEntries
Definition: xmlprmap.cxx:91
sal_uInt32 GetEntryType(sal_Int32 nIndex) const
Returns the type of an entry.
Definition: xmlprmap.cxx:164
#define XML_TYPE_PROP_MASK
Definition: xmltypes.hxx:91
void RemoveEntry(sal_Int32 nIndex)
Remove an entry.
Definition: xmlprmap.cxx:318