xref: /trunk/main/xmloff/source/style/xmlexppr.cxx (revision f6c78b21b9451944eb43023c08ad1a3c8ad54ae4)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/xml/AttributeData.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/beans/XPropertyState.hpp>
30 #include <com/sun/star/lang/XTypeProvider.hpp>
31 #include <com/sun/star/beans/XMultiPropertySet.hpp>
32 #include <com/sun/star/beans/XTolerantMultiPropertySet.hpp>
33 #include <com/sun/star/beans/TolerantPropertySetResultType.hpp>
34 #include <rtl/ustrbuf.hxx>
35 #include <list>
36 #include <unordered_map>
37 
38 #include <xmloff/xmlexppr.hxx>
39 #include <xmloff/xmltoken.hxx>
40 #include <xmloff/attrlist.hxx>
41 #include <xmloff/nmspmap.hxx>
42 #include "xmloff/xmlnmspe.hxx"
43 #include <xmloff/xmlexp.hxx>
44 #include <xmloff/xmlcnimp.hxx>
45 #include <xmloff/xmlprmap.hxx>
46 #include <xmloff/PropertySetInfoHash.hxx>
47 #include <comphelper/stl_types.hxx>
48 
49 #ifndef _SVSTDARR_USHORTS
50 #define _SVSTDARR_USHORTS
51 #include <svl/svstdarr.hxx>
52 #endif
53 
54 using ::rtl::OUString;
55 using ::rtl::OUStringBuffer;
56 
57 using namespace ::std;
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::beans;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::lang;
62 using namespace ::xmloff::token;
63 
64 #define GET_PROP_TYPE( f ) static_cast<sal_uInt16>((f & XML_TYPE_PROP_MASK) >> XML_TYPE_PROP_SHIFT)
65 
66 struct XMLPropTokens_Impl
67 {
68     sal_uInt16 nType;
69     XMLTokenEnum eToken;
70 };
71 
72 #define ENTRY(t) { GET_PROP_TYPE(XML_TYPE_PROP_##t), XML_##t##_PROPERTIES }
73 const sal_uInt16 MAX_PROP_TYPES =
74     (XML_TYPE_PROP_END >> XML_TYPE_PROP_SHIFT) -
75     (XML_TYPE_PROP_START >> XML_TYPE_PROP_SHIFT);
76 
77 static  XMLPropTokens_Impl aPropTokens[MAX_PROP_TYPES] =
78 {
79     ENTRY(CHART),
80     ENTRY(GRAPHIC),
81     ENTRY(TABLE),
82     ENTRY(TABLE_COLUMN),
83     ENTRY(TABLE_ROW),
84     ENTRY(TABLE_CELL),
85     ENTRY(LIST_LEVEL),
86     ENTRY(PARAGRAPH),
87     ENTRY(TEXT),
88     ENTRY(DRAWING_PAGE),
89     ENTRY(PAGE_LAYOUT),
90     ENTRY(HEADER_FOOTER),
91     ENTRY(RUBY),
92     ENTRY(SECTION)
93 };
94 
95 // The drawing-layer fill entries of header and footer; ODF 1.4 writes them in a style:background-fill child.
lcl_IsHeaderFooterFill(const UniReference<XMLPropertySetMapper> & rMapper,sal_Int32 nIdx)96 static sal_Bool lcl_IsHeaderFooterFill( const UniReference< XMLPropertySetMapper >& rMapper, sal_Int32 nIdx )
97 {
98     const OUString& rName = rMapper->GetEntryAPIName( nIdx );
99     return ( rMapper->GetEntryFlags( nIdx ) & XML_TYPE_PROP_MASK ) == XML_TYPE_PROP_HEADER_FOOTER &&
100         ( rName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "HeaderFill" ) ) ||
101           rName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "FooterFill" ) ) ) &&
102         ( rMapper->GetEntryNameSpace( nIdx ) == XML_NAMESPACE_DRAW ||
103           ( rMapper->GetEntryNameSpace( nIdx ) == XML_NAMESPACE_STYLE &&
104             IsXMLToken( rMapper->GetEntryXMLName( nIdx ), XML_REPEAT ) ) );
105 }
106 
107 // The shape fill entries of an Impress table cell style; ODF 1.4 writes them in a style:background-fill child
108 // of style:table-cell-properties.
lcl_IsTableCellFill(const UniReference<XMLPropertySetMapper> & rMapper,sal_Int32 nIdx)109 static sal_Bool lcl_IsTableCellFill( const UniReference< XMLPropertySetMapper >& rMapper, sal_Int32 nIdx )
110 {
111     return ( rMapper->GetEntryFlags( nIdx ) & XML_TYPE_PROP_MASK ) == XML_TYPE_PROP_GRAPHIC &&
112         rMapper->GetEntryAPIName( nIdx ).matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "Fill" ) ) &&
113         ( rMapper->GetEntryNameSpace( nIdx ) == XML_NAMESPACE_DRAW ||
114           ( rMapper->GetEntryNameSpace( nIdx ) == XML_NAMESPACE_STYLE &&
115             IsXMLToken( rMapper->GetEntryXMLName( nIdx ), XML_REPEAT ) ) );
116 }
117 
118 // style:background-fill requires draw:fill; style:repeat alone (the default bitmap mode) is not a fill.
lcl_DropFillWithoutStyle(SvXMLAttributeList & rAttrs,const SvXMLNamespaceMap & rNamespaceMap)119 static void lcl_DropFillWithoutStyle( SvXMLAttributeList& rAttrs, const SvXMLNamespaceMap& rNamespaceMap )
120 {
121     if( rAttrs.getLength() > 0 &&
122         rAttrs.getValueByName( rNamespaceMap.GetQNameByKey( XML_NAMESPACE_DRAW, GetXMLToken( XML_FILL ) ) ).getLength() == 0 )
123         rAttrs.Clear();
124 }
125 
lcl_HasTableCellProperties(const UniReference<XMLPropertySetMapper> & rMapper)126 static sal_Bool lcl_HasTableCellProperties( const UniReference< XMLPropertySetMapper >& rMapper )
127 {
128     for( sal_Int32 n = 0; n < rMapper->GetEntryCount(); ++n )
129         if( ( rMapper->GetEntryFlags( n ) & XML_TYPE_PROP_MASK ) == XML_TYPE_PROP_TABLE_CELL )
130             return sal_True;
131     return sal_False;
132 }
133 
134 ///////////////////////////////////////////////////////////////////////////////
135 //
136 // public methods
137 //
138 
139 ///////////////////////////////////////////////////////////////////////////
140 //
141 // Take all properties of the XPropertySet which are also found in the
142 // XMLPropertyMapEntry-array and which are not set to their default-value,
143 // if a state is available.
144 //
145 // After that I call the method 'ContextFilter'.
146 //
147 
148 typedef std::list<XMLPropertyState> XMLPropertyStateList_Impl;
149 
150 class XMLPropertyStates_Impl
151 {
152     XMLPropertyStateList_Impl           aPropStates;
153     XMLPropertyStateList_Impl::iterator aLastItr;
154     sal_uInt32                          nCount;
155 public:
156     XMLPropertyStates_Impl();
157     void AddPropertyState(const XMLPropertyState& rPropState);
158     void FillPropertyStateVector(std::vector<XMLPropertyState>& rVector);
159 };
160 
XMLPropertyStates_Impl()161 XMLPropertyStates_Impl::XMLPropertyStates_Impl() :
162     aPropStates(),
163     nCount(0)
164 {
165     aLastItr = aPropStates.begin();
166 }
167 
AddPropertyState(const XMLPropertyState & rPropState)168 void XMLPropertyStates_Impl::AddPropertyState(
169         const XMLPropertyState& rPropState)
170 {
171     XMLPropertyStateList_Impl::iterator aItr = aPropStates.begin();
172     sal_Bool bInserted(sal_False);
173     if (nCount)
174     {
175         if (aLastItr->mnIndex < rPropState.mnIndex)
176             aItr = ++aLastItr;
177     }
178     do
179     {
180         // TODO: one path required only
181         if (aItr == aPropStates.end())
182         {
183             aLastItr = aPropStates.insert(aPropStates.end(), rPropState);
184             bInserted = sal_True;
185             nCount++;
186         }
187         else if (aItr->mnIndex > rPropState.mnIndex)
188         {
189             aLastItr = aPropStates.insert(aItr, rPropState);
190             bInserted = sal_True;
191             nCount++;
192         }
193     }
194     while(!bInserted && (aItr++ != aPropStates.end()));
195 }
196 
FillPropertyStateVector(std::vector<XMLPropertyState> & rVector)197 void XMLPropertyStates_Impl::FillPropertyStateVector(
198         std::vector<XMLPropertyState>& rVector)
199 {
200     if (nCount)
201     {
202         rVector.resize(nCount, XMLPropertyState(-1));
203         ::std::copy( aPropStates.begin(), aPropStates.end(), rVector.begin() );
204     }
205 }
206 
207 class FilterPropertyInfo_Impl
208 {
209     const rtl::OUString     sApiName;
210     std::list<sal_uInt32>   aIndexes;
211     sal_uInt32              nCount;
212 
213 public:
214 
215     FilterPropertyInfo_Impl( const rtl::OUString& rApiName,
216                              const sal_uInt32 nIndex);
217 
GetApiName() const218     const OUString& GetApiName() const { return sApiName; }
GetIndexes()219     std::list<sal_uInt32>& GetIndexes() { return aIndexes; }
220 
AddIndex(sal_uInt32 nIndex)221     void AddIndex( sal_uInt32 nIndex )
222     {
223         aIndexes.push_back(nIndex);
224         nCount++;
225     }
226 
227     // for sort
operator <(const FilterPropertyInfo_Impl & rArg) const228     sal_Bool operator< ( const FilterPropertyInfo_Impl& rArg ) const
229     {
230         return (GetApiName() < rArg.GetApiName());
231     }
232 };
233 
FilterPropertyInfo_Impl(const rtl::OUString & rApiName,const sal_uInt32 nIndex)234 FilterPropertyInfo_Impl::FilterPropertyInfo_Impl(
235         const rtl::OUString& rApiName,
236         const sal_uInt32 nIndex ) :
237     sApiName( rApiName ),
238     aIndexes(),
239     nCount(1)
240 {
241     aIndexes.push_back(nIndex);
242 }
243 
244 typedef std::list<FilterPropertyInfo_Impl> FilterPropertyInfoList_Impl;
245 
246 // ----------------------------------------------------------------------------
247 
248 class FilterPropertiesInfo_Impl
249 {
250     sal_uInt32                              nCount;
251     FilterPropertyInfoList_Impl             aPropInfos;
252     FilterPropertyInfoList_Impl::iterator   aLastItr;
253 
254     Sequence <OUString>                     *pApiNames;
255 
256 public:
257     FilterPropertiesInfo_Impl();
258     ~FilterPropertiesInfo_Impl();
259 
260     void AddProperty(const rtl::OUString& rApiName, const sal_uInt32 nIndex);
261     const uno::Sequence<OUString>& GetApiNames();
262     void FillPropertyStateArray(
263             vector< XMLPropertyState >& rPropStates,
264             const Reference< XPropertySet >& xPropSet,
265             const UniReference< XMLPropertySetMapper >& maPropMapper,
266             const sal_Bool bDefault = sal_False);
GetPropertyCount() const267     sal_uInt32 GetPropertyCount() const { return nCount; }
268 };
269 
270 // ----------------------------------------------------------------------------
271 
272 typedef std::unordered_map
273 <
274     PropertySetInfoKey,
275     FilterPropertiesInfo_Impl *,
276     PropertySetInfoHash,
277     PropertySetInfoHash
278 >
279 FilterOropertiesHashMap_Impl;
280 
281 class FilterPropertiesInfos_Impl : public FilterOropertiesHashMap_Impl
282 {
283 public:
284     ~FilterPropertiesInfos_Impl ();
285 };
286 
~FilterPropertiesInfos_Impl()287 FilterPropertiesInfos_Impl::~FilterPropertiesInfos_Impl ()
288 {
289     FilterOropertiesHashMap_Impl::iterator aIter = begin();
290     FilterOropertiesHashMap_Impl::iterator aEnd = end();
291     while( aIter != aEnd )
292     {
293         delete (*aIter).second;
294         (*aIter).second = 0;
295         ++aIter;
296     }
297 }
298 
299 // ----------------------------------------------------------------------------
300 
FilterPropertiesInfo_Impl()301 FilterPropertiesInfo_Impl::FilterPropertiesInfo_Impl() :
302     nCount(0),
303     aPropInfos(),
304     pApiNames( 0 )
305 {
306     aLastItr = aPropInfos.begin();
307 }
308 
~FilterPropertiesInfo_Impl()309 FilterPropertiesInfo_Impl::~FilterPropertiesInfo_Impl()
310 {
311     delete pApiNames;
312 }
313 
AddProperty(const rtl::OUString & rApiName,const sal_uInt32 nIndex)314 void FilterPropertiesInfo_Impl::AddProperty(
315         const rtl::OUString& rApiName, const sal_uInt32 nIndex)
316 {
317     aPropInfos.push_back(FilterPropertyInfo_Impl(rApiName, nIndex));
318     nCount++;
319 
320     OSL_ENSURE( !pApiNames, "performance warning: API names already retrieved" );
321     if( pApiNames )
322     {
323         delete pApiNames;
324         pApiNames = NULL;
325     }
326 }
GetApiNames()327 const uno::Sequence<OUString>& FilterPropertiesInfo_Impl::GetApiNames()
328 {
329     OSL_ENSURE(nCount == aPropInfos.size(), "wrong property count");
330     if( !pApiNames )
331     {
332         // we have to do three things:
333         // 1) sort API names,
334         // 2) merge duplicates,
335         // 3) construct sequence
336 
337         // sort names
338         aPropInfos.sort();
339 
340         // merge duplicates
341         if ( nCount > 1 )
342         {
343             FilterPropertyInfoList_Impl::iterator aOld = aPropInfos.begin();
344             FilterPropertyInfoList_Impl::iterator aEnd = aPropInfos.end();
345             FilterPropertyInfoList_Impl::iterator aCurrent = aOld;
346             aCurrent++;
347 
348             while ( aCurrent != aEnd )
349             {
350                 // equal to next element?
351                 if ( aOld->GetApiName().equals( aCurrent->GetApiName() ) )
352                 {
353                     // if equal: merge index lists
354                     aOld->GetIndexes().merge( aCurrent->GetIndexes() );
355                     // erase element, and continue with next
356                     aCurrent = aPropInfos.erase( aCurrent );
357                     nCount--;
358                 }
359                 else
360                 {
361                     // remember old element and continue with next
362                     aOld = aCurrent;
363                     aCurrent++;
364                 }
365             }
366         }
367 
368         // construct sequence
369         pApiNames = new Sequence < OUString >( nCount );
370         OUString *pNames = pApiNames->getArray();
371         FilterPropertyInfoList_Impl::iterator aItr = aPropInfos.begin();
372         FilterPropertyInfoList_Impl::iterator aEnd = aPropInfos.end();
373         for ( ; aItr != aEnd; aItr++, pNames++)
374             *pNames = aItr->GetApiName();
375     }
376 
377     return *pApiNames;
378 }
379 
FillPropertyStateArray(vector<XMLPropertyState> & rPropStates,const Reference<XPropertySet> & rPropSet,const UniReference<XMLPropertySetMapper> & rPropMapper,const sal_Bool bDefault)380 void FilterPropertiesInfo_Impl::FillPropertyStateArray(
381         vector< XMLPropertyState >& rPropStates,
382         const Reference< XPropertySet >& rPropSet,
383         const UniReference< XMLPropertySetMapper >& rPropMapper,
384         const sal_Bool bDefault )
385 {
386     XMLPropertyStates_Impl aPropStates;
387 
388     const uno::Sequence<OUString>& rApiNames = GetApiNames();
389 
390     Reference < XTolerantMultiPropertySet > xTolPropSet( rPropSet, UNO_QUERY );
391     if (xTolPropSet.is())
392     {
393         if (!bDefault)
394         {
395             Sequence < beans::GetDirectPropertyTolerantResult > aResults(xTolPropSet->getDirectPropertyValuesTolerant(rApiNames));
396             sal_Int32 nResultCount(aResults.getLength());
397             if (nResultCount > 0)
398             {
399                 const beans::GetDirectPropertyTolerantResult *pResults = aResults.getConstArray();
400                 FilterPropertyInfoList_Impl::iterator aPropIter(aPropInfos.begin());
401                 XMLPropertyState aNewProperty( -1 );
402                 sal_uInt32 i = 0;
403                 while (nResultCount > 0 && i < nCount)
404                 {
405                     if (pResults->Name == aPropIter->GetApiName())
406                     {
407                         aNewProperty.mnIndex = -1;
408                         aNewProperty.maValue = pResults->Value;
409 
410                         for( std::list<sal_uInt32>::iterator aIndexItr(aPropIter->GetIndexes().begin());
411                             aIndexItr != aPropIter->GetIndexes().end();
412                             ++aIndexItr )
413                         {
414                             aNewProperty.mnIndex = *aIndexItr;
415                             aPropStates.AddPropertyState( aNewProperty );
416                         }
417                         ++pResults;
418                         --nResultCount;
419                     }
420                     ++aPropIter;
421                     ++i;
422                 }
423             }
424         }
425         else
426         {
427             Sequence < beans::GetPropertyTolerantResult > aResults(xTolPropSet->getPropertyValuesTolerant(rApiNames));
428             OSL_ENSURE( rApiNames.getLength() == aResults.getLength(), "wrong implemented XTolerantMultiPropertySet" );
429             const beans::GetPropertyTolerantResult *pResults = aResults.getConstArray();
430             FilterPropertyInfoList_Impl::iterator aPropIter(aPropInfos.begin());
431             XMLPropertyState aNewProperty( -1 );
432             sal_uInt32 nResultCount(aResults.getLength());
433             OSL_ENSURE( nCount == nResultCount, "wrong implemented XTolerantMultiPropertySet??" );
434             for( sal_uInt32 i = 0; i < nResultCount; ++i )
435             {
436                 if ((pResults->Result == beans::TolerantPropertySetResultType::SUCCESS) &&
437                     ((pResults->State == PropertyState_DIRECT_VALUE) || (pResults->State == PropertyState_DEFAULT_VALUE)))
438                 {
439                     aNewProperty.mnIndex = -1;
440                     aNewProperty.maValue = pResults->Value;
441 
442                     for( std::list<sal_uInt32>::iterator aIndexItr(aPropIter->GetIndexes().begin());
443                         aIndexItr != aPropIter->GetIndexes().end();
444                         ++aIndexItr )
445                     {
446                         aNewProperty.mnIndex = *aIndexItr;
447                         aPropStates.AddPropertyState( aNewProperty );
448                     }
449                 }
450                 ++pResults;
451                 ++aPropIter;
452             }
453         }
454     }
455     else
456     {
457         Sequence < PropertyState > aStates;
458         const PropertyState *pStates = 0;
459         Reference< XPropertyState > xPropState( rPropSet, UNO_QUERY );
460         if( xPropState.is() )
461         {
462             aStates = xPropState->getPropertyStates( rApiNames );
463             pStates = aStates.getConstArray();
464         }
465 
466         Reference < XMultiPropertySet > xMultiPropSet( rPropSet, UNO_QUERY );
467         if( xMultiPropSet.is() && !bDefault )
468         {
469             Sequence < Any > aValues;
470             if( pStates )
471             {
472                 // step 1: get value count
473                 sal_uInt32 nValueCount = 0;
474                 sal_uInt32 i;
475 
476                 for( i = 0; i < nCount; ++i, ++pStates )
477                 {
478                     if( (*pStates == PropertyState_DIRECT_VALUE)/* || (bDefault && (*pStates == PropertyState_DEFAULT_VALUE))*/ )
479                         nValueCount++;
480                 }
481 
482                 if( nValueCount )
483                 {
484                     // step 2: collect property names
485                     Sequence < OUString > aAPINames( nValueCount );
486                     OUString *pAPINames = aAPINames.getArray();
487 
488                     ::std::vector< FilterPropertyInfoList_Impl::iterator > aPropIters;
489                     aPropIters.reserve( nValueCount );
490 
491                     FilterPropertyInfoList_Impl::iterator aItr = aPropInfos.begin();
492                     OSL_ENSURE(aItr != aPropInfos.end(),"Invalid iterator!");
493 
494                     pStates = aStates.getConstArray();
495                     i = 0;
496                     while( i < nValueCount )
497                     {
498                         if( (*pStates == PropertyState_DIRECT_VALUE)/* || (bDefault && (*pStates == PropertyState_DEFAULT_VALUE))*/ )
499                         {
500                             *pAPINames++ = aItr->GetApiName();
501                             aPropIters.push_back( aItr );
502                             ++i;
503                         }
504                         ++aItr;
505                         ++pStates;
506                     }
507 
508                     aValues = xMultiPropSet->getPropertyValues( aAPINames );
509                     const Any *pValues = aValues.getConstArray();
510 
511                     ::std::vector< FilterPropertyInfoList_Impl::iterator >::const_iterator
512                         pPropIter = aPropIters.begin();
513 
514                     XMLPropertyState aNewProperty( -1 );
515                     for( i = 0; i < nValueCount; i++ )
516                     {
517                         aNewProperty.mnIndex = -1;
518                         aNewProperty.maValue = *pValues;
519 
520                         const ::std::list< sal_uInt32 >& rIndexes( (*pPropIter)->GetIndexes() );
521                         for (   std::list<sal_uInt32>::const_iterator aIndexItr = rIndexes.begin();
522                                 aIndexItr != rIndexes.end();
523                                 ++aIndexItr
524                             )
525                         {
526                             aNewProperty.mnIndex = *aIndexItr;
527                             aPropStates.AddPropertyState( aNewProperty );
528                         }
529 
530                         ++pPropIter;
531                         ++pValues;
532                     }
533                 }
534             }
535             else
536             {
537                 aValues = xMultiPropSet->getPropertyValues( rApiNames );
538                 const Any *pValues = aValues.getConstArray();
539 
540                 FilterPropertyInfoList_Impl::iterator aItr = aPropInfos.begin();
541                 for(sal_uInt32 i = 0; i < nCount; i++ )
542                 {
543                     // The value is stored in the PropertySet itself, add to list.
544                     XMLPropertyState aNewProperty( -1 );
545                     aNewProperty.maValue = *pValues;
546                     ++pValues;
547                     for( std::list<sal_uInt32>::iterator aIndexItr =
548                             aItr->GetIndexes().begin();
549                         aIndexItr != aItr->GetIndexes().end();
550                         aIndexItr++ )
551                     {
552                         aNewProperty.mnIndex = *aIndexItr;
553                         aPropStates.AddPropertyState( aNewProperty );
554                     }
555                     aItr++;
556                 }
557             }
558         }
559         else
560         {
561             FilterPropertyInfoList_Impl::iterator aItr = aPropInfos.begin();
562             for(sal_uInt32 i = 0; i < nCount; i++ )
563             {
564                 sal_Bool bDirectValue =
565                     !pStates || *pStates == PropertyState_DIRECT_VALUE;
566                 if( bDirectValue || bDefault )
567                 {
568                     // The value is stored in the PropertySet itself, add to list.
569                     sal_Bool bGotValue = sal_False;
570                     XMLPropertyState aNewProperty( -1 );
571                     for( std::list<sal_uInt32>::const_iterator aIndexItr =
572                             aItr->GetIndexes().begin();
573                         aIndexItr != aItr->GetIndexes().end();
574                         aIndexItr++ )
575                     {
576                         if( bDirectValue ||
577                             (rPropMapper->GetEntryFlags( *aIndexItr ) &
578                                             MID_FLAG_DEFAULT_ITEM_EXPORT) != 0 )
579                         {
580                             try
581                             {
582                                 if( !bGotValue )
583                                 {
584                                     aNewProperty.maValue =
585                                         rPropSet->getPropertyValue( aItr->GetApiName() );
586                                     bGotValue = sal_True;
587                                 }
588                                 aNewProperty.mnIndex = *aIndexItr;
589                                 aPropStates.AddPropertyState( aNewProperty );
590                             }
591                             catch( UnknownPropertyException& )
592                             {
593                                 // might be a problem of getImplemenetationId
594                                 OSL_ENSURE( sal_False, "unknown property in getPropertyValue" );
595                             }
596 
597                         }
598                     }
599                 }
600 
601                 aItr++;
602                 if( pStates )
603                     pStates++;
604             }
605         }
606     }
607     aPropStates.FillPropertyStateVector(rPropStates);
608 }
609 
610 ///////////////////////////////////////////////////////////////////////////////
611 //
612 // ctor/dtor , class SvXMLExportPropertyMapper
613 //
614 
SvXMLExportPropertyMapper(const UniReference<XMLPropertySetMapper> & rMapper)615 SvXMLExportPropertyMapper::SvXMLExportPropertyMapper(
616         const UniReference< XMLPropertySetMapper >& rMapper ) :
617     pCache( 0 ),
618     maPropMapper( rMapper )
619 {
620 }
621 
~SvXMLExportPropertyMapper()622 SvXMLExportPropertyMapper::~SvXMLExportPropertyMapper()
623 {
624     delete pCache;
625     mxNextMapper = 0;
626 }
627 
ChainExportMapper(const UniReference<SvXMLExportPropertyMapper> & rMapper)628 void SvXMLExportPropertyMapper::ChainExportMapper(
629         const UniReference< SvXMLExportPropertyMapper>& rMapper )
630 {
631     // add map entries from rMapper to current map
632     maPropMapper->AddMapperEntry( rMapper->getPropertySetMapper() );
633     // rMapper uses the same map as 'this'
634     rMapper->maPropMapper = maPropMapper;
635 
636     // set rMapper as last mapper in current chain
637     UniReference< SvXMLExportPropertyMapper > xNext = mxNextMapper;
638     if( xNext.is())
639     {
640         while( xNext->mxNextMapper.is())
641             xNext = xNext->mxNextMapper;
642         xNext->mxNextMapper = rMapper;
643     }
644     else
645         mxNextMapper = rMapper;
646 
647     // if rMapper was already chained, correct
648     // map pointer of successors
649     xNext = rMapper;
650 
651     while( xNext->mxNextMapper.is())
652     {
653         xNext = xNext->mxNextMapper;
654         xNext->maPropMapper = maPropMapper;
655     }
656 }
657 
658 
_Filter(const Reference<XPropertySet> xPropSet,const sal_Bool bDefault) const659 vector< XMLPropertyState > SvXMLExportPropertyMapper::_Filter(
660         const Reference< XPropertySet > xPropSet,
661         const sal_Bool bDefault ) const
662 {
663     vector< XMLPropertyState > aPropStateArray;
664 
665     // Retrieve XPropertySetInfo and XPropertyState
666     Reference< XPropertySetInfo > xInfo( xPropSet->getPropertySetInfo() );
667     if( !xInfo.is() )
668         return aPropStateArray;
669 
670     sal_Int32 nProps = maPropMapper->GetEntryCount();
671 
672     FilterPropertiesInfo_Impl *pFilterInfo = 0;
673 
674     Reference < XTypeProvider > xTypeProv( xPropSet, UNO_QUERY );
675     Sequence< sal_Int8 > aImplId;
676     if( xTypeProv.is() )
677     {
678         aImplId = xTypeProv->getImplementationId();
679         if( aImplId.getLength() == 16 )
680         {
681             if( pCache )
682             {
683                 // The key must not be created outside this block, because it
684                 // keeps a reference to the property set info.
685                 PropertySetInfoKey aKey( xInfo, aImplId );
686                 FilterPropertiesInfos_Impl::iterator aIter =
687                     pCache->find( aKey );
688                 if( aIter != pCache->end() )
689                     pFilterInfo = (*aIter).second;
690             }
691         }
692     }
693 
694     sal_Bool bDelInfo = sal_False;
695     if( !pFilterInfo )
696     {
697         pFilterInfo = new FilterPropertiesInfo_Impl;
698         for( sal_Int32 i=0; i < nProps; i++ )
699         {
700             // Are we allowed to ask for the property? (MID_FLAG_NO_PROP..)
701             // Does the PropertySet contain name of mpEntries-array ?
702             const OUString& rAPIName = maPropMapper->GetEntryAPIName( i );
703             const sal_Int32 nFlags = maPropMapper->GetEntryFlags( i );
704             if( (0 == (nFlags & MID_FLAG_NO_PROPERTY_EXPORT)) &&
705                 ( (0 != (nFlags & MID_FLAG_MUST_EXIST)) ||
706                   xInfo->hasPropertyByName( rAPIName ) ) )
707             {
708                 const SvtSaveOptions::ODFDefaultVersion nCurrentVersion( SvtSaveOptions().GetODFDefaultVersion() );
709                 const SvtSaveOptions::ODFDefaultVersion nEarliestODFVersionForExport(
710                         maPropMapper->GetEarliestODFVersionForExport( i ) );
711                 sal_Bool bExport( nCurrentVersion == SvtSaveOptions::ODFVER_UNKNOWN
712                         || nEarliestODFVersionForExport == SvtSaveOptions::ODFVER_UNKNOWN );
713                 if( nEarliestODFVersionForExport == SvtSaveOptions::ODFVER_EXTENSION )
714                     bExport = bExport || SvtSaveOptions::IsODFExtended( nCurrentVersion ) ||
715                         ( nCurrentVersion == SvtSaveOptions::ODFVER_014 && lcl_IsHeaderFooterFill( maPropMapper, i ) );
716                 else if( nEarliestODFVersionForExport == SvtSaveOptions::ODFVER_EXTENSION_PRE_013 )
717                     bExport = bExport || ( SvtSaveOptions::IsODFExtended( nCurrentVersion ) && nCurrentVersion < SvtSaveOptions::ODFVER_013 );
718                 else if( nEarliestODFVersionForExport == SvtSaveOptions::ODFVER_013 )
719                     bExport = nCurrentVersion >= SvtSaveOptions::ODFVER_013;   // the unknown mode keeps writing what it wrote before
720                 else
721                     bExport = bExport || nCurrentVersion >= nEarliestODFVersionForExport;
722                 if( bExport )
723                     pFilterInfo->AddProperty(rAPIName, i);
724             }
725         }
726 
727         if( xTypeProv.is() && aImplId.getLength() == 16 )
728         {
729             // Check whether the property set info is destroyed if it is
730             // assigned to a weak reference only. If it is destroyed, then
731             // every instance of getPropertySetInfo returns a new object.
732             // Such property set infos must not be cached.
733             WeakReference < XPropertySetInfo > xWeakInfo( xInfo );
734             xInfo = 0;
735             xInfo = xWeakInfo;
736             if( xInfo.is() )
737             {
738                 if( !pCache )
739                     ((SvXMLExportPropertyMapper *)this)->pCache =
740                         new FilterPropertiesInfos_Impl;
741                 PropertySetInfoKey aKey( xInfo, aImplId );
742                 (*pCache)[aKey] = pFilterInfo;
743             }
744             else
745                 bDelInfo = sal_True;
746         }
747         else
748         {
749             OSL_ENSURE(sal_False, "here is no TypeProvider or the ImplId is wrong");
750             bDelInfo = sal_True;
751         }
752     }
753 
754     if( pFilterInfo->GetPropertyCount() )
755     {
756         try
757         {
758             pFilterInfo->FillPropertyStateArray(aPropStateArray,
759                                                 xPropSet, maPropMapper,
760                                                 bDefault);
761         }
762         catch( UnknownPropertyException& )
763         {
764             // might be a problem of getImplemenetationId
765             OSL_ENSURE( sal_False, "unknown property in getPropertyStates" );
766         }
767     }
768 
769     // Call centext-filter
770     if( !aPropStateArray.empty() )
771         ContextFilter( aPropStateArray, xPropSet );
772 
773     // Have to do if we change from a vector to a list or something like that
774     /*vector< XMLPropertyState >::iterator aItr = aPropStateArray.begin();
775     while (aItr != aPropStateArray.end())
776     {
777         if (aItr->mnIndex == -1)
778             aItr = aPropStateArray.erase(aItr);
779         else
780             aItr++;
781     }*/
782 
783     if( bDelInfo )
784         delete pFilterInfo;
785 
786     return aPropStateArray;
787 }
788 
ContextFilter(vector<XMLPropertyState> & rProperties,Reference<XPropertySet> rPropSet) const789 void SvXMLExportPropertyMapper::ContextFilter(
790         vector< XMLPropertyState >& rProperties,
791         Reference< XPropertySet > rPropSet ) const
792 {
793     // Derived class could implement this.
794     if( mxNextMapper.is() )
795         mxNextMapper->ContextFilter( rProperties, rPropSet );
796 }
797 
798 ///////////////////////////////////////////////////////////////////////////
799 //
800 // Compares two Sequences of XMLPropertyState:
801 //  1.Number of elements equal ?
802 //  2.Index of each element equal ? (So I know whether the propertynames are the same)
803 //  3.Value of each element equal ?
804 //
Equals(const vector<XMLPropertyState> & aProperties1,const vector<XMLPropertyState> & aProperties2) const805 sal_Bool SvXMLExportPropertyMapper::Equals(
806         const vector< XMLPropertyState >& aProperties1,
807         const vector< XMLPropertyState >& aProperties2 ) const
808 {
809     sal_Bool bRet = sal_True;
810     sal_uInt32 nCount = aProperties1.size();
811 
812     if( nCount == aProperties2.size() )
813     {
814         sal_uInt32 nIndex = 0;
815         while( bRet && nIndex < nCount )
816         {
817             const XMLPropertyState& rProp1 = aProperties1[ nIndex ];
818             const XMLPropertyState& rProp2 = aProperties2[ nIndex ];
819 
820             // Compare index. If equal, compare value
821             if( rProp1.mnIndex == rProp2.mnIndex )
822             {
823                 if( rProp1.mnIndex != -1 )
824                 {
825                     // Now compare values
826                     if( ( maPropMapper->GetEntryType( rProp1.mnIndex ) &
827                           XML_TYPE_BUILDIN_CMP ) != 0 )
828                         // simple type ( binary compare )
829                         bRet = ( rProp1.maValue == rProp2.maValue );
830                     else
831                         // complex type ( ask for compare-function )
832                         bRet = maPropMapper->GetPropertyHandler(
833                                     rProp1.mnIndex )->equals( rProp1.maValue,
834                                                               rProp2.maValue );
835                 }
836             }
837             else
838                 bRet = sal_False;
839 
840             nIndex++;
841         }
842     }
843     else
844         bRet = sal_False;
845 
846     return bRet;
847 }
848 
849 
850 /** fills the given attribute list with the items in the given set
851 void SvXMLExportPropertyMapper::exportXML( SvXMLAttributeList& rAttrList,
852         const ::std::vector< XMLPropertyState >& rProperties,
853         const SvXMLUnitConverter& rUnitConverter,
854         const SvXMLNamespaceMap& rNamespaceMap,
855         sal_uInt16 nFlags ) const
856 {
857     _exportXML( rAttrList, rProperties, rUnitConverter, rNamespaceMap,
858                 nFlags, 0, -1, -1 );
859 }
860 
861 
862 void SvXMLExportPropertyMapper::exportXML( SvXMLAttributeList& rAttrList,
863         const ::std::vector< XMLPropertyState >& rProperties,
864         const SvXMLUnitConverter& rUnitConverter,
865         const SvXMLNamespaceMap& rNamespaceMap,
866         sal_Int32 nPropMapStartIdx, sal_Int32 nPropMapEndIdx,
867         sal_uInt16 nFlags ) const
868 {
869     _exportXML( rAttrList, rProperties, rUnitConverter, rNamespaceMap,
870                 nFlags, 0, nPropMapStartIdx, nPropMapEndIdx );
871 }
872 */
873 
874 
exportXML(SvXMLAttributeList & rAttrList,const XMLPropertyState & rProperty,const SvXMLUnitConverter & rUnitConverter,const SvXMLNamespaceMap & rNamespaceMap,sal_uInt16 nFlags) const875 void SvXMLExportPropertyMapper::exportXML( SvXMLAttributeList& rAttrList,
876         const XMLPropertyState& rProperty,
877         const SvXMLUnitConverter& rUnitConverter,
878         const SvXMLNamespaceMap& rNamespaceMap,
879         sal_uInt16 nFlags ) const
880 {
881     if( ( maPropMapper->GetEntryFlags( rProperty.mnIndex ) &
882                 MID_FLAG_ELEMENT_ITEM_EXPORT ) == 0 )
883         _exportXML( rAttrList, rProperty, rUnitConverter, rNamespaceMap,
884                     nFlags );
885 }
886 
exportXML(SvXMLExport & rExport,const::std::vector<XMLPropertyState> & rProperties,sal_uInt16 nFlags) const887 void SvXMLExportPropertyMapper::exportXML(
888         SvXMLExport& rExport,
889         const ::std::vector< XMLPropertyState >& rProperties,
890         sal_uInt16 nFlags ) const
891 {
892     exportXML( rExport, rProperties, -1, -1,  nFlags );
893 }
894 
exportXML(SvXMLExport & rExport,const::std::vector<XMLPropertyState> & rProperties,sal_Int32 nPropMapStartIdx,sal_Int32 nPropMapEndIdx,sal_uInt16 nFlags) const895 void SvXMLExportPropertyMapper::exportXML(
896         SvXMLExport& rExport,
897         const ::std::vector< XMLPropertyState >& rProperties,
898         sal_Int32 nPropMapStartIdx, sal_Int32 nPropMapEndIdx,
899         sal_uInt16 nFlags ) const
900 {
901     const SvtSaveOptions::ODFDefaultVersion eVersion( SvtSaveOptions().GetODFDefaultVersion() );
902     const bool b014 = eVersion == SvtSaveOptions::ODFVER_014 || eVersion == SvtSaveOptions::ODFVER_014_EXT;
903 
904     // ODF 1.4: the fill of a table cell style goes into a style:background-fill child of its table-cell-properties
905     ::std::vector< XMLPropertyState > aCellMainProps, aCellFillProps;
906     const ::std::vector< XMLPropertyState >* pAllProps = &rProperties;
907     bool bCellFill = false;
908     if( b014 && lcl_HasTableCellProperties( maPropMapper ) )
909     {
910         aCellMainProps = rProperties;
911         aCellFillProps = rProperties;
912         for( size_t n = 0; n < rProperties.size(); ++n )
913         {
914             const sal_Int32 nIdx = rProperties[n].mnIndex;
915             if( nIdx == -1 )
916                 continue;
917             if( lcl_IsTableCellFill( maPropMapper, nIdx ) )
918             {
919                 aCellMainProps[n].mnIndex = -1;
920                 bCellFill = true;
921             }
922             else
923                 aCellFillProps[n].mnIndex = -1;
924         }
925         if( bCellFill )
926             pAllProps = &aCellMainProps;
927     }
928 
929     sal_uInt16 nPropTypeFlags = bCellFill ? ( 1 << GET_PROP_TYPE( XML_TYPE_PROP_TABLE_CELL ) ) : 0;
930     for( sal_uInt16 i=0; i<MAX_PROP_TYPES; ++i )
931     {
932         sal_uInt16 nPropType = aPropTokens[i].nType;
933         if( 0==i || (nPropTypeFlags & (1 << nPropType)) != 0 )
934         {
935             SvUShorts aIndexArray;
936 
937             // ODF 1.4: header and footer fills go into a style:background-fill child
938             ::std::vector< XMLPropertyState > aMainProps, aFillProps;
939             const ::std::vector< XMLPropertyState >* pMainProps = pAllProps;
940             SvXMLAttributeList* pFillAttrs = 0;
941             uno::Reference< xml::sax::XAttributeList > xFillAttrs;
942             if( bCellFill && aPropTokens[i].eToken == XML_TABLE_CELL_PROPERTIES )
943             {
944                 pFillAttrs = new SvXMLAttributeList;
945                 xFillAttrs = pFillAttrs;
946                 sal_uInt16 nFillTypeFlags = 0;
947                 _exportXML( GET_PROP_TYPE( XML_TYPE_PROP_GRAPHIC ), nFillTypeFlags, *pFillAttrs, aCellFillProps,
948                             rExport.GetMM100UnitConverter(),
949                             rExport.GetNamespaceMap(),
950                             nFlags, 0,
951                             nPropMapStartIdx, nPropMapEndIdx );
952                 lcl_DropFillWithoutStyle( *pFillAttrs, rExport.GetNamespaceMap() );
953             }
954             else if( aPropTokens[i].eToken == XML_HEADER_FOOTER_PROPERTIES && b014 )
955             {
956                 aMainProps = rProperties;
957                 aFillProps = rProperties;
958                 bool bFill = false;
959                 for( size_t n = 0; n < rProperties.size(); ++n )
960                 {
961                     const sal_Int32 nIdx = rProperties[n].mnIndex;
962                     if( nIdx == -1 )
963                         continue;
964                     if( lcl_IsHeaderFooterFill( maPropMapper, nIdx ) )
965                     {
966                         aMainProps[n].mnIndex = -1;
967                         bFill = true;
968                     }
969                     else
970                         aFillProps[n].mnIndex = -1;
971                 }
972                 if( bFill )
973                 {
974                     pMainProps = &aMainProps;
975                     pFillAttrs = new SvXMLAttributeList;
976                     xFillAttrs = pFillAttrs;
977                     sal_uInt16 nFillTypeFlags = 0;
978                     _exportXML( nPropType, nFillTypeFlags, *pFillAttrs, aFillProps,
979                                 rExport.GetMM100UnitConverter(),
980                                 rExport.GetNamespaceMap(),
981                                 nFlags, 0,
982                                 nPropMapStartIdx, nPropMapEndIdx );
983                     lcl_DropFillWithoutStyle( *pFillAttrs, rExport.GetNamespaceMap() );
984                 }
985             }
986 
987             _exportXML( nPropType, nPropTypeFlags,
988                         rExport.GetAttrList(), *pMainProps,
989                         rExport.GetMM100UnitConverter(),
990                         rExport.GetNamespaceMap(),
991                         nFlags, &aIndexArray,
992                         nPropMapStartIdx, nPropMapEndIdx );
993 
994             if( rExport.GetAttrList().getLength() > 0L ||
995                 (nFlags & XML_EXPORT_FLAG_EMPTY) != 0 ||
996                 aIndexArray.Count() != 0 ||
997                 ( pFillAttrs && pFillAttrs->getLength() > 0 ) )
998             {
999                 sal_uInt16 nElemPrefix = XML_NAMESPACE_STYLE;
1000                 // ODF 1.4 Extended: paragraph fills are not valid in style:graphic-properties
1001                 if( aPropTokens[i].eToken == XML_GRAPHIC_PROPERTIES &&
1002                     SvtSaveOptions().GetODFDefaultVersion() == SvtSaveOptions::ODFVER_014_EXT &&
1003                     maPropMapper->GetEntryCount() > 1 &&
1004                     maPropMapper->GetEntryAPIName( 0 ).equalsAscii( "ParaUserDefinedAttributes" ) &&
1005                     maPropMapper->GetEntryAPIName( 1 ).equalsAscii( "FillStyle" ) )
1006                 {
1007                     const OUString aExtNs( RTL_CONSTASCII_USTRINGPARAM(
1008                         "urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" ) );
1009                     rExport.EnsureNamespace( aExtNs, OUString( RTL_CONSTASCII_USTRINGPARAM( "loext" ) ) );
1010                     nElemPrefix = rExport.GetNamespaceMap().GetKeyByName( aExtNs );
1011                 }
1012                 SvXMLElementExport aElem( rExport, nElemPrefix,
1013                                   aPropTokens[i].eToken,
1014                                   (nFlags & XML_EXPORT_FLAG_IGN_WS) != 0,
1015                                   sal_False );
1016 
1017                 exportElementItems( rExport, rProperties, nFlags, aIndexArray );
1018 
1019                 if( pFillAttrs && pFillAttrs->getLength() > 0 )
1020                 {
1021                     for( sal_Int16 n = 0; n < pFillAttrs->getLength(); ++n )
1022                         rExport.AddAttribute( pFillAttrs->getNameByIndex( n ),
1023                                               pFillAttrs->getValueByIndex( n ) );
1024                     SvXMLElementExport aFillElem( rExport, XML_NAMESPACE_STYLE,
1025                                       XML_BACKGROUND_FILL,
1026                                       (nFlags & XML_EXPORT_FLAG_IGN_WS) != 0,
1027                                       sal_False );
1028                 }
1029             }
1030         }
1031     }
1032 }
1033 
1034 /** this method is called for every item that has the
1035     MID_FLAG_SPECIAL_ITEM_EXPORT flag set */
handleSpecialItem(SvXMLAttributeList & rAttrList,const XMLPropertyState & rProperty,const SvXMLUnitConverter & rUnitConverter,const SvXMLNamespaceMap & rNamespaceMap,const::std::vector<XMLPropertyState> * pProperties,sal_uInt32 nIdx) const1036 void SvXMLExportPropertyMapper::handleSpecialItem(
1037         SvXMLAttributeList& rAttrList,
1038         const XMLPropertyState& rProperty,
1039         const SvXMLUnitConverter& rUnitConverter,
1040         const SvXMLNamespaceMap& rNamespaceMap,
1041         const ::std::vector< XMLPropertyState > *pProperties,
1042         sal_uInt32 nIdx ) const
1043 {
1044     OSL_ENSURE( mxNextMapper.is(), "special item not handled in xml export" );
1045     if( mxNextMapper.is() )
1046         mxNextMapper->handleSpecialItem( rAttrList, rProperty, rUnitConverter,
1047                                         rNamespaceMap, pProperties, nIdx );
1048 }
1049 
1050 /** this method is called for every item that has the
1051     MID_FLAG_ELEMENT_EXPORT flag set */
handleElementItem(SvXMLExport & rExport,const XMLPropertyState & rProperty,sal_uInt16 nFlags,const::std::vector<XMLPropertyState> * pProperties,sal_uInt32 nIdx) const1052 void SvXMLExportPropertyMapper::handleElementItem(
1053         SvXMLExport& rExport,
1054         const XMLPropertyState& rProperty,
1055         sal_uInt16 nFlags,
1056         const ::std::vector< XMLPropertyState > *pProperties,
1057         sal_uInt32 nIdx ) const
1058 {
1059     OSL_ENSURE( mxNextMapper.is(), "element item not handled in xml export" );
1060     if( mxNextMapper.is() )
1061         mxNextMapper->handleElementItem( rExport, rProperty, nFlags,
1062                                          pProperties, nIdx );
1063 }
1064 
1065 ///////////////////////////////////////////////////////////////////////////////
1066 //
1067 // protected methods
1068 //
1069 
1070 /** fills the given attribute list with the items in the given set */
_exportXML(sal_uInt16 nPropType,sal_uInt16 & rPropTypeFlags,SvXMLAttributeList & rAttrList,const::std::vector<XMLPropertyState> & rProperties,const SvXMLUnitConverter & rUnitConverter,const SvXMLNamespaceMap & rNamespaceMap,sal_uInt16 nFlags,SvUShorts * pIndexArray,sal_Int32 nPropMapStartIdx,sal_Int32 nPropMapEndIdx) const1071 void SvXMLExportPropertyMapper::_exportXML(
1072         sal_uInt16 nPropType, sal_uInt16& rPropTypeFlags,
1073         SvXMLAttributeList& rAttrList,
1074         const ::std::vector< XMLPropertyState >& rProperties,
1075         const SvXMLUnitConverter& rUnitConverter,
1076         const SvXMLNamespaceMap& rNamespaceMap,
1077         sal_uInt16 nFlags,
1078         SvUShorts* pIndexArray,
1079         sal_Int32 nPropMapStartIdx, sal_Int32 nPropMapEndIdx ) const
1080 {
1081     const sal_uInt32 nCount = rProperties.size();
1082     sal_uInt32 nIndex = 0;
1083 
1084     if( -1 == nPropMapStartIdx )
1085         nPropMapStartIdx = 0;
1086     if( -1 == nPropMapEndIdx )
1087         nPropMapEndIdx = maPropMapper->GetEntryCount();
1088 
1089     while( nIndex < nCount )
1090     {
1091         sal_Int32 nPropMapIdx = rProperties[nIndex].mnIndex;
1092         if( nPropMapIdx >= nPropMapStartIdx &&
1093             nPropMapIdx < nPropMapEndIdx  )// valid entry?
1094         {
1095             sal_uInt32 nEFlags = maPropMapper->GetEntryFlags( nPropMapIdx );
1096             sal_uInt16 nEPType = GET_PROP_TYPE(nEFlags);
1097             OSL_ENSURE( nEPType >= (XML_TYPE_PROP_START>>XML_TYPE_PROP_SHIFT),
1098                         "no prop type sepcified" );
1099             rPropTypeFlags |= (1 << nEPType);
1100             if( nEPType == nPropType )
1101             {
1102                 // we have a valid map entry here, so lets use it...
1103                 if( ( nEFlags & MID_FLAG_ELEMENT_ITEM_EXPORT ) != 0 )
1104                 {
1105                     // element items do not add any properties,
1106                     // we export it later
1107                     if( pIndexArray )
1108                         pIndexArray->Insert( (sal_uInt16)nIndex, pIndexArray->Count() );
1109                 }
1110                 else
1111                 {
1112                     _exportXML( rAttrList, rProperties[nIndex], rUnitConverter,
1113                                 rNamespaceMap, nFlags, &rProperties, nIndex );
1114                 }
1115             }
1116         }
1117 
1118         nIndex++;
1119     }
1120 }
1121 
_exportXML(SvXMLAttributeList & rAttrList,const XMLPropertyState & rProperty,const SvXMLUnitConverter & rUnitConverter,const SvXMLNamespaceMap & rNamespaceMap,sal_uInt16,const::std::vector<XMLPropertyState> * pProperties,sal_uInt32 nIdx) const1122 void SvXMLExportPropertyMapper::_exportXML(
1123         SvXMLAttributeList& rAttrList,
1124         const XMLPropertyState& rProperty,
1125         const SvXMLUnitConverter& rUnitConverter,
1126         const SvXMLNamespaceMap& rNamespaceMap,
1127         sal_uInt16 /*nFlags*/,
1128         const ::std::vector< XMLPropertyState > *pProperties,
1129         sal_uInt32 nIdx ) const
1130 {
1131     OUString sCDATA( GetXMLToken(XML_CDATA) );
1132 
1133     if ( ( maPropMapper->GetEntryFlags( rProperty.mnIndex ) &
1134                 MID_FLAG_SPECIAL_ITEM_EXPORT ) != 0 )
1135     {
1136         uno::Reference< container::XNameContainer > xAttrContainer;
1137         if( (rProperty.maValue >>= xAttrContainer) && xAttrContainer.is() )
1138         {
1139             SvXMLNamespaceMap *pNewNamespaceMap = 0;
1140             const SvXMLNamespaceMap *pNamespaceMap = &rNamespaceMap;
1141 
1142             uno::Sequence< OUString > aAttribNames( xAttrContainer->getElementNames() );
1143             const OUString* pAttribName = aAttribNames.getConstArray();
1144 
1145             const sal_Int32 nCount = aAttribNames.getLength();
1146 
1147             OUStringBuffer sNameBuffer;
1148             xml::AttributeData aData;
1149             const sal_Bool bSkipODFNamespaces( SvtSaveOptions().GetODFDefaultVersion() >= SvtSaveOptions::ODFVER_012 );
1150             // strict ODF 1.2: foreign attributes are invalid too
1151             const sal_Bool bSkipForeign( SvtSaveOptions::IsODFStrict( SvtSaveOptions().GetODFDefaultVersion() ) );
1152             for( sal_Int32 i=0; i < nCount; i++, pAttribName++ )
1153             {
1154                 xAttrContainer->getByName( *pAttribName ) >>= aData;
1155                 if( bSkipForeign || ( bSkipODFNamespaces && SvXMLIsODFNamespace( aData.Namespace ) ) )
1156                     continue;
1157                 OUString sAttribName( *pAttribName );
1158 
1159                 // extract namespace prefix from attribute name if it exists
1160                 OUString sPrefix;
1161                 const sal_Int32 nColonPos =
1162                     pAttribName->indexOf( sal_Unicode(':') );
1163                 if( nColonPos != -1 )
1164                     sPrefix = pAttribName->copy( 0, nColonPos );
1165 
1166                 if( sPrefix.getLength() )
1167                 {
1168                     OUString sNamespace( aData.Namespace );
1169 
1170                     // if the prefix isn't defined yet or has another meaning,
1171                     // we have to redefine it now.
1172                     sal_uInt16 nKey = pNamespaceMap->GetKeyByPrefix( sPrefix );
1173                     if( USHRT_MAX == nKey || pNamespaceMap->GetNameByKey( nKey ) != sNamespace )
1174                     {
1175                         sal_Bool bAddNamespace = sal_False;
1176                         if( USHRT_MAX == nKey )
1177                         {
1178                             // The prefix is unused, so it is sufficient
1179                             // to add it to the namespace map.
1180                             bAddNamespace = sal_True;
1181                         }
1182                         else
1183                         {
1184                             // check if there is a prefix registered for the
1185                             // namespace URI
1186                             nKey = pNamespaceMap->GetKeyByName( sNamespace );
1187                             if( XML_NAMESPACE_UNKNOWN == nKey )
1188                             {
1189                                 // There is no prefix for the namespace, so
1190                                 // we have to generate one and have to add it.
1191                                 sal_Int32 n=0;
1192                                 OUString sOrigPrefix( sPrefix );
1193                                 do
1194                                 {
1195                                     sNameBuffer.append( sOrigPrefix );
1196                                     sNameBuffer.append( ++n );
1197                                     sPrefix = sNameBuffer.makeStringAndClear();
1198                                     nKey = pNamespaceMap->GetKeyByPrefix( sPrefix );
1199                                 }
1200                                 while( nKey != USHRT_MAX );
1201 
1202                                 bAddNamespace = sal_True;
1203                             }
1204                             else
1205                             {
1206                                 // If there is a prefix for the namespace,
1207                                 // we reuse that.
1208                                 sPrefix = pNamespaceMap->GetPrefixByKey( nKey );
1209                             }
1210                             // In any case, the attribute name has to be adapted.
1211                             sNameBuffer.append( sPrefix );
1212                             sNameBuffer.append( sal_Unicode(':') );
1213                             sNameBuffer.append( pAttribName->copy( nColonPos+1 ) );
1214                             sAttribName = sNameBuffer.makeStringAndClear();
1215                         }
1216 
1217                         if( bAddNamespace )
1218                         {
1219                             if( !pNewNamespaceMap )
1220                             {
1221                                 pNewNamespaceMap = new SvXMLNamespaceMap( rNamespaceMap );
1222                                 pNamespaceMap = pNewNamespaceMap;
1223                             }
1224                             pNewNamespaceMap->Add( sPrefix, sNamespace );
1225                             sNameBuffer.append( GetXMLToken(XML_XMLNS) );
1226                             sNameBuffer.append( sal_Unicode(':') );
1227                             sNameBuffer.append( sPrefix );
1228                             rAttrList.AddAttribute( sNameBuffer.makeStringAndClear(),
1229                                                     sNamespace );
1230                         }
1231                     }
1232                 }
1233                 OUString sOldValue( rAttrList.getValueByName( sAttribName ) );
1234                 OSL_ENSURE( sOldValue.getLength() == 0, "alien attribute exists already" );
1235                 OSL_ENSURE(aData.Type == GetXMLToken(XML_CDATA), "different type to our default type which should be written out");
1236                 if( !sOldValue.getLength() )
1237                     rAttrList.AddAttribute( sAttribName, aData.Value );
1238             }
1239 
1240             delete pNewNamespaceMap;
1241         }
1242         else
1243         {
1244             handleSpecialItem( rAttrList, rProperty, rUnitConverter,
1245                                rNamespaceMap, pProperties, nIdx );
1246         }
1247     }
1248     else if ( ( maPropMapper->GetEntryFlags( rProperty.mnIndex ) &
1249                     MID_FLAG_ELEMENT_ITEM_EXPORT ) == 0 )
1250     {
1251         OUString aValue;
1252         const OUString sName( rNamespaceMap.GetQNameByKey(
1253                     maPropMapper->GetEntryNameSpace( rProperty.mnIndex ),
1254                     maPropMapper->GetEntryXMLName( rProperty.mnIndex ) ) );
1255 
1256         sal_Bool bRemove = sal_False;
1257         if( ( maPropMapper->GetEntryFlags( rProperty.mnIndex ) &
1258                     MID_FLAG_MERGE_ATTRIBUTE ) != 0 )
1259         {
1260             aValue = rAttrList.getValueByName( sName );
1261             bRemove = sal_True; //aValue.getLength() != 0;
1262         }
1263 
1264         if( maPropMapper->exportXML( aValue, rProperty, rUnitConverter ) )
1265         {
1266             if( bRemove )
1267                 rAttrList.RemoveAttribute( sName );
1268             rAttrList.AddAttribute( sName, aValue );
1269         }
1270     }
1271 }
1272 
exportElementItems(SvXMLExport & rExport,const::std::vector<XMLPropertyState> & rProperties,sal_uInt16 nFlags,const SvUShorts & rIndexArray) const1273 void SvXMLExportPropertyMapper::exportElementItems(
1274         SvXMLExport& rExport,
1275         const ::std::vector< XMLPropertyState >& rProperties,
1276         sal_uInt16 nFlags,
1277         const SvUShorts& rIndexArray ) const
1278 {
1279     const sal_uInt16 nCount = rIndexArray.Count();
1280 
1281     sal_Bool bItemsExported = sal_False;
1282     OUString sWS( GetXMLToken(XML_WS) );
1283     for( sal_uInt16 nIndex = 0; nIndex < nCount; nIndex++ )
1284     {
1285         const sal_uInt16 nElement = rIndexArray.GetObject( nIndex );
1286 
1287         OSL_ENSURE( 0 != ( maPropMapper->GetEntryFlags(
1288                 rProperties[nElement].mnIndex ) & MID_FLAG_ELEMENT_ITEM_EXPORT),
1289                 "wrong mid flag!" );
1290 
1291         rExport.IgnorableWhitespace();
1292         handleElementItem( rExport, rProperties[nElement],
1293                            nFlags, &rProperties, nElement );
1294         bItemsExported = sal_True;
1295     }
1296 
1297     if( bItemsExported )
1298         rExport.IgnorableWhitespace();
1299 }
1300