Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
22 : #include <com/sun/star/table/XTableRows.hpp>
23 : #include <com/sun/star/table/XMergeableCell.hpp>
24 : #include <com/sun/star/table/XMergeableCellRange.hpp>
25 : #include <com/sun/star/table/XTable.hpp>
26 : #include <com/sun/star/text/XText.hpp>
27 : #include <com/sun/star/container/XNameContainer.hpp>
28 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 :
30 : #include "xmloff/table/XMLTableImport.hxx"
31 : #include "xmloff/xmltkmap.hxx"
32 : #include "xmloff/maptype.hxx"
33 : #include "xmloff/xmlprmap.hxx"
34 : #include "xmloff/txtimp.hxx"
35 : #include "xmloff/xmlimp.hxx"
36 : #include "xmloff/nmspmap.hxx"
37 : #include "xmloff/xmlstyle.hxx"
38 : #include "xmloff/prstylei.hxx"
39 :
40 : #include "xmloff/xmlnmspe.hxx"
41 : #include "table.hxx"
42 :
43 : #include <boost/shared_ptr.hpp>
44 :
45 : // --------------------------------------------------------------------
46 :
47 : using namespace ::xmloff::token;
48 : using namespace ::com::sun::star::beans;
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::table;
51 : using namespace ::com::sun::star::xml::sax;
52 : using namespace ::com::sun::star::text;
53 : using namespace ::com::sun::star::style;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::container;
56 :
57 : // --------------------------------------------------------------------
58 :
59 16 : struct ColumnInfo
60 : {
61 : OUString msStyleName;
62 : sal_Bool mbVisibility;
63 : OUString msDefaultCellStyleName;
64 : };
65 :
66 : // --------------------------------------------------------------------
67 :
68 20 : class XMLProxyContext : public SvXMLImportContext
69 : {
70 : public:
71 : XMLProxyContext( SvXMLImport& rImport, const SvXMLImportContextRef& xParent, sal_uInt16 nPrfx, const OUString& rLName );
72 :
73 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
74 :
75 : private:
76 : SvXMLImportContextRef mxParent;
77 : };
78 :
79 : // --------------------------------------------------------------------
80 :
81 : struct MergeInfo
82 : {
83 : sal_Int32 mnStartColumn;
84 : sal_Int32 mnStartRow;
85 : sal_Int32 mnEndColumn;
86 : sal_Int32 mnEndRow;
87 :
88 0 : MergeInfo( sal_Int32 nStartColumn, sal_Int32 nStartRow, sal_Int32 nColumnSpan, sal_Int32 nRowSpan )
89 0 : : mnStartColumn( nStartColumn ), mnStartRow( nStartRow ), mnEndColumn( nStartColumn + nColumnSpan - 1 ), mnEndRow( nStartRow + nRowSpan - 1 ) {};
90 : };
91 :
92 : typedef std::vector< boost::shared_ptr< MergeInfo > > MergeInfoVector;
93 :
94 : // --------------------------------------------------------------------
95 :
96 : class XMLTableImportContext : public SvXMLImportContext
97 : {
98 : public:
99 : XMLTableImportContext( const rtl::Reference< XMLTableImport >& xThis, sal_uInt16 nPrfx, const OUString& rLName, Reference< XColumnRowRange >& xColumnRowRange );
100 : virtual ~XMLTableImportContext();
101 :
102 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
103 :
104 : virtual void StartElement( const Reference< XAttributeList >& xAttrList );
105 :
106 : virtual void EndElement();
107 :
108 : void InitColumns();
109 :
110 : SvXMLImportContext * ImportColumn( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
111 : SvXMLImportContext * ImportRow( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
112 : SvXMLImportContext * ImportCell( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
113 :
114 : OUString GetDefaultCellStyleName() const;
115 :
116 : rtl::Reference< XMLTableImport > mxTableImporter;
117 : ::com::sun::star::uno::Reference< ::com::sun::star::table::XTable > mxTable;
118 : Reference< XTableColumns > mxColumns;
119 : Reference< XTableRows > mxRows;
120 :
121 : std::vector< boost::shared_ptr< ColumnInfo > > maColumnInfos;
122 : sal_Int32 mnCurrentRow;
123 : sal_Int32 mnCurrentColumn;
124 :
125 : // default cell style name for the current row
126 : OUString msDefaultCellStyleName;
127 :
128 : MergeInfoVector maMergeInfos;
129 : };
130 :
131 : // --------------------------------------------------------------------
132 :
133 : class XMLCellImportContext : public SvXMLImportContext
134 : {
135 : public:
136 : XMLCellImportContext( SvXMLImport& rImport,
137 : const Reference< XMergeableCell >& xCell,
138 : const OUString& sDefaultCellStyleName,
139 : sal_uInt16 nPrfx, const OUString& rLName,
140 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
141 :
142 : virtual ~XMLCellImportContext();
143 :
144 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
145 :
146 : virtual void EndElement();
147 :
148 16 : sal_Int32 getColumnSpan() const { return mnColSpan; }
149 16 : sal_Int32 getRowSpan() const { return mnRowSpan; }
150 16 : sal_Int32 getRepeated() const { return mnRepeated; }
151 :
152 : Reference< XMergeableCell > mxCell;
153 : Reference< XTextCursor > mxCursor;
154 : Reference< XTextCursor > mxOldCursor;
155 : bool mbListContextPushed;
156 :
157 : sal_Int32 mnColSpan, mnRowSpan, mnRepeated;
158 : };
159 :
160 : // --------------------------------------------------------------------
161 :
162 2 : class XMLTableTemplateContext : public SvXMLStyleContext
163 : {
164 : public:
165 : XMLTableTemplateContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const Reference< XAttributeList >& xAttrList );
166 :
167 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
168 :
169 : virtual void StartElement( const Reference< XAttributeList >& xAttrList );
170 :
171 : virtual void EndElement();
172 :
173 : private:
174 : XMLTableTemplate maTableTemplate;
175 : OUString msTemplateStyleName;
176 : };
177 :
178 : // --------------------------------------------------------------------
179 : // class XMLProxyContext
180 : // --------------------------------------------------------------------
181 :
182 10 : XMLProxyContext::XMLProxyContext( SvXMLImport& rImport, const SvXMLImportContextRef& xParent, sal_uInt16 nPrfx, const OUString& rLName )
183 : : SvXMLImportContext( rImport, nPrfx, rLName )
184 10 : , mxParent( xParent )
185 : {
186 10 : }
187 :
188 : // --------------------------------------------------------------------
189 :
190 16 : SvXMLImportContext * XMLProxyContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
191 : {
192 16 : if( mxParent.Is() )
193 16 : return mxParent->CreateChildContext( nPrefix, rLocalName, xAttrList );
194 : else
195 0 : return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
196 : }
197 :
198 : // --------------------------------------------------------------------
199 : // class XMLTableImport
200 : // --------------------------------------------------------------------
201 :
202 8 : XMLTableImport::XMLTableImport( SvXMLImport& rImport, const rtl::Reference< XMLPropertySetMapper >& xCellPropertySetMapper, const rtl::Reference< XMLPropertyHandlerFactory >& xFactoryRef )
203 8 : : mrImport( rImport )
204 : {
205 8 : mxCellImportPropertySetMapper = new SvXMLImportPropertyMapper( xCellPropertySetMapper.get(), rImport );
206 8 : mxCellImportPropertySetMapper->ChainImportMapper(XMLTextImportHelper::CreateParaExtPropMapper(rImport));
207 :
208 :
209 8 : UniReference < XMLPropertySetMapper > xRowMapper( new XMLPropertySetMapper( getRowPropertiesMap(), xFactoryRef.get() ) );
210 8 : mxRowImportPropertySetMapper = new SvXMLImportPropertyMapper( xRowMapper, rImport );
211 :
212 16 : UniReference < XMLPropertySetMapper > xColMapper( new XMLPropertySetMapper( getColumnPropertiesMap(), xFactoryRef.get() ) );
213 16 : mxColumnImportPropertySetMapper = new SvXMLImportPropertyMapper( xColMapper, rImport );
214 8 : }
215 :
216 : // --------------------------------------------------------------------
217 :
218 16 : XMLTableImport::~XMLTableImport()
219 : {
220 16 : }
221 :
222 : // --------------------------------------------------------------------
223 :
224 2 : SvXMLImportContext* XMLTableImport::CreateTableContext( sal_uInt16 nPrfx, const OUString& rLName, Reference< XColumnRowRange >& xColumnRowRange )
225 : {
226 2 : rtl::Reference< XMLTableImport > xThis( this );
227 2 : return new XMLTableImportContext( xThis, nPrfx, rLName, xColumnRowRange );
228 : }
229 :
230 : // --------------------------------------------------------------------
231 :
232 1 : SvXMLStyleContext* XMLTableImport::CreateTableTemplateContext( sal_uInt16 nPrfx, const OUString& rLName, const Reference< XAttributeList >& xAttrList )
233 : {
234 1 : return new XMLTableTemplateContext( mrImport, nPrfx, rLName, xAttrList );
235 : }
236 :
237 : // --------------------------------------------------------------------
238 :
239 1 : void XMLTableImport::addTableTemplate( const OUString& rsStyleName, XMLTableTemplate& xTableTemplate )
240 : {
241 1 : boost::shared_ptr< XMLTableTemplate > xPtr( new XMLTableTemplate );
242 1 : xPtr->swap( xTableTemplate );
243 1 : maTableTemplates[rsStyleName] = xPtr;
244 1 : }
245 :
246 : // --------------------------------------------------------------------
247 :
248 7 : void XMLTableImport::finishStyles()
249 : {
250 7 : if( !maTableTemplates.empty() ) try
251 : {
252 1 : Reference< XStyleFamiliesSupplier > xFamiliesSupp( mrImport.GetModel(), UNO_QUERY_THROW );
253 2 : Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
254 2 : const OUString sFamilyName( "table" );
255 2 : const OUString sCellFamilyName( "cell" );
256 :
257 2 : Reference< XNameContainer > xTableFamily( xFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
258 2 : Reference< XNameAccess > xCellFamily( xFamilies->getByName( sCellFamilyName ), UNO_QUERY_THROW );
259 :
260 2 : Reference< XSingleServiceFactory > xFactory( xTableFamily, UNO_QUERY_THROW );
261 :
262 2 : for( XMLTableTemplateMap::iterator aTemplateIter( maTableTemplates.begin() ); aTemplateIter != maTableTemplates.end(); ++aTemplateIter ) try
263 : {
264 1 : const OUString sTemplateName( (*aTemplateIter).first );
265 2 : Reference< XNameReplace > xTemplate( xFactory->createInstance(), UNO_QUERY_THROW );
266 :
267 2 : boost::shared_ptr< XMLTableTemplate > xT( (*aTemplateIter).second );
268 :
269 8 : for( XMLTableTemplate::iterator aStyleIter( xT->begin() ); aStyleIter != xT->end(); ++aStyleIter ) try
270 : {
271 7 : const OUString sPropName( (*aStyleIter).first );
272 14 : const OUString sStyleName( (*aStyleIter).second );
273 14 : xTemplate->replaceByName( sPropName, xCellFamily->getByName( sStyleName ) );
274 : }
275 0 : catch( Exception& )
276 : {
277 : OSL_FAIL("xmloff::XMLTableImport::finishStyles(), exception caught!");
278 : }
279 :
280 1 : if( xTemplate.is() )
281 : {
282 1 : if( xTableFamily->hasByName( sTemplateName ) )
283 1 : xTableFamily->replaceByName( sTemplateName, Any( xTemplate ) );
284 : else
285 0 : xTableFamily->insertByName( sTemplateName, Any( xTemplate ) );
286 1 : }
287 :
288 : }
289 0 : catch( Exception& )
290 : {
291 : OSL_FAIL("xmloff::XMLTableImport::finishStyles(), exception caught!");
292 1 : }
293 : }
294 0 : catch( Exception& )
295 : {
296 : OSL_FAIL("xmloff::XMLTableImport::finishStyles(), exception caught!");
297 : }
298 7 : }
299 :
300 : // --------------------------------------------------------------------
301 : // class XMLTableImport
302 : // --------------------------------------------------------------------
303 :
304 :
305 2 : XMLTableImportContext::XMLTableImportContext( const rtl::Reference< XMLTableImport >& xImporter, sal_uInt16 nPrfx, const OUString& rLName, Reference< XColumnRowRange >& xColumnRowRange )
306 2 : : SvXMLImportContext( xImporter->mrImport, nPrfx, rLName )
307 : , mxTableImporter( xImporter )
308 : , mxTable( xColumnRowRange, UNO_QUERY )
309 2 : , mxColumns( xColumnRowRange->getColumns() )
310 2 : , mxRows( xColumnRowRange->getRows() )
311 : , mnCurrentRow( -1 )
312 8 : , mnCurrentColumn( -1 )
313 : {
314 2 : }
315 :
316 : // --------------------------------------------------------------------
317 :
318 4 : XMLTableImportContext::~XMLTableImportContext()
319 : {
320 4 : }
321 :
322 : // --------------------------------------------------------------------
323 :
324 8 : SvXMLImportContext * XMLTableImportContext::ImportColumn( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
325 : {
326 8 : if( mxColumns.is() && (mnCurrentRow == -1) ) try
327 : {
328 8 : boost::shared_ptr< ColumnInfo > xInfo ( new ColumnInfo );
329 :
330 8 : sal_Int32 nRepeated = 1;
331 :
332 : // read attributes for the table-column
333 8 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
334 16 : for(sal_Int16 i=0; i < nAttrCount; i++)
335 : {
336 8 : const OUString sAttrName( xAttrList->getNameByIndex( i ) );
337 16 : const OUString sValue( xAttrList->getValueByIndex( i ) );
338 16 : OUString aLocalName;
339 :
340 8 : sal_uInt16 nPrefix2 = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
341 8 : if( XML_NAMESPACE_TABLE == nPrefix2 )
342 : {
343 8 : if( IsXMLToken( aLocalName, XML_NUMBER_COLUMNS_REPEATED ) )
344 : {
345 0 : nRepeated = sValue.toInt32();
346 : }
347 8 : else if( IsXMLToken( aLocalName, XML_STYLE_NAME ) )
348 : {
349 8 : xInfo->msStyleName = sValue;
350 : }
351 0 : else if( IsXMLToken( aLocalName, XML_DEFAULT_CELL_STYLE_NAME ) )
352 : {
353 0 : xInfo->msDefaultCellStyleName = sValue;
354 : }
355 0 : else if( IsXMLToken( aLocalName, XML_VISIBILITY ) )
356 : {
357 0 : xInfo->mbVisibility = IsXMLToken( sValue, XML_VISIBLE );
358 : }
359 : }
360 0 : else if ( (XML_NAMESPACE_XML == nPrefix2) &&
361 0 : IsXMLToken(aLocalName, XML_ID) )
362 : {
363 : (void) sValue;
364 : //FIXME: TODO
365 : }
366 8 : }
367 :
368 8 : if( nRepeated <= 1 )
369 : {
370 8 : maColumnInfos.push_back( xInfo );
371 : }
372 : else
373 : {
374 0 : maColumnInfos.insert( maColumnInfos.end(), nRepeated, xInfo );
375 8 : }
376 : }
377 0 : catch( Exception& )
378 : {
379 : OSL_FAIL("xmloff::XMLTableImportContext::ImportTableColumn(), exception caught!");
380 : }
381 :
382 8 : return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList);
383 : }
384 :
385 : // --------------------------------------------------------------------
386 :
387 2 : void XMLTableImportContext::InitColumns()
388 : {
389 2 : if( mxColumns.is() ) try
390 : {
391 2 : const sal_Int32 nCount1 = mxColumns->getCount();
392 2 : const sal_Int32 nCount2 = sal::static_int_cast< sal_Int32 >( maColumnInfos.size() );
393 2 : if( nCount1 < nCount2 )
394 1 : mxColumns->insertByIndex( nCount1, nCount2 - nCount1 );
395 :
396 2 : SvXMLStylesContext * pAutoStyles = GetImport().GetShapeImport()->GetAutoStylesContext();
397 :
398 10 : for( sal_Int32 nCol = 0; nCol < nCount2; nCol++ )
399 : {
400 8 : boost::shared_ptr< ColumnInfo > xInfo( maColumnInfos[nCol] );
401 :
402 8 : if( pAutoStyles && !xInfo->msStyleName.isEmpty() )
403 : {
404 : const XMLPropStyleContext* pStyle =
405 : dynamic_cast< const XMLPropStyleContext* >(
406 8 : pAutoStyles->FindStyleChildContext(XML_STYLE_FAMILY_TABLE_COLUMN, xInfo->msStyleName) );
407 :
408 8 : if( pStyle )
409 : {
410 8 : Reference< XPropertySet > xColProps( mxColumns->getByIndex(nCol), UNO_QUERY_THROW );
411 8 : const_cast< XMLPropStyleContext* >( pStyle )->FillPropertySet( xColProps );
412 : }
413 : }
414 :
415 8 : }
416 : }
417 0 : catch( Exception& )
418 : {
419 : OSL_FAIL("xmloff::XMLTableImportContext::ImportTableColumn(), exception caught!");
420 : }
421 2 : }
422 :
423 : // --------------------------------------------------------------------
424 :
425 10 : SvXMLImportContext * XMLTableImportContext::ImportRow( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
426 : {
427 10 : if( mxRows.is() )
428 : {
429 10 : mnCurrentRow++;
430 10 : if( mnCurrentRow == 0 )
431 2 : InitColumns(); // first init columns
432 :
433 10 : mnCurrentColumn = -1;
434 :
435 10 : const sal_Int32 nRowCount = mxRows->getCount();
436 10 : if( ( nRowCount - 1) < mnCurrentRow )
437 : {
438 8 : const sal_Int32 nCount = mnCurrentRow - nRowCount + 1;
439 8 : mxRows->insertByIndex( nRowCount, nCount );
440 : }
441 :
442 10 : Reference< XPropertySet > xRowSet( mxRows->getByIndex(mnCurrentRow), UNO_QUERY );
443 :
444 20 : OUString sStyleName;
445 :
446 : // read attributes for the table-row
447 10 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
448 21 : for(sal_Int16 i=0; i < nAttrCount; i++)
449 : {
450 11 : const OUString sAttrName( xAttrList->getNameByIndex( i ) );
451 22 : const OUString sValue( xAttrList->getValueByIndex( i ) );
452 22 : OUString aLocalName;
453 :
454 11 : sal_uInt16 nPrefix2 = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
455 11 : if( nPrefix2 == XML_NAMESPACE_TABLE )
456 : {
457 11 : if( IsXMLToken( aLocalName, XML_STYLE_NAME ) )
458 : {
459 10 : sStyleName = sValue;
460 : }
461 1 : else if( IsXMLToken( aLocalName, XML_DEFAULT_CELL_STYLE_NAME ) )
462 : {
463 1 : msDefaultCellStyleName = sValue;
464 : }
465 : }
466 0 : else if ( (XML_NAMESPACE_XML == nPrefix2) &&
467 0 : IsXMLToken(aLocalName, XML_ID) )
468 : {
469 : (void) sValue;
470 : //FIXME: TODO
471 : }
472 11 : }
473 :
474 10 : if( !sStyleName.isEmpty() )
475 : {
476 10 : SvXMLStylesContext * pAutoStyles = GetImport().GetShapeImport()->GetAutoStylesContext();
477 10 : if( pAutoStyles )
478 : {
479 : const XMLPropStyleContext* pStyle =
480 : dynamic_cast< const XMLPropStyleContext* >(
481 10 : pAutoStyles->FindStyleChildContext(XML_STYLE_FAMILY_TABLE_ROW, sStyleName) );
482 :
483 10 : if( pStyle )
484 : {
485 10 : const_cast< XMLPropStyleContext* >( pStyle )->FillPropertySet( xRowSet );
486 : }
487 : }
488 10 : }
489 : }
490 :
491 10 : SvXMLImportContextRef xThis( this );
492 10 : return new XMLProxyContext( GetImport(), xThis, nPrefix, rLocalName );
493 : }
494 :
495 : // --------------------------------------------------------------------
496 :
497 16 : SvXMLImportContext * XMLTableImportContext::ImportCell( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
498 : {
499 16 : mnCurrentColumn++;
500 16 : if( mxColumns.is() ) try
501 : {
502 16 : if( mxColumns->getCount() <= mnCurrentColumn )
503 0 : mxColumns->insertByIndex( mxColumns->getCount(), mnCurrentColumn - mxColumns->getCount() + 1 );
504 :
505 16 : Reference< XMergeableCell > xCell( mxTable->getCellByPosition( mnCurrentColumn, mnCurrentRow ), UNO_QUERY_THROW );
506 16 : XMLCellImportContext* pCellContext = new XMLCellImportContext( GetImport(), xCell, GetDefaultCellStyleName(), nPrefix, rLocalName, xAttrList );
507 :
508 16 : const sal_Int32 nColumnSpan = pCellContext->getColumnSpan();
509 16 : const sal_Int32 nRowSpan = pCellContext->getRowSpan();
510 16 : if( (nColumnSpan > 1) || (nRowSpan > 1) )
511 0 : maMergeInfos.push_back( boost::shared_ptr< MergeInfo >( new MergeInfo( mnCurrentColumn, mnCurrentRow, nColumnSpan, nRowSpan ) ) );
512 :
513 16 : const sal_Int32 nRepeated = pCellContext->getRepeated();
514 16 : if( nRepeated > 1 )
515 : {
516 : OSL_FAIL("xmloff::XMLTableImportContext::ImportCell(), import of repeated Cells not implemented (TODO)");
517 0 : mnCurrentColumn += nRepeated - 1;
518 : }
519 :
520 16 : return pCellContext;
521 : }
522 0 : catch( Exception& )
523 : {
524 : OSL_FAIL("xmloff::XMLTableImportContext::ImportCell(), exception caught!");
525 : }
526 :
527 0 : return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList);
528 : }
529 :
530 : // --------------------------------------------------------------------
531 :
532 34 : SvXMLImportContext *XMLTableImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
533 : {
534 34 : if( nPrefix == XML_NAMESPACE_TABLE )
535 : {
536 34 : if( IsXMLToken( rLocalName, XML_TABLE_COLUMN ) )
537 8 : return ImportColumn( nPrefix, rLocalName, xAttrList );
538 26 : else if( IsXMLToken( rLocalName, XML_TABLE_ROW ) )
539 10 : return ImportRow( nPrefix, rLocalName, xAttrList );
540 16 : else if( IsXMLToken( rLocalName, XML_TABLE_CELL ) || IsXMLToken( rLocalName, XML_COVERED_TABLE_CELL ) )
541 16 : return ImportCell( nPrefix, rLocalName, xAttrList );
542 0 : else if( IsXMLToken( rLocalName, XML_TABLE_COLUMNS ) || IsXMLToken( rLocalName, XML_TABLE_ROWS ) )
543 : {
544 0 : SvXMLImportContextRef xThis( this );
545 0 : return new XMLProxyContext( GetImport(), xThis, nPrefix, rLocalName );
546 : }
547 : }
548 :
549 0 : return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList);
550 : }
551 :
552 : // --------------------------------------------------------------------
553 :
554 2 : void XMLTableImportContext::StartElement( const Reference< XAttributeList >& /*xAttrList*/ )
555 : {
556 2 : }
557 :
558 : // --------------------------------------------------------------------
559 :
560 2 : void XMLTableImportContext::EndElement()
561 : {
562 2 : if( !maMergeInfos.empty() )
563 : {
564 0 : MergeInfoVector::iterator aIter( maMergeInfos.begin() );
565 0 : while( aIter != maMergeInfos.end() )
566 : {
567 0 : boost::shared_ptr< MergeInfo > xInfo( (*aIter++) );
568 :
569 0 : if( xInfo.get() ) try
570 : {
571 0 : Reference< XCellRange > xRange( mxTable->getCellRangeByPosition( xInfo->mnStartColumn, xInfo->mnStartRow, xInfo->mnEndColumn, xInfo->mnEndRow ) );
572 0 : Reference< XMergeableCellRange > xCursor( mxTable->createCursorByRange( xRange ), UNO_QUERY_THROW );
573 0 : xCursor->merge();
574 : }
575 0 : catch( Exception& )
576 : {
577 : OSL_FAIL("XMLTableImportContext::EndElement(), exception caught while merging cells!");
578 : }
579 0 : }
580 : }
581 2 : }
582 :
583 : // --------------------------------------------------------------------
584 :
585 16 : OUString XMLTableImportContext::GetDefaultCellStyleName() const
586 : {
587 16 : OUString sStyleName( msDefaultCellStyleName );
588 :
589 : // if there is still no style name, try default style name from column
590 16 : if( (sStyleName.isEmpty()) && (mnCurrentColumn < sal::static_int_cast<sal_Int32>(maColumnInfos.size())) )
591 9 : sStyleName = maColumnInfos[mnCurrentColumn]->msDefaultCellStyleName;
592 :
593 16 : return sStyleName;
594 : }
595 :
596 : // --------------------------------------------------------------------
597 : // XMLCellImportContext
598 : // --------------------------------------------------------------------
599 :
600 16 : XMLCellImportContext::XMLCellImportContext( SvXMLImport& rImport, const Reference< XMergeableCell >& xCell, const OUString& sDefaultCellStyleName, sal_uInt16 nPrfx, const OUString& rLName, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList )
601 : : SvXMLImportContext( rImport, nPrfx, rLName )
602 : , mxCell( xCell )
603 : , mbListContextPushed( false )
604 : , mnColSpan( 1 )
605 : , mnRowSpan( 1 )
606 16 : , mnRepeated( 1 )
607 : {
608 16 : OUString sStyleName;
609 :
610 : // read attributes for the table-cell
611 16 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
612 25 : for(sal_Int16 i=0; i < nAttrCount; i++)
613 : {
614 9 : const OUString sAttrName( xAttrList->getNameByIndex( i ) );
615 18 : const OUString sValue( xAttrList->getValueByIndex( i ) );
616 18 : OUString aLocalName;
617 :
618 9 : sal_uInt16 nPrefix2 = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
619 9 : if( XML_NAMESPACE_TABLE == nPrefix2 )
620 : {
621 9 : if( IsXMLToken( aLocalName, XML_NUMBER_COLUMNS_REPEATED ) )
622 : {
623 0 : mnRepeated = sValue.toInt32();
624 : }
625 9 : else if( IsXMLToken( aLocalName, XML_NUMBER_COLUMNS_SPANNED ) )
626 : {
627 0 : mnColSpan = sValue.toInt32();
628 : }
629 9 : else if( IsXMLToken( aLocalName, XML_NUMBER_ROWS_SPANNED ) )
630 : {
631 0 : mnRowSpan = sValue.toInt32();
632 : }
633 9 : else if( IsXMLToken( aLocalName, XML_STYLE_NAME ) )
634 : {
635 9 : sStyleName = sValue;
636 : }
637 : }
638 0 : else if ( (XML_NAMESPACE_XML == nPrefix2) &&
639 0 : IsXMLToken(aLocalName, XML_ID) )
640 : {
641 : (void) sValue;
642 : //FIXME: TODO
643 : }
644 : //FIXME: RDFa (table:table-cell)
645 9 : }
646 :
647 : // if there is no style name at the cell, try default style name from row
648 16 : if( sStyleName.isEmpty() )
649 7 : sStyleName = sDefaultCellStyleName;
650 :
651 16 : if( !sStyleName.isEmpty() )
652 : {
653 16 : SvXMLStylesContext * pAutoStyles = GetImport().GetShapeImport()->GetAutoStylesContext();
654 16 : if( pAutoStyles )
655 : {
656 : const XMLPropStyleContext* pStyle =
657 : dynamic_cast< const XMLPropStyleContext* >(
658 16 : pAutoStyles->FindStyleChildContext(XML_STYLE_FAMILY_TABLE_CELL, sStyleName) );
659 :
660 16 : if( pStyle )
661 : {
662 16 : Reference< XPropertySet > xCellSet( mxCell, UNO_QUERY );
663 16 : if( xCellSet.is() )
664 16 : const_cast< XMLPropStyleContext* >( pStyle )->FillPropertySet( xCellSet );
665 : }
666 : }
667 16 : }
668 16 : }
669 :
670 : // --------------------------------------------------------------------
671 :
672 32 : XMLCellImportContext::~XMLCellImportContext()
673 : {
674 32 : }
675 :
676 : // --------------------------------------------------------------------
677 :
678 0 : SvXMLImportContext * XMLCellImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
679 : {
680 : // create text cursor on demand
681 0 : if( !mxCursor.is() )
682 : {
683 0 : Reference< XText > xText( mxCell, UNO_QUERY );
684 0 : if( xText.is() )
685 : {
686 0 : UniReference < XMLTextImportHelper > xTxtImport( GetImport().GetTextImport() );
687 0 : mxOldCursor = xTxtImport->GetCursor();
688 0 : mxCursor = xText->createTextCursor();
689 0 : if( mxCursor.is() )
690 0 : xTxtImport->SetCursor( mxCursor );
691 :
692 : // remember old list item and block (#91964#) and reset them
693 : // for the text frame
694 0 : xTxtImport->PushListContext();
695 0 : mbListContextPushed = true;
696 0 : }
697 : }
698 :
699 0 : SvXMLImportContext * pContext = 0;
700 :
701 : // if we have a text cursor, lets try to import some text
702 0 : if( mxCursor.is() )
703 : {
704 0 : pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList );
705 : }
706 :
707 0 : if( pContext )
708 0 : return pContext;
709 : else
710 0 : return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList);
711 : }
712 :
713 : // --------------------------------------------------------------------
714 :
715 : // --------------------------------------------------------------------
716 :
717 16 : void XMLCellImportContext::EndElement()
718 : {
719 16 : if(mxCursor.is())
720 : {
721 : // delete addition newline
722 0 : const OUString aEmpty;
723 0 : mxCursor->gotoEnd( sal_False );
724 0 : mxCursor->goLeft( 1, sal_True );
725 0 : mxCursor->setString( aEmpty );
726 :
727 : // reset cursor
728 0 : GetImport().GetTextImport()->ResetCursor();
729 : }
730 :
731 16 : if(mxOldCursor.is())
732 0 : GetImport().GetTextImport()->SetCursor( mxOldCursor );
733 :
734 : // reinstall old list item (if necessary) #91964#
735 16 : if (mbListContextPushed) {
736 0 : GetImport().GetTextImport()->PopListContext();
737 : }
738 16 : }
739 :
740 : // --------------------------------------------------------------------
741 : // class XMLTableTemplateContext
742 : // --------------------------------------------------------------------
743 :
744 1 : XMLTableTemplateContext::XMLTableTemplateContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const Reference< XAttributeList >& xAttrList )
745 1 : : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XML_STYLE_FAMILY_TABLE_TEMPLATE_ID, sal_False )
746 : {
747 1 : }
748 :
749 : // --------------------------------------------------------------------
750 :
751 1 : void XMLTableTemplateContext::StartElement( const Reference< XAttributeList >& xAttrList )
752 : {
753 1 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
754 1 : for(sal_Int16 i=0; i < nAttrCount; i++)
755 : {
756 1 : OUString sAttrName;
757 1 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &sAttrName );
758 1 : if( (nAttrPrefix == XML_NAMESPACE_TEXT ) && IsXMLToken( sAttrName, XML_STYLE_NAME ) )
759 : {
760 1 : msTemplateStyleName = xAttrList->getValueByIndex( i );
761 1 : break;
762 : }
763 0 : }
764 1 : }
765 :
766 : // --------------------------------------------------------------------
767 :
768 1 : void XMLTableTemplateContext::EndElement()
769 : {
770 1 : rtl::Reference< XMLTableImport > xTableImport( GetImport().GetShapeImport()->GetShapeTableImport() );
771 1 : if( xTableImport.is() )
772 1 : xTableImport->addTableTemplate( msTemplateStyleName, maTableTemplate );
773 1 : }
774 :
775 : // --------------------------------------------------------------------
776 :
777 7 : SvXMLImportContext * XMLTableTemplateContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
778 : {
779 7 : if( nPrefix == XML_NAMESPACE_TABLE )
780 : {
781 7 : const TableStyleElement* pElements = getTableStyleMap();
782 40 : while( (pElements->meElement != XML_TOKEN_END) && !IsXMLToken( rLocalName, pElements->meElement ) )
783 26 : pElements++;
784 :
785 7 : if( pElements->meElement != XML_TOKEN_END )
786 : {
787 7 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
788 7 : for(sal_Int16 i=0; i < nAttrCount; i++)
789 : {
790 7 : OUString sAttrName;
791 7 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &sAttrName );
792 14 : if( (nAttrPrefix == XML_NAMESPACE_TEXT || nAttrPrefix == XML_NAMESPACE_TABLE) &&
793 7 : IsXMLToken( sAttrName, XML_STYLE_NAME ) )
794 : {
795 7 : maTableTemplate[pElements->msStyleName] = xAttrList->getValueByIndex( i );
796 7 : break;
797 : }
798 0 : }
799 : }
800 : }
801 :
802 7 : return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
803 : }
804 :
805 : // --------------------------------------------------------------------
806 :
807 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|