update credits
[LibreOffice.git] / sw / source / filter / xml / xmlfmt.cxx
blob452271c6e75ff4b30a2bafe9b1428d8f3dbcdb6d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/ustrbuf.hxx>
22 #include <xmloff/nmspmap.hxx>
23 #include <format.hxx>
24 #include <fmtcol.hxx>
25 #include <hints.hxx>
26 #include <poolfmt.hxx>
27 #include <charfmt.hxx>
28 #include <paratr.hxx>
29 #include <doc.hxx>
30 #include "docary.hxx"
31 #include "unostyle.hxx"
32 #include "fmtpdsc.hxx"
33 #include "pagedesc.hxx"
34 #include <xmloff/xmlnmspe.hxx>
35 #include <xmloff/i18nmap.hxx>
36 #include <xmloff/xmltkmap.hxx>
37 #include "xmlitem.hxx"
38 #include <xmloff/xmlstyle.hxx>
39 #include <xmloff/txtstyli.hxx>
40 #include <xmloff/txtimp.hxx>
41 #include <xmloff/families.hxx>
42 #include <xmloff/XMLTextMasterStylesContext.hxx>
43 #include <xmloff/XMLTextShapeStyleContext.hxx>
44 #include <xmloff/XMLGraphicsDefaultStyle.hxx>
45 #include "xmlimp.hxx"
46 #include "xmltbli.hxx"
47 #include "cellatr.hxx"
48 #include <SwStyleNameMapper.hxx>
49 #include <xmloff/attrlist.hxx>
50 #include <unotxdoc.hxx>
51 #include <docsh.hxx>
54 using namespace ::com::sun::star;
55 using namespace ::xmloff::token;
57 class SwXMLConditionParser_Impl
59 OUString sInput;
61 sal_uInt32 nCondition;
62 sal_uInt32 nSubCondition;
64 sal_Int32 nPos;
65 sal_Int32 nLength;
67 inline sal_Bool SkipWS();
68 inline sal_Bool MatchChar( sal_Unicode c );
69 inline sal_Bool MatchName( OUString& rName );
70 inline sal_Bool MatchNumber( sal_uInt32& rNumber );
72 public:
74 SwXMLConditionParser_Impl( const OUString& rInp );
76 bool IsValid() const { return 0 != nCondition; }
78 sal_uInt32 GetCondition() const { return nCondition; }
79 sal_uInt32 GetSubCondition() const { return nSubCondition; }
82 inline sal_Bool SwXMLConditionParser_Impl::SkipWS()
84 while( nPos < nLength && ' ' == sInput[nPos] )
85 nPos++;
86 return sal_True;
89 inline sal_Bool SwXMLConditionParser_Impl::MatchChar( sal_Unicode c )
91 sal_Bool bRet = sal_False;
92 if( nPos < nLength && c == sInput[nPos] )
94 nPos++;
95 bRet = sal_True;
97 return bRet;
100 inline sal_Bool SwXMLConditionParser_Impl::MatchName( OUString& rName )
102 OUStringBuffer sBuffer( nLength );
103 while( nPos < nLength &&
104 ( ('a' <= sInput[nPos] && sInput[nPos] <= 'z') ||
105 '-' == sInput[nPos] ) )
107 sBuffer.append( sInput[nPos] );
108 nPos++;
110 rName = sBuffer.makeStringAndClear();
111 return !rName.isEmpty();
114 inline sal_Bool SwXMLConditionParser_Impl::MatchNumber( sal_uInt32& rNumber )
116 OUStringBuffer sBuffer( nLength );
117 while( nPos < nLength && '0' <= sInput[nPos] && sInput[nPos] <= '9' )
119 sBuffer.append( sInput[nPos] );
120 nPos++;
123 OUString sNum( sBuffer.makeStringAndClear() );
124 if( !sNum.isEmpty() )
125 rNumber = sNum.toInt32();
126 return !sNum.isEmpty();
129 SwXMLConditionParser_Impl::SwXMLConditionParser_Impl( const OUString& rInp ) :
130 sInput( rInp ),
131 nCondition( 0 ),
132 nSubCondition( 0 ),
133 nPos( 0 ),
134 nLength( rInp.getLength() )
136 OUString sFunc;
137 bool bHasSub = false;
138 sal_uInt32 nSub = 0;
139 sal_Bool bOK = SkipWS() && MatchName( sFunc ) && SkipWS() &&
140 MatchChar( '(' ) && SkipWS() && MatchChar( ')' ) && SkipWS();
141 if( bOK && MatchChar( '=' ) )
143 bOK = SkipWS() && MatchNumber( nSub ) && SkipWS();
144 bHasSub = true;
147 bOK &= nPos == nLength;
149 if( bOK )
151 if( IsXMLToken( sFunc, XML_ENDNOTE ) && !bHasSub )
152 nCondition = PARA_IN_ENDNOTE;
153 else if( IsXMLToken( sFunc, XML_FOOTER ) && !bHasSub )
154 nCondition = PARA_IN_FOOTER;
155 else if( IsXMLToken( sFunc, XML_FOOTNOTE ) && !bHasSub )
156 nCondition = PARA_IN_FOOTENOTE;
157 else if( IsXMLToken( sFunc, XML_HEADER ) && !bHasSub )
158 nCondition = PARA_IN_HEADER;
159 else if( IsXMLToken( sFunc, XML_LIST_LEVEL) &&
160 nSub >=1 && nSub <= MAXLEVEL )
162 nCondition = PARA_IN_LIST;
163 nSubCondition = nSub-1;
165 else if( IsXMLToken( sFunc, XML_OUTLINE_LEVEL) &&
166 nSub >=1 && nSub <= MAXLEVEL )
168 nCondition = PARA_IN_OUTLINE;
169 nSubCondition = nSub-1;
171 else if( IsXMLToken( sFunc, XML_SECTION ) && !bHasSub )
173 nCondition = PARA_IN_SECTION;
175 else if( IsXMLToken( sFunc, XML_TABLE ) && !bHasSub )
177 nCondition = PARA_IN_TABLEBODY;
179 else if( IsXMLToken( sFunc, XML_TABLE_HEADER ) && !bHasSub )
181 nCondition = PARA_IN_TABLEHEAD;
183 else if( IsXMLToken( sFunc, XML_TEXT_BOX ) && !bHasSub )
185 nCondition = PARA_IN_FRAME;
190 // ---------------------------------------------------------------------
192 class SwXMLConditionContext_Impl : public SvXMLImportContext
194 sal_uInt32 nCondition;
195 sal_uInt32 nSubCondition;
197 OUString sApplyStyle;
199 void ParseCondition( const OUString& rCond );
201 public:
203 SwXMLConditionContext_Impl(
204 SvXMLImport& rImport, sal_uInt16 nPrfx,
205 const OUString& rLName,
206 const uno::Reference< xml::sax::XAttributeList > & xAttrList );
207 virtual ~SwXMLConditionContext_Impl();
209 TYPEINFO();
211 bool IsValid() const { return 0 != nCondition; }
213 sal_uInt32 GetCondition() const { return nCondition; }
214 sal_uInt32 GetSubCondition() const { return nSubCondition; }
215 const OUString& GetApplyStyle() const { return sApplyStyle; }
218 SwXMLConditionContext_Impl::SwXMLConditionContext_Impl(
219 SvXMLImport& rImport, sal_uInt16 nPrfx,
220 const OUString& rLName,
221 const uno::Reference< xml::sax::XAttributeList > & xAttrList ) :
222 SvXMLImportContext( rImport, nPrfx, rLName ),
223 nCondition( 0 ),
224 nSubCondition( 0 )
226 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
227 for( sal_Int16 i=0; i < nAttrCount; i++ )
229 const OUString& rAttrName = xAttrList->getNameByIndex( i );
230 OUString aLocalName;
231 sal_uInt16 nPrefix =
232 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
233 &aLocalName );
234 const OUString& rValue = xAttrList->getValueByIndex( i );
236 // TODO: use a map here
237 if( XML_NAMESPACE_STYLE == nPrefix )
239 if( IsXMLToken( aLocalName, XML_CONDITION ) )
241 SwXMLConditionParser_Impl aCondParser( rValue );
242 if( aCondParser.IsValid() )
244 nCondition = aCondParser.GetCondition();
245 nSubCondition = aCondParser.GetSubCondition();
248 else if( IsXMLToken( aLocalName, XML_APPLY_STYLE_NAME ) )
250 sApplyStyle = rValue;
256 SwXMLConditionContext_Impl::~SwXMLConditionContext_Impl()
260 TYPEINIT1( SwXMLConditionContext_Impl, XMLTextStyleContext );
262 // ---------------------------------------------------------------------
264 typedef std::vector<SwXMLConditionContext_Impl*> SwXMLConditions_Impl;
266 class SwXMLTextStyleContext_Impl : public XMLTextStyleContext
268 SwXMLConditions_Impl *pConditions;
270 protected:
272 virtual uno::Reference < style::XStyle > Create();
274 public:
276 TYPEINFO();
278 SwXMLTextStyleContext_Impl( SwXMLImport& rImport, sal_uInt16 nPrfx,
279 const OUString& rLName,
280 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
281 sal_uInt16 nFamily,
282 SvXMLStylesContext& rStyles );
283 virtual ~SwXMLTextStyleContext_Impl();
285 virtual SvXMLImportContext *CreateChildContext(
286 sal_uInt16 nPrefix,
287 const OUString& rLocalName,
288 const uno::Reference< xml::sax::XAttributeList > & xAttrList );
290 virtual void Finish( sal_Bool bOverwrite );
293 TYPEINIT1( SwXMLTextStyleContext_Impl, XMLTextStyleContext );
295 uno::Reference < style::XStyle > SwXMLTextStyleContext_Impl::Create()
297 uno::Reference < style::XStyle > xNewStyle;
299 if( pConditions && XML_STYLE_FAMILY_TEXT_PARAGRAPH == GetFamily() )
301 uno::Reference< lang::XMultiServiceFactory > xFactory( GetImport().GetModel(),
302 uno::UNO_QUERY );
303 if( xFactory.is() )
305 OUString sServiceName(
306 "com.sun.star.style.ConditionalParagraphStyle" );
307 uno::Reference < uno::XInterface > xIfc =
308 xFactory->createInstance( sServiceName );
309 if( xIfc.is() )
310 xNewStyle = uno::Reference < style::XStyle >( xIfc, uno::UNO_QUERY );
313 else
315 xNewStyle = XMLTextStyleContext::Create();
318 return xNewStyle;
321 SwXMLTextStyleContext_Impl::SwXMLTextStyleContext_Impl( SwXMLImport& rImport,
322 sal_uInt16 nPrfx, const OUString& rLName,
323 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
324 sal_uInt16 nFamily,
325 SvXMLStylesContext& rStyles ) :
326 XMLTextStyleContext( rImport, nPrfx, rLName, xAttrList, rStyles, nFamily ),
327 pConditions( 0 )
331 SwXMLTextStyleContext_Impl::~SwXMLTextStyleContext_Impl()
333 if( pConditions )
335 while( !pConditions->empty() )
337 SwXMLConditionContext_Impl *pCond = &*pConditions->back();
338 pConditions->pop_back();
339 pCond->ReleaseRef();
341 delete pConditions;
345 SvXMLImportContext *SwXMLTextStyleContext_Impl::CreateChildContext(
346 sal_uInt16 nPrefix,
347 const OUString& rLocalName,
348 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
350 SvXMLImportContext *pContext = 0;
352 if( XML_NAMESPACE_STYLE == nPrefix && IsXMLToken( rLocalName, XML_MAP ) )
354 SwXMLConditionContext_Impl *pCond =
355 new SwXMLConditionContext_Impl( GetImport(), nPrefix,
356 rLocalName, xAttrList );
357 if( pCond->IsValid() )
359 if( !pConditions )
360 pConditions = new SwXMLConditions_Impl;
361 pConditions->push_back( pCond );
362 pCond->AddRef();
364 pContext = pCond;
367 if( !pContext )
368 pContext = XMLTextStyleContext::CreateChildContext( nPrefix, rLocalName,
369 xAttrList );
371 return pContext;
374 void SwXMLTextStyleContext_Impl::Finish( sal_Bool bOverwrite )
376 XMLTextStyleContext::Finish( bOverwrite );
378 if( !pConditions || XML_STYLE_FAMILY_TEXT_PARAGRAPH != GetFamily() )
379 return;
381 uno::Reference < style::XStyle > xStyle = GetStyle();
382 if( !xStyle.is() )
383 return;
385 const SwXStyle* pStyle = 0;
386 uno::Reference<lang::XUnoTunnel> xStyleTunnel( xStyle, uno::UNO_QUERY);
387 if( xStyleTunnel.is() )
389 pStyle = reinterpret_cast< SwXStyle * >(
390 sal::static_int_cast< sal_IntPtr >( xStyleTunnel->getSomething( SwXStyle::getUnoTunnelId() )));
392 if( !pStyle )
393 return;
395 const SwDoc *pDoc = pStyle->GetDoc();
397 SwTxtFmtColl *pColl = pDoc->FindTxtFmtCollByName( pStyle->GetStyleName() );
398 OSL_ENSURE( pColl, "Text collection not found" );
399 if( !pColl || RES_CONDTXTFMTCOLL != pColl->Which() )
400 return;
402 sal_uInt16 nCount = pConditions->size();
403 String aString;
404 OUString sName;
405 for( sal_uInt16 i = 0; i < nCount; i++ )
407 const SwXMLConditionContext_Impl *pCond = (*pConditions)[i];
408 OUString aDisplayName(
409 GetImport().GetStyleDisplayName( XML_STYLE_FAMILY_TEXT_PARAGRAPH,
410 pCond->GetApplyStyle() ) );
411 SwStyleNameMapper::FillUIName( aDisplayName,
412 aString,
413 nsSwGetPoolIdFromName::GET_POOLID_TXTCOLL,
414 true);
415 sName = aString;
416 SwTxtFmtColl* pCondColl = pDoc->FindTxtFmtCollByName( sName );
417 OSL_ENSURE( pCondColl,
418 "SwXMLItemSetStyleContext_Impl::ConnectConditions: cond coll missing" );
419 if( pCondColl )
421 SwCollCondition aCond( pCondColl, pCond->GetCondition(),
422 pCond->GetSubCondition() );
423 ((SwConditionTxtFmtColl*)pColl)->InsertCondition( aCond );
428 // ---------------------------------------------------------------------
430 class SwXMLItemSetStyleContext_Impl : public SvXMLStyleContext
432 OUString sMasterPageName;
433 SfxItemSet *pItemSet;
434 SwXMLTextStyleContext_Impl *pTextStyle;
435 SvXMLStylesContext &rStyles;
437 OUString sDataStyleName;
439 bool bHasMasterPageName : 1;
440 bool bPageDescConnected : 1;
441 bool bDataStyleIsResolved;
443 SvXMLImportContext *CreateItemSetContext(
444 sal_uInt16 nPrefix,
445 const OUString& rLName,
446 const uno::Reference< xml::sax::XAttributeList > & xAttrList);
448 protected:
450 virtual void SetAttribute( sal_uInt16 nPrefixKey,
451 const OUString& rLocalName,
452 const OUString& rValue );
454 const SwXMLImport& GetSwImport() const
455 { return (const SwXMLImport&)GetImport(); }
456 SwXMLImport& GetSwImport() { return (SwXMLImport&)GetImport(); }
458 public:
460 TYPEINFO();
462 SwXMLItemSetStyleContext_Impl(
463 SwXMLImport& rImport, sal_uInt16 nPrfx,
464 const OUString& rLName,
465 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
466 SvXMLStylesContext& rStylesC,
467 sal_uInt16 nFamily);
468 virtual ~SwXMLItemSetStyleContext_Impl();
470 virtual void CreateAndInsert( sal_Bool bOverwrite );
471 virtual SvXMLImportContext *CreateChildContext(
472 sal_uInt16 nPrefix,
473 const OUString& rLocalName,
474 const uno::Reference< xml::sax::XAttributeList > & xAttrList );
476 // The item set may be empty!
477 SfxItemSet *GetItemSet() { return pItemSet; }
478 const SfxItemSet *GetItemSet() const { return pItemSet; }
480 const OUString& GetMasterPageName() const { return sMasterPageName; }
481 bool HasMasterPageName() const { return bHasMasterPageName; }
483 bool IsPageDescConnected() const { return bPageDescConnected; }
484 void ConnectPageDesc();
486 bool ResolveDataStyleName();
489 void SwXMLItemSetStyleContext_Impl::SetAttribute( sal_uInt16 nPrefixKey,
490 const OUString& rLocalName,
491 const OUString& rValue )
493 if( XML_NAMESPACE_STYLE == nPrefixKey )
495 if ( IsXMLToken( rLocalName, XML_MASTER_PAGE_NAME ) )
497 sMasterPageName = rValue;
498 bHasMasterPageName = true;
500 else if ( IsXMLToken( rLocalName, XML_DATA_STYLE_NAME ) )
502 // if we have a valid data style name
503 if (!rValue.isEmpty())
505 sDataStyleName = rValue;
506 bDataStyleIsResolved = false; // needs to be resolved
509 else
511 SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
514 else
516 SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
520 SvXMLImportContext *SwXMLItemSetStyleContext_Impl::CreateItemSetContext(
521 sal_uInt16 nPrefix, const OUString& rLName,
522 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
524 OSL_ENSURE( !pItemSet,
525 "SwXMLItemSetStyleContext_Impl::CreateItemSetContext: item set exists" );
527 SvXMLImportContext *pContext = 0;
529 SwDoc* pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
531 SfxItemPool& rItemPool = pDoc->GetAttrPool();
532 switch( GetFamily() )
534 case XML_STYLE_FAMILY_TABLE_TABLE:
535 pItemSet = new SfxItemSet( rItemPool, aTableSetRange );
536 break;
537 case XML_STYLE_FAMILY_TABLE_COLUMN:
538 pItemSet = new SfxItemSet( rItemPool, RES_FRM_SIZE, RES_FRM_SIZE, 0 );
539 break;
540 case XML_STYLE_FAMILY_TABLE_ROW:
541 pItemSet = new SfxItemSet( rItemPool, aTableLineSetRange );
542 break;
543 case XML_STYLE_FAMILY_TABLE_CELL:
544 pItemSet = new SfxItemSet( rItemPool, aTableBoxSetRange );
545 break;
546 default:
547 OSL_ENSURE( !this,
548 "SwXMLItemSetStyleContext_Impl::CreateItemSetContext: unknown family" );
549 break;
551 if( pItemSet )
552 pContext = GetSwImport().CreateTableItemImportContext(
553 nPrefix, rLName, xAttrList, GetFamily(),
554 *pItemSet );
555 if( !pContext )
557 delete pItemSet;
558 pItemSet = 0;
561 return pContext;
564 TYPEINIT1( SwXMLItemSetStyleContext_Impl, SvXMLStyleContext );
566 SwXMLItemSetStyleContext_Impl::SwXMLItemSetStyleContext_Impl( SwXMLImport& rImport,
567 sal_uInt16 nPrfx, const OUString& rLName,
568 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
569 SvXMLStylesContext& rStylesC,
570 sal_uInt16 nFamily ) :
571 SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, nFamily ),
572 pItemSet( 0 ),
573 pTextStyle( 0 ),
574 rStyles( rStylesC ),
575 bHasMasterPageName( false ),
576 bPageDescConnected( false ),
577 bDataStyleIsResolved( true )
581 SwXMLItemSetStyleContext_Impl::~SwXMLItemSetStyleContext_Impl()
583 delete pItemSet;
586 void SwXMLItemSetStyleContext_Impl::CreateAndInsert( sal_Bool bOverwrite )
588 if( pTextStyle )
589 pTextStyle->CreateAndInsert( bOverwrite );
592 SvXMLImportContext *SwXMLItemSetStyleContext_Impl::CreateChildContext(
593 sal_uInt16 nPrefix,
594 const OUString& rLocalName,
595 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
597 SvXMLImportContext *pContext = 0;
599 if( XML_NAMESPACE_STYLE == nPrefix )
601 if( IsXMLToken( rLocalName, XML_TABLE_PROPERTIES ) ||
602 IsXMLToken( rLocalName, XML_TABLE_COLUMN_PROPERTIES ) ||
603 IsXMLToken( rLocalName, XML_TABLE_ROW_PROPERTIES ) ||
604 IsXMLToken( rLocalName, XML_TABLE_CELL_PROPERTIES ) )
606 pContext = CreateItemSetContext( nPrefix, rLocalName, xAttrList );
608 else if( IsXMLToken( rLocalName, XML_TEXT_PROPERTIES ) ||
609 IsXMLToken( rLocalName, XML_PARAGRAPH_PROPERTIES ))
611 if( !pTextStyle )
613 SvXMLAttributeList *pTmp = new SvXMLAttributeList;
614 OUString aStr = GetImport().GetNamespaceMap().GetQNameByKey( nPrefix, GetXMLToken(XML_NAME) );
615 pTmp->AddAttribute( aStr, GetName() );
616 uno::Reference <xml::sax::XAttributeList> xTmpAttrList = pTmp;
617 pTextStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), nPrefix,
618 rLocalName, xTmpAttrList, XML_STYLE_FAMILY_TEXT_PARAGRAPH, rStyles );
619 pTextStyle->StartElement( xTmpAttrList );
620 rStyles.AddStyle( *pTextStyle );
622 pContext = pTextStyle->CreateChildContext( nPrefix, rLocalName, xAttrList );
626 if( !pContext )
627 pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
628 xAttrList );
630 return pContext;
633 void SwXMLItemSetStyleContext_Impl::ConnectPageDesc()
635 if( bPageDescConnected || !HasMasterPageName() )
636 return;
637 bPageDescConnected = true;
639 SwDoc *pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
641 String sName;
642 // #i40788# - first determine the display name of the page style,
643 // then map this name to the corresponding user interface name.
644 sName = GetImport().GetStyleDisplayName( XML_STYLE_FAMILY_MASTER_PAGE,
645 GetMasterPageName() );
646 SwStyleNameMapper::FillUIName( sName,
647 sName,
648 nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC,
649 true);
650 SwPageDesc *pPageDesc = pDoc->FindPageDescByName( sName );
651 if( !pPageDesc )
653 // If the page style is a pool style, then we maybe have to create it
654 // first if it hasn't been used by now.
655 sal_uInt16 nPoolId = SwStyleNameMapper::GetPoolIdFromUIName( sName, nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC );
656 if( USHRT_MAX != nPoolId )
657 pPageDesc = pDoc->GetPageDescFromPool( nPoolId, false );
660 if( !pPageDesc )
661 return;
663 if( !pItemSet )
665 SfxItemPool& rItemPool = pDoc->GetAttrPool();
666 pItemSet = new SfxItemSet( rItemPool, aTableSetRange );
669 const SfxPoolItem *pItem;
670 SwFmtPageDesc *pFmtPageDesc = 0;
671 if( SFX_ITEM_SET == pItemSet->GetItemState( RES_PAGEDESC, sal_False,
672 &pItem ) )
674 if( ((SwFmtPageDesc *)pItem)->GetPageDesc() != pPageDesc )
675 pFmtPageDesc = new SwFmtPageDesc( *(SwFmtPageDesc *)pItem );
677 else
678 pFmtPageDesc = new SwFmtPageDesc();
680 if( pFmtPageDesc )
682 pFmtPageDesc->RegisterToPageDesc( *pPageDesc );
683 pItemSet->Put( *pFmtPageDesc );
684 delete pFmtPageDesc;
688 bool SwXMLItemSetStyleContext_Impl::ResolveDataStyleName()
690 // resolve, if not already done
691 if (! bDataStyleIsResolved)
693 // get the format key
694 sal_Int32 nFormat =
695 GetImport().GetTextImport()->GetDataStyleKey(sDataStyleName);
697 // if the key is valid, insert Item into ItemSet
698 if( -1 != nFormat )
700 if( !pItemSet )
702 SwDoc *pDoc = SwImport::GetDocFromXMLImport( GetSwImport() );
704 SfxItemPool& rItemPool = pDoc->GetAttrPool();
705 pItemSet = new SfxItemSet( rItemPool, aTableBoxSetRange );
707 SwTblBoxNumFormat aNumFormatItem(nFormat);
708 pItemSet->Put(aNumFormatItem);
711 // now resolved
712 bDataStyleIsResolved = true;
713 return true;
715 else
717 // was already resolved; nothing to do
718 return false;
722 // ---------------------------------------------------------------------
724 class SwXMLStylesContext_Impl : public SvXMLStylesContext
726 SwXMLItemSetStyleContext_Impl *GetSwStyle( sal_uInt16 i ) const;
728 SwXMLImport& GetSwImport() { return (SwXMLImport&)GetImport(); }
729 const SwXMLImport& GetSwImport() const
730 { return (const SwXMLImport&)GetImport(); }
732 protected:
734 virtual SvXMLStyleContext *CreateStyleStyleChildContext( sal_uInt16 nFamily,
735 sal_uInt16 nPrefix, const OUString& rLocalName,
736 const uno::Reference< xml::sax::XAttributeList > & xAttrList );
737 virtual SvXMLStyleContext *CreateDefaultStyleStyleChildContext(
738 sal_uInt16 nFamily, sal_uInt16 nPrefix, const OUString& rLocalName,
739 const uno::Reference< xml::sax::XAttributeList > & xAttrList );
740 // HACK
741 virtual UniReference < SvXMLImportPropertyMapper > GetImportPropertyMapper(
742 sal_uInt16 nFamily ) const;
744 virtual uno::Reference < container::XNameContainer >
745 GetStylesContainer( sal_uInt16 nFamily ) const;
746 virtual OUString GetServiceName( sal_uInt16 nFamily ) const;
747 // HACK
749 public:
751 TYPEINFO();
753 SwXMLStylesContext_Impl(
754 SwXMLImport& rImport, sal_uInt16 nPrfx,
755 const OUString& rLName ,
756 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
757 sal_Bool bAuto );
758 virtual ~SwXMLStylesContext_Impl();
760 virtual sal_Bool InsertStyleFamily( sal_uInt16 nFamily ) const;
762 virtual void EndElement();
765 TYPEINIT1( SwXMLStylesContext_Impl, SvXMLStylesContext );
767 inline SwXMLItemSetStyleContext_Impl *SwXMLStylesContext_Impl::GetSwStyle(
768 sal_uInt16 i ) const
770 return PTR_CAST( SwXMLItemSetStyleContext_Impl, GetStyle( i ) );
773 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleStyleChildContext(
774 sal_uInt16 nFamily, sal_uInt16 nPrefix, const OUString& rLocalName,
775 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
777 SvXMLStyleContext *pStyle = 0;
779 switch( nFamily )
781 case XML_STYLE_FAMILY_TEXT_PARAGRAPH:
782 pStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), nPrefix,
783 rLocalName, xAttrList, nFamily, *this );
784 break;
785 case XML_STYLE_FAMILY_TABLE_TABLE:
786 case XML_STYLE_FAMILY_TABLE_COLUMN:
787 case XML_STYLE_FAMILY_TABLE_ROW:
788 case XML_STYLE_FAMILY_TABLE_CELL:
789 pStyle = new SwXMLItemSetStyleContext_Impl( GetSwImport(), nPrefix,
790 rLocalName, xAttrList, *this, nFamily );
791 break;
792 case XML_STYLE_FAMILY_SD_GRAPHICS_ID:
793 // As long as there are no element items, we can use the text
794 // style class.
795 pStyle = new XMLTextShapeStyleContext( GetImport(), nPrefix,
796 rLocalName, xAttrList, *this, nFamily );
797 break;
798 default:
799 pStyle = SvXMLStylesContext::CreateStyleStyleChildContext( nFamily,
800 nPrefix,
801 rLocalName,
802 xAttrList );
803 break;
806 return pStyle;
809 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateDefaultStyleStyleChildContext(
810 sal_uInt16 nFamily, sal_uInt16 nPrefix, const OUString& rLocalName,
811 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
813 SvXMLStyleContext *pStyle = 0;
815 switch( nFamily )
817 case XML_STYLE_FAMILY_TEXT_PARAGRAPH:
818 case XML_STYLE_FAMILY_TABLE_TABLE:
819 case XML_STYLE_FAMILY_TABLE_ROW:
820 pStyle = new XMLTextStyleContext( GetImport(), nPrefix, rLocalName,
821 xAttrList, *this, nFamily,
822 sal_True );
823 break;
824 case XML_STYLE_FAMILY_SD_GRAPHICS_ID:
825 // There are no writer specific defaults for graphic styles!
826 pStyle = new XMLGraphicsDefaultStyle( GetImport(), nPrefix,
827 rLocalName, xAttrList, *this );
828 break;
829 default:
830 pStyle = SvXMLStylesContext::CreateDefaultStyleStyleChildContext( nFamily,
831 nPrefix,
832 rLocalName,
833 xAttrList );
834 break;
837 return pStyle;
841 SwXMLStylesContext_Impl::SwXMLStylesContext_Impl(
842 SwXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
843 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
844 sal_Bool bAuto ) :
845 SvXMLStylesContext( rImport, nPrfx, rLName, xAttrList, bAuto )
849 SwXMLStylesContext_Impl::~SwXMLStylesContext_Impl()
853 sal_Bool SwXMLStylesContext_Impl::InsertStyleFamily( sal_uInt16 nFamily ) const
855 const SwXMLImport& rSwImport = GetSwImport();
856 sal_uInt16 nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
858 sal_Bool bIns = sal_True;
859 switch( nFamily )
861 case XML_STYLE_FAMILY_TEXT_PARAGRAPH:
862 bIns = (nStyleFamilyMask & SFX_STYLE_FAMILY_PARA) != 0;
863 break;
864 case XML_STYLE_FAMILY_TEXT_TEXT:
865 bIns = (nStyleFamilyMask & SFX_STYLE_FAMILY_CHAR) != 0;
866 break;
867 case XML_STYLE_FAMILY_SD_GRAPHICS_ID:
868 bIns = (nStyleFamilyMask & SFX_STYLE_FAMILY_FRAME) != 0;
869 break;
870 case XML_STYLE_FAMILY_TEXT_LIST:
871 bIns = (nStyleFamilyMask & SFX_STYLE_FAMILY_PSEUDO) != 0;
872 break;
873 case XML_STYLE_FAMILY_TEXT_OUTLINE:
874 case XML_STYLE_FAMILY_TEXT_FOOTNOTECONFIG:
875 case XML_STYLE_FAMILY_TEXT_ENDNOTECONFIG:
876 case XML_STYLE_FAMILY_TEXT_LINENUMBERINGCONFIG:
877 case XML_STYLE_FAMILY_TEXT_BIBLIOGRAPHYCONFIG:
878 bIns = !(rSwImport.IsInsertMode() || rSwImport.IsStylesOnlyMode() ||
879 rSwImport.IsBlockMode());
880 break;
881 default:
882 bIns = SvXMLStylesContext::InsertStyleFamily( nFamily );
883 break;
886 return bIns;
889 UniReference < SvXMLImportPropertyMapper > SwXMLStylesContext_Impl::GetImportPropertyMapper(
890 sal_uInt16 nFamily ) const
892 UniReference < SvXMLImportPropertyMapper > xMapper;
893 if( nFamily == XML_STYLE_FAMILY_TABLE_TABLE )
894 xMapper = XMLTextImportHelper::CreateTableDefaultExtPropMapper(
895 const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
896 else if( nFamily == XML_STYLE_FAMILY_TABLE_ROW )
897 xMapper = XMLTextImportHelper::CreateTableRowDefaultExtPropMapper(
898 const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
899 else
900 xMapper = SvXMLStylesContext::GetImportPropertyMapper( nFamily );
901 return xMapper;
904 uno::Reference < container::XNameContainer > SwXMLStylesContext_Impl::GetStylesContainer(
905 sal_uInt16 nFamily ) const
907 uno::Reference < container::XNameContainer > xStyles;
908 if( XML_STYLE_FAMILY_SD_GRAPHICS_ID == nFamily )
909 xStyles = ((SvXMLImport *)&GetImport())->GetTextImport()->GetFrameStyles();
910 else
911 xStyles = SvXMLStylesContext::GetStylesContainer( nFamily );
913 return xStyles;
916 OUString SwXMLStylesContext_Impl::GetServiceName( sal_uInt16 nFamily ) const
918 String sServiceName;
919 if( XML_STYLE_FAMILY_SD_GRAPHICS_ID == nFamily )
920 sServiceName = OUString("com.sun.star.style.FrameStyle");
921 else
922 sServiceName = SvXMLStylesContext::GetServiceName( nFamily );
924 return sServiceName;
927 void SwXMLStylesContext_Impl::EndElement()
929 GetSwImport().InsertStyles( IsAutomaticStyle() );
932 // ---------------------------------------------------------------------
934 class SwXMLMasterStylesContext_Impl : public XMLTextMasterStylesContext
936 protected:
937 virtual sal_Bool InsertStyleFamily( sal_uInt16 nFamily ) const;
939 SwXMLImport& GetSwImport() { return (SwXMLImport&)GetImport(); }
940 const SwXMLImport& GetSwImport() const
941 { return (const SwXMLImport&)GetImport(); }
943 public:
945 TYPEINFO();
947 SwXMLMasterStylesContext_Impl(
948 SwXMLImport& rImport, sal_uInt16 nPrfx,
949 const OUString& rLName ,
950 const uno::Reference< xml::sax::XAttributeList > & xAttrList );
951 virtual ~SwXMLMasterStylesContext_Impl();
952 virtual void EndElement();
955 TYPEINIT1( SwXMLMasterStylesContext_Impl, XMLTextMasterStylesContext );
957 SwXMLMasterStylesContext_Impl::SwXMLMasterStylesContext_Impl(
958 SwXMLImport& rImport, sal_uInt16 nPrfx,
959 const OUString& rLName ,
960 const uno::Reference< xml::sax::XAttributeList > & xAttrList ) :
961 XMLTextMasterStylesContext( rImport, nPrfx, rLName, xAttrList )
965 SwXMLMasterStylesContext_Impl::~SwXMLMasterStylesContext_Impl()
969 sal_Bool SwXMLMasterStylesContext_Impl::InsertStyleFamily( sal_uInt16 nFamily ) const
971 sal_Bool bIns;
973 const SwXMLImport& rSwImport = GetSwImport();
974 sal_uInt16 nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
975 if( XML_STYLE_FAMILY_MASTER_PAGE == nFamily )
976 bIns = (nStyleFamilyMask & SFX_STYLE_FAMILY_PAGE) != 0;
977 else
978 bIns = XMLTextMasterStylesContext::InsertStyleFamily( nFamily );
980 return bIns;
983 void SwXMLMasterStylesContext_Impl::EndElement()
985 FinishStyles( !GetSwImport().IsInsertMode() );
986 GetSwImport().FinishStyles();
988 // ---------------------------------------------------------------------
990 SvXMLImportContext *SwXMLImport::CreateStylesContext(
991 const OUString& rLocalName,
992 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
993 sal_Bool bAuto )
995 SvXMLStylesContext *pContext =
996 new SwXMLStylesContext_Impl( *this, XML_NAMESPACE_OFFICE, rLocalName,
997 xAttrList, bAuto );
998 if( bAuto )
999 SetAutoStyles( pContext );
1000 else
1001 SetStyles( pContext );
1003 return pContext;
1006 SvXMLImportContext *SwXMLImport::CreateMasterStylesContext(
1007 const OUString& rLocalName,
1008 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
1010 SvXMLStylesContext *pContext =
1011 new SwXMLMasterStylesContext_Impl( *this, XML_NAMESPACE_OFFICE, rLocalName,
1012 xAttrList );
1013 SetMasterStyles( pContext );
1015 return pContext;
1018 void SwXMLImport::InsertStyles( sal_Bool bAuto )
1020 if( bAuto && GetAutoStyles() )
1021 GetAutoStyles()->CopyAutoStylesToDoc();
1022 if( !bAuto && GetStyles() )
1023 GetStyles()->CopyStylesToDoc( !IsInsertMode(), sal_False );
1026 void SwXMLImport::FinishStyles()
1028 if( GetStyles() )
1029 GetStyles()->FinishStyles( !IsInsertMode() );
1032 void SwXMLImport::UpdateTxtCollConditions( SwDoc *pDoc )
1034 if( !pDoc )
1035 pDoc = SwImport::GetDocFromXMLImport( *this );
1037 const SwTxtFmtColls& rColls = *pDoc->GetTxtFmtColls();
1038 sal_uInt16 nCount = rColls.size();
1039 for( sal_uInt16 i=0; i < nCount; i++ )
1041 SwTxtFmtColl *pColl = rColls[i];
1042 if( pColl && RES_CONDTXTFMTCOLL == pColl->Which() )
1044 const SwFmtCollConditions& rConditions =
1045 ((const SwConditionTxtFmtColl *)pColl)->GetCondColls();
1046 bool bSendModify = false;
1047 for( sal_uInt16 j=0; j < rConditions.size() && !bSendModify; j++ )
1049 const SwCollCondition& rCond = rConditions[j];
1050 switch( rCond.GetCondition() )
1052 case PARA_IN_TABLEHEAD:
1053 case PARA_IN_TABLEBODY:
1054 case PARA_IN_FOOTER:
1055 case PARA_IN_HEADER:
1056 bSendModify = true;
1057 break;
1060 if( bSendModify )
1062 SwCondCollCondChg aMsg( pColl );
1063 pColl->ModifyNotification( &aMsg, &aMsg );
1069 bool SwXMLImport::FindAutomaticStyle(
1070 sal_uInt16 nFamily,
1071 const OUString& rName,
1072 const SfxItemSet **ppItemSet,
1073 OUString *pParent ) const
1075 SwXMLItemSetStyleContext_Impl *pStyle = 0;
1076 if( GetAutoStyles() )
1078 pStyle = PTR_CAST( SwXMLItemSetStyleContext_Impl,
1079 GetAutoStyles()->
1080 FindStyleChildContext( nFamily, rName,
1081 sal_True ) );
1082 if( pStyle )
1084 if( ppItemSet )
1086 if( XML_STYLE_FAMILY_TABLE_TABLE == pStyle->GetFamily() &&
1087 pStyle->HasMasterPageName() &&
1088 !pStyle->IsPageDescConnected() )
1089 pStyle->ConnectPageDesc();
1090 (*ppItemSet) = pStyle->GetItemSet();
1092 // resolve data style name late
1093 if( XML_STYLE_FAMILY_TABLE_CELL == pStyle->GetFamily() &&
1094 pStyle->ResolveDataStyleName() )
1096 (*ppItemSet) = pStyle->GetItemSet();
1101 if( pParent )
1102 *pParent = pStyle->GetParentName();
1106 return pStyle != 0;
1109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */