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 #include <StyleSheetTable.hxx>
24 #include <dmapper/DomainMapper.hxx>
25 #include <NumberingManager.hxx>
26 #include <ConversionHelper.hxx>
27 #include <TblStylePrHandler.hxx>
28 #include <BorderHandler.hxx>
29 #include <doctok/resourceids.hxx>
30 #include <ooxml/resourceids.hxx>
31 #include <vector>
32 #include <com/sun/star/beans/XMultiPropertySet.hpp>
33 #include <com/sun/star/beans/XPropertyState.hpp>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/text/XChapterNumberingSupplier.hpp>
37 #include <com/sun/star/text/XTextDocument.hpp>
38 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
39 #include <com/sun/star/style/XStyle.hpp>
40 #include <com/sun/star/style/ParagraphAdjust.hpp>
41 #include <com/sun/star/text/WritingMode.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <map>
44 #include <stdio.h>
45 #include <rtl/ustrbuf.hxx>
46 
47 #include <dmapperLoggers.hxx>
48 
49 using namespace ::com::sun::star;
50 namespace writerfilter {
51 namespace dmapper
52 {
53 
54 typedef ::std::map< ::rtl::OUString, ::rtl::OUString> StringPairMap_t;
55 
56 /*-- 21.06.2006 07:34:44---------------------------------------------------
57 
58   -----------------------------------------------------------------------*/
StyleSheetEntry()59 StyleSheetEntry::StyleSheetEntry() :
60         sStyleIdentifierI()
61         ,sStyleIdentifierD()
62         ,bIsDefaultStyle(false)
63         ,bInvalidHeight(false)
64         ,bHasUPE(false)
65         ,nStyleTypeCode(STYLE_TYPE_UNKNOWN)
66         ,sBaseStyleIdentifier()
67         ,sNextStyleIdentifier()
68         ,pProperties(new StyleSheetPropertyMap)
69 {
70 #if OSL_DEBUG_LEVEL > 1
71             nStyleTypeCode = STYLE_TYPE_PARA;
72 #endif
73 }
74 
~StyleSheetEntry()75 StyleSheetEntry::~StyleSheetEntry()
76 {
77 }
78 
TableStyleSheetEntry(StyleSheetEntry & rEntry,StyleSheetTable * pStyles)79 TableStyleSheetEntry::TableStyleSheetEntry( StyleSheetEntry& rEntry, StyleSheetTable* pStyles ):
80     StyleSheetEntry( ),
81     m_pStyleSheet( pStyles )
82 {
83 
84     bIsDefaultStyle = rEntry.bIsDefaultStyle;
85     bInvalidHeight = rEntry.bInvalidHeight;
86     bHasUPE = rEntry.bHasUPE;
87     nStyleTypeCode = STYLE_TYPE_TABLE;
88     sBaseStyleIdentifier = rEntry.sBaseStyleIdentifier;
89     sNextStyleIdentifier = rEntry.sNextStyleIdentifier;
90     sStyleName = rEntry.sStyleName;
91     sStyleName1 = rEntry.sStyleName1;
92 	sStyleIdentifierI = rEntry.sStyleIdentifierI;
93 	sStyleIdentifierD = rEntry.sStyleIdentifierD;
94 
95     m_nColBandSize = 1;
96     m_nRowBandSize = 1;
97 }
98 
~TableStyleSheetEntry()99 TableStyleSheetEntry::~TableStyleSheetEntry( )
100 {
101     m_pStyleSheet = NULL;
102 }
103 
AddTblStylePr(TblStyleType nType,PropertyMapPtr pProps)104 void TableStyleSheetEntry::AddTblStylePr( TblStyleType nType, PropertyMapPtr pProps )
105 {
106     static TblStyleType pTypesToFix[] =
107     {
108         TBL_STYLE_FIRSTROW,
109         TBL_STYLE_LASTROW,
110         TBL_STYLE_FIRSTCOL,
111         TBL_STYLE_LASTCOL
112     };
113 
114     static PropertyIds pPropsToCheck[] =
115     {
116         PROP_BOTTOM_BORDER,
117         PROP_TOP_BORDER,
118         PROP_RIGHT_BORDER,
119         PROP_LEFT_BORDER
120     };
121 
122     int i = 0;
123     while ( i < 4 )
124     {
125         if ( nType == pTypesToFix[i] )
126         {
127             PropertyIds nChecked = pPropsToCheck[i];
128             PropertyMap::iterator pCheckedIt = pProps->find( PropertyDefinition( nChecked, false )  );
129 
130             PropertyIds nInsideProp = ( i < 2 ) ? META_PROP_HORIZONTAL_BORDER : META_PROP_VERTICAL_BORDER;
131             PropertyMap::iterator pInsideIt = pProps->find( PropertyDefinition( nInsideProp, false )  );
132 
133             bool bHasChecked = pCheckedIt != pProps->end( );
134             bool bHasInside = pInsideIt != pProps->end( );
135 
136             if ( bHasChecked && bHasInside )
137             {
138                 // In this case, remove the inside border
139                 pProps->erase( pInsideIt );
140             }
141 
142             i = 4; // Stop looping stupidly
143         }
144         i++;
145     }
146 
147     // Append the tblStylePr
148     m_aStyles[nType] = pProps;
149 }
150 
GetProperties(sal_Int32 nMask,StyleSheetEntryDequePtr pStack)151 PropertyMapPtr TableStyleSheetEntry::GetProperties( sal_Int32 nMask, StyleSheetEntryDequePtr pStack )
152 {
153     PropertyMapPtr pProps( new PropertyMap );
154 
155     // First get the parent properties
156     StyleSheetEntryPtr pEntry = m_pStyleSheet->FindParentStyleSheet( sBaseStyleIdentifier );
157 
158     if ( pEntry.get( ) )
159     {
160         if (pStack.get() == NULL)
161             pStack.reset(new StyleSheetEntryDeque());
162 
163         StyleSheetEntryDeque::const_iterator aIt = find(pStack->begin(), pStack->end(), pEntry);
164 
165         if (aIt != pStack->end())
166         {
167             pStack->push_back(pEntry);
168 
169         TableStyleSheetEntry* pParent = static_cast<TableStyleSheetEntry *>( pEntry.get( ) );
170             pProps->insert( pParent->GetProperties( nMask ), (pStack.get() != NULL) );
171 
172             pStack->pop_back();
173     }
174     }
175 
176     // And finally get the mask ones
177     pProps->insert( GetLocalPropertiesFromMask( nMask ) );
178 
179     return pProps;
180 }
181 
lcl_mergeProps(PropertyMapPtr pToFill,PropertyMapPtr pToAdd,TblStyleType nStyleId)182 void lcl_mergeProps( PropertyMapPtr pToFill,  PropertyMapPtr pToAdd, TblStyleType nStyleId )
183 {
184     static PropertyIds pPropsToCheck[] =
185     {
186         PROP_BOTTOM_BORDER,
187         PROP_TOP_BORDER,
188         PROP_RIGHT_BORDER,
189         PROP_LEFT_BORDER,
190     };
191 
192     bool pRemoveInside[] =
193     {
194         ( nStyleId == TBL_STYLE_FIRSTROW ),
195         ( nStyleId == TBL_STYLE_LASTROW ),
196         ( nStyleId == TBL_STYLE_LASTCOL ),
197         ( nStyleId == TBL_STYLE_FIRSTCOL )
198     };
199 
200     for ( unsigned i = 0 ; i != sizeof(pPropsToCheck) / sizeof(PropertyIds); i++ )
201     {
202         PropertyIds nId = pPropsToCheck[i];
203         PropertyDefinition aProp( nId, false );
204         PropertyMap::iterator pIt = pToAdd->find( aProp );
205 
206         if ( pIt != pToAdd->end( ) )
207         {
208             PropertyMap::iterator pDestIt = pToFill->find( aProp );
209 
210             if ( pRemoveInside[i] )
211             {
212                 // Remove the insideH and insideV depending on the cell pos
213                 PropertyIds nInsideProp = ( i < 2 ) ? META_PROP_HORIZONTAL_BORDER : META_PROP_VERTICAL_BORDER;
214                 pDestIt = pToFill->find( PropertyDefinition( nInsideProp, false ) );
215                 if ( pDestIt != pToFill->end( ) )
216                     pToFill->erase( pDestIt );
217             }
218         }
219     }
220 
221     pToFill->insert( pToAdd );
222 }
223 
MergePropertiesFromMask(const short nBit,const sal_Int32 nMask,const TblStyleType nStyleId,PropertyMapPtr pToFill)224 void TableStyleSheetEntry::MergePropertiesFromMask(const short nBit, const sal_Int32 nMask,
225                                                    const TblStyleType nStyleId,
226                                                    PropertyMapPtr pToFill)
227 {
228     TblStylePrs::iterator pIt = m_aStyles.find( nStyleId );
229 
230     short nTestBit = 1 << nBit;
231     sal_Int32 nBitMask = sal_Int32( nTestBit );
232     if ( ( nMask & nBitMask ) && ( pIt != m_aStyles.end( ) ) )
233         lcl_mergeProps( pToFill, pIt->second, nStyleId );
234 }
235 
GetLocalPropertiesFromMask(const sal_Int32 nMask)236 PropertyMapPtr TableStyleSheetEntry::GetLocalPropertiesFromMask( const sal_Int32 nMask )
237 {
238     // Order from right to left
239     static TblStyleType aBitsOrder[] =
240     {
241         TBL_STYLE_SWCELL,
242         TBL_STYLE_SECELL,
243         TBL_STYLE_NWCELL,
244         TBL_STYLE_NECELL,
245         TBL_STYLE_BAND2HORZ,
246         TBL_STYLE_BAND1HORZ,
247         TBL_STYLE_BAND2VERT,
248         TBL_STYLE_BAND1VERT,
249         TBL_STYLE_LASTCOL,
250         TBL_STYLE_FIRSTCOL,
251         TBL_STYLE_LASTROW,
252         TBL_STYLE_FIRSTROW,
253         TBL_STYLE_UNKNOWN
254     };
255 
256     // Get the properties applying according to the mask
257     PropertyMapPtr pProps( new PropertyMap( ) );
258     short nBit = 4;
259     do
260     {
261         MergePropertiesFromMask(nBit, nMask, aBitsOrder[nBit], pProps);
262         nBit++;
263     }
264     while ( nBit < 13 );
265 
266     nBit = 0;
267     do
268     {
269         MergePropertiesFromMask(nBit, nMask, aBitsOrder[nBit], pProps);
270         nBit++;
271     }
272     while ( nBit < 4 );         // nw/ne/sw/se overwrite others
273 
274     return pProps;
275 }
276 
277 /*-- 06.02.2008 11:30:46---------------------------------------------------
278 
279   -----------------------------------------------------------------------*/
280 struct ListCharStylePropertyMap_t
281 {
282     ::rtl::OUString         sCharStyleName;
283     PropertyValueVector_t   aPropertyValues;
284 
ListCharStylePropertyMap_twriterfilter::dmapper::ListCharStylePropertyMap_t285     ListCharStylePropertyMap_t(const ::rtl::OUString& rCharStyleName, const PropertyValueVector_t& rPropertyValues):
286         sCharStyleName( rCharStyleName ),
287         aPropertyValues( rPropertyValues )
288         {}
289 };
290 typedef std::vector< ListCharStylePropertyMap_t > ListCharStylePropertyVector_t;
291 /*-- 19.06.2006 12:04:32---------------------------------------------------
292 
293   -----------------------------------------------------------------------*/
294 struct StyleSheetTable_Impl
295 {
296     DomainMapper&                           m_rDMapper;
297     uno::Reference< text::XTextDocument>    m_xTextDocument;
298     uno::Reference< beans::XPropertySet>    m_xTextDefaults;
299     std::vector< StyleSheetEntryPtr >       m_aStyleSheetEntries;
300     StyleSheetEntryPtr                      m_pCurrentEntry;
301     PropertyMapPtr                          m_pDefaultParaProps, m_pDefaultCharProps;
302     PropertyMapPtr                          m_pCurrentProps;
303     StringPairMap_t                         m_aStyleNameMap;
304     ListCharStylePropertyVector_t           m_aListCharStylePropertyVector;
305 
306 
307     StyleSheetTable_Impl(DomainMapper& rDMapper, uno::Reference< text::XTextDocument> xTextDocument);
308 
309     ::rtl::OUString HasListCharStyle( const PropertyValueVector_t& rCharProperties );
310 };
311 /*-- 15.11.2007 08:30:02---------------------------------------------------
312 
313   -----------------------------------------------------------------------*/
StyleSheetTable_Impl(DomainMapper & rDMapper,uno::Reference<text::XTextDocument> xTextDocument)314 StyleSheetTable_Impl::StyleSheetTable_Impl(DomainMapper& rDMapper, uno::Reference< text::XTextDocument> xTextDocument ) :
315             m_rDMapper( rDMapper ),
316             m_xTextDocument( xTextDocument ),
317             m_pCurrentEntry(),
318             m_pDefaultParaProps(new PropertyMap),
319             m_pDefaultCharProps(new PropertyMap)
320 {
321     //set font height default to 10pt
322     uno::Any aVal = uno::makeAny( double(10.) );
323     m_pDefaultCharProps->Insert( PROP_CHAR_HEIGHT, true, aVal );
324     m_pDefaultCharProps->Insert( PROP_CHAR_HEIGHT_ASIAN, true, aVal );
325     m_pDefaultCharProps->Insert( PROP_CHAR_HEIGHT_COMPLEX, true, aVal );
326 }
327 /*-- 06.02.2008 11:45:21---------------------------------------------------
328 
329   -----------------------------------------------------------------------*/
HasListCharStyle(const PropertyValueVector_t & rPropValues)330 ::rtl::OUString StyleSheetTable_Impl::HasListCharStyle( const PropertyValueVector_t& rPropValues )
331 {
332     ::rtl::OUString sRet;
333     ListCharStylePropertyVector_t::const_iterator aListVectorIter = m_aListCharStylePropertyVector.begin();
334     while( aListVectorIter != m_aListCharStylePropertyVector.end() )
335     {
336         //if size is identical
337         if( aListVectorIter->aPropertyValues.size() == rPropValues.size() )
338         {
339             bool bBreak = false;
340             //then search for all contained properties
341             PropertyValueVector_t::const_iterator aList1Iter = rPropValues.begin();
342             while( aList1Iter != rPropValues.end() && !bBreak)
343             {
344                 //find the property
345                 bool bElementFound = false;
346                 PropertyValueVector_t::const_iterator aList2Iter = aListVectorIter->aPropertyValues.begin();
347                 while( aList2Iter != aListVectorIter->aPropertyValues.end() && !bBreak )
348                 {
349                     if( aList2Iter->Name == aList1Iter->Name )
350                     {
351                         bElementFound = true;
352                         if( aList2Iter->Value != aList1Iter->Value )
353                             bBreak = true;
354                         break;
355                     }
356                     ++aList2Iter;
357                 }
358                 //set break flag if property hasn't been found
359                 if(!bElementFound )
360                 {
361                     bBreak = true;
362                     break;
363                 }
364                 ++aList1Iter;
365             }
366             if( !bBreak )
367                 return aListVectorIter->sCharStyleName;
368         }
369         ++aListVectorIter;
370     }
371     return sRet;
372 }
373 /*-- 19.06.2006 12:04:32---------------------------------------------------
374 
375   -----------------------------------------------------------------------*/
StyleSheetTable(DomainMapper & rDMapper,uno::Reference<text::XTextDocument> xTextDocument)376 StyleSheetTable::StyleSheetTable(DomainMapper& rDMapper, uno::Reference< text::XTextDocument> xTextDocument)
377 : LoggedProperties(dmapper_logger, "StyleSheetTable")
378 , LoggedTable(dmapper_logger, "StyleSheetTable")
379 , m_pImpl( new StyleSheetTable_Impl(rDMapper, xTextDocument) )
380 {
381 }
382 /*-- 19.06.2006 12:04:33---------------------------------------------------
383 
384   -----------------------------------------------------------------------*/
~StyleSheetTable()385 StyleSheetTable::~StyleSheetTable()
386 {
387     delete m_pImpl;
388 }
389 /*-- 19.06.2006 12:04:33---------------------------------------------------
390 
391   -----------------------------------------------------------------------*/
lcl_attribute(Id Name,Value & val)392 void StyleSheetTable::lcl_attribute(Id Name, Value & val)
393 {
394     OSL_ENSURE( m_pImpl->m_pCurrentEntry, "current entry has to be set here");
395     if(!m_pImpl->m_pCurrentEntry)
396         return ;
397     int nIntValue = val.getInt();
398     (void)nIntValue;
399     ::rtl::OUString sValue = val.getString();
400 //    printf ( "StyleSheetTable::attribute(0x%.4x, 0x%.4x) [%s]\n", (unsigned int)Name, (unsigned int)nIntValue, ::rtl::OUStringToOString(sValue, RTL_TEXTENCODING_DONTKNOW).getStr());
401     /* WRITERFILTERSTATUS: table: StyleSheetTable_attributedata */
402     switch(Name)
403     {
404         case NS_rtf::LN_ISTD:
405             /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
406             m_pImpl->m_pCurrentEntry->sStyleIdentifierD = ::rtl::OUString::valueOf(static_cast<sal_Int32>(nIntValue), 16);
407         break;
408         case NS_rtf::LN_STI:
409         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
410         {
411             ::rtl::OUString tempStyleIdentifier = GetStyleIdFromIndex(static_cast<sal_uInt32>(nIntValue));
412             if (tempStyleIdentifier.getLength())
413                 m_pImpl->m_pCurrentEntry->sStyleIdentifierI = tempStyleIdentifier;
414             if (nIntValue == 0 || nIntValue == 65)
415                 m_pImpl->m_pCurrentEntry->bIsDefaultStyle = true;
416         }
417         break;
418         case NS_rtf::LN_SGC:
419         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
420             m_pImpl->m_pCurrentEntry->nStyleTypeCode = (StyleType)nIntValue;
421         break;
422         case NS_rtf::LN_ISTDBASE:
423         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
424             if (static_cast<sal_uInt32>(nIntValue) != 0xfff)
425                 m_pImpl->m_pCurrentEntry->sBaseStyleIdentifier = ::rtl::OUString::valueOf(static_cast<sal_Int32>(nIntValue), 16);
426         break;
427         case NS_rtf::LN_ISTDNEXT:
428         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
429             if (static_cast<sal_uInt32>(nIntValue) != 0xfff)
430                 m_pImpl->m_pCurrentEntry->sNextStyleIdentifier = ::rtl::OUString::valueOf(static_cast<sal_Int32>(nIntValue), 16);
431         break;
432         case NS_rtf::LN_FSCRATCH:
433         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
434         case NS_rtf::LN_FINVALHEIGHT:
435         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
436         case NS_rtf::LN_FHASUPE:
437         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
438         case NS_rtf::LN_FMASSCOPY:
439         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
440         case NS_rtf::LN_CUPX:
441         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
442         case NS_rtf::LN_BCHUPE:
443         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
444         case NS_rtf::LN_FAUTOREDEF:
445         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
446         case NS_rtf::LN_FHIDDEN:
447         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
448         case NS_rtf::LN_UNUSED8_3:
449         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
450             //noone seems to care about it
451         break;
452         case NS_rtf::LN_XSTZNAME:
453         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
454             m_pImpl->m_pCurrentEntry->sStyleName1 = sValue;
455             if (m_pImpl->m_pCurrentEntry->sStyleIdentifierI.getLength())
456                 m_pImpl->m_pCurrentEntry->sStyleIdentifierI = sValue;
457         break;
458         case NS_rtf::LN_XSTZNAME1:
459         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
460             m_pImpl->m_pCurrentEntry->sStyleName = sValue;
461             if (m_pImpl->m_pCurrentEntry->sStyleIdentifierI.getLength())
462                 m_pImpl->m_pCurrentEntry->sStyleIdentifierI = sValue;
463         break;
464         case NS_rtf::LN_UPX:
465         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
466             resolveAttributeProperties(val);
467         break;
468         case NS_ooxml::LN_CT_Style_type:
469         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
470         {
471 /*          defaults should be set at the service "com.sun.star.text.Defaults"
472              if (nIntValue == 1)
473                 *(m_pImpl->m_pCurrentEntry->pProperties) = *(m_pImpl->m_pDefaultParaProps);
474             else if (nIntValue == 2)
475                 *(m_pImpl->m_pCurrentEntry->pProperties) = *(m_pImpl->m_pDefaultCharProps);*/
476             StyleType nType = ( StyleType ) nIntValue;
477             if ( nType == STYLE_TYPE_TABLE )
478             {
479                 StyleSheetEntryPtr pEntry = m_pImpl->m_pCurrentEntry;
480                 TableStyleSheetEntryPtr pTableEntry( new TableStyleSheetEntry( *pEntry.get( ), this ) );
481                 m_pImpl->m_pCurrentEntry = pTableEntry;
482             }
483             else
484                 m_pImpl->m_pCurrentEntry->nStyleTypeCode = (StyleType)nIntValue;
485         }
486         break;
487         case NS_ooxml::LN_CT_Style_default:
488         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
489             m_pImpl->m_pCurrentEntry->bIsDefaultStyle = (nIntValue != 0);
490         break;
491         case NS_ooxml::LN_CT_Style_customStyle:
492         /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
493         break;
494         case NS_ooxml::LN_CT_Style_styleId:
495         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
496             m_pImpl->m_pCurrentEntry->sStyleIdentifierI = sValue;
497             m_pImpl->m_pCurrentEntry->sStyleIdentifierD = sValue;
498         break;
499         case NS_ooxml::LN_CT_TblWidth_w:
500         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
501             dynamic_cast< StyleSheetPropertyMap* >( m_pImpl->m_pCurrentEntry->pProperties.get() )->SetCT_TblWidth_w( nIntValue );
502         break;
503         case NS_ooxml::LN_CT_TblWidth_type:
504         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
505             dynamic_cast< StyleSheetPropertyMap* >( m_pImpl->m_pCurrentEntry->pProperties.get() )->SetCT_TblWidth_type( nIntValue );
506         break;
507         default:
508         {
509 #ifdef DEBUG_DOMAINMAPPER
510             dmapper_logger->element("unhandled");
511 #endif
512         }
513         break;
514     }
515 }
516 /*-- 19.06.2006 12:04:33---------------------------------------------------
517 
518   -----------------------------------------------------------------------*/
lcl_sprm(Sprm & rSprm)519 void StyleSheetTable::lcl_sprm(Sprm & rSprm)
520 {
521     sal_uInt32 nSprmId = rSprm.getId();
522     Value::Pointer_t pValue = rSprm.getValue();
523     sal_Int32 nIntValue = pValue.get() ? pValue->getInt() : 0;
524     (void)nIntValue;
525     rtl::OUString sStringValue = pValue.get() ? pValue->getString() : rtl::OUString();
526     //printf ( "StyleSheetTable::sprm(0x%.4x, 0x%.4x) [%s]\n", (unsigned int)nSprmId, (unsigned int)nIntValue, ::rtl::OUStringToOString(sStringValue, RTL_TEXTENCODING_DONTKNOW).getStr());
527 
528     /* WRITERFILTERSTATUS: table: StyleSheetTable_sprm */
529     switch(nSprmId)
530     {
531         case NS_ooxml::LN_CT_Style_name:
532         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
533             //this is only a UI name!
534             m_pImpl->m_pCurrentEntry->sStyleName = sStringValue;
535             m_pImpl->m_pCurrentEntry->sStyleName1 = sStringValue;
536             break;
537         case NS_ooxml::LN_CT_Style_basedOn:
538         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
539             m_pImpl->m_pCurrentEntry->sBaseStyleIdentifier = sStringValue;
540             break;
541         case NS_ooxml::LN_CT_Style_next:
542         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
543             m_pImpl->m_pCurrentEntry->sNextStyleIdentifier = sStringValue;
544             break;
545         case NS_ooxml::LN_CT_Style_aliases:
546         case NS_ooxml::LN_CT_Style_link:
547         case NS_ooxml::LN_CT_Style_autoRedefine:
548         case NS_ooxml::LN_CT_Style_hidden:
549         case NS_ooxml::LN_CT_Style_uiPriority:
550         case NS_ooxml::LN_CT_Style_semiHidden:
551         case NS_ooxml::LN_CT_Style_unhideWhenUsed:
552         case NS_ooxml::LN_CT_Style_qFormat:
553         case NS_ooxml::LN_CT_Style_locked:
554         case NS_ooxml::LN_CT_Style_personal:
555         case NS_ooxml::LN_CT_Style_personalCompose:
556         case NS_ooxml::LN_CT_Style_personalReply:
557         case NS_ooxml::LN_CT_Style_rsid:
558         case NS_ooxml::LN_CT_Style_trPr:
559             /* WRITERFILTERSTATUS: done: 0, planned: 0, spent: 0 */
560         break;
561         case NS_ooxml::LN_CT_Style_tcPr:
562         {
563             resolveSprmProps(rSprm);
564         }
565         break;
566         case NS_ooxml::LN_CT_Style_tblPr: //contains table properties
567         case NS_ooxml::LN_CT_Style_tblStylePr: //contains  to table properties
568 
569         case NS_ooxml::LN_CT_TblPrBase_tblInd: //table properties - at least width value and type
570         case NS_ooxml::LN_EG_RPrBase_rFonts: //table fonts
571         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
572         {
573             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
574             if( pProperties.get())
575             {
576                 TblStylePrHandlerPtr pTblStylePrHandler( new TblStylePrHandler( m_pImpl->m_rDMapper ) );
577                 pProperties->resolve( *pTblStylePrHandler );
578 
579                 // Add the properties to the table style
580                 TblStyleType nType = pTblStylePrHandler->getType( );
581                 PropertyMapPtr pProps = pTblStylePrHandler->getProperties( );
582                 StyleSheetEntry *  pEntry = m_pImpl->m_pCurrentEntry.get();
583 
584                 if (nType == TBL_STYLE_UNKNOWN)
585                 {
586                     pEntry->pProperties->insert(pProps);
587                 }
588                 else
589                 {
590                     TableStyleSheetEntry * pTableEntry = dynamic_cast<TableStyleSheetEntry*>( pEntry );
591                     if (pTableEntry != NULL)
592                         pTableEntry->AddTblStylePr( nType, pProps );
593                 }
594             }
595             break;
596         }
597         case NS_ooxml::LN_CT_PPrDefault_pPr:
598         case NS_ooxml::LN_CT_DocDefaults_pPrDefault:
599         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
600             m_pImpl->m_rDMapper.PushStyleSheetProperties( m_pImpl->m_pDefaultParaProps );
601             m_pImpl->m_rDMapper.sprm( rSprm );
602             m_pImpl->m_rDMapper.PopStyleSheetProperties();
603             applyDefaults( true );
604         break;
605         case NS_ooxml::LN_CT_RPrDefault_rPr:
606         case NS_ooxml::LN_CT_DocDefaults_rPrDefault:
607         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
608             m_pImpl->m_rDMapper.PushStyleSheetProperties( m_pImpl->m_pDefaultCharProps );
609             m_pImpl->m_rDMapper.sprm( rSprm );
610             m_pImpl->m_rDMapper.PopStyleSheetProperties();
611             applyDefaults( false );
612         break;
613         case NS_ooxml::LN_CT_TblPrBase_jc:     //table alignment - row properties!
614         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
615              m_pImpl->m_pCurrentEntry->pProperties->Insert( PROP_HORI_ORIENT, false,
616                 uno::makeAny( ConversionHelper::convertTableJustification( nIntValue )));
617         break;
618         case NS_ooxml::LN_CT_TrPrBase_jc:     //table alignment - row properties!
619         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
620                 dynamic_cast< StyleSheetPropertyMap* >( m_pImpl->m_pCurrentEntry->pProperties.get() )->SetCT_TrPrBase_jc(nIntValue);
621         break;
622         case NS_ooxml::LN_CT_TblPrBase_tblBorders: //table borders, might be defined in table style
623         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
624         {
625             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
626             if( pProperties.get())
627             {
628                 BorderHandlerPtr pBorderHandler( new BorderHandler(m_pImpl->m_rDMapper.IsOOXMLImport()) );
629                 pProperties->resolve(*pBorderHandler);
630                 m_pImpl->m_pCurrentEntry->pProperties->insert( pBorderHandler->getProperties(), true );
631             }
632         }
633         break;
634         case NS_ooxml::LN_CT_TblPrBase_tblStyleRowBandSize:
635         case NS_ooxml::LN_CT_TblPrBase_tblStyleColBandSize:
636         {
637             StyleSheetEntry* pEntry = m_pImpl->m_pCurrentEntry.get( );
638             TableStyleSheetEntry *pTEntry = static_cast<TableStyleSheetEntry*>( pEntry );
639             if ( pTEntry )
640             {
641                 if ( nSprmId == NS_ooxml::LN_CT_TblPrBase_tblStyleRowBandSize )
642                     pTEntry->m_nRowBandSize = nIntValue;
643                 else
644                     pTEntry->m_nColBandSize = nIntValue;
645             }
646         }
647         break;
648         case NS_ooxml::LN_CT_TblPrBase_tblCellMar:
649             //no cell margins in styles
650         break;
651         case NS_ooxml::LN_CT_Style_pPr:
652         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
653             // no break
654         case NS_ooxml::LN_CT_Style_rPr:
655         /* WRITERFILTERSTATUS: done: 100, planned: 0, spent: 0 */
656             // no break
657         default:
658             {
659                 if (!m_pImpl->m_pCurrentEntry)
660                     break;
661 
662                 TablePropertiesHandlerPtr pTblHandler( new TablePropertiesHandler( true ) );
663                 pTblHandler->SetProperties( m_pImpl->m_pCurrentEntry->pProperties );
664                 if ( !pTblHandler->sprm( rSprm ) )
665                 {
666                     m_pImpl->m_rDMapper.PushStyleSheetProperties( m_pImpl->m_pCurrentEntry->pProperties );
667 
668                     PropertyMapPtr pProps(new PropertyMap());
669                     m_pImpl->m_rDMapper.sprmWithProps( rSprm, pProps );
670 
671                     m_pImpl->m_pCurrentEntry->pProperties->insert(pProps);
672 
673                     m_pImpl->m_rDMapper.PopStyleSheetProperties( );
674                 }
675             }
676             break;
677 }
678 }
679 /*-- 19.06.2006 12:04:33---------------------------------------------------
680 
681   -----------------------------------------------------------------------*/
lcl_entry(int,writerfilter::Reference<Properties>::Pointer_t ref)682 void StyleSheetTable::lcl_entry(int /*pos*/, writerfilter::Reference<Properties>::Pointer_t ref)
683 {
684     //create a new style entry
685     // printf("StyleSheetTable::entry(...)\n");
686     OSL_ENSURE( !m_pImpl->m_pCurrentEntry, "current entry has to be NULL here");
687     StyleSheetEntryPtr pNewEntry( new StyleSheetEntry );
688     m_pImpl->m_pCurrentEntry = pNewEntry;
689     m_pImpl->m_rDMapper.PushStyleSheetProperties( m_pImpl->m_pCurrentEntry->pProperties );
690     ref->resolve(*this);
691     //append it to the table
692     m_pImpl->m_rDMapper.PopStyleSheetProperties();
693     if( !m_pImpl->m_rDMapper.IsOOXMLImport() || m_pImpl->m_pCurrentEntry->sStyleName.getLength() >0)
694     {
695         m_pImpl->m_pCurrentEntry->sConvertedStyleName = ConvertStyleName( m_pImpl->m_pCurrentEntry->sStyleName );
696         m_pImpl->m_aStyleSheetEntries.push_back( m_pImpl->m_pCurrentEntry );
697     }
698     else
699     {
700         //TODO: this entry contains the default settings - they have to be added to the settings
701     }
702 
703     StyleSheetEntryPtr pEmptyEntry;
704     m_pImpl->m_pCurrentEntry = pEmptyEntry;
705 }
706 /*-- 21.06.2006 15:34:49---------------------------------------------------
707     sorting helper
708   -----------------------------------------------------------------------*/
709 typedef std::vector< beans::PropertyValue > _PropValVector;
710 class PropValVector : public _PropValVector
711 {
712 public:
PropValVector()713     PropValVector(){}
714 
715     void    Insert( beans::PropertyValue aVal );
716     uno::Sequence< uno::Any > getValues();
717     uno::Sequence< ::rtl::OUString > getNames();
718 };
Insert(beans::PropertyValue aVal)719 void    PropValVector::Insert( beans::PropertyValue aVal )
720 {
721     _PropValVector::iterator aIt = begin();
722     while(aIt != end())
723     {
724         if(aIt->Name > aVal.Name)
725         {
726             insert( aIt, aVal );
727             return;
728         }
729         ++aIt;
730     }
731     push_back( aVal );
732 }
getValues()733 uno::Sequence< uno::Any > PropValVector::getValues()
734 {
735     uno::Sequence< uno::Any > aRet( size() );
736     uno::Any* pValues = aRet.getArray();
737     sal_Int32 nVal = 0;
738     _PropValVector::iterator aIt = begin();
739     while(aIt != end())
740     {
741         pValues[nVal++] = aIt->Value;
742         ++aIt;
743     }
744     return aRet;
745 }
getNames()746 uno::Sequence< ::rtl::OUString > PropValVector::getNames()
747 {
748     uno::Sequence< ::rtl::OUString > aRet( size() );
749     ::rtl::OUString* pNames = aRet.getArray();
750     sal_Int32 nVal = 0;
751     _PropValVector::iterator aIt = begin();
752     while(aIt != end())
753     {
754         pNames[nVal++] = aIt->Name;
755         ++aIt;
756     }
757     return aRet;
758 }
759 /*-- 21.06.2006 13:35:48---------------------------------------------------
760 
761   -----------------------------------------------------------------------*/
ApplyStyleSheets(FontTablePtr rFontTable)762 void StyleSheetTable::ApplyStyleSheets( FontTablePtr rFontTable )
763 {
764     try
765     {
766         uno::Reference< style::XStyleFamiliesSupplier > xStylesSupplier( m_pImpl->m_xTextDocument, uno::UNO_QUERY_THROW );
767         uno::Reference< lang::XMultiServiceFactory > xDocFactory( m_pImpl->m_xTextDocument, uno::UNO_QUERY_THROW );
768         uno::Reference< container::XNameAccess > xStyleFamilies = xStylesSupplier->getStyleFamilies();
769         uno::Reference<container::XNameContainer> xCharStyles;
770         uno::Reference<container::XNameContainer> xParaStyles;
771 
772         PropertyNameSupplier& rPropNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
773         xStyleFamilies->getByName(rPropNameSupplier.GetName( PROP_CHARACTER_STYLES )) >>= xCharStyles;
774         xStyleFamilies->getByName(rPropNameSupplier.GetName( PROP_PARAGRAPH_STYLES )) >>= xParaStyles;
775         if(xCharStyles.is() && xParaStyles.is())
776         {
777             std::vector< StyleSheetEntryPtr >::iterator aIt = m_pImpl->m_aStyleSheetEntries.begin();
778             while( aIt != m_pImpl->m_aStyleSheetEntries.end() )
779             {
780                 StyleSheetEntryPtr pEntry = *aIt;
781                 if( pEntry->nStyleTypeCode == STYLE_TYPE_CHAR || pEntry->nStyleTypeCode == STYLE_TYPE_PARA )
782                 {
783                     bool bParaStyle = pEntry->nStyleTypeCode == STYLE_TYPE_PARA;
784                     bool bInsert = false;
785                     uno::Reference< container::XNameContainer > xStyles = bParaStyle ? xParaStyles : xCharStyles;
786                     uno::Reference< style::XStyle > xStyle;
787                     ::rtl::OUString sConvertedStyleName = ConvertStyleName( pEntry->sStyleName );
788                     if(xStyles->hasByName( sConvertedStyleName ))
789                         xStyles->getByName( sConvertedStyleName ) >>= xStyle;
790                     else
791                     {
792                         bInsert = true;
793                         xStyle = uno::Reference< style::XStyle >(xDocFactory->createInstance(
794                                     bParaStyle ?
795                                         rPropNameSupplier.GetName( PROP_SERVICE_PARA_STYLE ) :
796                                         rPropNameSupplier.GetName( PROP_SERVICE_CHAR_STYLE )),
797                                         uno::UNO_QUERY_THROW);
798                     }
799                     if( pEntry->sBaseStyleIdentifier.getLength() )
800                     {
801                         try
802                         {
803                             //TODO: Handle cases where a paragraph <> character style relation is needed
804                             StyleSheetEntryPtr pParent = FindStyleSheetByISTD( pEntry->sBaseStyleIdentifier );
805                             if (pParent.get() != NULL)
806                                 xStyle->setParentStyle(ConvertStyleName( pParent->sStyleName ));
807                         }
808                         catch( const uno::RuntimeException& )
809                         {
810                             OSL_ENSURE( false, "Styles parent could not be set");
811                         }
812                     }
813                     else if( bParaStyle )
814                     {
815                         //now it's time to set the default parameters - for paragraph styles
816                         //Fonts: Western first entry in font table
817                         //CJK: second entry
818                         //CTL: third entry, if it exists
819 
820                         sal_uInt32 nFontCount = rFontTable->size();
821                         if( !m_pImpl->m_rDMapper.IsOOXMLImport() && nFontCount > 2 )
822                         {
823                             uno::Any aTwoHundredFortyTwip = uno::makeAny(12.);
824     //                      font size to 240 twip (12 pts) for all if not set
825                             pEntry->pProperties->Insert(PROP_CHAR_HEIGHT, true, aTwoHundredFortyTwip, false);
826     //                      western font not already set -> apply first font
827                             const FontEntry::Pointer_t pWesternFontEntry(rFontTable->getFontEntry( 0 ));
828                             rtl::OUString sWesternFontName = pWesternFontEntry->sFontName;
829                             pEntry->pProperties->Insert(PROP_CHAR_FONT_NAME, true, uno::makeAny( sWesternFontName ), false);
830 
831     //                      CJK  ... apply second font
832                             const FontEntry::Pointer_t pCJKFontEntry(rFontTable->getFontEntry( 2 ));
833                             pEntry->pProperties->Insert(PROP_CHAR_FONT_NAME_ASIAN, true, uno::makeAny( pCJKFontEntry->sFontName ), false);
834                             pEntry->pProperties->Insert(PROP_CHAR_HEIGHT_ASIAN, true, aTwoHundredFortyTwip, false);
835     //                      CTL  ... apply third font, if available
836                             if( nFontCount > 3 )
837                             {
838                                 const FontEntry::Pointer_t pCTLFontEntry(rFontTable->getFontEntry( 3 ));
839                                 pEntry->pProperties->Insert(PROP_CHAR_FONT_NAME_COMPLEX, true, uno::makeAny( pCTLFontEntry->sFontName ), false);
840                                 pEntry->pProperties->Insert(PROP_CHAR_HEIGHT_COMPLEX, true, aTwoHundredFortyTwip, false);
841                             }
842                         }
843     //                  Widow/Orphan -> set both to two if not already set
844                         uno::Any aTwo = uno::makeAny(sal_Int8(2));
845                         pEntry->pProperties->Insert(PROP_PARA_WIDOWS, true, aTwo, false);
846                         pEntry->pProperties->Insert(PROP_PARA_ORPHANS, true, aTwo, false);
847     //                  Left-to-right direction if not already set
848                         pEntry->pProperties->Insert(PROP_WRITING_MODE, true, uno::makeAny( sal_Int16(text::WritingMode_LR_TB) ), false);
849     //                  font color COL_AUTO if not already set
850                         pEntry->pProperties->Insert(PROP_CHAR_COLOR, true, uno::makeAny( sal_Int32(0xffffffff) ), false);
851                     }
852 
853                     uno::Sequence< beans::PropertyValue > aPropValues = pEntry->pProperties->GetPropertyValues();
854                     bool bAddFollowStyle = false;
855                     if(bParaStyle && !pEntry->sNextStyleIdentifier.getLength() )
856                     {
857                             bAddFollowStyle = true;
858                     }
859                     //remove Left/RightMargin values from TOX heading styles
860                     if( bParaStyle )
861                     {
862                         // Set the outline levels
863                         const StyleSheetPropertyMap* pStyleSheetProperties = dynamic_cast<const StyleSheetPropertyMap*>(pEntry ? pEntry->pProperties.get() : 0);
864                         if ( pStyleSheetProperties )
865                         {
866                             aPropValues.realloc( aPropValues.getLength( ) + 1 );
867 
868                             beans::PropertyValue aLvlVal( rPropNameSupplier.GetName( PROP_OUTLINE_LEVEL ), 0,
869                                     uno::makeAny( sal_Int16( pStyleSheetProperties->GetOutlineLevel( ) ) ),
870                                     beans::PropertyState_DIRECT_VALUE );
871                             aPropValues[ aPropValues.getLength( ) - 1 ] = aLvlVal;
872 
873                             if ( pStyleSheetProperties->GetOutlineLevel( ) == 0 )
874                             {
875                                 aPropValues.realloc( aPropValues.getLength( ) + 1 );
876                                 beans::PropertyValue aStyleVal( rPropNameSupplier.GetName( PROP_NUMBERING_STYLE_NAME ), 0,
877                                         uno::makeAny( rtl::OUString::createFromAscii( "" ) ),
878                                         beans::PropertyState_DIRECT_VALUE );
879                                 aPropValues[ aPropValues.getLength( ) - 1 ] = aStyleVal;
880                             }
881                         }
882 
883                         uno::Reference< beans::XPropertyState >xState( xStyle, uno::UNO_QUERY_THROW );
884                         if( sConvertedStyleName.equalsAscii( "Contents Heading" ) ||
885                             sConvertedStyleName.equalsAscii( "User Index Heading" ) ||
886                             sConvertedStyleName.equalsAscii( "Index Heading" ))
887                         {
888                             //left margin is set to NULL by default
889                             uno::Reference< beans::XPropertyState >xState1( xStyle, uno::UNO_QUERY_THROW );
890                             xState1->setPropertyToDefault(rPropNameSupplier.GetName( PROP_PARA_LEFT_MARGIN ));
891                         }
892                         else if( sConvertedStyleName.equalsAscii( "Text body" ) )
893                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_PARA_BOTTOM_MARGIN ));
894                         else if( sConvertedStyleName.equalsAscii( "Heading 1" ) ||
895                                 sConvertedStyleName.equalsAscii( "Heading 2" ) ||
896                                 sConvertedStyleName.equalsAscii( "Heading 3" ) ||
897                                 sConvertedStyleName.equalsAscii( "Heading 4" ) ||
898                                 sConvertedStyleName.equalsAscii( "Heading 5" ) ||
899                                 sConvertedStyleName.equalsAscii( "Heading 6" ) ||
900                                 sConvertedStyleName.equalsAscii( "Heading 7" ) ||
901                                 sConvertedStyleName.equalsAscii( "Heading 8" ) ||
902                                 sConvertedStyleName.equalsAscii( "Heading 9" ) )
903                         {
904                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_WEIGHT ));
905                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_WEIGHT_ASIAN ));
906                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_WEIGHT_COMPLEX ));
907                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_POSTURE ));
908                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_POSTURE_ASIAN ));
909                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_POSTURE_COMPLEX ));
910                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_PROP_HEIGHT        ));
911                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_PROP_HEIGHT_ASIAN  ));
912                             xState->setPropertyToDefault(rPropNameSupplier.GetName( PROP_CHAR_PROP_HEIGHT_COMPLEX));
913 
914                         } else if(sConvertedStyleName.equalsAscii( "Title" ) ||
915 								sConvertedStyleName.equalsAscii( "Subtitle" ))	{
916 							//set the default adjust for ParaStyle Title and Subtitle to left
917 							try
918 							{
919 								uno::Reference< beans::XPropertySet > xProp( xStyle, uno::UNO_QUERY );
920 								if( xProp.is() )
921 								{
922 									uno::Any aMSDefaultVal = uno::makeAny( (sal_Int16)style::ParagraphAdjust_LEFT );
923 									xProp->setPropertyValue( rPropNameSupplier.GetName( PROP_PARA_ADJUST), aMSDefaultVal );
924 								}
925 							}
926 							catch(...)
927 							{
928 								OSL_ENSURE( false, "Default ParaAdjust style property could not be set");
929 							}
930 						}
931                     }
932 
933                     if(bAddFollowStyle || aPropValues.getLength())
934                     {
935                         PropValVector aSortedPropVals;
936                         for( sal_Int32 nProp = 0; nProp < aPropValues.getLength(); ++nProp)
937                         {
938                                 // Don't add the style name properties
939                             bool bIsParaStyleName = aPropValues[nProp].Name.equalsAscii( "ParaStyleName" );
940                             bool bIsCharStyleName = aPropValues[nProp].Name.equalsAscii( "CharStyleName" );
941                             if ( !bIsParaStyleName && !bIsCharStyleName )
942                             {
943                                 aSortedPropVals.Insert( aPropValues[nProp] );
944                             }
945                         }
946                         if(bAddFollowStyle)
947                         {
948                             //find the name of the Next style
949                             std::vector< StyleSheetEntryPtr >::iterator aNextStyleIt = m_pImpl->m_aStyleSheetEntries.begin();
950                             for( ; aNextStyleIt !=  m_pImpl->m_aStyleSheetEntries.end(); ++aNextStyleIt )
951                             {
952                                 if( ( *aNextStyleIt )->sStyleName.getLength() &&
953                                         ( *aNextStyleIt )->sStyleName == pEntry->sNextStyleIdentifier)
954                                 {
955                                     beans::PropertyValue aNew;
956                                     aNew.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FollowStyle"));
957                                     aNew.Value = uno::makeAny(ConvertStyleName( ( *aNextStyleIt )->sStyleIdentifierD ));
958                                     aSortedPropVals.Insert( aNew );
959                                     break;
960                                 }
961                             }
962                         }
963 
964                         try
965                         {
966                             uno::Reference< beans::XMultiPropertySet > xMultiPropertySet( xStyle, uno::UNO_QUERY_THROW);
967                             xMultiPropertySet->setPropertyValues( aSortedPropVals.getNames(), aSortedPropVals.getValues() );
968                         }
969                         catch( const lang::WrappedTargetException& rWrapped)
970                         {
971                             (void) rWrapped;
972                             rtl::OString aMessage("Some style properties could not be set");
973 #if OSL_DEBUG_LEVEL > 0
974                             beans::UnknownPropertyException aUnknownPropertyException;
975 
976                             if( rWrapped.TargetException >>= aUnknownPropertyException )
977                             {
978                                 aMessage += rtl::OString(": " );
979                                 ::rtl::OString sTemp;
980                                 aUnknownPropertyException.Message.convertToString(&sTemp, RTL_TEXTENCODING_ASCII_US, 0 );
981                                 aMessage += sTemp;
982                             }
983 #endif
984                             OSL_ENSURE( false, aMessage.getStr());
985                         }
986                         catch( const uno::Exception& rEx)
987                         {
988                             (void) rEx;
989                             OSL_ENSURE( false, "Some style properties could not be set");
990                         }
991                     }
992                     if(bInsert)
993                     {
994                         xStyles->insertByName( sConvertedStyleName, uno::makeAny( xStyle) );
995                     }
996                 }
997                 ++aIt;
998             }
999         }
1000     }
1001     catch( uno::Exception& rEx)
1002     {
1003         (void)rEx;
1004         OSL_ENSURE( false, "Styles could not be imported completely");
1005     }
1006 }
1007 /*-- 22.06.2006 15:56:56---------------------------------------------------
1008 
1009   -----------------------------------------------------------------------*/
FindStyleSheetByISTD(const::rtl::OUString & sIndex)1010 const StyleSheetEntryPtr StyleSheetTable::FindStyleSheetByISTD(const ::rtl::OUString& sIndex)
1011 {
1012     StyleSheetEntryPtr pRet;
1013     for( sal_uInt32 nPos = 0; nPos < m_pImpl->m_aStyleSheetEntries.size(); ++nPos )
1014     {
1015         if( m_pImpl->m_aStyleSheetEntries[nPos]->sStyleIdentifierD == sIndex)
1016         {
1017             pRet = m_pImpl->m_aStyleSheetEntries[nPos];
1018             break;
1019         }
1020     }
1021     return pRet;
1022 }
1023 /*-- 28.12.2007 14:45:45---------------------------------------------------
1024 
1025   -----------------------------------------------------------------------*/
FindStyleSheetByStyleName(const::rtl::OUString & sIndex)1026 const StyleSheetEntryPtr StyleSheetTable::FindStyleSheetByStyleName(const ::rtl::OUString& sIndex)
1027 {
1028     StyleSheetEntryPtr pRet;
1029     for( sal_uInt32 nPos = 0; nPos < m_pImpl->m_aStyleSheetEntries.size(); ++nPos )
1030     {
1031         if( m_pImpl->m_aStyleSheetEntries[nPos]->sStyleName == sIndex)
1032         {
1033             pRet = m_pImpl->m_aStyleSheetEntries[nPos];
1034             break;
1035         }
1036     }
1037     return pRet;
1038 }
1039 /*-- 28.12.2007 14:45:45---------------------------------------------------
1040 
1041   -----------------------------------------------------------------------*/
FindStyleSheetByConvertedStyleName(const::rtl::OUString & sIndex)1042 const StyleSheetEntryPtr StyleSheetTable::FindStyleSheetByConvertedStyleName(const ::rtl::OUString& sIndex)
1043 {
1044     StyleSheetEntryPtr pRet;
1045     for( sal_uInt32 nPos = 0; nPos < m_pImpl->m_aStyleSheetEntries.size(); ++nPos )
1046     {
1047         if( m_pImpl->m_aStyleSheetEntries[nPos]->sConvertedStyleName == sIndex)
1048         {
1049             pRet = m_pImpl->m_aStyleSheetEntries[nPos];
1050             break;
1051         }
1052     }
1053     return pRet;
1054 }
1055 
1056 /*-- 17.07.2006 11:47:00---------------------------------------------------
1057 
1058   -----------------------------------------------------------------------*/
FindParentStyleSheet(::rtl::OUString sBaseStyle)1059 const StyleSheetEntryPtr StyleSheetTable::FindParentStyleSheet(::rtl::OUString sBaseStyle)
1060 {
1061     if( !sBaseStyle.getLength() )
1062     {
1063         StyleSheetEntryPtr pEmptyPtr;
1064         return pEmptyPtr;
1065     }
1066     if( m_pImpl->m_pCurrentEntry)
1067         sBaseStyle = m_pImpl->m_pCurrentEntry->sBaseStyleIdentifier;
1068 
1069     return FindStyleSheetByISTD( sBaseStyle );
1070 }
1071 /*-- 21.12.2006 15:58:23---------------------------------------------------
1072 
1073   -----------------------------------------------------------------------*/
1074 static const sal_Char *aStyleNamePairs[] =
1075 {
1076     "Normal",                     "Standard",
1077     "heading 1",                  "Heading 1",
1078     "heading 2",                  "Heading 2",
1079     "heading 3",                  "Heading 3",
1080     "heading 4",                  "Heading 4",
1081     "heading 5",                  "Heading 5",
1082     "heading 6",                  "Heading 6",
1083     "heading 7",                  "Heading 7",
1084     "heading 8",                  "Heading 8",
1085     "heading 9",                  "Heading 9",
1086     "Heading1",                   "Heading 1",
1087     "Heading2",                   "Heading 2",
1088     "Heading3",                   "Heading 3",
1089     "Heading4",                   "Heading 4",
1090     "Heading5",                   "Heading 5",
1091     "Heading6",                   "Heading 6",
1092     "Heading7",                   "Heading 7",
1093     "Heading8",                   "Heading 8",
1094     "Heading9",                   "Heading 9",
1095     "Heading 1",                  "Heading 1",
1096     "Heading 2",                  "Heading 2",
1097     "Heading 3",                  "Heading 3",
1098     "Heading 4",                  "Heading 4",
1099     "Heading 5",                  "Heading 5",
1100     "Heading 6",                  "Heading 6",
1101     "Heading 7",                  "Heading 7",
1102     "Heading 8",                  "Heading 8",
1103     "Heading 9",                  "Heading 9",
1104     "Index 1",                   "Index 1",
1105     "Index 2",                   "Index 2",
1106     "Index 3",                   "Index 3",
1107     "Index 4",                   "",
1108     "Index 5",                   "",
1109     "Index 6",                   "",
1110     "Index 7",                   "",
1111     "Index 8",                   "",
1112     "Index 9",                   "",
1113     "TOC 1",                     "Contents 1",
1114     "TOC 2",                     "Contents 2",
1115     "TOC 3",                     "Contents 3",
1116     "TOC 4",                     "Contents 4",
1117     "TOC 5",                     "Contents 5",
1118     "TOC 6",                     "Contents 6",
1119     "TOC 7",                     "Contents 7",
1120     "TOC 8",                     "Contents 8",
1121     "TOC 9",                     "Contents 9",
1122     "TOC Heading",               "Contents Heading",
1123     "TOCHeading",                "Contents Heading",
1124     "toc 1",                     "Contents 1",
1125     "toc 2",                     "Contents 2",
1126     "toc 3",                     "Contents 3",
1127     "toc 4",                     "Contents 4",
1128     "toc 5",                     "Contents 5",
1129     "toc 6",                     "Contents 6",
1130     "toc 7",                     "Contents 7",
1131     "toc 8",                     "Contents 8",
1132     "toc 9",                     "Contents 9",
1133     "TOC1",                     "Contents 1",
1134     "TOC2",                     "Contents 2",
1135     "TOC3",                     "Contents 3",
1136     "TOC4",                     "Contents 4",
1137     "TOC5",                     "Contents 5",
1138     "TOC6",                     "Contents 6",
1139     "TOC7",                     "Contents 7",
1140     "TOC8",                     "Contents 8",
1141     "TOC9",                     "Contents 9",
1142     "Normal Indent",             "",
1143     "Footnote Text",             "Footnote",
1144     "Annotation Text",           "",
1145     "Header",                    "Header",
1146     "header",                    "Header",
1147     "Footer",                    "Footer",
1148     "footer",                    "Footer",
1149     "Index Heading",             "Index Heading",
1150     "Caption",                   "",
1151     "Table of Figures",          "",
1152     "Envelope Address",          "Addressee",
1153     "Envelope Return",           "Sender",
1154     "Footnote Reference",        "Footnote anchor",
1155     "Annotation Reference",      "",
1156     "Line Number",               "Line numbering",
1157     "Page Number",               "Page Number",
1158     "Endnote Reference",         "Endnote anchor",
1159     "Endnote Text",              "Endnote Symbol",
1160     "Table of Authorities",      "",
1161     "Macro Text",                "",
1162     "TOA Heading",               "",
1163     "List",                      "List",
1164     "List 2",                    "",
1165     "List 3",                    "",
1166     "List 4",                    "",
1167     "List 5",                    "",
1168     "List Bullet",               "",
1169     "List Bullet 2",             "",
1170     "List Bullet 3",             "",
1171     "List Bullet 4",             "",
1172     "List Bullet 5",             "",
1173     "List Number",               "",
1174     "List Number 2",             "",
1175     "List Number 3",             "",
1176     "List Number 4",             "",
1177     "List Number 5",             "",
1178     "Title",                     "Title",
1179     "Closing",                   "",
1180     "Signature",                 "Signature",
1181     "Default Paragraph Font",    "",
1182     "DefaultParagraphFont",      "Default Paragraph Font",
1183     "Body Text",                 "Text body",
1184     "BodyText",                  "Text body",
1185     "BodyTextIndentItalic",     "Text body indent italic",
1186     "Body Text Indent",          "Text body indent",
1187     "BodyTextIndent",           "Text body indent",
1188     "BodyTextIndent2",          "Text body indent2",
1189     "List Continue",             "",
1190     "List Continue 2",           "",
1191     "List Continue 3",           "",
1192     "List Continue 4",           "",
1193     "List Continue 5",           "",
1194     "Message Header",            "",
1195     "Subtitle",                  "Subtitle",
1196     "Salutation",                "",
1197     "Date",                      "",
1198     "Body Text First Indent",    "Body Text Indent",
1199     "Body Text First Indent 2",  "",
1200     "Note Heading",              "",
1201     "Body Text 2",               "",
1202     "Body Text 3",               "",
1203     "Body Text Indent 2",        "",
1204     "Body Text Indent 3",        "",
1205     "Block Text",                "",
1206     "Hyperlink",                 "Internet link",
1207     "Followed Hyperlink",        "Visited Internet Link",
1208     "Strong",                    "Strong Emphasis",
1209     "Emphasis",                  "Emphasis",
1210     "Document Map",              "",
1211     "Plain Text",                "",
1212     "NoList",                   "No List",
1213     "AbstractHeading",          "Abstract Heading",
1214     "AbstractBody",             "Abstract Body",
1215     "PageNumber",               "page number"
1216     "TableNormal",              "Normal Table",
1217     "DocumentMap",              "Document Map"
1218 };
1219 
1220 
ConvertStyleName(const::rtl::OUString & rWWName,bool bExtendedSearch)1221 ::rtl::OUString StyleSheetTable::ConvertStyleName( const ::rtl::OUString& rWWName, bool bExtendedSearch)
1222 {
1223     ::rtl::OUString sRet( rWWName );
1224     if( bExtendedSearch )
1225     {
1226         //search for the rWWName in the IdentifierD of the existing styles and convert the sStyleName member
1227         std::vector< StyleSheetEntryPtr >::iterator aIt = m_pImpl->m_aStyleSheetEntries.begin();
1228         //TODO: performance issue - put styles list into a map sorted by it's sStyleIdentifierD members
1229         while( aIt != m_pImpl->m_aStyleSheetEntries.end() )
1230         {
1231             if( rWWName == ( *aIt )->sStyleIdentifierD )
1232                 sRet = ( *aIt )->sStyleName;
1233             ++aIt;
1234         }
1235     }
1236     if(!m_pImpl->m_aStyleNameMap.size())
1237     {
1238         for( sal_uInt32 nPair = 0; nPair < sizeof(aStyleNamePairs) / sizeof( sal_Char*) / 2; ++nPair)
1239         {
1240                 m_pImpl->m_aStyleNameMap.insert( StringPairMap_t::value_type(
1241                     ::rtl::OUString::createFromAscii(aStyleNamePairs[2 * nPair]),
1242                     ::rtl::OUString::createFromAscii(aStyleNamePairs[2 * nPair + 1]) ));
1243         }
1244     }
1245     StringPairMap_t::iterator aIt = m_pImpl->m_aStyleNameMap.find( sRet );
1246     if(aIt != m_pImpl->m_aStyleNameMap.end() && aIt->second.getLength())
1247         sRet = aIt->second;
1248     return sRet;
1249 }
1250 
GetStyleIdFromIndex(const sal_uInt32 sti)1251 ::rtl::OUString StyleSheetTable::GetStyleIdFromIndex(const sal_uInt32 sti)
1252 {
1253     ::rtl::OUString sRet;
1254     if (sti >= (sizeof(aStyleNamePairs) / sizeof( sal_Char*) / 2))
1255         sRet = ::rtl::OUString();
1256     else
1257         sRet = ::rtl::OUString::createFromAscii(aStyleNamePairs[2 * sti]);
1258     return sRet;
1259 }
1260 
resolveSprmProps(Sprm & rSprm)1261 void StyleSheetTable::resolveSprmProps(Sprm & rSprm)
1262 {
1263     writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
1264     if( pProperties.get())
1265         pProperties->resolve(*this);
1266 }
1267 
resolveAttributeProperties(Value & val)1268 void StyleSheetTable::resolveAttributeProperties(Value & val)
1269 {
1270     writerfilter::Reference<Properties>::Pointer_t pProperties = val.getProperties();
1271     if( pProperties.get())
1272         pProperties->resolve(*this);
1273 }
1274 /*-- 18.07.2007 15:59:34---------------------------------------------------
1275 
1276   -----------------------------------------------------------------------*/
applyDefaults(bool bParaProperties)1277 void StyleSheetTable::applyDefaults(bool bParaProperties)
1278 {
1279     try{
1280         if(!m_pImpl->m_xTextDefaults.is())
1281         {
1282             m_pImpl->m_xTextDefaults = uno::Reference< beans::XPropertySet>(
1283                 m_pImpl->m_rDMapper.GetTextFactory()->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Defaults"))),
1284                 uno::UNO_QUERY_THROW );
1285         }
1286         PropertyNameSupplier& rPropNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
1287         if( bParaProperties && m_pImpl->m_pDefaultParaProps.get() && m_pImpl->m_pDefaultParaProps->size())
1288         {
1289             PropertyMap::iterator aMapIter = m_pImpl->m_pDefaultParaProps->begin();
1290             for( ; aMapIter != m_pImpl->m_pDefaultParaProps->end(); ++aMapIter )
1291             {
1292                 try
1293                 {
1294                     m_pImpl->m_xTextDefaults->setPropertyValue(rPropNameSupplier.GetName( aMapIter->first.eId ), aMapIter->second);
1295                 }
1296                 catch( const uno::Exception& )
1297                 {
1298                     OSL_ENSURE( false, "setPropertyValue exception");
1299                 }
1300             }
1301         }
1302         if( !bParaProperties && m_pImpl->m_pDefaultCharProps.get() && m_pImpl->m_pDefaultCharProps->size())
1303         {
1304             PropertyMap::iterator aMapIter = m_pImpl->m_pDefaultCharProps->begin();
1305             for( ; aMapIter != m_pImpl->m_pDefaultCharProps->end(); ++aMapIter )
1306             {
1307                 try
1308                 {
1309                     m_pImpl->m_xTextDefaults->setPropertyValue(rPropNameSupplier.GetName( aMapIter->first.eId ), aMapIter->second);
1310                 }
1311                 catch( const uno::Exception& )
1312                 {
1313                     OSL_ENSURE( false, "setPropertyValue exception");
1314                 }
1315             }
1316         }
1317     }
1318     catch( const uno::Exception& )
1319     {
1320     }
1321 }
1322 /*-- 05.02.2008 10:27:36---------------------------------------------------
1323 
1324   -----------------------------------------------------------------------*/
getOrCreateCharStyle(PropertyValueVector_t & rCharProperties)1325 ::rtl::OUString StyleSheetTable::getOrCreateCharStyle( PropertyValueVector_t& rCharProperties )
1326 {
1327     //find out if any of the styles already has the required properties then return it's name
1328     ::rtl::OUString sListLabel = m_pImpl->HasListCharStyle(rCharProperties);
1329     if( sListLabel.getLength() )
1330         return sListLabel;
1331     const char cListLabel[] = "ListLabel ";
1332     uno::Reference< style::XStyleFamiliesSupplier > xStylesSupplier( m_pImpl->m_xTextDocument, uno::UNO_QUERY_THROW );
1333     uno::Reference< container::XNameAccess > xStyleFamilies = xStylesSupplier->getStyleFamilies();
1334     uno::Reference<container::XNameContainer> xCharStyles;
1335     xStyleFamilies->getByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharacterStyles"))) >>= xCharStyles;
1336     //search for all character styles with the name sListLabel + <index>
1337     sal_Int32 nStyleFound = 0;
1338     uno::Sequence< ::rtl::OUString > aStyleNames = xCharStyles->getElementNames();
1339     const ::rtl::OUString* pStyleNames = aStyleNames.getConstArray();
1340     for( sal_Int32 nStyle = 0; nStyle < aStyleNames.getLength(); ++nStyle )
1341     {
1342         if( pStyleNames[nStyle].matchAsciiL( cListLabel, sizeof( cListLabel ) - 1  ))
1343         {
1344             ::rtl::OUString sSuffix = pStyleNames[nStyle].copy( sizeof( cListLabel ) - 1 );
1345             sal_Int32 nSuffix = sSuffix.toInt32();
1346             if( nSuffix > 0 )
1347             {
1348                 if( nSuffix > nStyleFound )
1349                     nStyleFound = nSuffix;
1350             }
1351         }
1352     }
1353     sListLabel = ::rtl::OUString::createFromAscii( cListLabel );
1354     sListLabel += ::rtl::OUString::valueOf( ++nStyleFound );
1355     //create a new one otherwise
1356     uno::Reference< lang::XMultiServiceFactory > xDocFactory( m_pImpl->m_xTextDocument, uno::UNO_QUERY_THROW );
1357     PropertyNameSupplier& rPropNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
1358     try
1359     {
1360         uno::Reference< style::XStyle > xStyle( xDocFactory->createInstance(
1361             rPropNameSupplier.GetName( PROP_SERVICE_CHAR_STYLE )), uno::UNO_QUERY_THROW);
1362         //uno::Reference< container::XNamed >xNamed( xStyle, uno::UNO_QUERY_THROW );
1363         //xNamed->setName( sListLabel );
1364         uno::Reference< beans::XPropertySet > xStyleProps(xStyle, uno::UNO_QUERY_THROW );
1365         PropertyValueVector_t::const_iterator aCharPropIter = rCharProperties.begin();
1366         while( aCharPropIter != rCharProperties.end())
1367         {
1368             try
1369             {
1370                 xStyleProps->setPropertyValue( aCharPropIter->Name, aCharPropIter->Value );
1371             }
1372             catch( const uno::Exception& rEx )
1373             {
1374                 (void)rEx;
1375                 OSL_ENSURE( false, "Exception in StyleSheetTable::getOrCreateCharStyle - Style::setPropertyValue");
1376             }
1377             ++aCharPropIter;
1378         }
1379         xCharStyles->insertByName( sListLabel, uno::makeAny( xStyle) );
1380         m_pImpl->m_aListCharStylePropertyVector.push_back( ListCharStylePropertyMap_t( sListLabel, rCharProperties ));
1381     }
1382     catch( const uno::Exception& rEx )
1383     {
1384         (void)rEx;
1385         OSL_ENSURE( false, "Exception in StyleSheetTable::getOrCreateCharStyle");
1386     }
1387 
1388     return sListLabel;
1389 }
1390 
1391 }//namespace dmapper
1392 }//namespace writerfilter
1393