android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / filter / xml / xmlfmt.cxx
blob9bd4d2a5f41ba51553fedcffe43fd84b43d54fff
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>
21 #include <sal/log.hxx>
22 #include <osl/diagnose.h>
23 #include <comphelper/diagnose_ex.hxx>
24 #include <fmtcol.hxx>
25 #include <hints.hxx>
26 #include <doc.hxx>
27 #include <docary.hxx>
28 #include <IDocumentStylePoolAccess.hxx>
29 #include <unoprnms.hxx>
30 #include <fmtpdsc.hxx>
31 #include <pagedesc.hxx>
32 #include <xmloff/maptype.hxx>
33 #include <xmloff/xmlnumfi.hxx>
34 #include <xmloff/xmlprmap.hxx>
35 #include <xmloff/xmlnamespace.hxx>
36 #include <xmloff/xmlstyle.hxx>
37 #include <xmloff/txtstyli.hxx>
38 #include <xmloff/txtimp.hxx>
39 #include <xmloff/families.hxx>
40 #include <xmloff/XMLTextMasterStylesContext.hxx>
41 #include <xmloff/XMLTextShapeStyleContext.hxx>
42 #include <xmloff/XMLGraphicsDefaultStyle.hxx>
43 #include <xmloff/XMLDrawingPageStyleContext.hxx>
44 #include <xmloff/XMLTextMasterPageContext.hxx>
45 #include <xmloff/table/XMLTableImport.hxx>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/frame/XModel.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
49 #include "xmlimp.hxx"
50 #include <cellatr.hxx>
51 #include <SwStyleNameMapper.hxx>
52 #include <ccoll.hxx>
54 #include <memory>
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::uno;
59 using namespace ::xmloff::token;
61 namespace {
63 class SwXMLConditionParser_Impl
65 OUString m_sInput;
67 Master_CollCondition m_nCondition;
68 sal_uInt32 m_nSubCondition;
70 sal_Int32 m_nPos;
71 sal_Int32 m_nLength;
73 inline bool SkipWS();
74 inline bool MatchChar( sal_Unicode c );
75 inline bool MatchName( OUString& rName );
76 inline bool MatchNumber( sal_uInt32& rNumber );
78 public:
80 explicit SwXMLConditionParser_Impl( const OUString& rInp );
82 bool IsValid() const { return Master_CollCondition::NONE != m_nCondition; }
84 Master_CollCondition GetCondition() const { return m_nCondition; }
85 sal_uInt32 GetSubCondition() const { return m_nSubCondition; }
90 inline bool SwXMLConditionParser_Impl::SkipWS()
92 while( m_nPos < m_nLength && ' ' == m_sInput[m_nPos] )
93 m_nPos++;
94 return true;
97 inline bool SwXMLConditionParser_Impl::MatchChar( sal_Unicode c )
99 bool bRet = false;
100 if( m_nPos < m_nLength && c == m_sInput[m_nPos] )
102 m_nPos++;
103 bRet = true;
105 return bRet;
108 inline bool SwXMLConditionParser_Impl::MatchName( OUString& rName )
110 OUStringBuffer sBuffer( m_nLength );
111 while( m_nPos < m_nLength &&
112 ( ('a' <= m_sInput[m_nPos] && m_sInput[m_nPos] <= 'z') ||
113 '-' == m_sInput[m_nPos] ) )
115 sBuffer.append( m_sInput[m_nPos] );
116 m_nPos++;
118 rName = sBuffer.makeStringAndClear();
119 return !rName.isEmpty();
122 inline bool SwXMLConditionParser_Impl::MatchNumber( sal_uInt32& rNumber )
124 OUStringBuffer sBuffer( m_nLength );
125 while( m_nPos < m_nLength && '0' <= m_sInput[m_nPos] && m_sInput[m_nPos] <= '9' )
127 sBuffer.append( m_sInput[m_nPos] );
128 m_nPos++;
131 OUString sNum( sBuffer.makeStringAndClear() );
132 if( !sNum.isEmpty() )
133 rNumber = sNum.toInt32();
134 return !sNum.isEmpty();
137 SwXMLConditionParser_Impl::SwXMLConditionParser_Impl( const OUString& rInp ) :
138 m_sInput( rInp ),
139 m_nCondition( Master_CollCondition::NONE ),
140 m_nSubCondition( 0 ),
141 m_nPos( 0 ),
142 m_nLength( rInp.getLength() )
144 OUString sFunc;
145 bool bHasSub = false;
146 sal_uInt32 nSub = 0;
147 bool bOK = SkipWS() && MatchName( sFunc ) && SkipWS() &&
148 MatchChar( '(' ) && SkipWS() && MatchChar( ')' ) && SkipWS();
149 if( bOK && MatchChar( '=' ) )
151 bOK = SkipWS() && MatchNumber( nSub ) && SkipWS();
152 bHasSub = true;
155 bOK &= m_nPos == m_nLength;
157 if( !bOK )
158 return;
160 if( IsXMLToken( sFunc, XML_ENDNOTE ) && !bHasSub )
161 m_nCondition = Master_CollCondition::PARA_IN_ENDNOTE;
162 else if( IsXMLToken( sFunc, XML_FOOTER ) && !bHasSub )
163 m_nCondition = Master_CollCondition::PARA_IN_FOOTER;
164 else if( IsXMLToken( sFunc, XML_FOOTNOTE ) && !bHasSub )
165 m_nCondition = Master_CollCondition::PARA_IN_FOOTNOTE;
166 else if( IsXMLToken( sFunc, XML_HEADER ) && !bHasSub )
167 m_nCondition = Master_CollCondition::PARA_IN_HEADER;
168 else if( IsXMLToken( sFunc, XML_LIST_LEVEL) &&
169 nSub >=1 && nSub <= MAXLEVEL )
171 m_nCondition = Master_CollCondition::PARA_IN_LIST;
172 m_nSubCondition = nSub-1;
174 else if( IsXMLToken( sFunc, XML_OUTLINE_LEVEL) &&
175 nSub >=1 && nSub <= MAXLEVEL )
177 m_nCondition = Master_CollCondition::PARA_IN_OUTLINE;
178 m_nSubCondition = nSub-1;
180 else if( IsXMLToken( sFunc, XML_SECTION ) && !bHasSub )
182 m_nCondition = Master_CollCondition::PARA_IN_SECTION;
184 else if( IsXMLToken( sFunc, XML_TABLE ) && !bHasSub )
186 m_nCondition = Master_CollCondition::PARA_IN_TABLEBODY;
188 else if( IsXMLToken( sFunc, XML_TABLE_HEADER ) && !bHasSub )
190 m_nCondition = Master_CollCondition::PARA_IN_TABLEHEAD;
192 else if( IsXMLToken( sFunc, XML_TEXT_BOX ) && !bHasSub )
194 m_nCondition = Master_CollCondition::PARA_IN_FRAME;
198 namespace {
200 class SwXMLConditionContext_Impl : public SvXMLImportContext
202 Master_CollCondition m_nCondition;
203 sal_uInt32 m_nSubCondition;
205 OUString m_sApplyStyle;
207 public:
209 SwXMLConditionContext_Impl(
210 SvXMLImport& rImport, sal_Int32 nElement,
211 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList );
213 bool IsValid() const { return Master_CollCondition::NONE != m_nCondition; }
215 Master_CollCondition getCondition() const { return m_nCondition; }
216 sal_uInt32 getSubCondition() const { return m_nSubCondition; }
217 OUString const &getApplyStyle() const { return m_sApplyStyle; }
222 SwXMLConditionContext_Impl::SwXMLConditionContext_Impl(
223 SvXMLImport& rImport, sal_Int32 /*nElement*/,
224 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) :
225 SvXMLImportContext( rImport ),
226 m_nCondition( Master_CollCondition::NONE ),
227 m_nSubCondition( 0 )
229 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
231 OUString sValue = aIter.toString();
232 switch (aIter.getToken())
234 case XML_ELEMENT(STYLE, XML_CONDITION):
236 SwXMLConditionParser_Impl aCondParser( sValue );
237 if( aCondParser.IsValid() )
239 m_nCondition = aCondParser.GetCondition();
240 m_nSubCondition = aCondParser.GetSubCondition();
242 break;
244 case XML_ELEMENT(STYLE, XML_APPLY_STYLE_NAME):
245 m_sApplyStyle = sValue;
246 break;
247 default:
248 XMLOFF_WARN_UNKNOWN("sw", aIter);
253 typedef std::vector<rtl::Reference<SwXMLConditionContext_Impl>> SwXMLConditions_Impl;
255 namespace {
257 class SwXMLTextStyleContext_Impl : public XMLTextStyleContext
259 std::unique_ptr<SwXMLConditions_Impl> m_pConditions;
261 protected:
263 virtual uno::Reference < style::XStyle > Create() override;
264 virtual void Finish( bool bOverwrite ) override;
266 public:
269 SwXMLTextStyleContext_Impl( SwXMLImport& rImport,
270 XmlStyleFamily nFamily,
271 SvXMLStylesContext& rStyles );
273 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
274 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
279 uno::Reference < style::XStyle > SwXMLTextStyleContext_Impl::Create()
281 uno::Reference < style::XStyle > xNewStyle;
282 if( m_pConditions && XmlStyleFamily::TEXT_PARAGRAPH == GetFamily() )
284 uno::Reference< lang::XMultiServiceFactory > xFactory( GetImport().GetModel(),
285 uno::UNO_QUERY );
286 if( xFactory.is() )
288 uno::Reference < uno::XInterface > xIfc =
289 xFactory->createInstance( "com.sun.star.style.ConditionalParagraphStyle" );
290 if( xIfc.is() )
291 xNewStyle.set( xIfc, uno::UNO_QUERY );
294 else
296 xNewStyle = XMLTextStyleContext::Create();
299 return xNewStyle;
302 void
303 SwXMLTextStyleContext_Impl::Finish( bool bOverwrite )
305 if( m_pConditions && XmlStyleFamily::TEXT_PARAGRAPH == GetFamily() && GetStyle().is() )
307 CommandStruct const*const pCommands = SwCondCollItem::GetCmds();
309 Reference< XPropertySet > xPropSet( GetStyle(), UNO_QUERY );
311 uno::Sequence< beans::NamedValue > aSeq( m_pConditions->size() );
312 auto aSeqRange = asNonConstRange(aSeq);
314 for (std::vector<rtl::Reference<SwXMLConditionContext_Impl>>::size_type i = 0;
315 i < m_pConditions->size(); ++i)
317 assert((*m_pConditions)[i]->IsValid()); // checked before inserting
318 Master_CollCondition nCond = (*m_pConditions)[i]->getCondition();
319 sal_uInt32 nSubCond = (*m_pConditions)[i]->getSubCondition();
321 for (size_t j = 0; j < COND_COMMAND_COUNT; ++j)
323 if (pCommands[j].nCnd == nCond &&
324 pCommands[j].nSubCond == nSubCond)
326 aSeqRange[i].Name = GetCommandContextByIndex( j );
327 aSeqRange[i].Value <<= GetImport().GetStyleDisplayName(
328 GetFamily(), (*m_pConditions)[i]->getApplyStyle() );
329 break;
336 xPropSet->setPropertyValue(UNO_NAME_PARA_STYLE_CONDITIONS, uno::Any(aSeq));
338 catch (uno::Exception const&)
340 TOOLS_WARN_EXCEPTION("sw.xml", "exception when setting ParaStyleConditions");
343 XMLTextStyleContext::Finish( bOverwrite );
346 SwXMLTextStyleContext_Impl::SwXMLTextStyleContext_Impl( SwXMLImport& rImport,
347 XmlStyleFamily nFamily,
348 SvXMLStylesContext& rStyles ) :
349 XMLTextStyleContext( rImport, rStyles, nFamily )
353 css::uno::Reference< css::xml::sax::XFastContextHandler > SwXMLTextStyleContext_Impl::createFastChildContext(
354 sal_Int32 nElement,
355 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
357 if( nElement == XML_ELEMENT(STYLE, XML_MAP) )
359 rtl::Reference<SwXMLConditionContext_Impl> xCond{
360 new SwXMLConditionContext_Impl( GetImport(), nElement, xAttrList )};
361 if( xCond->IsValid() )
363 if( !m_pConditions )
364 m_pConditions = std::make_unique<SwXMLConditions_Impl>();
365 m_pConditions->push_back( xCond );
367 return xCond;
370 return XMLTextStyleContext::createFastChildContext( nElement, xAttrList );
373 namespace {
375 class SwXMLCellStyleContext : public XMLPropStyleContext
377 OUString m_sDataStyleName;
378 void AddDataFormat();
379 public:
380 using XMLPropStyleContext::XMLPropStyleContext;
381 virtual void FillPropertySet(const css::uno::Reference<css::beans::XPropertySet>& rPropSet) override;
382 virtual void SetAttribute(sal_Int32 nElement, const OUString& rValue) override;
385 class SwXMLItemSetStyleContext_Impl : public SvXMLStyleContext
387 OUString m_sMasterPageName;
388 std::optional<SfxItemSet> m_oItemSet;
389 SwXMLTextStyleContext_Impl *m_pTextStyle;
390 SvXMLStylesContext &m_rStyles;
392 OUString m_sDataStyleName;
394 bool m_bHasMasterPageName : 1;
395 bool m_bPageDescConnected : 1;
396 bool m_bDataStyleIsResolved;
398 SvXMLImportContext *CreateItemSetContext(
399 sal_Int32 nElement,
400 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList);
402 protected:
404 virtual void SetAttribute( sal_Int32 nElement,
405 const OUString& rValue ) override;
407 SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
409 public:
412 SwXMLItemSetStyleContext_Impl(
413 SwXMLImport& rImport,
414 SvXMLStylesContext& rStylesC,
415 XmlStyleFamily nFamily);
417 virtual void CreateAndInsert( bool bOverwrite ) override;
419 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
420 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
422 // The item set may be empty!
423 SfxItemSet *GetItemSet() { return m_oItemSet ? &*m_oItemSet : nullptr; }
425 bool HasMasterPageName() const { return m_bHasMasterPageName; }
427 bool IsPageDescConnected() const { return m_bPageDescConnected; }
428 void ConnectPageDesc();
430 bool ResolveDataStyleName();
435 void SwXMLCellStyleContext::AddDataFormat()
437 if (m_sDataStyleName.isEmpty() || IsDefaultStyle())
438 return;
440 const SvXMLNumFormatContext* pStyle = static_cast<const SvXMLNumFormatContext*>(
441 GetStyles()->FindStyleChildContext(XmlStyleFamily::DATA_STYLE, m_sDataStyleName, true));
443 if (!pStyle)
445 SAL_WARN("sw.xml", "not possible to get data style " << m_sDataStyleName);
446 return;
449 sal_Int32 nNumberFormat = const_cast<SvXMLNumFormatContext*>(pStyle)->GetKey();
450 if (nNumberFormat < 0)
451 return;
453 rtl::Reference<SvXMLImportPropertyMapper> xPropertyMapper(GetStyles()->GetImportPropertyMapper(GetFamily()));
454 if (!xPropertyMapper.is())
456 SAL_WARN("sw.xml", "there is no import prop mapper");
457 return;
460 const rtl::Reference<XMLPropertySetMapper>& xPropertySetMapper(xPropertyMapper->getPropertySetMapper());
461 sal_Int32 nIndex = xPropertySetMapper->GetEntryIndex(XML_NAMESPACE_STYLE, GetXMLToken(XML_DATA_STYLE_NAME), 0);
462 if (nIndex < 0)
464 SAL_WARN("sw.xml", "could not find id for " << GetXMLToken(XML_DATA_STYLE_NAME));
465 return;
468 auto aIter = std::find_if(GetProperties().begin(), GetProperties().end(),
469 [&nIndex](const XMLPropertyState& rProp) {
470 return rProp.mnIndex == nIndex;
473 if (aIter != GetProperties().end())
474 aIter->maValue <<= nNumberFormat;
475 else
476 GetProperties().push_back(XMLPropertyState(nIndex, Any(nNumberFormat)));
479 void SwXMLCellStyleContext::FillPropertySet(const css::uno::Reference<css::beans::XPropertySet>& rPropSet)
481 AddDataFormat();
482 XMLPropStyleContext::FillPropertySet(rPropSet);
485 void SwXMLCellStyleContext::SetAttribute(sal_Int32 nElement, const OUString& rValue)
487 if ((nElement & TOKEN_MASK) == XML_DATA_STYLE_NAME)
488 m_sDataStyleName = rValue;
489 else
490 XMLPropStyleContext::SetAttribute(nElement, rValue);
493 void SwXMLItemSetStyleContext_Impl::SetAttribute( sal_Int32 nElement,
494 const OUString& rValue )
496 switch(nElement)
498 case XML_ELEMENT(STYLE, XML_MASTER_PAGE_NAME):
500 m_sMasterPageName = rValue;
501 m_bHasMasterPageName = true;
502 break;
504 case XML_ELEMENT(STYLE, XML_DATA_STYLE_NAME):
506 // if we have a valid data style name
507 if (!rValue.isEmpty())
509 m_sDataStyleName = rValue;
510 m_bDataStyleIsResolved = false; // needs to be resolved
512 break;
514 default:
515 SvXMLStyleContext::SetAttribute( nElement, rValue );
519 SvXMLImportContext *SwXMLItemSetStyleContext_Impl::CreateItemSetContext(
520 sal_Int32 nElement,
521 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
523 OSL_ENSURE( !m_oItemSet,
524 "SwXMLItemSetStyleContext_Impl::CreateItemSetContext: item set exists" );
526 SvXMLImportContext *pContext = nullptr;
528 SwDoc* pDoc = GetSwImport().getDoc();
530 SfxItemPool& rItemPool = pDoc->GetAttrPool();
531 switch( GetFamily() )
533 case XmlStyleFamily::TABLE_TABLE:
534 m_oItemSet.emplace( rItemPool, aTableSetRange );
535 break;
536 case XmlStyleFamily::TABLE_COLUMN:
537 m_oItemSet.emplace( rItemPool, svl::Items<RES_FRM_SIZE, RES_FRM_SIZE> );
538 break;
539 case XmlStyleFamily::TABLE_ROW:
540 m_oItemSet.emplace( rItemPool, aTableLineSetRange );
541 break;
542 case XmlStyleFamily::TABLE_CELL:
543 m_oItemSet.emplace( rItemPool, aTableBoxSetRange );
544 break;
545 default:
546 OSL_ENSURE( false,
547 "SwXMLItemSetStyleContext_Impl::CreateItemSetContext: unknown family" );
548 break;
550 if( m_oItemSet )
551 pContext = GetSwImport().CreateTableItemImportContext(
552 nElement, xAttrList, GetFamily(),
553 *m_oItemSet );
554 if( !pContext )
556 m_oItemSet.reset();
559 return pContext;
563 SwXMLItemSetStyleContext_Impl::SwXMLItemSetStyleContext_Impl( SwXMLImport& rImport,
564 SvXMLStylesContext& rStylesC,
565 XmlStyleFamily nFamily ) :
566 SvXMLStyleContext( rImport, nFamily ),
567 m_pTextStyle( nullptr ),
568 m_rStyles( rStylesC ),
569 m_bHasMasterPageName( false ),
570 m_bPageDescConnected( false ),
571 m_bDataStyleIsResolved( true )
575 void SwXMLItemSetStyleContext_Impl::CreateAndInsert( bool bOverwrite )
577 if( m_pTextStyle )
578 m_pTextStyle->CreateAndInsert( bOverwrite );
581 css::uno::Reference< css::xml::sax::XFastContextHandler > SwXMLItemSetStyleContext_Impl::createFastChildContext(
582 sal_Int32 nElement,
583 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
585 switch (nElement)
587 case XML_ELEMENT(STYLE, XML_TABLE_PROPERTIES):
588 case XML_ELEMENT(STYLE, XML_TABLE_COLUMN_PROPERTIES):
589 case XML_ELEMENT(STYLE, XML_TABLE_ROW_PROPERTIES):
590 case XML_ELEMENT(STYLE, XML_TABLE_CELL_PROPERTIES):
591 return CreateItemSetContext( nElement, xAttrList );
592 case XML_ELEMENT(STYLE, XML_TEXT_PROPERTIES):
593 case XML_ELEMENT(STYLE, XML_PARAGRAPH_PROPERTIES):
595 if( !m_pTextStyle )
597 m_pTextStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), XmlStyleFamily::TEXT_PARAGRAPH, m_rStyles );
598 rtl::Reference<sax_fastparser::FastAttributeList> xTmpAttrList = new sax_fastparser::FastAttributeList(nullptr);
599 xTmpAttrList->add(XML_ELEMENT(STYLE, XML_NAME), GetName().toUtf8() );
600 m_pTextStyle->startFastElement( nElement, xTmpAttrList );
601 m_rStyles.AddStyle( *m_pTextStyle );
603 return m_pTextStyle->createFastChildContext( nElement, xAttrList );
605 default:
606 XMLOFF_WARN_UNKNOWN_ELEMENT("sw", nElement);
609 return nullptr;
612 void SwXMLItemSetStyleContext_Impl::ConnectPageDesc()
614 if( m_bPageDescConnected || !HasMasterPageName() )
615 return;
616 m_bPageDescConnected = true;
618 SwDoc *pDoc = GetSwImport().getDoc();
620 // #i40788# - first determine the display name of the page style,
621 // then map this name to the corresponding user interface name.
622 OUString sName = GetImport().GetStyleDisplayName( XmlStyleFamily::MASTER_PAGE,
623 m_sMasterPageName );
624 SwStyleNameMapper::FillUIName( sName,
625 sName,
626 SwGetPoolIdFromName::PageDesc);
627 SwPageDesc *pPageDesc = pDoc->FindPageDesc(sName);
628 if( !pPageDesc )
630 // If the page style is a pool style, then we maybe have to create it
631 // first if it hasn't been used by now.
632 const sal_uInt16 nPoolId = SwStyleNameMapper::GetPoolIdFromUIName( sName, SwGetPoolIdFromName::PageDesc );
633 if( USHRT_MAX != nPoolId )
634 pPageDesc = pDoc->getIDocumentStylePoolAccess().GetPageDescFromPool( nPoolId, false );
637 if( !pPageDesc )
638 return;
640 if( !m_oItemSet )
642 SfxItemPool& rItemPool = pDoc->GetAttrPool();
643 m_oItemSet.emplace( rItemPool, aTableSetRange );
646 std::unique_ptr<SwFormatPageDesc> pFormatPageDesc;
647 if( const SwFormatPageDesc* pItem = m_oItemSet->GetItemIfSet( RES_PAGEDESC, false ) )
649 if( pItem->GetPageDesc() != pPageDesc )
650 pFormatPageDesc.reset(new SwFormatPageDesc( *pItem ));
652 else
653 pFormatPageDesc.reset(new SwFormatPageDesc());
655 if( pFormatPageDesc )
657 pFormatPageDesc->RegisterToPageDesc( *pPageDesc );
658 m_oItemSet->Put( std::move(pFormatPageDesc) );
662 bool SwXMLItemSetStyleContext_Impl::ResolveDataStyleName()
664 // resolve, if not already done
665 if (! m_bDataStyleIsResolved)
667 // get the format key
668 sal_Int32 nFormat =
669 GetImport().GetTextImport()->GetDataStyleKey(m_sDataStyleName);
671 // if the key is valid, insert Item into ItemSet
672 if( -1 != nFormat )
674 if( !m_oItemSet )
676 SwDoc *pDoc = GetSwImport().getDoc();
678 SfxItemPool& rItemPool = pDoc->GetAttrPool();
679 m_oItemSet.emplace( rItemPool, aTableBoxSetRange );
681 SwTableBoxNumFormat aNumFormatItem(nFormat);
682 m_oItemSet->Put(aNumFormatItem);
685 // now resolved
686 m_bDataStyleIsResolved = true;
687 return true;
689 else
691 // was already resolved; nothing to do
692 return false;
696 namespace {
698 class SwXMLStylesContext_Impl : public SvXMLStylesContext
700 SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
701 const SwXMLImport& GetSwImport() const
702 { return static_cast<const SwXMLImport&>(GetImport()); }
704 protected:
706 using SvXMLStylesContext::CreateStyleChildContext;
707 virtual SvXMLStyleContext *CreateStyleChildContext( sal_Int32 nElement,
708 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList ) override;
710 using SvXMLStylesContext::CreateStyleStyleChildContext;
711 virtual SvXMLStyleContext *CreateStyleStyleChildContext( XmlStyleFamily nFamily,
712 sal_Int32 nElement,
713 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) override;
714 using SvXMLStylesContext::CreateDefaultStyleStyleChildContext;
715 virtual SvXMLStyleContext *CreateDefaultStyleStyleChildContext(
716 XmlStyleFamily nFamily, sal_Int32 nElement,
717 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList ) override;
718 // HACK
719 virtual rtl::Reference < SvXMLImportPropertyMapper > GetImportPropertyMapper(
720 XmlStyleFamily nFamily ) const override;
722 virtual uno::Reference < container::XNameContainer >
723 GetStylesContainer( XmlStyleFamily nFamily ) const override;
724 virtual OUString GetServiceName( XmlStyleFamily nFamily ) const override;
725 // HACK
727 public:
729 SwXMLStylesContext_Impl(
730 SwXMLImport& rImport,
731 bool bAuto );
733 virtual bool InsertStyleFamily( XmlStyleFamily nFamily ) const override;
735 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
740 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleChildContext(
741 sal_Int32 nElement,
742 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList )
744 SvXMLStyleContext* pContext = nullptr;
746 if(nElement == XML_ELEMENT(TABLE, XML_TABLE_TEMPLATE))
748 rtl::Reference<XMLTableImport> xTableImport = GetImport().GetShapeImport()->GetShapeTableImport();
749 pContext = xTableImport->CreateTableTemplateContext(nElement, xAttrList);
751 if (!pContext)
752 pContext = SvXMLStylesContext::CreateStyleChildContext(nElement, xAttrList);
754 return pContext;
757 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateStyleStyleChildContext(
758 XmlStyleFamily nFamily, sal_Int32 nElement,
759 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
761 SvXMLStyleContext *pStyle = nullptr;
763 switch( nFamily )
765 case XmlStyleFamily::TEXT_PARAGRAPH:
766 pStyle = new SwXMLTextStyleContext_Impl( GetSwImport(), nFamily, *this );
767 break;
768 case XmlStyleFamily::TABLE_TABLE:
769 case XmlStyleFamily::TABLE_COLUMN:
770 case XmlStyleFamily::TABLE_ROW:
771 case XmlStyleFamily::TABLE_CELL:
772 // Distinguish real and automatic styles.
773 if (IsAutomaticStyle())
774 pStyle = new SwXMLItemSetStyleContext_Impl(GetSwImport(), *this, nFamily);
775 else if (nFamily == XmlStyleFamily::TABLE_CELL) // Real cell styles are used for table-template import.
776 pStyle = new SwXMLCellStyleContext(GetSwImport(), *this, nFamily);
777 else
778 SAL_WARN("sw.xml", "Context does not exists for non automatic table, column or row style.");
779 break;
780 case XmlStyleFamily::SD_GRAPHICS_ID:
781 // As long as there are no element items, we can use the text
782 // style class.
783 pStyle = new XMLTextShapeStyleContext( GetImport(), *this, nFamily );
784 break;
785 case XmlStyleFamily::SD_DRAWINGPAGE_ID:
786 pStyle = new XMLDrawingPageStyleContext(GetImport(),
787 *this, g_MasterPageContextIDs, g_MasterPageFamilies);
788 break;
789 default:
790 pStyle = SvXMLStylesContext::CreateStyleStyleChildContext( nFamily,
791 nElement,
792 xAttrList );
793 break;
796 return pStyle;
799 SvXMLStyleContext *SwXMLStylesContext_Impl::CreateDefaultStyleStyleChildContext(
800 XmlStyleFamily nFamily, sal_Int32 nElement,
801 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
803 SvXMLStyleContext *pStyle = nullptr;
805 switch( nFamily )
807 case XmlStyleFamily::TEXT_PARAGRAPH:
808 case XmlStyleFamily::TABLE_TABLE:
809 case XmlStyleFamily::TABLE_ROW:
810 pStyle = new XMLTextStyleContext( GetImport(),
811 *this, nFamily,
812 true );
813 break;
814 case XmlStyleFamily::SD_GRAPHICS_ID:
815 // There are no writer specific defaults for graphic styles!
816 pStyle = new XMLGraphicsDefaultStyle( GetImport(), *this );
817 break;
818 default:
819 pStyle = SvXMLStylesContext::CreateDefaultStyleStyleChildContext( nFamily,
820 nElement,
821 xAttrList );
822 break;
825 return pStyle;
828 SwXMLStylesContext_Impl::SwXMLStylesContext_Impl(
829 SwXMLImport& rImport,
830 bool bAuto ) :
831 SvXMLStylesContext( rImport, bAuto )
835 bool SwXMLStylesContext_Impl::InsertStyleFamily( XmlStyleFamily nFamily ) const
837 const SwXMLImport& rSwImport = GetSwImport();
838 const SfxStyleFamily nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
840 bool bIns = true;
841 switch( nFamily )
843 case XmlStyleFamily::TEXT_PARAGRAPH:
844 bIns = bool(nStyleFamilyMask & SfxStyleFamily::Para);
845 break;
846 case XmlStyleFamily::TEXT_TEXT:
847 bIns = bool(nStyleFamilyMask & SfxStyleFamily::Char);
848 break;
849 case XmlStyleFamily::SD_GRAPHICS_ID:
850 bIns = bool(nStyleFamilyMask & SfxStyleFamily::Frame);
851 break;
852 case XmlStyleFamily::TEXT_LIST:
853 bIns = bool(nStyleFamilyMask & SfxStyleFamily::Pseudo);
854 break;
855 case XmlStyleFamily::TEXT_OUTLINE:
856 case XmlStyleFamily::TEXT_FOOTNOTECONFIG:
857 case XmlStyleFamily::TEXT_ENDNOTECONFIG:
858 case XmlStyleFamily::TEXT_LINENUMBERINGCONFIG:
859 case XmlStyleFamily::TEXT_BIBLIOGRAPHYCONFIG:
860 bIns = !(rSwImport.IsInsertMode() || rSwImport.IsStylesOnlyMode() ||
861 rSwImport.IsBlockMode());
862 break;
863 default:
864 bIns = SvXMLStylesContext::InsertStyleFamily( nFamily );
865 break;
868 return bIns;
871 rtl::Reference < SvXMLImportPropertyMapper > SwXMLStylesContext_Impl::GetImportPropertyMapper(
872 XmlStyleFamily nFamily ) const
874 rtl::Reference < SvXMLImportPropertyMapper > xMapper;
875 if( nFamily == XmlStyleFamily::TABLE_TABLE )
876 xMapper = XMLTextImportHelper::CreateTableDefaultExtPropMapper(
877 const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
878 else if( nFamily == XmlStyleFamily::TABLE_ROW )
879 xMapper = XMLTextImportHelper::CreateTableRowDefaultExtPropMapper(
880 const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
881 else if( nFamily == XmlStyleFamily::TABLE_CELL )
882 xMapper = XMLTextImportHelper::CreateTableCellExtPropMapper(
883 const_cast<SwXMLStylesContext_Impl*>( this )->GetImport() );
884 else if (nFamily == XmlStyleFamily::SD_DRAWINGPAGE_ID)
886 xMapper = XMLTextImportHelper::CreateDrawingPageExtPropMapper(
887 const_cast<SwXMLStylesContext_Impl*>(this)->GetImport());
889 else
890 xMapper = SvXMLStylesContext::GetImportPropertyMapper( nFamily );
891 return xMapper;
894 uno::Reference < container::XNameContainer > SwXMLStylesContext_Impl::GetStylesContainer(
895 XmlStyleFamily nFamily ) const
897 uno::Reference < container::XNameContainer > xStyles;
898 if( XmlStyleFamily::SD_GRAPHICS_ID == nFamily )
899 xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetFrameStyles();
900 else if( XmlStyleFamily::TABLE_CELL == nFamily )
901 xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetCellStyles();
903 if (!xStyles.is())
904 xStyles = SvXMLStylesContext::GetStylesContainer( nFamily );
906 return xStyles;
909 OUString SwXMLStylesContext_Impl::GetServiceName( XmlStyleFamily nFamily ) const
911 if( XmlStyleFamily::SD_GRAPHICS_ID == nFamily )
912 return "com.sun.star.style.FrameStyle";
913 else if( XmlStyleFamily::TABLE_CELL == nFamily )
914 return "com.sun.star.style.CellStyle";
916 return SvXMLStylesContext::GetServiceName( nFamily );
919 void SwXMLStylesContext_Impl::endFastElement(sal_Int32 )
921 GetSwImport().InsertStyles( IsAutomaticStyle() );
922 if (!IsAutomaticStyle())
923 GetImport().GetShapeImport()->GetShapeTableImport()->finishStyles();
926 namespace {
928 class SwXMLMasterStylesContext_Impl : public XMLTextMasterStylesContext
930 protected:
931 virtual bool InsertStyleFamily( XmlStyleFamily nFamily ) const override;
933 SwXMLImport& GetSwImport() { return static_cast<SwXMLImport&>(GetImport()); }
934 const SwXMLImport& GetSwImport() const
935 { return static_cast<const SwXMLImport&>(GetImport()); }
937 public:
940 SwXMLMasterStylesContext_Impl( SwXMLImport& rImport );
942 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
947 SwXMLMasterStylesContext_Impl::SwXMLMasterStylesContext_Impl(
948 SwXMLImport& rImport ) :
949 XMLTextMasterStylesContext( rImport )
953 bool SwXMLMasterStylesContext_Impl::InsertStyleFamily( XmlStyleFamily nFamily ) const
955 bool bIns;
957 const SwXMLImport& rSwImport = GetSwImport();
958 const SfxStyleFamily nStyleFamilyMask = rSwImport.GetStyleFamilyMask();
959 if( XmlStyleFamily::MASTER_PAGE == nFamily )
960 bIns = bool(nStyleFamilyMask & SfxStyleFamily::Page);
961 else
962 bIns = XMLTextMasterStylesContext::InsertStyleFamily( nFamily );
964 return bIns;
967 void SwXMLMasterStylesContext_Impl::endFastElement(sal_Int32 )
969 FinishStyles( !GetSwImport().IsInsertMode() );
970 GetSwImport().FinishStyles();
973 SvXMLImportContext *SwXMLImport::CreateStylesContext(
974 bool bAuto )
976 SvXMLStylesContext *pContext = new SwXMLStylesContext_Impl( *this, bAuto );
977 if( bAuto )
978 SetAutoStyles( pContext );
979 else
980 SetStyles( pContext );
982 return pContext;
985 SvXMLImportContext *SwXMLImport::CreateMasterStylesContext()
987 SvXMLStylesContext *pContext =
988 new SwXMLMasterStylesContext_Impl( *this );
989 SetMasterStyles( pContext );
991 return pContext;
994 void SwXMLImport::InsertStyles( bool bAuto )
996 if( bAuto && GetAutoStyles() )
997 GetAutoStyles()->CopyAutoStylesToDoc();
998 if( !bAuto && GetStyles() )
999 GetStyles()->CopyStylesToDoc( !IsInsertMode(), false );
1002 void SwXMLImport::FinishStyles()
1004 if( GetStyles() )
1005 GetStyles()->FinishStyles( !IsInsertMode() );
1008 void SwXMLImport::UpdateTextCollConditions( SwDoc *pDoc )
1010 if( !pDoc )
1011 pDoc = getDoc();
1013 const SwTextFormatColls& rColls = *pDoc->GetTextFormatColls();
1014 const size_t nCount = rColls.size();
1015 for( size_t i=0; i < nCount; ++i )
1017 SwTextFormatColl *pColl = rColls[i];
1018 if( pColl && RES_CONDTXTFMTCOLL == pColl->Which() )
1020 const SwFormatCollConditions& rConditions =
1021 static_cast<const SwConditionTextFormatColl *>(pColl)->GetCondColls();
1022 bool bSendModify = false;
1023 for( size_t j=0; j < rConditions.size() && !bSendModify; ++j )
1025 const SwCollCondition& rCond = *rConditions[j];
1026 switch( rCond.GetCondition() )
1028 case Master_CollCondition::PARA_IN_TABLEHEAD:
1029 case Master_CollCondition::PARA_IN_TABLEBODY:
1030 case Master_CollCondition::PARA_IN_FOOTER:
1031 case Master_CollCondition::PARA_IN_HEADER:
1032 bSendModify = true;
1033 break;
1034 default: break;
1037 if(bSendModify)
1038 pColl->GetNotifier().Broadcast(sw::CondCollCondChg(*pColl));
1043 bool SwXMLImport::FindAutomaticStyle(
1044 XmlStyleFamily nFamily,
1045 const OUString& rName,
1046 const SfxItemSet **ppItemSet ) const
1048 SwXMLItemSetStyleContext_Impl *pStyle = nullptr;
1049 if( GetAutoStyles() )
1051 pStyle = const_cast<SwXMLItemSetStyleContext_Impl*>(dynamic_cast< const SwXMLItemSetStyleContext_Impl* >(
1052 GetAutoStyles()->
1053 FindStyleChildContext( nFamily, rName,
1054 true ) ) );
1055 if( pStyle )
1057 if( ppItemSet )
1059 if( XmlStyleFamily::TABLE_TABLE == pStyle->GetFamily() &&
1060 pStyle->HasMasterPageName() &&
1061 !pStyle->IsPageDescConnected() )
1062 pStyle->ConnectPageDesc();
1063 (*ppItemSet) = pStyle->GetItemSet();
1065 // resolve data style name late
1066 if( XmlStyleFamily::TABLE_CELL == pStyle->GetFamily() &&
1067 pStyle->ResolveDataStyleName() )
1069 (*ppItemSet) = pStyle->GetItemSet();
1076 return pStyle != nullptr;
1079 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */