lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / xmloff / source / style / xmlprmap.cxx
blob046fd3de88624a83e917cbc3c2a1a3f547b1877e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmloff/xmlprmap.hxx>
21 #include <xmloff/xmlprhdl.hxx>
22 #include <xmloff/xmltypes.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <xmloff/maptype.hxx>
25 #include <xmloff/prhdlfac.hxx>
26 #include <tools/debug.hxx>
28 #include "xmlbahdl.hxx"
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/beans/XPropertyState.hpp>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Sequence.hxx>
35 #include <vector>
37 using namespace ::std;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using ::xmloff::token::GetXMLToken;
43 /** Helper-class for XML-im/export:
44 - Holds a pointer to a given array of XMLPropertyMapEntry
45 - Provides several methods to access data from this array
46 - Holds a Sequence of XML-names (for properties)
47 - The filter takes all properties of the XPropertySet which are also
48 in the XMLPropertyMapEntry and which are have not a default value
49 and put them into a vector of XMLPropertyStae
50 - this class knows how to compare, im/export properties
52 Attention: At all methods, which get an index as parameter, there is no
53 range validation to save runtime !!
55 struct XMLPropertySetMapperEntry_Impl
57 OUString sXMLAttributeName;
58 OUString sAPIPropertyName;
59 sal_Int32 nType;
60 sal_uInt16 nXMLNameSpace;
61 sal_Int16 nContextId;
62 SvtSaveOptions::ODFDefaultVersion nEarliestODFVersionForExport;
63 bool bImportOnly;
64 const XMLPropertyHandler *pHdl;
66 XMLPropertySetMapperEntry_Impl(
67 const XMLPropertyMapEntry& rMapEntry,
68 const rtl::Reference< XMLPropertyHandlerFactory >& rFactory );
70 XMLPropertySetMapperEntry_Impl(
71 const XMLPropertySetMapperEntry_Impl& rEntry );
73 sal_uInt32 GetPropType() const { return nType & XML_TYPE_PROP_MASK; }
76 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
77 const XMLPropertyMapEntry& rMapEntry,
78 const rtl::Reference< XMLPropertyHandlerFactory >& rFactory ) :
79 sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
80 sAPIPropertyName( OUString(rMapEntry.msApiName, rMapEntry.nApiNameLength,
81 RTL_TEXTENCODING_ASCII_US ) ),
82 nType( rMapEntry.mnType ),
83 nXMLNameSpace( rMapEntry.mnNameSpace ),
84 nContextId( rMapEntry.mnContextId ),
85 nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
86 bImportOnly( rMapEntry.mbImportOnly),
87 pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
89 assert(pHdl);
92 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
93 const XMLPropertySetMapperEntry_Impl& rEntry ) :
94 sXMLAttributeName( rEntry.sXMLAttributeName),
95 sAPIPropertyName( rEntry.sAPIPropertyName),
96 nType( rEntry.nType),
97 nXMLNameSpace( rEntry.nXMLNameSpace),
98 nContextId( rEntry.nContextId),
99 nEarliestODFVersionForExport( rEntry.nEarliestODFVersionForExport ),
100 bImportOnly( rEntry.bImportOnly),
101 pHdl( rEntry.pHdl)
103 assert(pHdl);
106 struct XMLPropertySetMapper::Impl
108 std::vector<XMLPropertySetMapperEntry_Impl> maMapEntries;
109 std::vector<rtl::Reference <XMLPropertyHandlerFactory> > maHdlFactories;
111 bool mbOnlyExportMappings;
113 Impl( bool bForExport ) : mbOnlyExportMappings(bForExport) {}
116 // Ctor
117 XMLPropertySetMapper::XMLPropertySetMapper(
118 const XMLPropertyMapEntry* pEntries, const rtl::Reference<XMLPropertyHandlerFactory>& rFactory,
119 bool bForExport ) :
120 mpImpl(new Impl(bForExport))
122 mpImpl->maHdlFactories.push_back(rFactory);
123 if( pEntries )
125 const XMLPropertyMapEntry* pIter = pEntries;
127 if (mpImpl->mbOnlyExportMappings)
129 while( pIter->msApiName )
131 if (!pIter->mbImportOnly)
133 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
134 mpImpl->maMapEntries.push_back( aEntry );
136 pIter++;
139 else
141 while( pIter->msApiName )
143 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
144 mpImpl->maMapEntries.push_back( aEntry );
145 pIter++;
151 XMLPropertySetMapper::~XMLPropertySetMapper()
153 delete mpImpl;
156 void XMLPropertySetMapper::AddMapperEntry(
157 const rtl::Reference < XMLPropertySetMapper >& rMapper )
159 for( vector < rtl::Reference < XMLPropertyHandlerFactory > >::iterator
160 aFIter = rMapper->mpImpl->maHdlFactories.begin();
161 aFIter != rMapper->mpImpl->maHdlFactories.end();
162 ++aFIter )
164 mpImpl->maHdlFactories.push_back(*aFIter);
167 for( vector < XMLPropertySetMapperEntry_Impl >::iterator
168 aEIter = rMapper->mpImpl->maMapEntries.begin();
169 aEIter != rMapper->mpImpl->maMapEntries.end();
170 ++aEIter )
172 if (!mpImpl->mbOnlyExportMappings || !(*aEIter).bImportOnly)
173 mpImpl->maMapEntries.push_back( *aEIter );
177 sal_Int32 XMLPropertySetMapper::GetEntryCount() const
179 return mpImpl->maMapEntries.size();
182 sal_uInt32 XMLPropertySetMapper::GetEntryFlags( sal_Int32 nIndex ) const
184 assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
185 return mpImpl->maMapEntries[nIndex].nType & ~MID_FLAG_MASK;
188 sal_uInt32 XMLPropertySetMapper::GetEntryType( sal_Int32 nIndex, bool bWithFlags ) const
190 assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
191 sal_uInt32 nType = mpImpl->maMapEntries[nIndex].nType;
192 if( !bWithFlags )
193 nType = nType & MID_FLAG_MASK;
194 return nType;
197 sal_uInt16 XMLPropertySetMapper::GetEntryNameSpace( sal_Int32 nIndex ) const
199 assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
200 return mpImpl->maMapEntries[nIndex].nXMLNameSpace;
203 const OUString& XMLPropertySetMapper::GetEntryXMLName( sal_Int32 nIndex ) const
205 assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
206 return mpImpl->maMapEntries[nIndex].sXMLAttributeName;
209 const OUString& XMLPropertySetMapper::GetEntryAPIName( sal_Int32 nIndex ) const
211 assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
212 return mpImpl->maMapEntries[nIndex].sAPIPropertyName;
215 sal_Int16 XMLPropertySetMapper::GetEntryContextId( sal_Int32 nIndex ) const
217 assert((-1 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
218 return nIndex == -1 ? 0 : mpImpl->maMapEntries[nIndex].nContextId;
221 SvtSaveOptions::ODFDefaultVersion XMLPropertySetMapper::GetEarliestODFVersionForExport( sal_Int32 nIndex ) const
223 assert((-1 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
224 return nIndex == -1 ? SvtSaveOptions::ODFVER_UNKNOWN : mpImpl->maMapEntries[nIndex].nEarliestODFVersionForExport;
227 const XMLPropertyHandler* XMLPropertySetMapper::GetPropertyHandler( sal_Int32 nIndex ) const
229 assert((0 <= nIndex) && (nIndex < static_cast<sal_Int32>(mpImpl->maMapEntries.size())));
230 return mpImpl->maMapEntries[nIndex].pHdl;
233 // Export a Property
234 bool XMLPropertySetMapper::exportXML(
235 OUString& rStrExpValue,
236 const XMLPropertyState& rProperty,
237 const SvXMLUnitConverter& rUnitConverter ) const
239 bool bRet = false;
241 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
243 assert(pHdl);
244 if( pHdl )
245 bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
246 rUnitConverter );
248 return bRet;
251 // Import a Property
252 bool XMLPropertySetMapper::importXML(
253 const OUString& rStrImpValue,
254 XMLPropertyState& rProperty,
255 const SvXMLUnitConverter& rUnitConverter ) const
257 bool bRet = false;
259 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
261 if( pHdl )
262 bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
263 rUnitConverter );
265 return bRet;
268 // Search for the given name and the namespace in the list and return
269 // the index of the entry
270 // If there is no matching entry the method returns -1
271 sal_Int32 XMLPropertySetMapper::GetEntryIndex(
272 sal_uInt16 nNamespace,
273 const OUString& rStrName,
274 sal_uInt32 nPropType,
275 sal_Int32 nStartAt /* = -1 */ ) const
277 sal_Int32 nEntries = GetEntryCount();
278 sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
280 if ( nEntries && nIndex < nEntries )
284 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
285 if( (!nPropType || nPropType == rEntry.GetPropType()) &&
286 rEntry.nXMLNameSpace == nNamespace &&
287 rStrName == rEntry.sXMLAttributeName )
288 return nIndex;
289 else
290 nIndex++;
292 } while( nIndex<nEntries );
295 return -1;
298 /** searches for an entry that matches the given api name, namespace and local name or -1 if nothing found */
299 sal_Int32 XMLPropertySetMapper::FindEntryIndex(
300 const sal_Char* sApiName,
301 sal_uInt16 nNameSpace,
302 const OUString& sXMLName ) const
304 sal_Int32 nIndex = 0;
305 sal_Int32 nEntries = GetEntryCount();
309 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
310 if( rEntry.nXMLNameSpace == nNameSpace &&
311 rEntry.sXMLAttributeName.equals( sXMLName ) &&
312 rEntry.sAPIPropertyName.equalsAscii( sApiName ) )
313 return nIndex;
314 else
315 nIndex++;
317 } while( nIndex < nEntries );
319 return -1;
322 sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
324 const sal_Int32 nEntries = GetEntryCount();
326 if ( nEntries )
328 sal_Int32 nIndex = 0;
331 const XMLPropertySetMapperEntry_Impl& rEntry = mpImpl->maMapEntries[nIndex];
332 if( rEntry.nContextId == nContextId )
333 return nIndex;
334 else
335 nIndex++;
337 } while( nIndex < nEntries );
340 return -1;
343 void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
345 const sal_Int32 nEntries = GetEntryCount();
346 if( nIndex>=nEntries || nIndex<0 )
347 return;
348 vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = mpImpl->maMapEntries.begin();
349 for( sal_Int32 nN=0; nN<nIndex; nN++ )
350 ++aEIter;
351 mpImpl->maMapEntries.erase( aEIter );
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */