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