xref: /trunk/main/xmloff/source/table/XMLTableExport.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
163bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
363bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
463bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
563bba73cSAndrew Rist  * distributed with this work for additional information
663bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
763bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
863bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
963bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1163bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1363bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1463bba73cSAndrew Rist  * software distributed under the License is distributed on an
1563bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1663bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
1763bba73cSAndrew Rist  * specific language governing permissions and limitations
1863bba73cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2063bba73cSAndrew Rist  *************************************************************/
2163bba73cSAndrew Rist 
2263bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include "xmloff/dllapi.h"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "sal/config.h"
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/ustring.hxx>
32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
36cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
37cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
38cdf0e10cSrcweir #include <com/sun/star/table/XCellRange.hpp>
39cdf0e10cSrcweir #include <com/sun/star/table/XColumnRowRange.hpp>
40cdf0e10cSrcweir #include <com/sun/star/table/CellContentType.hpp>
41cdf0e10cSrcweir #include <com/sun/star/table/XMergeableCell.hpp>
42cdf0e10cSrcweir #include <com/sun/star/style/XStyle.hpp>
43cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "xmloff/table/XMLTableExport.hxx"
46cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
47cdf0e10cSrcweir #include <xmloff/xmlprmap.hxx>
48cdf0e10cSrcweir #include <xmloff/xmlexppr.hxx>
49cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
50cdf0e10cSrcweir #include "table.hxx"
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using ::rtl::OUString;
53cdf0e10cSrcweir using namespace ::xmloff::token;
54cdf0e10cSrcweir using namespace ::com::sun::star::uno;
55cdf0e10cSrcweir using namespace ::com::sun::star::lang;
56cdf0e10cSrcweir using namespace ::com::sun::star::table;
57cdf0e10cSrcweir using namespace ::com::sun::star::beans;
58cdf0e10cSrcweir using namespace ::com::sun::star::container;
59cdf0e10cSrcweir using namespace ::com::sun::star::text;
60cdf0e10cSrcweir using namespace ::com::sun::star::style;
61cdf0e10cSrcweir using namespace ::xmloff::token;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir // --------------------------------------------------------------------
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #define _MAP(name,prefix,token,type,context)  { name, sizeof(name)-1, prefix, token, type, context, SvtSaveOptions::ODFVER_010 }
66cdf0e10cSrcweir #define CMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_COLUMN,context)
67cdf0e10cSrcweir #define RMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_ROW,context)
68cdf0e10cSrcweir #define MAP_END { 0L, 0, 0, XML_EMPTY, 0, 0, SvtSaveOptions::ODFVER_010 }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir // --------------------------------------------------------------------
71cdf0e10cSrcweir 
getColumnPropertiesMap()72cdf0e10cSrcweir const XMLPropertyMapEntry* getColumnPropertiesMap()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     static const XMLPropertyMapEntry aXMLColumnProperties[] =
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         CMAP( "Width",          XML_NAMESPACE_STYLE,    XML_COLUMN_WIDTH,               XML_TYPE_MEASURE,   0 ),
77cdf0e10cSrcweir         CMAP( "OptimalWidth",   XML_NAMESPACE_STYLE,    XML_USE_OPTIMAL_COLUMN_WIDTH,   XML_TYPE_BOOL, 0 ),
78cdf0e10cSrcweir         MAP_END
79cdf0e10cSrcweir     };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     return &aXMLColumnProperties[0];
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // --------------------------------------------------------------------
85cdf0e10cSrcweir 
getRowPropertiesMap()86cdf0e10cSrcweir const XMLPropertyMapEntry* getRowPropertiesMap()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     static const XMLPropertyMapEntry aXMLRowProperties[] =
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         RMAP( "Height",         XML_NAMESPACE_STYLE, XML_ROW_HEIGHT,                    XML_TYPE_MEASURE,   0 ),
91cdf0e10cSrcweir         RMAP( "OptimalHeight",  XML_NAMESPACE_STYLE, XML_MIN_ROW_HEIGHT,                XML_TYPE_MEASURE,   0 ),
92cdf0e10cSrcweir         RMAP( "OptimalWidth",   XML_NAMESPACE_STYLE, XML_USE_OPTIMAL_ROW_HEIGHT,        XML_TYPE_BOOL, 0 ),
93cdf0e10cSrcweir         MAP_END
94cdf0e10cSrcweir     };
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     return &aXMLRowProperties[0];
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir // --------------------------------------------------------------------
100cdf0e10cSrcweir 
101cdf0e10cSrcweir class StringStatisticHelper : public std::map< OUString, sal_Int32 >
102cdf0e10cSrcweir {
103cdf0e10cSrcweir public:
104cdf0e10cSrcweir     void add( const OUString& rStyleName );
clear()105cdf0e10cSrcweir     void clear() { std::map< OUString, sal_Int32 >::clear(); }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     sal_Int32 getModeString( /* out */ OUString& rModeString );
108cdf0e10cSrcweir };
109cdf0e10cSrcweir 
110cdf0e10cSrcweir // --------------------------------------------------------------------
111cdf0e10cSrcweir 
add(const OUString & rStyleName)112cdf0e10cSrcweir void StringStatisticHelper::add( const OUString& rStyleName )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     std::map< OUString, sal_Int32 >::iterator iter( find( rStyleName ) );
115cdf0e10cSrcweir     if( iter == end() )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         (*this)[rStyleName] = 1;
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir     else
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         (*iter).second += 1;
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // --------------------------------------------------------------------
126cdf0e10cSrcweir 
getModeString(OUString & rStyleName)127cdf0e10cSrcweir sal_Int32 StringStatisticHelper::getModeString( OUString& rStyleName )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     sal_Int32 nMax = 0;
130cdf0e10cSrcweir     for( std::map< OUString, sal_Int32 >::iterator iter( begin() ); iter != end(); iter++ )
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         if( (*iter).second > nMax )
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             rStyleName = (*iter).first;
135cdf0e10cSrcweir             nMax = (*iter).second;
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     return nMax;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir // --------------------------------------------------------------------
143cdf0e10cSrcweir // class XMLTableExport
144cdf0e10cSrcweir // --------------------------------------------------------------------
145cdf0e10cSrcweir 
XMLTableExport(SvXMLExport & rExp,const rtl::Reference<SvXMLExportPropertyMapper> & xExportPropertyMapper,const rtl::Reference<XMLPropertyHandlerFactory> & xFactoryRef)146cdf0e10cSrcweir XMLTableExport::XMLTableExport(SvXMLExport& rExp, const rtl::Reference< SvXMLExportPropertyMapper  >& xExportPropertyMapper, const rtl::Reference< XMLPropertyHandlerFactory >& xFactoryRef )
147cdf0e10cSrcweir : mrExport( rExp )
148cdf0e10cSrcweir , mbExportTables( false )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir     Reference< XMultiServiceFactory > xFac( rExp.GetModel(), UNO_QUERY );
151cdf0e10cSrcweir     if( xFac.is() ) try
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         Sequence< OUString > sSNS( xFac->getAvailableServiceNames() );
154cdf0e10cSrcweir         sal_Int32 n = sSNS.getLength();
155cdf0e10cSrcweir         const OUString* pSNS( sSNS.getConstArray() );
156cdf0e10cSrcweir         while( --n > 0 )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             if( (*pSNS++).equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.drawing.TableShape") ) )
159cdf0e10cSrcweir             {
160cdf0e10cSrcweir                 mbExportTables = true;
161cdf0e10cSrcweir                 break;
162cdf0e10cSrcweir             }
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir     catch( Exception& e )
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         (void)e;
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     mxCellExportPropertySetMapper = xExportPropertyMapper;
171cdf0e10cSrcweir     mxCellExportPropertySetMapper->ChainExportMapper(XMLTextParagraphExport::CreateParaExtPropMapper(rExp));
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     mxRowExportPropertySetMapper = new SvXMLExportPropertyMapper( new XMLPropertySetMapper( getRowPropertiesMap(), xFactoryRef.get() ) );
174cdf0e10cSrcweir     mxColumnExportPropertySetMapper = new SvXMLExportPropertyMapper( new XMLPropertySetMapper( getColumnPropertiesMap(), xFactoryRef.get() ) );
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_COLUMN,
177cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_NAME)),
178cdf0e10cSrcweir         mxColumnExportPropertySetMapper.get(),
179cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_PREFIX)));
180cdf0e10cSrcweir     mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_ROW,
181cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_ROW_STYLES_NAME)),
182cdf0e10cSrcweir         mxRowExportPropertySetMapper.get(),
183cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_ROW_STYLES_PREFIX)));
184cdf0e10cSrcweir //  mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_TABLE
185cdf0e10cSrcweir //      OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME)),
186cdf0e10cSrcweir //      xTableStylesExportPropertySetMapper,
187cdf0e10cSrcweir //      OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_TABLE_STYLES_PREFIX)));
188cdf0e10cSrcweir     mrExport.GetAutoStylePool()->AddFamily(XML_STYLE_FAMILY_TABLE_CELL,
189cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME)),
190cdf0e10cSrcweir         mxCellExportPropertySetMapper.get(),
191cdf0e10cSrcweir         OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_CELL_STYLES_PREFIX)));
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // --------------------------------------------------------------------
195cdf0e10cSrcweir 
~XMLTableExport()196cdf0e10cSrcweir XMLTableExport::~XMLTableExport ()
197cdf0e10cSrcweir {
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir // --------------------------------------------------------------------
201cdf0e10cSrcweir 
has_states(const std::vector<XMLPropertyState> & xPropStates)202cdf0e10cSrcweir static bool has_states( const std::vector< XMLPropertyState >& xPropStates )
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     if( !xPropStates.empty() )
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         std::vector< XMLPropertyState >::const_iterator aIter( xPropStates.begin() );
207cdf0e10cSrcweir         std::vector< XMLPropertyState >::const_iterator aEnd( xPropStates.end() );
208cdf0e10cSrcweir         while( aIter != aEnd )
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             if( aIter->mnIndex != -1 )
211cdf0e10cSrcweir                 return true;
212cdf0e10cSrcweir             aIter++;
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir     return false;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir // --------------------------------------------------------------------
219cdf0e10cSrcweir 
collectTableAutoStyles(const Reference<XColumnRowRange> & xColumnRowRange)220cdf0e10cSrcweir  void XMLTableExport::collectTableAutoStyles(const Reference < XColumnRowRange >& xColumnRowRange)
221cdf0e10cSrcweir  {
222cdf0e10cSrcweir      if( !mbExportTables )
223cdf0e10cSrcweir          return;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     boost::shared_ptr< XMLTableInfo > pTableInfo( new XMLTableInfo() );
226cdf0e10cSrcweir     maTableInfoMap[xColumnRowRange] = pTableInfo;
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     try
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         Reference< XIndexAccess > xIndexAccessCols( xColumnRowRange->getColumns(), UNO_QUERY_THROW );
231cdf0e10cSrcweir         const sal_Int32 nColumnCount = xIndexAccessCols->getCount();
232cdf0e10cSrcweir         for( sal_Int32 nColumn = 0; nColumn < nColumnCount; ++nColumn ) try
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir             Reference< XPropertySet > xPropSet( xIndexAccessCols->getByIndex(nColumn) , UNO_QUERY_THROW );
235cdf0e10cSrcweir             std::vector< XMLPropertyState > xPropStates( mxColumnExportPropertySetMapper->Filter( xPropSet ) );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             if( has_states( xPropStates ) )
238cdf0e10cSrcweir             {
239cdf0e10cSrcweir                 const OUString sStyleName( mrExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_TABLE_COLUMN, xPropStates) );
240cdf0e10cSrcweir                 Reference< XInterface > xKey( xPropSet, UNO_QUERY );
241cdf0e10cSrcweir                 pTableInfo->maColumnStyleMap[xKey] = sStyleName;
242cdf0e10cSrcweir             }
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir         catch( Exception& )
245cdf0e10cSrcweir         {
246cdf0e10cSrcweir             DBG_ERROR("xmloff::XMLTableExport::collectTableAutoStyles(), exception during column style collection!");
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir         Reference< XIndexAccess > xIndexAccessRows( xColumnRowRange->getRows(), UNO_QUERY_THROW );
250cdf0e10cSrcweir         const sal_Int32 nRowCount = xIndexAccessRows->getCount();
251cdf0e10cSrcweir         pTableInfo->maDefaultRowCellStyles.resize(nRowCount);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         StringStatisticHelper aStringStatistic;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow ) try
256cdf0e10cSrcweir         {
257cdf0e10cSrcweir             Reference< XPropertySet > xPropSet( xIndexAccessRows->getByIndex(nRow) , UNO_QUERY_THROW );
258cdf0e10cSrcweir             std::vector< XMLPropertyState > xRowPropStates( mxRowExportPropertySetMapper->Filter( xPropSet ) );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir             if( has_states( xRowPropStates ) )
261cdf0e10cSrcweir             {
262cdf0e10cSrcweir                 const OUString sStyleName( mrExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_TABLE_ROW, xRowPropStates) );
263cdf0e10cSrcweir                 Reference< XInterface > xKey( xPropSet, UNO_QUERY );
264cdf0e10cSrcweir                 pTableInfo->maRowStyleMap[xKey] = sStyleName;
265cdf0e10cSrcweir             }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir             // get the current row
268cdf0e10cSrcweir             Reference< XCellRange > xCellRange( xPropSet, UNO_QUERY_THROW );
269cdf0e10cSrcweir             for ( sal_Int32 nColumn = 0; nColumn < nColumnCount; ++nColumn )
270cdf0e10cSrcweir             {
27186e1cf34SPedro Giffuni                 // get current cell, remarks row index is 0, because we get the range for each row separate
272cdf0e10cSrcweir                 Reference< XPropertySet > xCellSet( xCellRange->getCellByPosition(nColumn, 0), UNO_QUERY_THROW );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir                 // get style
275cdf0e10cSrcweir                 OUString sParentStyleName;
276cdf0e10cSrcweir                 Reference< XPropertySetInfo > xPropertySetInfo( xCellSet->getPropertySetInfo() );
277cdf0e10cSrcweir                 if( xPropertySetInfo.is() && xPropertySetInfo->hasPropertyByName( OUString(RTL_CONSTASCII_USTRINGPARAM("Style"))) )
278cdf0e10cSrcweir                 {
279cdf0e10cSrcweir                     Reference< XStyle > xStyle( xCellSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("Style"))), UNO_QUERY );
280cdf0e10cSrcweir                     if( xStyle.is() )
281cdf0e10cSrcweir                         sParentStyleName = xStyle->getName();
282cdf0e10cSrcweir                 }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir                 // create auto style, if needed
285cdf0e10cSrcweir                 OUString sStyleName;
286cdf0e10cSrcweir                 std::vector< XMLPropertyState > xCellPropStates( mxCellExportPropertySetMapper->Filter( xCellSet ) );
287cdf0e10cSrcweir                 if( has_states( xCellPropStates ) )
288cdf0e10cSrcweir                     sStyleName = mrExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_TABLE_CELL, xCellPropStates);
289cdf0e10cSrcweir                 else
290cdf0e10cSrcweir                     sStyleName = sParentStyleName;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir                 if( sStyleName.getLength() )
293cdf0e10cSrcweir                 {
294cdf0e10cSrcweir                     Reference< XInterface > xKey( xCellSet, UNO_QUERY );
295cdf0e10cSrcweir                     pTableInfo->maCellStyleMap[xKey] = sStyleName;
296cdf0e10cSrcweir                 }
297cdf0e10cSrcweir 
298cdf0e10cSrcweir                 // create auto style for text
299cdf0e10cSrcweir                 Reference< XText > xText(xCellSet, UNO_QUERY);
300cdf0e10cSrcweir                 if(xText.is() && xText->getString().getLength())
301cdf0e10cSrcweir                     GetExport().GetTextParagraphExport()->collectTextAutoStyles( xText );
302cdf0e10cSrcweir 
303cdf0e10cSrcweir                 aStringStatistic.add( sStyleName );
304cdf0e10cSrcweir             }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir             OUString sDefaultCellStyle;
307cdf0e10cSrcweir             if( aStringStatistic.getModeString( sDefaultCellStyle ) > 1 )
308cdf0e10cSrcweir                 pTableInfo->maDefaultRowCellStyles[nRow] = sDefaultCellStyle;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir             aStringStatistic.clear();
311cdf0e10cSrcweir         }
312cdf0e10cSrcweir         catch( Exception& )
313cdf0e10cSrcweir         {
314cdf0e10cSrcweir             DBG_ERROR("xmloff::XMLTableExport::collectTableAutoStyles(), exception during column style collection!");
315cdf0e10cSrcweir         }
316cdf0e10cSrcweir     }
317cdf0e10cSrcweir     catch( Exception& )
318cdf0e10cSrcweir     {
319cdf0e10cSrcweir         DBG_ERROR("xmloff::XMLTableExport::collectTableAutoStyles(), exception caught!");
320cdf0e10cSrcweir     }
321cdf0e10cSrcweir  }
322cdf0e10cSrcweir 
323cdf0e10cSrcweir  // --------------------------------------------------------------------
324cdf0e10cSrcweir 
exportTable(const Reference<XColumnRowRange> & xColumnRowRange)325cdf0e10cSrcweir  void XMLTableExport::exportTable( const Reference < XColumnRowRange >& xColumnRowRange )
326cdf0e10cSrcweir  {
327cdf0e10cSrcweir      if( !mbExportTables )
328cdf0e10cSrcweir          return;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     try
331cdf0e10cSrcweir     {
332cdf0e10cSrcweir         boost::shared_ptr< XMLTableInfo > pTableInfo( maTableInfoMap[xColumnRowRange] );
333cdf0e10cSrcweir 
334cdf0e10cSrcweir         // get row and column count
335cdf0e10cSrcweir         Reference< XIndexAccess > xIndexAccess( xColumnRowRange->getRows(), UNO_QUERY_THROW );
336cdf0e10cSrcweir         Reference< XIndexAccess > xIndexAccessCols( xColumnRowRange->getColumns(), UNO_QUERY_THROW );
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         const sal_Int32 rowCount = xIndexAccess->getCount();
339cdf0e10cSrcweir         const sal_Int32 columnCount = xIndexAccessCols->getCount();
340cdf0e10cSrcweir 
341cdf0e10cSrcweir         SvXMLElementExport tableElement( mrExport, XML_NAMESPACE_TABLE, XML_TABLE, sal_True, sal_True );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         // export table columns
344cdf0e10cSrcweir         ExportTableColumns( xIndexAccessCols, pTableInfo );
345cdf0e10cSrcweir 
346cdf0e10cSrcweir         // start iterating rows and columns
347cdf0e10cSrcweir         for ( sal_Int32 rowIndex = 0; rowIndex < rowCount; rowIndex++ )
348cdf0e10cSrcweir         {
349cdf0e10cSrcweir             // get the current row
350cdf0e10cSrcweir             Reference< XCellRange > xCellRange( xIndexAccess->getByIndex(rowIndex), UNO_QUERY_THROW );
351cdf0e10cSrcweir 
352cdf0e10cSrcweir             OUString sDefaultCellStyle;
353cdf0e10cSrcweir 
354cdf0e10cSrcweir             // table:style-name
355cdf0e10cSrcweir             if( pTableInfo.get() )
356cdf0e10cSrcweir             {
357cdf0e10cSrcweir                 Reference< XInterface > xKey( xCellRange, UNO_QUERY );
358cdf0e10cSrcweir                 const OUString sStyleName( pTableInfo->maRowStyleMap[xKey] );
359cdf0e10cSrcweir                 if( sStyleName.getLength() )
360cdf0e10cSrcweir                     mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_STYLE_NAME, sStyleName );
361cdf0e10cSrcweir 
362cdf0e10cSrcweir                 sDefaultCellStyle = pTableInfo->maDefaultRowCellStyles[rowIndex];
363cdf0e10cSrcweir                 if( sDefaultCellStyle.getLength() )
364cdf0e10cSrcweir                     mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DEFAULT_CELL_STYLE_NAME, sDefaultCellStyle );
365cdf0e10cSrcweir             }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir             // write row element
368cdf0e10cSrcweir             SvXMLElementExport tableRowElement( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_ROW, sal_True, sal_True );
369cdf0e10cSrcweir 
370cdf0e10cSrcweir             for ( sal_Int32 columnIndex = 0; columnIndex < columnCount; columnIndex++ )
371cdf0e10cSrcweir             {
37286e1cf34SPedro Giffuni                 // get current cell, remarks row index is 0, because we get the range for each row separate
373cdf0e10cSrcweir                 Reference< XCell > xCell( xCellRange->getCellByPosition(columnIndex, 0), UNO_QUERY_THROW );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir                 // use XMergeableCell interface from offapi
376cdf0e10cSrcweir                 Reference< XMergeableCell > xMergeableCell( xCell, UNO_QUERY_THROW );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir                 // export cell
379cdf0e10cSrcweir                 ExportCell( xCell, pTableInfo, sDefaultCellStyle );
380cdf0e10cSrcweir             }
381cdf0e10cSrcweir         }
382cdf0e10cSrcweir     }
383cdf0e10cSrcweir     catch( Exception )
384cdf0e10cSrcweir     {
385*4fe6f2d1SMatthias Seidel         DBG_ERROR( "XMLTableExport::exportTable(), exception caught!" );
386cdf0e10cSrcweir     }
387cdf0e10cSrcweir  }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir // --------------------------------------------------------------------
390cdf0e10cSrcweir // Export the table columns
391cdf0e10cSrcweir // --------------------------------------------------------------------
392cdf0e10cSrcweir 
ExportTableColumns(const Reference<XIndexAccess> & xtableColumnsIndexAccess,const boost::shared_ptr<XMLTableInfo> & pTableInfo)393cdf0e10cSrcweir  void XMLTableExport::ExportTableColumns( const Reference < XIndexAccess >& xtableColumnsIndexAccess, const boost::shared_ptr< XMLTableInfo >& pTableInfo )
394cdf0e10cSrcweir  {
395cdf0e10cSrcweir     const sal_Int32 nColumnCount = xtableColumnsIndexAccess->getCount();
396cdf0e10cSrcweir     for( sal_Int32 nColumn = 0; nColumn < nColumnCount; ++nColumn )
397cdf0e10cSrcweir     {
398cdf0e10cSrcweir         Reference< XPropertySet > xColumnProperties( xtableColumnsIndexAccess->getByIndex(nColumn) , UNO_QUERY );
399cdf0e10cSrcweir         if ( xColumnProperties.is() )
400cdf0e10cSrcweir         {
401cdf0e10cSrcweir             // table:style-name
402cdf0e10cSrcweir             if( pTableInfo.get() )
403cdf0e10cSrcweir             {
404cdf0e10cSrcweir                 Reference< XInterface > xKey( xColumnProperties, UNO_QUERY );
405cdf0e10cSrcweir                 const OUString sStyleName( pTableInfo->maColumnStyleMap[xKey] );
406cdf0e10cSrcweir                 if( sStyleName.getLength() )
407cdf0e10cSrcweir                     mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_STYLE_NAME, sStyleName );
408cdf0e10cSrcweir             }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir             // TODO: All columns first have to be checked if some ones
411cdf0e10cSrcweir             // have identical properties. If yes, attr table:number-columns-repeated
412cdf0e10cSrcweir             // has to be written.
413cdf0e10cSrcweir             SvXMLElementExport tableColumnElement( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_COLUMN, sal_True, sal_True );
414cdf0e10cSrcweir         }
415cdf0e10cSrcweir     }
416cdf0e10cSrcweir  }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir // --------------------------------------------------------------------
419cdf0e10cSrcweir // ODF export for a table cell.
420cdf0e10cSrcweir // --------------------------------------------------------------------
421cdf0e10cSrcweir 
ExportCell(const Reference<XCell> & xCell,const boost::shared_ptr<XMLTableInfo> & pTableInfo,const OUString & rDefaultCellStyle)422cdf0e10cSrcweir  void XMLTableExport::ExportCell( const Reference < XCell >& xCell, const boost::shared_ptr< XMLTableInfo >& pTableInfo, const OUString& rDefaultCellStyle )
423cdf0e10cSrcweir  {
424cdf0e10cSrcweir     bool bIsMerged = false;
425cdf0e10cSrcweir     sal_Int32 nRowSpan = 0;
426cdf0e10cSrcweir     sal_Int32 nColSpan = 0;
427cdf0e10cSrcweir 
428cdf0e10cSrcweir     try
429cdf0e10cSrcweir     {
430cdf0e10cSrcweir         if( pTableInfo.get() )
431cdf0e10cSrcweir         {
432cdf0e10cSrcweir             // table:style-name
433cdf0e10cSrcweir             Reference< XInterface > xKey( xCell, UNO_QUERY );
434cdf0e10cSrcweir             const OUString sStyleName( pTableInfo->maCellStyleMap[xKey] );
435cdf0e10cSrcweir             if( sStyleName.getLength() && (sStyleName != rDefaultCellStyle) )
436cdf0e10cSrcweir                 mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_STYLE_NAME, sStyleName );
437cdf0e10cSrcweir         }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir         Reference< XMergeableCell > xMerge( xCell, UNO_QUERY );
440cdf0e10cSrcweir         if( xMerge.is() )
441cdf0e10cSrcweir         {
442cdf0e10cSrcweir             bIsMerged = xMerge->isMerged();
443cdf0e10cSrcweir             nRowSpan = xMerge->getRowSpan();
444cdf0e10cSrcweir             nColSpan = xMerge->getColumnSpan();
445cdf0e10cSrcweir         }
446cdf0e10cSrcweir         DBG_ASSERT( (nRowSpan >= 1) && (nColSpan >= 1), "xmloff::XMLTableExport::ExportCell(), illegal row or col span < 1?" );
447cdf0e10cSrcweir     }
448cdf0e10cSrcweir     catch ( Exception )
449cdf0e10cSrcweir     {
450cdf0e10cSrcweir         DBG_ERROR( "exception while exporting a table cell" );
451cdf0e10cSrcweir     }
452cdf0e10cSrcweir 
453cdf0e10cSrcweir     // table:number-columns-repeated
454cdf0e10cSrcweir     // todo
455cdf0e10cSrcweir 
456cdf0e10cSrcweir     // table:number-columns-spanned
457cdf0e10cSrcweir     if( nColSpan > 1 )
458cdf0e10cSrcweir         mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NUMBER_COLUMNS_SPANNED, OUString::valueOf( nColSpan ) );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     // table:number-rows-spanned
461cdf0e10cSrcweir     if( nRowSpan > 1 )
462cdf0e10cSrcweir         mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NUMBER_ROWS_SPANNED, OUString::valueOf( nRowSpan ) );
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     // <table:table-cell> or <table:covered-table-cell>
465cdf0e10cSrcweir     SvXMLElementExport tableCellElement( mrExport, XML_NAMESPACE_TABLE, bIsMerged ? XML_COVERED_TABLE_CELL : XML_TABLE_CELL, sal_True, sal_True );
466cdf0e10cSrcweir 
467cdf0e10cSrcweir     // export cells text content
468cdf0e10cSrcweir     ImpExportText( xCell );
469cdf0e10cSrcweir  }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir // --------------------------------------------------------------------
472cdf0e10cSrcweir // ODF export of the text contents of a table cell.
473cdf0e10cSrcweir // Remarks: Up to now we only export text contents!
474cdf0e10cSrcweir // TODO: Check against nested tables ....
475cdf0e10cSrcweir // --------------------------------------------------------------------
476cdf0e10cSrcweir 
ImpExportText(const Reference<XCell> & xCell)477cdf0e10cSrcweir  void XMLTableExport::ImpExportText( const Reference< XCell >& xCell )
478cdf0e10cSrcweir  {
479cdf0e10cSrcweir     Reference< XText > xText( xCell, UNO_QUERY );
480cdf0e10cSrcweir     if( xText.is() && xText->getString().getLength())
481cdf0e10cSrcweir         mrExport.GetTextParagraphExport()->exportText( xText );
482cdf0e10cSrcweir  }
483cdf0e10cSrcweir 
484cdf0e10cSrcweir // --------------------------------------------------------------------
485cdf0e10cSrcweir 
exportTableStyles()486cdf0e10cSrcweir void XMLTableExport::exportTableStyles()
487cdf0e10cSrcweir {
488cdf0e10cSrcweir      if( !mbExportTables )
489cdf0e10cSrcweir          return;
490cdf0e10cSrcweir 
491cdf0e10cSrcweir     XMLStyleExport aStEx(mrExport, OUString(), mrExport.GetAutoStylePool().get());
492cdf0e10cSrcweir 
493cdf0e10cSrcweir     // write graphic family styles
494cdf0e10cSrcweir     aStEx.exportStyleFamily("cell", OUString(RTL_CONSTASCII_USTRINGPARAM(XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME)), mxCellExportPropertySetMapper.get(), sal_True, XML_STYLE_FAMILY_TABLE_CELL);
495cdf0e10cSrcweir 
496cdf0e10cSrcweir     exportTableTemplates();
497cdf0e10cSrcweir }
498cdf0e10cSrcweir 
499cdf0e10cSrcweir // --------------------------------------------------------------------
500cdf0e10cSrcweir // Export the collected automatic styles
501cdf0e10cSrcweir // --------------------------------------------------------------------
502cdf0e10cSrcweir 
exportAutoStyles()503cdf0e10cSrcweir void XMLTableExport::exportAutoStyles()
504cdf0e10cSrcweir {
505cdf0e10cSrcweir      if( !mbExportTables )
506cdf0e10cSrcweir          return;
507cdf0e10cSrcweir 
508cdf0e10cSrcweir     mrExport.GetAutoStylePool()->exportXML( XML_STYLE_FAMILY_TABLE_COLUMN, mrExport.GetDocHandler(), mrExport.GetMM100UnitConverter(), mrExport.GetNamespaceMap() );
509cdf0e10cSrcweir     mrExport.GetAutoStylePool()->exportXML( XML_STYLE_FAMILY_TABLE_ROW, mrExport.GetDocHandler(), mrExport.GetMM100UnitConverter(), mrExport.GetNamespaceMap() );
510cdf0e10cSrcweir     mrExport.GetAutoStylePool()->exportXML( XML_STYLE_FAMILY_TABLE_CELL, mrExport.GetDocHandler(), mrExport.GetMM100UnitConverter(), mrExport.GetNamespaceMap() );
511cdf0e10cSrcweir }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir // --------------------------------------------------------------------
514cdf0e10cSrcweir 
getTableStyleMap()515cdf0e10cSrcweir const TableStyleElement* getTableStyleMap()
516cdf0e10cSrcweir {
517cdf0e10cSrcweir     static struct TableStyleElement gTableStyleElements[] =
518cdf0e10cSrcweir     {
519cdf0e10cSrcweir         { XML_FIRST_ROW, OUString( RTL_CONSTASCII_USTRINGPARAM( "first-row" ) ) },
520cdf0e10cSrcweir         { XML_LAST_ROW, OUString( RTL_CONSTASCII_USTRINGPARAM( "last-row" ) ) },
521cdf0e10cSrcweir         { XML_FIRST_COLUMN, OUString( RTL_CONSTASCII_USTRINGPARAM( "first-column" ) ) },
522cdf0e10cSrcweir         { XML_LAST_COLUMN, OUString( RTL_CONSTASCII_USTRINGPARAM( "last-column" ) ) },
523cdf0e10cSrcweir         { XML_EVEN_ROWS, OUString( RTL_CONSTASCII_USTRINGPARAM( "even-rows" ) ) },
524cdf0e10cSrcweir         { XML_ODD_ROWS, OUString( RTL_CONSTASCII_USTRINGPARAM( "odd-rows" ) ) },
525cdf0e10cSrcweir         { XML_EVEN_COLUMNS, OUString( RTL_CONSTASCII_USTRINGPARAM( "even-columns" ) ) },
526cdf0e10cSrcweir         { XML_ODD_COLUMNS, OUString( RTL_CONSTASCII_USTRINGPARAM( "odd-columns" ) ) },
527cdf0e10cSrcweir         { XML_BODY, OUString( RTL_CONSTASCII_USTRINGPARAM( "body" ) ) },
528cdf0e10cSrcweir         { XML_TOKEN_END, OUString() }
529cdf0e10cSrcweir     };
530cdf0e10cSrcweir 
531cdf0e10cSrcweir     return &gTableStyleElements[0];
532cdf0e10cSrcweir }
533cdf0e10cSrcweir 
534cdf0e10cSrcweir // --------------------------------------------------------------------
535cdf0e10cSrcweir 
exportTableTemplates()536cdf0e10cSrcweir void XMLTableExport::exportTableTemplates()
537cdf0e10cSrcweir {
538cdf0e10cSrcweir      if( !mbExportTables )
539cdf0e10cSrcweir          return;
540cdf0e10cSrcweir 
541cdf0e10cSrcweir     try
542cdf0e10cSrcweir     {
543cdf0e10cSrcweir         Reference< XStyleFamiliesSupplier > xFamiliesSupp( mrExport.GetModel(), UNO_QUERY_THROW );
544cdf0e10cSrcweir         Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
545cdf0e10cSrcweir         const OUString sFamilyName( RTL_CONSTASCII_USTRINGPARAM("table" ) );
546cdf0e10cSrcweir         Reference< XIndexAccess > xTableFamily( xFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
547cdf0e10cSrcweir 
548cdf0e10cSrcweir         for( sal_Int32 nIndex = 0; nIndex < xTableFamily->getCount(); nIndex++ ) try
549cdf0e10cSrcweir         {
550cdf0e10cSrcweir             Reference< XStyle > xTableStyle( xTableFamily->getByIndex( nIndex ), UNO_QUERY_THROW );
551cdf0e10cSrcweir             if( !xTableStyle->isInUse() )
552cdf0e10cSrcweir                 continue;
553cdf0e10cSrcweir 
554cdf0e10cSrcweir             Reference< XNameAccess > xStyleNames( xTableStyle, UNO_QUERY_THROW );
555cdf0e10cSrcweir 
556cdf0e10cSrcweir             mrExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xTableStyle->getName() ) );
557cdf0e10cSrcweir             SvXMLElementExport tableTemplate( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_TEMPLATE, sal_True, sal_True );
558cdf0e10cSrcweir 
559cdf0e10cSrcweir             const TableStyleElement* pElements = getTableStyleMap();
560cdf0e10cSrcweir             while( pElements->meElement != XML_TOKEN_END )
561cdf0e10cSrcweir             {
562cdf0e10cSrcweir                 try
563cdf0e10cSrcweir                 {
564cdf0e10cSrcweir                     Reference< XStyle > xStyle( xStyleNames->getByName( pElements->msStyleName ), UNO_QUERY );
565cdf0e10cSrcweir                     if( xStyle.is() )
566cdf0e10cSrcweir                     {
567cdf0e10cSrcweir                         mrExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xStyle->getName() ) );
568cdf0e10cSrcweir                         SvXMLElementExport element( mrExport, XML_NAMESPACE_TABLE, pElements->meElement, sal_True, sal_True );
569cdf0e10cSrcweir                     }
570cdf0e10cSrcweir                 }
571cdf0e10cSrcweir                 catch( Exception& )
572cdf0e10cSrcweir                 {
573cdf0e10cSrcweir                     DBG_ERROR("xmloff::XMLTableExport::exportTableTemplates(), exception caught!");
574cdf0e10cSrcweir                 }
575cdf0e10cSrcweir 
576cdf0e10cSrcweir                 pElements++;
577cdf0e10cSrcweir             }
578cdf0e10cSrcweir         }
579cdf0e10cSrcweir         catch( Exception& )
580cdf0e10cSrcweir         {
581cdf0e10cSrcweir             DBG_ERROR("xmloff::XMLTableExport::exportTableDesigns(), exception caught while exporting a table design!");
582cdf0e10cSrcweir         }
583cdf0e10cSrcweir     }
584cdf0e10cSrcweir     catch( Exception& )
585cdf0e10cSrcweir     {
586cdf0e10cSrcweir         DBG_ERROR("xmloff::XMLTableExport::exportTableDesigns(), exception caught!");
587cdf0e10cSrcweir     }
588cdf0e10cSrcweir }
589cdf0e10cSrcweir 
590cdf0e10cSrcweir // --------------------------------------------------------------------
591