bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / table / XMLTableImport.cxx
blob40899a4fda846573c8e4e1a9b98aea6f94b3227d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
24 #include <com/sun/star/table/XTableRows.hpp>
25 #include <com/sun/star/table/XMergeableCell.hpp>
26 #include <com/sun/star/table/XMergeableCellRange.hpp>
27 #include <com/sun/star/table/XTable.hpp>
28 #include <com/sun/star/text/XText.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
32 #include <com/sun/star/style/XStyle.hpp>
33 #include <comphelper/sequence.hxx>
34 #include <comphelper/diagnose_ex.hxx>
36 #include <utility>
37 #include <xmloff/table/XMLTableImport.hxx>
38 #include <xmloff/xmltypes.hxx>
39 #include <xmloff/maptype.hxx>
40 #include <xmloff/xmlprmap.hxx>
41 #include <xmloff/txtimp.hxx>
42 #include <xmloff/xmlimp.hxx>
43 #include <xmloff/namespacemap.hxx>
44 #include <xmloff/xmlstyle.hxx>
45 #include <xmloff/prstylei.hxx>
47 #include <xmloff/xmlnamespace.hxx>
48 #include <xmloff/xmluconv.hxx>
49 #include "table.hxx"
51 #include <sal/log.hxx>
53 #include <memory>
55 using namespace ::xmloff::token;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::table;
59 using namespace ::com::sun::star::xml::sax;
60 using namespace ::com::sun::star::text;
61 using namespace ::com::sun::star::style;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::container;
65 namespace {
67 struct ColumnInfo
69 OUString msStyleName;
70 OUString msDefaultCellStyleName;
73 class XMLProxyContext : public SvXMLImportContext
75 public:
76 XMLProxyContext( SvXMLImport& rImport, SvXMLImportContextRef xParent );
78 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
79 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
81 private:
82 SvXMLImportContextRef mxParent;
85 struct MergeInfo
87 sal_Int32 mnStartColumn;
88 sal_Int32 mnStartRow;
89 sal_Int32 mnEndColumn;
90 sal_Int32 mnEndRow;
92 MergeInfo( sal_Int32 nStartColumn, sal_Int32 nStartRow, sal_Int32 nColumnSpan, sal_Int32 nRowSpan )
93 : mnStartColumn( nStartColumn ), mnStartRow( nStartRow ), mnEndColumn( nStartColumn + nColumnSpan - 1 ), mnEndRow( nStartRow + nRowSpan - 1 ) {};
96 class XMLCellImportPropertyMapper : public SvXMLImportPropertyMapper
98 public:
99 using SvXMLImportPropertyMapper::SvXMLImportPropertyMapper;
101 bool handleSpecialItem(
102 XMLPropertyState& rProperty,
103 std::vector< XMLPropertyState >& rProperties,
104 const OUString& rValue,
105 const SvXMLUnitConverter& rUnitConverter,
106 const SvXMLNamespaceMap& /*rNamespaceMap*/) const override
108 assert(getPropertySetMapper()->GetEntryXMLName(rProperty.mnIndex) == GetXMLToken(XML_BACKGROUND_COLOR));
109 (void)rProperty;
111 auto nIndex = getPropertySetMapper()->GetEntryIndex(XML_NAMESPACE_DRAW, GetXMLToken(XML_FILL), 0);
112 XMLPropertyState aFillProperty(nIndex);
114 if (IsXMLToken(rValue, XML_TRANSPARENT))
116 getPropertySetMapper()->importXML(GetXMLToken(XML_NONE), aFillProperty, rUnitConverter);
117 rProperties.push_back(aFillProperty);
119 else
121 getPropertySetMapper()->importXML(GetXMLToken(XML_SOLID), aFillProperty, rUnitConverter);
122 rProperties.push_back(aFillProperty);
124 nIndex = getPropertySetMapper()->GetEntryIndex(XML_NAMESPACE_DRAW, GetXMLToken(XML_FILL_COLOR), 0);
125 XMLPropertyState aColorProperty(nIndex);
126 getPropertySetMapper()->importXML(rValue, aColorProperty, rUnitConverter);
127 rProperties.push_back(aColorProperty);
130 return false;
136 class XMLTableImportContext : public SvXMLImportContext
138 public:
139 XMLTableImportContext( const rtl::Reference< XMLTableImport >& xThis, Reference< XColumnRowRange > const & xColumnRowRange );
141 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
142 sal_Int32 nElement,
143 const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
145 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
147 void InitColumns();
149 SvXMLImportContextRef ImportColumn( const Reference< XFastAttributeList >& xAttrList );
150 SvXMLImportContext * ImportRow( const Reference< XFastAttributeList >& xAttrList );
151 SvXMLImportContextRef ImportCell( sal_Int32 nElement, const Reference< XFastAttributeList >& xAttrList );
153 OUString GetDefaultCellStyleName() const;
155 css::uno::Reference< css::table::XTable > mxTable;
156 Reference< XTableColumns > mxColumns;
157 Reference< XTableRows > mxRows;
159 std::vector< std::shared_ptr< ColumnInfo > > maColumnInfos;
160 sal_Int32 mnCurrentRow;
161 sal_Int32 mnCurrentColumn;
163 // default cell style name for the current row
164 OUString msDefaultCellStyleName;
166 std::vector< std::shared_ptr< MergeInfo > > maMergeInfos;
169 namespace {
171 class XMLCellImportContext : public SvXMLImportContext
173 public:
174 XMLCellImportContext( SvXMLImport& rImport,
175 const Reference< XMergeableCell >& xCell,
176 const OUString& sDefaultCellStyleName,
177 sal_Int32 nElement,
178 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList );
180 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
181 sal_Int32 nElement,
182 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override;
184 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
186 sal_Int32 getColumnSpan() const { return mnColSpan; }
187 sal_Int32 getRowSpan() const { return mnRowSpan; }
188 sal_Int32 getRepeated() const { return mnRepeated; }
190 Reference< XMergeableCell > mxCell;
191 Reference< XTextCursor > mxCursor;
192 Reference< XTextCursor > mxOldCursor;
193 bool mbListContextPushed;
195 sal_Int32 mnColSpan, mnRowSpan, mnRepeated;
198 class XMLTableTemplateContext : public SvXMLStyleContext
200 public:
201 XMLTableTemplateContext( SvXMLImport& rImport );
203 // Create child element.
204 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
205 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
207 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
209 protected:
210 virtual void SetAttribute( sal_Int32 nElement,
211 const OUString& rValue ) override;
212 private:
213 XMLTableTemplate maTableTemplate;
214 OUString msTemplateStyleName;
220 XMLProxyContext::XMLProxyContext( SvXMLImport& rImport, SvXMLImportContextRef xParent )
221 : SvXMLImportContext( rImport )
222 , mxParent(std::move( xParent ))
226 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLProxyContext::createFastChildContext( sal_Int32 nElement, const Reference< XFastAttributeList >& xAttrList )
228 if( mxParent.is() )
229 return mxParent->createFastChildContext( nElement, xAttrList );
230 return nullptr;
234 XMLTableImport::XMLTableImport( SvXMLImport& rImport, const rtl::Reference< XMLPropertySetMapper >& xCellPropertySetMapper, const rtl::Reference< XMLPropertyHandlerFactory >& xFactoryRef )
235 : mrImport( rImport )
236 , mbWriter( false )
238 // check if called by Writer
239 Reference<XMultiServiceFactory> xFac(rImport.GetModel(), UNO_QUERY);
240 if (xFac.is()) try
242 Sequence<OUString> sSNS = xFac->getAvailableServiceNames();
243 mbWriter = comphelper::findValue(sSNS, "com.sun.star.style.TableStyle") != -1;
245 catch(const Exception&)
247 SAL_WARN("xmloff.table", "Error while checking available service names");
250 if (mbWriter)
252 mxCellImportPropertySetMapper = XMLTextImportHelper::CreateTableCellExtPropMapper(rImport);
254 else
256 mxCellImportPropertySetMapper = new SvXMLImportPropertyMapper( xCellPropertySetMapper, rImport );
257 mxCellImportPropertySetMapper->ChainImportMapper(XMLTextImportHelper::CreateParaExtPropMapper(rImport));
258 mxCellImportPropertySetMapper->ChainImportMapper(new XMLCellImportPropertyMapper(new XMLPropertySetMapper(getCellPropertiesMap(), xFactoryRef, true), rImport));
261 rtl::Reference < XMLPropertySetMapper > xRowMapper( new XMLPropertySetMapper( getRowPropertiesMap(), xFactoryRef, false ) );
262 mxRowImportPropertySetMapper = new SvXMLImportPropertyMapper( xRowMapper, rImport );
264 rtl::Reference < XMLPropertySetMapper > xColMapper( new XMLPropertySetMapper( getColumnPropertiesMap(), xFactoryRef, false ) );
265 mxColumnImportPropertySetMapper = new SvXMLImportPropertyMapper( xColMapper, rImport );
268 XMLTableImport::~XMLTableImport()
272 SvXMLImportContext* XMLTableImport::CreateTableContext( Reference< XColumnRowRange > const & xColumnRowRange )
274 rtl::Reference< XMLTableImport > xThis( this );
275 return new XMLTableImportContext( xThis, xColumnRowRange );
278 SvXMLStyleContext* XMLTableImport::CreateTableTemplateContext( sal_Int32 /*nElement*/, const Reference< XFastAttributeList >& /*xAttrList*/ )
280 return new XMLTableTemplateContext( mrImport );
283 void XMLTableImport::addTableTemplate( const OUString& rsStyleName, XMLTableTemplate& xTableTemplate )
285 auto xPtr = std::make_shared<XMLTableTemplate>();
286 xPtr->swap( xTableTemplate );
287 maTableTemplates.emplace_back(rsStyleName, xPtr);
290 void XMLTableImport::finishStyles()
292 if( maTableTemplates.empty() )
293 return;
297 Reference< XStyleFamiliesSupplier > xFamiliesSupp( mrImport.GetModel(), UNO_QUERY_THROW );
298 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
300 const OUString aTableFamily(mbWriter ? u"TableStyles" : u"table");
301 const OUString aCellFamily(mbWriter ? u"CellStyles" : u"cell");
302 Reference< XNameContainer > xTableFamily( xFamilies->getByName( aTableFamily ), UNO_QUERY_THROW );
303 Reference< XNameAccess > xCellFamily( xFamilies->getByName( aCellFamily ), UNO_QUERY_THROW );
305 Reference< XSingleServiceFactory > xFactory( xTableFamily, UNO_QUERY );
306 assert(xFactory.is() != mbWriter);
307 Reference< XMultiServiceFactory > xMultiFactory( mrImport.GetModel(), UNO_QUERY_THROW );
309 for( const auto& rTemplate : maTableTemplates ) try
311 const OUString sTemplateName( rTemplate.first );
312 Reference< XNameReplace > xTemplate(xFactory ? xFactory->createInstance() :
313 xMultiFactory->createInstance("com.sun.star.style.TableStyle"), UNO_QUERY_THROW);
315 std::shared_ptr< XMLTableTemplate > xT( rTemplate.second );
317 for( const auto& rStyle : *xT ) try
319 const OUString sPropName( rStyle.first );
320 const OUString sStyleName( mrImport.GetStyleDisplayName(XmlStyleFamily::TABLE_CELL, rStyle.second) );
321 xTemplate->replaceByName( sPropName, xCellFamily->getByName( sStyleName ) );
323 catch( Exception& )
325 TOOLS_WARN_EXCEPTION("xmloff.table", "");
328 if( xTemplate.is() )
330 if( xTableFamily->hasByName( sTemplateName ) )
331 xTableFamily->replaceByName( sTemplateName, Any( xTemplate ) );
332 else
333 xTableFamily->insertByName( sTemplateName, Any( xTemplate ) );
337 catch( Exception& )
339 TOOLS_WARN_EXCEPTION("xmloff.table", "");
342 catch( Exception& )
344 TOOLS_WARN_EXCEPTION("xmloff.table", "");
349 XMLTableImportContext::XMLTableImportContext( const rtl::Reference< XMLTableImport >& xImporter, Reference< XColumnRowRange > const & xColumnRowRange )
350 : SvXMLImportContext( xImporter->mrImport )
351 , mxTable( xColumnRowRange, UNO_QUERY )
352 , mxColumns( xColumnRowRange->getColumns() )
353 , mxRows( xColumnRowRange->getRows() )
354 , mnCurrentRow( -1 )
355 , mnCurrentColumn( -1 )
359 SvXMLImportContextRef XMLTableImportContext::ImportColumn( const Reference< XFastAttributeList >& xAttrList )
361 if( mxColumns.is() && (mnCurrentRow == -1) ) try
363 auto xInfo = std::make_shared<ColumnInfo>();
365 sal_Int32 nRepeated = 1;
367 // read attributes for the table-column
368 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
370 switch (aIter.getToken())
372 case XML_ELEMENT(TABLE, XML_NUMBER_COLUMNS_REPEATED):
373 nRepeated = aIter.toInt32();
374 break;
375 case XML_ELEMENT(TABLE, XML_STYLE_NAME):
376 xInfo->msStyleName = aIter.toString();
377 break;
378 case XML_ELEMENT(TABLE, XML_DEFAULT_CELL_STYLE_NAME):
379 xInfo->msDefaultCellStyleName = aIter.toString();
380 break;
381 case XML_ELEMENT(XML, XML_ID):
382 //FIXME: TODO
383 break;
387 if( nRepeated <= 1 )
389 maColumnInfos.push_back( xInfo );
391 else
393 maColumnInfos.insert( maColumnInfos.end(), nRepeated, xInfo );
396 catch( Exception& )
398 TOOLS_WARN_EXCEPTION("xmloff.table", "");
401 return nullptr;
404 void XMLTableImportContext::InitColumns()
406 if( !mxColumns.is() )
407 return;
411 const sal_Int32 nCount1 = mxColumns->getCount();
412 const sal_Int32 nCount2 = sal::static_int_cast< sal_Int32 >( maColumnInfos.size() );
413 if( nCount1 < nCount2 )
414 mxColumns->insertByIndex( nCount1, nCount2 - nCount1 );
416 SvXMLStylesContext * pAutoStyles = GetImport().GetShapeImport()->GetAutoStylesContext();
418 for( sal_Int32 nCol = 0; nCol < nCount2; nCol++ )
420 std::shared_ptr< ColumnInfo > xInfo( maColumnInfos[nCol] );
422 if( pAutoStyles && !xInfo->msStyleName.isEmpty() )
424 const XMLPropStyleContext* pStyle =
425 dynamic_cast< const XMLPropStyleContext* >(
426 pAutoStyles->FindStyleChildContext(XmlStyleFamily::TABLE_COLUMN, xInfo->msStyleName) );
428 if( pStyle )
430 Reference< XPropertySet > xColProps( mxColumns->getByIndex(nCol), UNO_QUERY_THROW );
431 const_cast< XMLPropStyleContext* >( pStyle )->FillPropertySet( xColProps );
437 catch( Exception& )
439 TOOLS_WARN_EXCEPTION("xmloff.table", "");
443 SvXMLImportContext * XMLTableImportContext::ImportRow( const Reference< XFastAttributeList >& xAttrList )
445 if( mxRows.is() )
447 mnCurrentRow++;
448 if( mnCurrentRow == 0 )
449 InitColumns(); // first init columns
451 mnCurrentColumn = -1;
453 const sal_Int32 nRowCount = mxRows->getCount();
454 if( ( nRowCount - 1) < mnCurrentRow )
456 const sal_Int32 nCount = mnCurrentRow - nRowCount + 1;
457 mxRows->insertByIndex( nRowCount, nCount );
460 Reference< XPropertySet > xRowSet( mxRows->getByIndex(mnCurrentRow), UNO_QUERY );
462 OUString sStyleName;
464 // read attributes for the table-row
465 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
467 switch(aIter.getToken())
469 case XML_ELEMENT(TABLE, XML_STYLE_NAME):
470 sStyleName = aIter.toString();
471 break;
472 case XML_ELEMENT(TABLE, XML_DEFAULT_CELL_STYLE_NAME):
473 msDefaultCellStyleName = aIter.toString();
474 break;
475 case XML_ELEMENT(XML, XML_ID):
476 //FIXME: TODO
477 break;
481 if( !sStyleName.isEmpty() )
483 SvXMLStylesContext * pAutoStyles = GetImport().GetShapeImport()->GetAutoStylesContext();
484 if( pAutoStyles )
486 const XMLPropStyleContext* pStyle =
487 dynamic_cast< const XMLPropStyleContext* >(
488 pAutoStyles->FindStyleChildContext(XmlStyleFamily::TABLE_ROW, sStyleName) );
490 if( pStyle )
492 const_cast< XMLPropStyleContext* >( pStyle )->FillPropertySet( xRowSet );
498 SvXMLImportContextRef xThis( this );
499 return new XMLProxyContext( GetImport(), xThis );
502 SvXMLImportContextRef XMLTableImportContext::ImportCell( sal_Int32 nElement, const Reference< XFastAttributeList >& xAttrList )
504 mnCurrentColumn++;
505 if( mxColumns.is() ) try
507 if( mxColumns->getCount() <= mnCurrentColumn )
508 mxColumns->insertByIndex( mxColumns->getCount(), mnCurrentColumn - mxColumns->getCount() + 1 );
510 Reference< XMergeableCell > xCell( mxTable->getCellByPosition( mnCurrentColumn, mnCurrentRow ), UNO_QUERY_THROW );
511 XMLCellImportContext* pCellContext = new XMLCellImportContext( GetImport(), xCell, GetDefaultCellStyleName(), nElement, xAttrList );
513 const sal_Int32 nColumnSpan = pCellContext->getColumnSpan();
514 const sal_Int32 nRowSpan = pCellContext->getRowSpan();
515 if( (nColumnSpan > 1) || (nRowSpan > 1) )
516 maMergeInfos.push_back( std::make_shared< MergeInfo >( mnCurrentColumn, mnCurrentRow, nColumnSpan, nRowSpan ) );
518 const sal_Int32 nRepeated = pCellContext->getRepeated();
519 if( nRepeated > 1 )
521 OSL_FAIL("xmloff::XMLTableImportContext::ImportCell(), import of repeated Cells not implemented (TODO)");
522 mnCurrentColumn += nRepeated - 1;
525 return pCellContext;
527 catch( Exception& )
529 TOOLS_WARN_EXCEPTION("xmloff.table", "");
532 return nullptr;
535 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableImportContext::createFastChildContext(
536 sal_Int32 nElement,
537 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
539 switch (nElement)
541 case XML_ELEMENT(TABLE, XML_TABLE_CELL):
542 case XML_ELEMENT(TABLE, XML_COVERED_TABLE_CELL):
543 return ImportCell( nElement, xAttrList );
544 case XML_ELEMENT(TABLE, XML_TABLE_COLUMN):
545 return ImportColumn( xAttrList );
546 case XML_ELEMENT(TABLE, XML_TABLE_ROW):
547 return ImportRow( xAttrList );
548 case XML_ELEMENT(TABLE, XML_TABLE_COLUMNS):
549 case XML_ELEMENT(TABLE, XML_TABLE_ROWS):
551 SvXMLImportContextRef xThis( this );
552 return new XMLProxyContext( GetImport(), xThis );
555 SAL_WARN("xmloff", "unknown element");
556 return nullptr;
559 void XMLTableImportContext::endFastElement(sal_Int32 )
561 for( const std::shared_ptr< MergeInfo >& xInfo : maMergeInfos )
563 if( xInfo ) try
565 Reference< XCellRange > xRange( mxTable->getCellRangeByPosition( xInfo->mnStartColumn, xInfo->mnStartRow, xInfo->mnEndColumn, xInfo->mnEndRow ) );
566 Reference< XMergeableCellRange > xCursor( mxTable->createCursorByRange( xRange ), UNO_QUERY_THROW );
567 xCursor->merge();
569 catch( Exception& )
571 TOOLS_WARN_EXCEPTION("xmloff.table", "");
576 OUString XMLTableImportContext::GetDefaultCellStyleName() const
578 OUString sStyleName( msDefaultCellStyleName );
580 // if there is still no style name, try default style name from column
581 if( (sStyleName.isEmpty()) && (mnCurrentColumn < sal::static_int_cast<sal_Int32>(maColumnInfos.size())) )
582 sStyleName = maColumnInfos[mnCurrentColumn]->msDefaultCellStyleName;
584 return sStyleName;
587 // XMLCellImportContext
589 XMLCellImportContext::XMLCellImportContext( SvXMLImport& rImport,
590 const Reference< XMergeableCell >& xCell,
591 const OUString& sDefaultCellStyleName,
592 sal_Int32 /*nElement*/,
593 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
594 : SvXMLImportContext( rImport )
595 , mxCell( xCell )
596 , mbListContextPushed( false )
597 , mnColSpan( 1 )
598 , mnRowSpan( 1 )
599 , mnRepeated( 1 )
601 OUString sStyleName;
603 // read attributes for the table-cell
604 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
606 switch (aIter.getToken())
608 case XML_ELEMENT(TABLE, XML_NUMBER_COLUMNS_REPEATED):
609 mnRepeated = aIter.toInt32();
610 break;
611 case XML_ELEMENT(TABLE, XML_NUMBER_COLUMNS_SPANNED):
612 mnColSpan = aIter.toInt32();
613 break;
614 case XML_ELEMENT(TABLE, XML_NUMBER_ROWS_SPANNED):
615 mnRowSpan = aIter.toInt32();
616 break;
617 case XML_ELEMENT(TABLE, XML_STYLE_NAME):
618 sStyleName = aIter.toString();
619 break;
620 case XML_ELEMENT(XML, XML_ID):
621 //FIXME: TODO
622 break;
623 //FIXME: RDFa (table:table-cell)
624 default:
625 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
629 // if there is no style name at the cell, try default style name from row
630 if( sStyleName.isEmpty() )
631 sStyleName = sDefaultCellStyleName;
633 if( sStyleName.isEmpty() )
634 return;
636 SvXMLStylesContext * pAutoStyles = GetImport().GetShapeImport()->GetAutoStylesContext();
637 if( !pAutoStyles )
638 return;
640 const XMLPropStyleContext* pStyle =
641 dynamic_cast< const XMLPropStyleContext* >(
642 pAutoStyles->FindStyleChildContext(XmlStyleFamily::TABLE_CELL, sStyleName) );
644 if( pStyle )
646 Reference< XPropertySet > xCellSet( mxCell, UNO_QUERY );
647 if( xCellSet.is() )
648 const_cast< XMLPropStyleContext* >( pStyle )->FillPropertySet( xCellSet );
652 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLCellImportContext::createFastChildContext(
653 sal_Int32 nElement,
654 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
656 // create text cursor on demand
657 if( !mxCursor.is() )
659 Reference< XText > xText( mxCell, UNO_QUERY );
660 if( xText.is() )
662 rtl::Reference < XMLTextImportHelper > xTxtImport( GetImport().GetTextImport() );
663 mxOldCursor = xTxtImport->GetCursor();
664 mxCursor = xText->createTextCursor();
665 if( mxCursor.is() )
666 xTxtImport->SetCursor( mxCursor );
668 // remember old list item and block (#91964#) and reset them
669 // for the text frame
670 xTxtImport->PushListContext();
671 mbListContextPushed = true;
675 SvXMLImportContext * pContext = nullptr;
677 // if we have a text cursor, lets try to import some text
678 if( mxCursor.is() )
680 pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nElement, xAttrList );
683 if (!pContext)
684 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
685 return pContext;
688 void XMLCellImportContext::endFastElement(sal_Int32 )
690 if(mxCursor.is())
692 // delete addition newline
693 mxCursor->gotoEnd( false );
694 mxCursor->goLeft( 1, true );
695 mxCursor->setString( "" );
697 // reset cursor
698 GetImport().GetTextImport()->ResetCursor();
701 if(mxOldCursor.is())
702 GetImport().GetTextImport()->SetCursor( mxOldCursor );
704 // reinstall old list item (if necessary) #91964#
705 if (mbListContextPushed) {
706 GetImport().GetTextImport()->PopListContext();
711 XMLTableTemplateContext::XMLTableTemplateContext( SvXMLImport& rImport )
712 : SvXMLStyleContext( rImport, XmlStyleFamily::TABLE_TEMPLATE_ID, false )
716 void XMLTableTemplateContext::SetAttribute( sal_Int32 nElement,
717 const OUString& rValue )
719 if( nElement == XML_ELEMENT(TEXT, XML_STYLE_NAME)
720 // Writer specific: according to oasis odf 1.2 prefix should be "table" and element name should be "name"
721 || nElement == XML_ELEMENT(TABLE, XML_NAME) )
723 msTemplateStyleName = rValue;
727 void XMLTableTemplateContext::endFastElement(sal_Int32 )
729 rtl::Reference< XMLTableImport > xTableImport( GetImport().GetShapeImport()->GetShapeTableImport() );
730 if( xTableImport.is() )
731 xTableImport->addTableTemplate( msTemplateStyleName, maTableTemplate );
734 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableTemplateContext::createFastChildContext(
735 sal_Int32 nElement,
736 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
738 if( IsTokenInNamespace(nElement, XML_NAMESPACE_TABLE) )
740 const TableStyleElement* pElements = getTableStyleMap();
741 sal_Int32 nLocalName = nElement & TOKEN_MASK;
742 while( (pElements->meElement != XML_TOKEN_END) && pElements->meElement != nLocalName)
743 pElements++;
745 if( pElements->meElement != XML_TOKEN_END )
747 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
749 switch (aIter.getToken())
751 case XML_ELEMENT(TEXT, XML_STYLE_NAME):
752 case XML_ELEMENT(TABLE, XML_STYLE_NAME):
753 maTableTemplate[pElements->msStyleName] = aIter.toString();
754 break;
755 default:
756 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
760 } else if (IsTokenInNamespace(nElement, XML_NAMESPACE_LO_EXT)) // Writer specific cell styles
762 const TableStyleElement* pElements = getWriterSpecificTableStyleMap();
763 sal_Int32 nLocalName = nElement & TOKEN_MASK;
764 while( (pElements->meElement != XML_TOKEN_END) && pElements->meElement != nLocalName)
765 pElements++;
767 if (pElements->meElement != XML_TOKEN_END)
769 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
771 switch (aIter.getToken())
773 case XML_ELEMENT(TEXT, XML_STYLE_NAME):
774 case XML_ELEMENT(TABLE, XML_STYLE_NAME):
775 maTableTemplate[pElements->msStyleName] = aIter.toString();
776 break;
777 default:
778 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
784 return nullptr;
787 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */