xref: /trunk/main/xmloff/source/table/XMLTableExport.cxx (revision 00334f0500eab61b955155eb52d7fbfc6125e737)
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 "xmloff/dllapi.h"
27 
28 #include "sal/config.h"
29 #include <osl/diagnose.h>
30 
31 #include <rtl/ustring.hxx>
32 #include <rtl/ustrbuf.hxx>
33 
34 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
35 #include <com/sun/star/text/XText.hpp>
36 #include <com/sun/star/container/XNamed.hpp>
37 #include <com/sun/star/container/XEnumerationAccess.hpp>
38 #include <com/sun/star/table/XCellRange.hpp>
39 #include <com/sun/star/table/XColumnRowRange.hpp>
40 #include <com/sun/star/table/CellContentType.hpp>
41 #include <com/sun/star/table/XMergeableCell.hpp>
42 #include <com/sun/star/style/XStyle.hpp>
43 #include <com/sun/star/beans/XPropertySetInfo.hpp>
44 #include <com/sun/star/drawing/FillStyle.hpp>
45 
46 #include "xmloff/table/XMLTableExport.hxx"
47 #include "xmloff/xmlnmspe.hxx"
48 #include <xmloff/xmlprmap.hxx>
49 #include <xmloff/xmlexppr.hxx>
50 #include <xmloff/xmlexp.hxx>
51 #include "table.hxx"
52 
53 using ::rtl::OUString;
54 using namespace ::xmloff::token;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::table;
58 using namespace ::com::sun::star::beans;
59 using namespace ::com::sun::star::container;
60 using namespace ::com::sun::star::text;
61 using namespace ::com::sun::star::style;
62 using namespace ::xmloff::token;
63 
64 // --------------------------------------------------------------------
65 
66 #define _MAP(name,prefix,token,type,context)  { name, sizeof(name)-1, prefix, token, type, context, SvtSaveOptions::ODFVER_010 }
67 #define CMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_COLUMN,context)
68 #define RMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_ROW,context)
69 #define CELLMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_CELL,context)
70 #define MAP_END { 0L, 0, 0, XML_EMPTY, 0, 0, SvtSaveOptions::ODFVER_010 }
71 
72 // --------------------------------------------------------------------
73 
getColumnPropertiesMap()74 const XMLPropertyMapEntry* getColumnPropertiesMap()
75 {
76     static const XMLPropertyMapEntry aXMLColumnProperties[] =
77     {
78         CMAP( "Width",          XML_NAMESPACE_STYLE,    XML_COLUMN_WIDTH,               XML_TYPE_MEASURE,   0 ),
79         CMAP( "OptimalWidth",   XML_NAMESPACE_STYLE,    XML_USE_OPTIMAL_COLUMN_WIDTH,   XML_TYPE_BOOL, 0 ),
80         MAP_END
81     };
82 
83     return &aXMLColumnProperties[0];
84 }
85 
86 // --------------------------------------------------------------------
87 
getRowPropertiesMap()88 const XMLPropertyMapEntry* getRowPropertiesMap()
89 {
90     static const XMLPropertyMapEntry aXMLRowProperties[] =
91     {
92         RMAP( "Height",         XML_NAMESPACE_STYLE, XML_ROW_HEIGHT,                    XML_TYPE_MEASURE,   0 ),
93         RMAP( "OptimalHeight",  XML_NAMESPACE_STYLE, XML_MIN_ROW_HEIGHT,                XML_TYPE_MEASURE,   0 ),
94         RMAP( "OptimalWidth",   XML_NAMESPACE_STYLE, XML_USE_OPTIMAL_ROW_HEIGHT,        XML_TYPE_BOOL, 0 ),
95         MAP_END
96     };
97 
98     return &aXMLRowProperties[0];
99 }
100 
101 // --------------------------------------------------------------------
102 
getCellPropertiesMap()103 const XMLPropertyMapEntry* getCellPropertiesMap()
104 {
105     static const XMLPropertyMapEntry aXMLCellProperties[] =
106     {
107         CELLMAP( "FillColor",       XML_NAMESPACE_FO,   XML_BACKGROUND_COLOR,   XML_TYPE_COLOR, 0 ),
108         // ODF 1.4 modes only (see the context filter): the text distances and the wrap option of a cell
109         CELLMAP( "TextUpperDistance",   XML_NAMESPACE_FO,   XML_PADDING_TOP,        XML_TYPE_MEASURE,   0 ),
110         CELLMAP( "TextLowerDistance",   XML_NAMESPACE_FO,   XML_PADDING_BOTTOM,     XML_TYPE_MEASURE,   0 ),
111         CELLMAP( "TextLeftDistance",    XML_NAMESPACE_FO,   XML_PADDING_LEFT,       XML_TYPE_MEASURE,   0 ),
112         CELLMAP( "TextRightDistance",   XML_NAMESPACE_FO,   XML_PADDING_RIGHT,      XML_TYPE_MEASURE,   0 ),
113         CELLMAP( "TextWordWrap",        XML_NAMESPACE_FO,   XML_WRAP_OPTION,        XML_TYPE_WRAP_OPTION,   0 ),
114         MAP_END
115     };
116 
117     return &aXMLCellProperties[0];
118 }
119 
120 // --------------------------------------------------------------------
121 
122 // ODF 1.2 strict: a table cell style has no style:graphic-properties; only a solid fill survives, as fo:background-color.
123 // ODF 1.4 (both modes): the fill goes to a style:background-fill child, the text distances and the wrap option to
124 // style:table-cell-properties; other graphic properties are not allowed in a cell style.
125 class XMLTableCellExportPropertyMapper : public SvXMLExportPropertyMapper
126 {
127 public:
XMLTableCellExportPropertyMapper(const UniReference<XMLPropertySetMapper> & rMapper)128     XMLTableCellExportPropertyMapper( const UniReference< XMLPropertySetMapper >& rMapper ) : SvXMLExportPropertyMapper( rMapper ) {}
129 
130 protected:
131     virtual void ContextFilter( std::vector< XMLPropertyState >& rProperties, Reference< XPropertySet > rPropSet ) const;
132 };
133 
lcl_IsCellTextBox(const OUString & rApiName)134 static bool lcl_IsCellTextBox( const OUString& rApiName )
135 {
136     return rApiName.equalsAscii( "TextLeftDistance" ) || rApiName.equalsAscii( "TextRightDistance" ) ||
137         rApiName.equalsAscii( "TextUpperDistance" ) || rApiName.equalsAscii( "TextLowerDistance" ) ||
138         rApiName.equalsAscii( "TextWordWrap" );
139 }
140 
ContextFilter(std::vector<XMLPropertyState> & rProperties,Reference<XPropertySet> rPropSet) const141 void XMLTableCellExportPropertyMapper::ContextFilter( std::vector< XMLPropertyState >& rProperties, Reference< XPropertySet > rPropSet ) const
142 {
143     const SvtSaveOptions::ODFDefaultVersion eVersion = SvtSaveOptions().GetODFDefaultVersion();
144     const bool bStrict = SvtSaveOptions::IsODFStrict( eVersion );
145     // ODF 1.4 keeps the fill, in a style:background-fill child
146     const bool b014 = eVersion == SvtSaveOptions::ODFVER_014;
147     const bool b014Any = b014 || eVersion == SvtSaveOptions::ODFVER_014_EXT;
148     bool bSolid = false;
149     if( bStrict ) try
150     {
151         ::com::sun::star::drawing::FillStyle eFillStyle = ::com::sun::star::drawing::FillStyle_NONE;
152         bSolid = ( rPropSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("FillStyle") ) ) >>= eFillStyle ) && eFillStyle == ::com::sun::star::drawing::FillStyle_SOLID;
153     }
154     catch( Exception& )
155     {
156     }
157 
158     for( std::vector< XMLPropertyState >::iterator aIter( rProperties.begin() ); aIter != rProperties.end(); ++aIter )
159     {
160         if( aIter->mnIndex == -1 )
161             continue;
162 
163         const sal_uInt32 nPropType = maPropMapper->GetEntryFlags( aIter->mnIndex ) & XML_TYPE_PROP_MASK;
164         const OUString& rApiName = maPropMapper->GetEntryAPIName( aIter->mnIndex );
165         if( nPropType == XML_TYPE_PROP_TABLE_CELL )
166         {
167             if( lcl_IsCellTextBox( rApiName ) )
168             {
169                 if( !b014Any )
170                     aIter->mnIndex = -1;
171             }
172             else if( !bSolid )
173                 aIter->mnIndex = -1;
174         }
175         else if( nPropType == XML_TYPE_PROP_GRAPHIC )
176         {
177             const bool bFill = rApiName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "Fill" ) );
178             if( b014Any ? !bFill : ( bStrict && !( b014 && bFill ) ) )
179                 aIter->mnIndex = -1;
180         }
181     }
182 
183     SvXMLExportPropertyMapper::ContextFilter( rProperties, rPropSet );
184 }
185 
186 // --------------------------------------------------------------------
187 
188 class StringStatisticHelper : public std::map< OUString, sal_Int32 >
189 {
190 public:
191     void add( const OUString& rStyleName );
clear()192     void clear() { std::map< OUString, sal_Int32 >::clear(); }
193 
194     sal_Int32 getModeString( /* out */ OUString& rModeString );
195 };
196 
197 // --------------------------------------------------------------------
198 
add(const OUString & rStyleName)199 void StringStatisticHelper::add( const OUString& rStyleName )
200 {
201     std::map< OUString, sal_Int32 >::iterator iter( find( rStyleName ) );
202     if( iter == end() )
203     {
204         (*this)[rStyleName] = 1;
205     }
206     else
207     {
208         (*iter).second += 1;
209     }
210 }
211 
212 // --------------------------------------------------------------------
213 
getModeString(OUString & rStyleName)214 sal_Int32 StringStatisticHelper::getModeString( OUString& rStyleName )
215 {
216     sal_Int32 nMax = 0;
217     for( std::map< OUString, sal_Int32 >::iterator iter( begin() ); iter != end(); iter++ )
218     {
219         if( (*iter).second > nMax )
220         {
221             rStyleName = (*iter).first;
222             nMax = (*iter).second;
223         }
224     }
225 
226     return nMax;
227 }
228 
229 // --------------------------------------------------------------------
230 // class XMLTableExport
231 // --------------------------------------------------------------------
232 
XMLTableExport(SvXMLExport & rExp,const rtl::Reference<SvXMLExportPropertyMapper> & xExportPropertyMapper,const rtl::Reference<XMLPropertyHandlerFactory> & xFactoryRef)233 XMLTableExport::XMLTableExport(SvXMLExport& rExp, const rtl::Reference< SvXMLExportPropertyMapper  >& xExportPropertyMapper, const rtl::Reference< XMLPropertyHandlerFactory >& xFactoryRef )
234 : mrExport( rExp )
235 , mbExportTables( false )
236 {
237     Reference< XMultiServiceFactory > xFac( rExp.GetModel(), UNO_QUERY );
238     if( xFac.is() ) try
239     {
240         Sequence< OUString > sSNS( xFac->getAvailableServiceNames() );
241         sal_Int32 n = sSNS.getLength();
242         const OUString* pSNS( sSNS.getConstArray() );
243         while( --n > 0 )
244         {
245             if( (*pSNS++).equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.drawing.TableShape") ) )
246             {
247                 mbExportTables = true;
248                 break;
249             }
250         }
251     }
252     catch( Exception& e )
253     {
254         (void)e;
255     }
256 
257     mxCellExportPropertySetMapper = xExportPropertyMapper;
258     mxCellExportPropertySetMapper->ChainExportMapper(XMLTextParagraphExport::CreateParaExtPropMapper(rExp));
259     mxCellExportPropertySetMapper->ChainExportMapper(new XMLTableCellExportPropertyMapper( new XMLPropertySetMapper( getCellPropertiesMap(), xFactoryRef.get() ) ));
260 
261     mxRowExportPropertySetMapper = new SvXMLExportPropertyMapper( new XMLPropertySetMapper( getRowPropertiesMap(), xFactoryRef.get() ) );
262     mxColumnExportPropertySetMapper = new SvXMLExportPropertyMapper( new XMLPropertySetMapper( getColumnPropertiesMap(), xFactoryRef.get() ) );
263 
264     mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_COLUMN,
265         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_NAME)),
266         mxColumnExportPropertySetMapper.get(),
267         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_PREFIX)));
268     mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_ROW,
269         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_ROW_STYLES_NAME)),
270         mxRowExportPropertySetMapper.get(),
271         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_ROW_STYLES_PREFIX)));
272 //  mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_TABLE
273 //      OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME)),
274 //      xTableStylesExportPropertySetMapper,
275 //      OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_TABLE_STYLES_PREFIX)));
276     mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_CELL,
277         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME)),
278         mxCellExportPropertySetMapper.get(),
279         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_CELL_STYLES_PREFIX)));
280 }
281 
282 // --------------------------------------------------------------------
283 
~XMLTableExport()284 XMLTableExport::~XMLTableExport ()
285 {
286 }
287 
288 // --------------------------------------------------------------------
289 
290 // a 0 width or height is not a positiveLength
drop_zero_measure(std::vector<XMLPropertyState> & rStates,const UniReference<XMLPropertySetMapper> & xMapper,const sal_Char * pApiName)291 static void drop_zero_measure( std::vector< XMLPropertyState >& rStates, const UniReference< XMLPropertySetMapper >& xMapper, const sal_Char* pApiName )
292 {
293     for( std::vector< XMLPropertyState >::iterator aIter( rStates.begin() ); aIter != rStates.end(); ++aIter )
294     {
295         sal_Int32 nValue = 0;
296         if( aIter->mnIndex != -1 && ( aIter->maValue >>= nValue ) && nValue == 0 &&
297             xMapper->GetEntryAPIName( aIter->mnIndex ).equalsAscii( pApiName ) )
298             aIter->mnIndex = -1;
299     }
300 }
301 
has_states(const std::vector<XMLPropertyState> & xPropStates)302 static bool has_states( const std::vector< XMLPropertyState >& xPropStates )
303 {
304     if( !xPropStates.empty() )
305     {
306         std::vector< XMLPropertyState >::const_iterator aIter( xPropStates.begin() );
307         std::vector< XMLPropertyState >::const_iterator aEnd( xPropStates.end() );
308         while( aIter != aEnd )
309         {
310             if( aIter->mnIndex != -1 )
311                 return true;
312             aIter++;
313         }
314     }
315     return false;
316 }
317 
318 // --------------------------------------------------------------------
319 
collectTableAutoStyles(const Reference<XColumnRowRange> & xColumnRowRange)320  void XMLTableExport::collectTableAutoStyles(const Reference < XColumnRowRange >& xColumnRowRange)
321  {
322      if( !mbExportTables )
323          return;
324 
325     boost::shared_ptr< XMLTableInfo > pTableInfo( new XMLTableInfo() );
326     maTableInfoMap[xColumnRowRange] = pTableInfo;
327 
328     try
329     {
330         Reference< XIndexAccess > xIndexAccessCols( xColumnRowRange->getColumns(), UNO_QUERY_THROW );
331         const sal_Int32 nColumnCount = xIndexAccessCols->getCount();
332         for( sal_Int32 nColumn = 0; nColumn < nColumnCount; ++nColumn ) try
333         {
334             Reference< XPropertySet > xPropSet( xIndexAccessCols->getByIndex(nColumn) , UNO_QUERY_THROW );
335             std::vector< XMLPropertyState > xPropStates( mxColumnExportPropertySetMapper->Filter( xPropSet ) );
336             drop_zero_measure( xPropStates, mxColumnExportPropertySetMapper->getPropertySetMapper(), "Width" );
337 
338             if( has_states( xPropStates ) )
339             {
340                 const OUString sStyleName( mrExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_TABLE_COLUMN, xPropStates) );
341                 Reference< XInterface > xKey( xPropSet, UNO_QUERY );
342                 pTableInfo->maColumnStyleMap[xKey] = sStyleName;
343             }
344         }
345         catch( Exception& )
346         {
347             DBG_ERROR("xmloff::XMLTableExport::collectTableAutoStyles(), exception during column style collection!");
348         }
349 
350         Reference< XIndexAccess > xIndexAccessRows( xColumnRowRange->getRows(), UNO_QUERY_THROW );
351         const sal_Int32 nRowCount = xIndexAccessRows->getCount();
352         pTableInfo->maDefaultRowCellStyles.resize(nRowCount);
353 
354         StringStatisticHelper aStringStatistic;
355 
356         for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow ) try
357         {
358             Reference< XPropertySet > xPropSet( xIndexAccessRows->getByIndex(nRow) , UNO_QUERY_THROW );
359             std::vector< XMLPropertyState > xRowPropStates( mxRowExportPropertySetMapper->Filter( xPropSet ) );
360             drop_zero_measure( xRowPropStates, mxRowExportPropertySetMapper->getPropertySetMapper(), "Height" );
361 
362             if( has_states( xRowPropStates ) )
363             {
364                 const OUString sStyleName( mrExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_TABLE_ROW, xRowPropStates) );
365                 Reference< XInterface > xKey( xPropSet, UNO_QUERY );
366                 pTableInfo->maRowStyleMap[xKey] = sStyleName;
367             }
368 
369             // get the current row
370             Reference< XCellRange > xCellRange( xPropSet, UNO_QUERY_THROW );
371             for ( sal_Int32 nColumn = 0; nColumn < nColumnCount; ++nColumn )
372             {
373                 // get current cell, remarks row index is 0, because we get the range for each row separate
374                 Reference< XPropertySet > xCellSet( xCellRange->getCellByPosition(nColumn, 0), UNO_QUERY_THROW );
375 
376                 // get style
377                 OUString sParentStyleName;
378                 Reference< XPropertySetInfo > xPropertySetInfo( xCellSet->getPropertySetInfo() );
379                 if( xPropertySetInfo.is() && xPropertySetInfo->hasPropertyByName( OUString(RTL_CONSTASCII_USTRINGPARAM("Style"))) )
380                 {
381                     Reference< XStyle > xStyle( xCellSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("Style"))), UNO_QUERY );
382                     if( xStyle.is() )
383                         sParentStyleName = xStyle->getName();
384                 }
385 
386                 // create auto style, if needed
387                 OUString sStyleName;
388                 std::vector< XMLPropertyState > xCellPropStates( mxCellExportPropertySetMapper->Filter( xCellSet ) );
389                 if( has_states( xCellPropStates ) )
390                     sStyleName = mrExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_TABLE_CELL, xCellPropStates);
391                 else
392                     sStyleName = sParentStyleName;
393 
394                 if( sStyleName.getLength() )
395                 {
396                     Reference< XInterface > xKey( xCellSet, UNO_QUERY );
397                     pTableInfo->maCellStyleMap[xKey] = sStyleName;
398                 }
399 
400                 // create auto style for text
401                 Reference< XText > xText(xCellSet, UNO_QUERY);
402                 if(xText.is() && xText->getString().getLength())
403                     GetExport().GetTextParagraphExport()->collectTextAutoStyles( xText );
404 
405                 aStringStatistic.add( sStyleName );
406             }
407 
408             OUString sDefaultCellStyle;
409             if( aStringStatistic.getModeString( sDefaultCellStyle ) > 1 )
410                 pTableInfo->maDefaultRowCellStyles[nRow] = sDefaultCellStyle;
411 
412             aStringStatistic.clear();
413         }
414         catch( Exception& )
415         {
416             DBG_ERROR("xmloff::XMLTableExport::collectTableAutoStyles(), exception during column style collection!");
417         }
418     }
419     catch( Exception& )
420     {
421         DBG_ERROR("xmloff::XMLTableExport::collectTableAutoStyles(), exception caught!");
422     }
423  }
424 
425  // --------------------------------------------------------------------
426 
exportTable(const Reference<XColumnRowRange> & xColumnRowRange)427  void XMLTableExport::exportTable( const Reference < XColumnRowRange >& xColumnRowRange )
428  {
429      if( !mbExportTables )
430          return;
431 
432     try
433     {
434         boost::shared_ptr< XMLTableInfo > pTableInfo( maTableInfoMap[xColumnRowRange] );
435 
436         // get row and column count
437         Reference< XIndexAccess > xIndexAccess( xColumnRowRange->getRows(), UNO_QUERY_THROW );
438         Reference< XIndexAccess > xIndexAccessCols( xColumnRowRange->getColumns(), UNO_QUERY_THROW );
439 
440         const sal_Int32 rowCount = xIndexAccess->getCount();
441         const sal_Int32 columnCount = xIndexAccessCols->getCount();
442 
443         SvXMLElementExport tableElement( mrExport, XML_NAMESPACE_TABLE, XML_TABLE, sal_True, sal_True );
444 
445         // export table columns
446         ExportTableColumns( xIndexAccessCols, pTableInfo );
447 
448         // start iterating rows and columns
449         for ( sal_Int32 rowIndex = 0; rowIndex < rowCount; rowIndex++ )
450         {
451             // get the current row
452             Reference< XCellRange > xCellRange( xIndexAccess->getByIndex(rowIndex), UNO_QUERY_THROW );
453 
454             OUString sDefaultCellStyle;
455 
456             // table:style-name
457             if( pTableInfo.get() )
458             {
459                 Reference< XInterface > xKey( xCellRange, UNO_QUERY );
460                 const OUString sStyleName( pTableInfo->maRowStyleMap[xKey] );
461                 if( sStyleName.getLength() )
462                     mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_STYLE_NAME, sStyleName );
463 
464                 sDefaultCellStyle = pTableInfo->maDefaultRowCellStyles[rowIndex];
465                 if( sDefaultCellStyle.getLength() )
466                     mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DEFAULT_CELL_STYLE_NAME, sDefaultCellStyle );
467             }
468 
469             // write row element
470             SvXMLElementExport tableRowElement( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_ROW, sal_True, sal_True );
471 
472             for ( sal_Int32 columnIndex = 0; columnIndex < columnCount; columnIndex++ )
473             {
474                 // get current cell, remarks row index is 0, because we get the range for each row separate
475                 Reference< XCell > xCell( xCellRange->getCellByPosition(columnIndex, 0), UNO_QUERY_THROW );
476 
477                 // use XMergeableCell interface from offapi
478                 Reference< XMergeableCell > xMergeableCell( xCell, UNO_QUERY_THROW );
479 
480                 // export cell
481                 ExportCell( xCell, pTableInfo, sDefaultCellStyle );
482             }
483         }
484     }
485     catch( Exception )
486     {
487         DBG_ERROR( "XMLTableExport::exportTable(), exception caught!" );
488     }
489  }
490 
491 // --------------------------------------------------------------------
492 // Export the table columns
493 // --------------------------------------------------------------------
494 
ExportTableColumns(const Reference<XIndexAccess> & xtableColumnsIndexAccess,const boost::shared_ptr<XMLTableInfo> & pTableInfo)495  void XMLTableExport::ExportTableColumns( const Reference < XIndexAccess >& xtableColumnsIndexAccess, const boost::shared_ptr< XMLTableInfo >& pTableInfo )
496  {
497     const sal_Int32 nColumnCount = xtableColumnsIndexAccess->getCount();
498     for( sal_Int32 nColumn = 0; nColumn < nColumnCount; ++nColumn )
499     {
500         Reference< XPropertySet > xColumnProperties( xtableColumnsIndexAccess->getByIndex(nColumn) , UNO_QUERY );
501         if ( xColumnProperties.is() )
502         {
503             // table:style-name
504             if( pTableInfo.get() )
505             {
506                 Reference< XInterface > xKey( xColumnProperties, UNO_QUERY );
507                 const OUString sStyleName( pTableInfo->maColumnStyleMap[xKey] );
508                 if( sStyleName.getLength() )
509                     mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_STYLE_NAME, sStyleName );
510             }
511 
512             // TODO: All columns first have to be checked if some ones
513             // have identical properties. If yes, attr table:number-columns-repeated
514             // has to be written.
515             SvXMLElementExport tableColumnElement( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_COLUMN, sal_True, sal_True );
516         }
517     }
518  }
519 
520 // --------------------------------------------------------------------
521 // ODF export for a table cell.
522 // --------------------------------------------------------------------
523 
ExportCell(const Reference<XCell> & xCell,const boost::shared_ptr<XMLTableInfo> & pTableInfo,const OUString & rDefaultCellStyle)524  void XMLTableExport::ExportCell( const Reference < XCell >& xCell, const boost::shared_ptr< XMLTableInfo >& pTableInfo, const OUString& rDefaultCellStyle )
525  {
526     bool bIsMerged = false;
527     sal_Int32 nRowSpan = 0;
528     sal_Int32 nColSpan = 0;
529 
530     try
531     {
532         if( pTableInfo.get() )
533         {
534             // table:style-name
535             Reference< XInterface > xKey( xCell, UNO_QUERY );
536             const OUString sStyleName( pTableInfo->maCellStyleMap[xKey] );
537             if( sStyleName.getLength() && (sStyleName != rDefaultCellStyle) )
538                 mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_STYLE_NAME, sStyleName );
539         }
540 
541         Reference< XMergeableCell > xMerge( xCell, UNO_QUERY );
542         if( xMerge.is() )
543         {
544             bIsMerged = xMerge->isMerged();
545             nRowSpan = xMerge->getRowSpan();
546             nColSpan = xMerge->getColumnSpan();
547         }
548         DBG_ASSERT( (nRowSpan >= 1) && (nColSpan >= 1), "xmloff::XMLTableExport::ExportCell(), illegal row or col span < 1?" );
549     }
550     catch ( Exception )
551     {
552         DBG_ERROR( "exception while exporting a table cell" );
553     }
554 
555     // table:number-columns-repeated
556     // todo
557 
558     // table:number-columns-spanned
559     if( nColSpan > 1 )
560         mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NUMBER_COLUMNS_SPANNED, OUString::valueOf( nColSpan ) );
561 
562     // table:number-rows-spanned
563     if( nRowSpan > 1 )
564         mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NUMBER_ROWS_SPANNED, OUString::valueOf( nRowSpan ) );
565 
566     // <table:table-cell> or <table:covered-table-cell>
567     SvXMLElementExport tableCellElement( mrExport, XML_NAMESPACE_TABLE, bIsMerged ? XML_COVERED_TABLE_CELL : XML_TABLE_CELL, sal_True, sal_True );
568 
569     // export cells text content
570     ImpExportText( xCell );
571  }
572 
573 // --------------------------------------------------------------------
574 // ODF export of the text contents of a table cell.
575 // Remarks: Up to now we only export text contents!
576 // TODO: Check against nested tables ....
577 // --------------------------------------------------------------------
578 
ImpExportText(const Reference<XCell> & xCell)579  void XMLTableExport::ImpExportText( const Reference< XCell >& xCell )
580  {
581     Reference< XText > xText( xCell, UNO_QUERY );
582     if( xText.is() && xText->getString().getLength())
583         mrExport.GetTextParagraphExport()->exportText( xText );
584  }
585 
586 // --------------------------------------------------------------------
587 
exportTableStyles()588 void XMLTableExport::exportTableStyles()
589 {
590      if( !mbExportTables )
591          return;
592 
593     XMLStyleExport aStEx(mrExport, OUString(), mrExport.GetAutoStylePool().get());
594 
595     // write graphic family styles
596     aStEx.exportStyleFamily("cell", OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME)), mxCellExportPropertySetMapper.get(), sal_True, XML_STYLE_FAMILY_TABLE_CELL);
597 
598     exportTableTemplates();
599 }
600 
601 // --------------------------------------------------------------------
602 // Export the collected automatic styles
603 // --------------------------------------------------------------------
604 
exportAutoStyles()605 void XMLTableExport::exportAutoStyles()
606 {
607      if( !mbExportTables )
608          return;
609 
610     mrExport.GetAutoStylePool()->exportXML( XML_STYLE_FAMILY_TABLE_COLUMN, mrExport.GetDocHandler(), mrExport.GetMM100UnitConverter(), mrExport.GetNamespaceMap() );
611     mrExport.GetAutoStylePool()->exportXML( XML_STYLE_FAMILY_TABLE_ROW, mrExport.GetDocHandler(), mrExport.GetMM100UnitConverter(), mrExport.GetNamespaceMap() );
612     mrExport.GetAutoStylePool()->exportXML( XML_STYLE_FAMILY_TABLE_CELL, mrExport.GetDocHandler(), mrExport.GetMM100UnitConverter(), mrExport.GetNamespaceMap() );
613 }
614 
615 // --------------------------------------------------------------------
616 
getTableStyleMap()617 const TableStyleElement* getTableStyleMap()
618 {
619     static struct TableStyleElement gTableStyleElements[] =
620     {
621         { XML_FIRST_ROW, OUString( RTL_CONSTASCII_USTRINGPARAM( "first-row" ) ) },
622         { XML_LAST_ROW, OUString( RTL_CONSTASCII_USTRINGPARAM( "last-row" ) ) },
623         { XML_FIRST_COLUMN, OUString( RTL_CONSTASCII_USTRINGPARAM( "first-column" ) ) },
624         { XML_LAST_COLUMN, OUString( RTL_CONSTASCII_USTRINGPARAM( "last-column" ) ) },
625         { XML_EVEN_ROWS, OUString( RTL_CONSTASCII_USTRINGPARAM( "even-rows" ) ) },
626         { XML_ODD_ROWS, OUString( RTL_CONSTASCII_USTRINGPARAM( "odd-rows" ) ) },
627         { XML_EVEN_COLUMNS, OUString( RTL_CONSTASCII_USTRINGPARAM( "even-columns" ) ) },
628         { XML_ODD_COLUMNS, OUString( RTL_CONSTASCII_USTRINGPARAM( "odd-columns" ) ) },
629         { XML_BODY, OUString( RTL_CONSTASCII_USTRINGPARAM( "body" ) ) },
630         { XML_TOKEN_END, OUString() }
631     };
632 
633     return &gTableStyleElements[0];
634 }
635 
636 // --------------------------------------------------------------------
637 
exportTableTemplates()638 void XMLTableExport::exportTableTemplates()
639 {
640      if( !mbExportTables )
641          return;
642 
643     // ODF 1.2 strict: table:name, table:style-name and the part order of the schema (body before even-rows)
644     const bool bStrict = SvtSaveOptions::IsODFStrict( mrExport.getDefaultVersion() );
645     const TableStyleElement* pMap = getTableStyleMap();
646     static const sal_Int32 aStrictPartOrder[] = { 0, 1, 2, 3, 8, 4, 5, 6, 7 };
647 
648     try
649     {
650         Reference< XStyleFamiliesSupplier > xFamiliesSupp( mrExport.GetModel(), UNO_QUERY_THROW );
651         Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
652         const OUString sFamilyName( RTL_CONSTASCII_USTRINGPARAM("table" ) );
653         Reference< XIndexAccess > xTableFamily( xFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
654 
655         for( sal_Int32 nIndex = 0; nIndex < xTableFamily->getCount(); nIndex++ ) try
656         {
657             Reference< XStyle > xTableStyle( xTableFamily->getByIndex( nIndex ), UNO_QUERY_THROW );
658             if( !xTableStyle->isInUse() )
659                 continue;
660 
661             Reference< XNameAccess > xStyleNames( xTableStyle, UNO_QUERY_THROW );
662 
663             if( bStrict )
664             {
665                 mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NAME, GetExport().EncodeStyleName( xTableStyle->getName() ) );
666                 mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "first-row-start-column", "row" );
667                 mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "first-row-end-column", "row" );
668                 mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "last-row-start-column", "row" );
669                 mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "last-row-end-column", "row" );
670             }
671             else
672                 mrExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xTableStyle->getName() ) );
673             SvXMLElementExport tableTemplate( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_TEMPLATE, sal_True, sal_True );
674 
675             const TableStyleElement* pElements = getTableStyleMap();
676             sal_Int32 nPart = 0;
677             while( pElements->meElement != XML_TOKEN_END )
678             {
679                 try
680                 {
681                     const TableStyleElement* pPart = bStrict ? pMap + aStrictPartOrder[nPart] : pElements;
682                     Reference< XStyle > xStyle( xStyleNames->getByName( pPart->msStyleName ), UNO_QUERY );
683                     if( xStyle.is() )
684                     {
685                         mrExport.AddAttribute(bStrict ? XML_NAMESPACE_TABLE : XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xStyle->getName() ) );
686                         SvXMLElementExport element( mrExport, XML_NAMESPACE_TABLE, pPart->meElement, sal_True, sal_True );
687                     }
688                 }
689                 catch( Exception& )
690                 {
691                     DBG_ERROR("xmloff::XMLTableExport::exportTableTemplates(), exception caught!");
692                 }
693 
694                 pElements++;
695                 nPart++;
696             }
697         }
698         catch( Exception& )
699         {
700             DBG_ERROR("xmloff::XMLTableExport::exportTableDesigns(), exception caught while exporting a table design!");
701         }
702     }
703     catch( Exception& )
704     {
705         DBG_ERROR("xmloff::XMLTableExport::exportTableDesigns(), exception caught!");
706     }
707 }
708 
709 // --------------------------------------------------------------------
710