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