Update git submodules
[LibreOffice.git] / xmloff / source / style / xmlstyle.cxx
blobf1b3526ef97074840088b7a4d5017ba07d849a90
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 <config_wasm_strip.h>
22 #include <sal/config.h>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/frame/XModel.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
28 #include <com/sun/star/style/XAutoStylesSupplier.hpp>
29 #include <com/sun/star/style/XAutoStyleFamily.hpp>
30 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
31 #include <PageMasterPropMapper.hxx>
33 #include <comphelper/diagnose_ex.hxx>
34 #include <sal/log.hxx>
35 #include <svl/style.hxx>
36 #include <utility>
37 #include <xmloff/xmlnamespace.hxx>
38 #include <xmloff/xmltoken.hxx>
40 #include <xmloff/families.hxx>
41 #include <xmloff/xmlimp.hxx>
42 #include <xmloff/xmlnumi.hxx>
43 #include <xmloff/xmlimppr.hxx>
44 #include <xmloff/xmlstyle.hxx>
45 #include <xmloff/txtstyli.hxx>
46 #include <xmloff/xmlnumfi.hxx>
47 #include <XMLChartStyleContext.hxx>
48 #include <XMLChartPropertySetMapper.hxx>
49 #include <XMLThemeContext.hxx>
50 #include <xmloff/XMLShapeStyleContext.hxx>
51 #include "FillStyleContext.hxx"
52 #include <XMLFootnoteConfigurationImportContext.hxx>
53 #include <XMLIndexBibliographyConfigurationContext.hxx>
54 #include <XMLLineNumberingImportContext.hxx>
55 #include <PageMasterImportContext.hxx>
56 #include "PageMasterImportPropMapper.hxx"
58 #include <memory>
59 #include <set>
60 #include <string_view>
61 #include <vector>
63 using namespace ::com::sun::star;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::container;
66 using namespace ::com::sun::star::style;
67 using namespace ::xmloff::token;
69 constexpr OUStringLiteral gsParaStyleServiceName( u"com.sun.star.style.ParagraphStyle" );
70 constexpr OUStringLiteral gsTextStyleServiceName( u"com.sun.star.style.CharacterStyle" );
71 constexpr OUString gsParagraphStyles(u"ParagraphStyles"_ustr);
72 constexpr OUString gsCharacterStyles(u"CharacterStyles"_ustr);
74 void SvXMLStyleContext::SetAttribute( sal_Int32 nElement,
75 const OUString& rValue )
77 switch (nElement)
79 case XML_ELEMENT(STYLE, XML_FAMILY):
81 if( IsXMLToken( rValue, XML_PARAGRAPH ) )
82 mnFamily = XmlStyleFamily(SfxStyleFamily::Para);
83 else if( IsXMLToken( rValue, XML_TEXT ) )
84 mnFamily = XmlStyleFamily(SfxStyleFamily::Char);
85 break;
87 case XML_ELEMENT(STYLE, XML_NAME):
88 maName = rValue;
89 break;
90 case XML_ELEMENT(STYLE, XML_DISPLAY_NAME):
91 maDisplayName = rValue;
92 break;
93 case XML_ELEMENT(STYLE, XML_PARENT_STYLE_NAME):
94 maParentName = rValue;
95 break;
96 case XML_ELEMENT(STYLE, XML_NEXT_STYLE_NAME):
97 maFollow = rValue;
98 break;
99 case XML_ELEMENT(LO_EXT, XML_LINKED_STYLE_NAME):
100 maLinked = rValue;
101 break;
102 case XML_ELEMENT(STYLE, XML_HIDDEN):
103 case XML_ELEMENT(LO_EXT, XML_HIDDEN):
104 mbHidden = rValue.toBoolean();
105 break;
110 SvXMLStyleContext::SvXMLStyleContext(
111 SvXMLImport& rImp,
112 XmlStyleFamily nFam, bool bDefault ) :
113 SvXMLImportContext( rImp ),
114 mbHidden( false ),
115 mnFamily( nFam ),
116 mbValid( true ),
117 mbNew( true ),
118 mbDefaultStyle( bDefault )
122 SvXMLStyleContext::~SvXMLStyleContext()
126 void SvXMLStyleContext::startFastElement(
127 sal_Int32 /*nElement*/,
128 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
130 for( auto &it : sax_fastparser::castToFastAttributeList( xAttrList ) )
131 SetAttribute( it.getToken(), it.toString() );
134 void SvXMLStyleContext::SetDefaults()
138 void SvXMLStyleContext::CreateAndInsert( bool /*bOverwrite*/ )
142 void SvXMLStyleContext::CreateAndInsertLate( bool /*bOverwrite*/ )
146 void SvXMLStyleContext::Finish( bool /*bOverwrite*/ )
150 bool SvXMLStyleContext::IsTransient() const
152 return false;
155 namespace {
156 struct StyleIndexCompareByName
158 bool operator()(const SvXMLStyleContext* r1, const SvXMLStyleContext* r2) const
160 if( r1->GetFamily() < r2->GetFamily() )
161 return true;
162 if( r1->GetFamily() > r2->GetFamily() )
163 return false;
164 return r1->GetName() < r2->GetName();
167 struct StyleIndexCompareByDisplayName
169 bool operator()(const SvXMLStyleContext* r1, const SvXMLStyleContext* r2) const
171 if( r1->GetFamily() < r2->GetFamily() )
172 return true;
173 if( r1->GetFamily() > r2->GetFamily() )
174 return false;
175 return r1->GetDisplayName() < r2->GetDisplayName();
180 class SvXMLStylesContext_Impl
182 std::vector<rtl::Reference<SvXMLStyleContext>> aStyles;
183 // it would be better if we could share one vector for the styles and the index, but some code in calc
184 // is sensitive to having styles re-ordered
185 mutable SvXMLStylesContext::StyleIndex maStylesIndexByName;
186 mutable SvXMLStylesContext::StyleIndex maStylesIndexByDisplayName;
187 bool bAutomaticStyle;
189 #if OSL_DEBUG_LEVEL > 0
190 mutable sal_uInt32 m_nIndexCreated;
191 #endif
193 public:
194 explicit SvXMLStylesContext_Impl( bool bAuto );
196 size_t GetStyleCount() const { return aStyles.size(); }
198 SvXMLStyleContext *GetStyle( size_t i )
200 return i < aStyles.size() ? aStyles[ i ].get() : nullptr;
203 inline void AddStyle( SvXMLStyleContext *pStyle );
204 void dispose();
206 const SvXMLStyleContext *FindStyleChildContext( XmlStyleFamily nFamily,
207 const OUString& rName,
208 bool bCreateIndex ) const;
210 std::pair<SvXMLStylesContext::StyleIndex::const_iterator, SvXMLStylesContext::StyleIndex::const_iterator>
211 FindStyleChildContextByDisplayNamePrefix( XmlStyleFamily nFamily,
212 const OUString& rPrefix ) const;
214 bool IsAutomaticStyle() const { return bAutomaticStyle; }
216 private:
217 void BuildNameIndex() const;
218 void BuildDisplayNameIndex() const;
221 SvXMLStylesContext_Impl::SvXMLStylesContext_Impl( bool bAuto ) :
222 bAutomaticStyle( bAuto )
223 #if OSL_DEBUG_LEVEL > 0
224 , m_nIndexCreated( 0 )
225 #endif
228 inline void SvXMLStylesContext_Impl::AddStyle( SvXMLStyleContext *pStyle )
230 #if OSL_DEBUG_LEVEL > 0
231 // for (auto const & xStyle : aStyles)
232 // if (xStyle->GetFamily() == pStyle->GetFamily() && xStyle->GetName() == pStyle->GetName())
233 // assert(false && "duplicate style");
234 #endif
235 aStyles.emplace_back(pStyle );
237 maStylesIndexByName.clear();
238 maStylesIndexByDisplayName.clear();
241 void SvXMLStylesContext_Impl::dispose()
243 aStyles.clear();
246 const SvXMLStyleContext *SvXMLStylesContext_Impl::FindStyleChildContext( XmlStyleFamily nFamily,
247 const OUString& rName,
248 bool bCreateIndex ) const
250 const SvXMLStyleContext *pStyle = nullptr;
252 if( maStylesIndexByName.empty() && bCreateIndex && !aStyles.empty() )
253 BuildNameIndex();
255 if( !maStylesIndexByName.empty() )
257 auto it = std::lower_bound(maStylesIndexByName.begin(), maStylesIndexByName.end(), true,
258 [&nFamily, &rName](const SvXMLStyleContext* lhs, bool /*rhs*/)
260 if (lhs->GetFamily() < nFamily)
261 return true;
262 if (lhs->GetFamily() > nFamily)
263 return false;
264 return lhs->GetName() < rName;
266 if (it != maStylesIndexByName.end() && (*it)->GetFamily() == nFamily && (*it)->GetName() == rName)
267 pStyle = *it;
269 else
271 for( size_t i = 0; !pStyle && i < aStyles.size(); i++ )
273 const SvXMLStyleContext *pS = aStyles[ i ].get();
274 if( pS->GetFamily() == nFamily &&
275 pS->GetName() == rName )
276 pStyle = pS;
279 return pStyle;
282 namespace
284 struct PrefixProbeLowerBound
286 XmlStyleFamily nFamily;
287 const OUString& rPrefix;
289 bool operator()(const SvXMLStyleContext* lhs, bool /*rhs*/)
291 if (lhs->GetFamily() < nFamily)
292 return true;
293 if (lhs->GetFamily() > nFamily)
294 return false;
295 return lhs->GetDisplayName() < rPrefix;
298 struct PrefixProbeUpperBound
300 XmlStyleFamily nFamily;
301 const OUString& rPrefix;
303 bool operator()(bool /*lhs*/, const SvXMLStyleContext* rhs)
305 if (nFamily < rhs->GetFamily())
306 return true;
307 if (nFamily > rhs->GetFamily())
308 return false;
309 std::u16string_view rhsName = rhs->GetDisplayName();
310 // For the upper bound we want to view the vector's data as if
311 // every element was truncated to the size of the prefix.
312 // Then perform a normal match.
313 rhsName = rhsName.substr(0, rPrefix.getLength());
314 // compare UP TO the length of the prefix and no farther
315 if (int cmp = rPrefix.compareTo(rhsName))
316 return cmp < 0;
317 // The strings are equal to the length of the prefix so
318 // behave as if they are equal. That means s1 < s2 == false
319 return false;
324 std::pair<SvXMLStylesContext::StyleIndex::const_iterator, SvXMLStylesContext::StyleIndex::const_iterator>
325 SvXMLStylesContext_Impl::FindStyleChildContextByDisplayNamePrefix( XmlStyleFamily nFamily,
326 const OUString& rPrefix ) const
328 if( maStylesIndexByDisplayName.empty() )
329 BuildDisplayNameIndex();
330 auto itStart = std::lower_bound(maStylesIndexByDisplayName.begin(), maStylesIndexByDisplayName.end(), true, PrefixProbeLowerBound{nFamily,rPrefix});
331 auto itEnd = std::upper_bound(itStart, maStylesIndexByDisplayName.end(), true, PrefixProbeUpperBound{nFamily,rPrefix});
332 return {itStart, itEnd};
335 void SvXMLStylesContext_Impl::BuildNameIndex() const
337 maStylesIndexByName.reserve(aStyles.size());
339 for (const auto & i : aStyles)
340 maStylesIndexByName.push_back(i.get());
341 std::sort(maStylesIndexByName.begin(), maStylesIndexByName.end(), StyleIndexCompareByName());
343 #if OSL_DEBUG_LEVEL > 0
344 SAL_WARN_IF(0 != m_nIndexCreated, "xmloff.style",
345 "Performance warning: sdbcx::Index created multiple times");
346 ++m_nIndexCreated;
347 #endif
350 void SvXMLStylesContext_Impl::BuildDisplayNameIndex() const
352 maStylesIndexByDisplayName.reserve(aStyles.size());
353 for (const auto & i : aStyles)
354 maStylesIndexByDisplayName.push_back(i.get());
355 std::sort(maStylesIndexByDisplayName.begin(), maStylesIndexByDisplayName.end(), StyleIndexCompareByDisplayName());
358 sal_uInt32 SvXMLStylesContext::GetStyleCount() const
360 return mpImpl->GetStyleCount();
363 SvXMLStyleContext *SvXMLStylesContext::GetStyle( sal_uInt32 i )
365 return mpImpl->GetStyle( i );
368 const SvXMLStyleContext *SvXMLStylesContext::GetStyle( sal_uInt32 i ) const
370 return mpImpl->GetStyle( i );
373 bool SvXMLStylesContext::IsAutomaticStyle() const
375 return mpImpl->IsAutomaticStyle();
378 SvXMLStyleContext *SvXMLStylesContext::CreateStyleChildContext(
379 sal_Int32 nElement,
380 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
382 SvXMLStyleContext *pStyle = nullptr;
384 if(GetImport().GetDataStylesImport())
386 pStyle = GetImport().GetDataStylesImport()->CreateChildContext(GetImport(), nElement,
387 xAttrList, *this);
388 if (pStyle)
389 return pStyle;
392 switch (nElement)
394 case XML_ELEMENT(STYLE, XML_STYLE):
395 case XML_ELEMENT(STYLE, XML_DEFAULT_STYLE):
397 XmlStyleFamily nFamily = XmlStyleFamily::DATA_STYLE;
398 for( auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ) )
400 if( aIter.getToken() == XML_ELEMENT(STYLE, XML_FAMILY) )
402 nFamily = GetFamily( aIter.toString() );
403 break;
406 pStyle = XML_ELEMENT(STYLE, XML_STYLE)==nElement
407 ? CreateStyleStyleChildContext( nFamily, nElement, xAttrList )
408 : CreateDefaultStyleStyleChildContext( nFamily, nElement, xAttrList );
409 break;
411 case XML_ELEMENT(TEXT, XML_BIBLIOGRAPHY_CONFIGURATION):
412 pStyle = new XMLIndexBibliographyConfigurationContext(GetImport());
413 break;
414 case XML_ELEMENT(TEXT, XML_NOTES_CONFIGURATION):
415 pStyle = new XMLFootnoteConfigurationImportContext(
416 GetImport(), nElement, xAttrList);
417 break;
418 case XML_ELEMENT(TEXT, XML_LINENUMBERING_CONFIGURATION):
419 pStyle = new XMLLineNumberingImportContext(GetImport());
420 break;
421 case XML_ELEMENT(STYLE, XML_PAGE_LAYOUT):
422 case XML_ELEMENT(STYLE, XML_DEFAULT_PAGE_LAYOUT):
424 //there is not page family in ODF now, so I specify one for it
425 bool bDefaultStyle = XML_ELEMENT(STYLE, XML_DEFAULT_PAGE_LAYOUT) == nElement;
426 pStyle = new PageStyleContext( GetImport(), *this, bDefaultStyle );
428 break;
429 case XML_ELEMENT(TEXT, XML_LIST_STYLE):
430 pStyle = new SvxXMLListStyleContext( GetImport() );
431 break;
432 case XML_ELEMENT(TEXT, XML_OUTLINE_STYLE):
433 pStyle = new SvxXMLListStyleContext( GetImport(), true );
434 break;
436 // FillStyles
438 case XML_ELEMENT(DRAW, XML_GRADIENT):
440 pStyle = new XMLGradientStyleContext( GetImport(), nElement, xAttrList );
441 break;
443 case XML_ELEMENT(DRAW, XML_HATCH):
445 pStyle = new XMLHatchStyleContext( GetImport(), nElement, xAttrList );
446 break;
448 case XML_ELEMENT(DRAW, XML_FILL_IMAGE):
450 pStyle = new XMLBitmapStyleContext( GetImport(), nElement, xAttrList );
451 break;
453 case XML_ELEMENT(DRAW, XML_OPACITY):
455 pStyle = new XMLTransGradientStyleContext( GetImport(), nElement, xAttrList );
456 break;
458 case XML_ELEMENT(DRAW, XML_MARKER):
460 pStyle = new XMLMarkerStyleContext( GetImport(), nElement, xAttrList );
461 break;
463 case XML_ELEMENT(DRAW, XML_STROKE_DASH):
465 pStyle = new XMLDashStyleContext( GetImport(), nElement, xAttrList );
466 break;
470 if (!pStyle)
471 SAL_WARN("xmloff", "Unknown element " << SvXMLImport::getPrefixAndNameFromToken(nElement));
473 return pStyle;
476 SvXMLStyleContext *SvXMLStylesContext::CreateStyleStyleChildContext(
477 XmlStyleFamily nFamily, sal_Int32 /*nElement*/,
478 const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
480 SvXMLStyleContext *pStyle = nullptr;
482 switch( nFamily )
484 case XmlStyleFamily::TEXT_PARAGRAPH:
485 case XmlStyleFamily::TEXT_TEXT:
486 case XmlStyleFamily::TEXT_SECTION:
487 pStyle = new XMLTextStyleContext( GetImport(), *this, nFamily );
488 break;
490 case XmlStyleFamily::TEXT_RUBY:
491 pStyle = new XMLPropStyleContext( GetImport(), *this, nFamily );
492 break;
493 #if !ENABLE_WASM_STRIP_CHART
494 // WASM_CHART change
495 case XmlStyleFamily::SCH_CHART_ID:
496 pStyle = new XMLChartStyleContext( GetImport(), *this, nFamily );
497 break;
498 #endif
499 case XmlStyleFamily::SD_GRAPHICS_ID:
500 case XmlStyleFamily::SD_PRESENTATION_ID:
501 case XmlStyleFamily::SD_POOL_ID:
502 pStyle = new XMLShapeStyleContext( GetImport(), *this, nFamily );
503 break;
504 default: break;
507 return pStyle;
510 SvXMLStyleContext *SvXMLStylesContext::CreateDefaultStyleStyleChildContext(
511 XmlStyleFamily /*nFamily*/, sal_Int32 /*nElement*/,
512 const uno::Reference< xml::sax::XFastAttributeList > & )
514 return nullptr;
517 bool SvXMLStylesContext::InsertStyleFamily( XmlStyleFamily ) const
519 return true;
522 XmlStyleFamily SvXMLStylesContext::GetFamily( std::u16string_view rValue )
524 XmlStyleFamily nFamily = XmlStyleFamily::DATA_STYLE;
525 if( IsXMLToken( rValue, XML_PARAGRAPH ) )
527 nFamily = XmlStyleFamily::TEXT_PARAGRAPH;
529 else if( IsXMLToken( rValue, XML_TEXT ) )
531 nFamily = XmlStyleFamily::TEXT_TEXT;
533 else if( IsXMLToken( rValue, XML_DATA_STYLE ) )
535 nFamily = XmlStyleFamily::DATA_STYLE;
537 else if ( IsXMLToken( rValue, XML_SECTION ) )
539 nFamily = XmlStyleFamily::TEXT_SECTION;
541 else if( IsXMLToken( rValue, XML_TABLE ) )
543 nFamily = XmlStyleFamily::TABLE_TABLE;
545 else if( IsXMLToken( rValue, XML_TABLE_COLUMN ) )
546 nFamily = XmlStyleFamily::TABLE_COLUMN;
547 else if( IsXMLToken( rValue, XML_TABLE_ROW ) )
548 nFamily = XmlStyleFamily::TABLE_ROW;
549 else if( IsXMLToken( rValue, XML_TABLE_CELL ) )
550 nFamily = XmlStyleFamily::TABLE_CELL;
551 else if ( rValue == XML_STYLE_FAMILY_SD_GRAPHICS_NAME )
553 nFamily = XmlStyleFamily::SD_GRAPHICS_ID;
555 else if ( rValue == XML_STYLE_FAMILY_SD_PRESENTATION_NAME )
557 nFamily = XmlStyleFamily::SD_PRESENTATION_ID;
559 else if ( rValue == XML_STYLE_FAMILY_SD_POOL_NAME )
561 nFamily = XmlStyleFamily::SD_POOL_ID;
563 else if ( rValue == XML_STYLE_FAMILY_SD_DRAWINGPAGE_NAME )
565 nFamily = XmlStyleFamily::SD_DRAWINGPAGE_ID;
567 else if ( rValue == XML_STYLE_FAMILY_SCH_CHART_NAME )
569 nFamily = XmlStyleFamily::SCH_CHART_ID;
571 else if ( IsXMLToken( rValue, XML_RUBY ) )
573 nFamily = XmlStyleFamily::TEXT_RUBY;
576 return nFamily;
579 rtl::Reference < SvXMLImportPropertyMapper > SvXMLStylesContext::GetImportPropertyMapper(
580 XmlStyleFamily nFamily ) const
582 rtl::Reference < SvXMLImportPropertyMapper > xMapper;
584 switch( nFamily )
586 case XmlStyleFamily::TEXT_PARAGRAPH:
587 if( !mxParaImpPropMapper.is() )
589 SvXMLStylesContext * pThis = const_cast<SvXMLStylesContext *>(this);
590 pThis->mxParaImpPropMapper =
591 pThis->GetImport().GetTextImport()
592 ->GetParaImportPropertySetMapper();
594 xMapper = mxParaImpPropMapper;
595 break;
596 case XmlStyleFamily::TEXT_TEXT:
597 if( !mxTextImpPropMapper.is() )
599 SvXMLStylesContext * pThis = const_cast<SvXMLStylesContext *>(this);
600 pThis->mxTextImpPropMapper =
601 pThis->GetImport().GetTextImport()
602 ->GetTextImportPropertySetMapper();
604 xMapper = mxTextImpPropMapper;
605 break;
607 case XmlStyleFamily::TEXT_SECTION:
608 // don't cache section mapper, as it's rarely used
609 // *sigh*, cast to non-const, because this is a const method,
610 // but SvXMLImport::GetTextImport() isn't.
611 xMapper = const_cast<SvXMLStylesContext*>(this)->GetImport().GetTextImport()->
612 GetSectionImportPropertySetMapper();
613 break;
615 case XmlStyleFamily::TEXT_RUBY:
616 // don't cache section mapper, as it's rarely used
617 // *sigh*, cast to non-const, because this is a const method,
618 // but SvXMLImport::GetTextImport() isn't.
619 xMapper = const_cast<SvXMLStylesContext*>(this)->GetImport().GetTextImport()->
620 GetRubyImportPropertySetMapper();
621 break;
623 case XmlStyleFamily::SD_GRAPHICS_ID:
624 case XmlStyleFamily::SD_PRESENTATION_ID:
625 case XmlStyleFamily::SD_POOL_ID:
626 if(!mxShapeImpPropMapper.is())
628 rtl::Reference< XMLShapeImportHelper > aImpHelper = const_cast<SvXMLImport&>(GetImport()).GetShapeImport();
629 const_cast<SvXMLStylesContext*>(this)->mxShapeImpPropMapper =
630 aImpHelper->GetPropertySetMapper();
632 xMapper = mxShapeImpPropMapper;
633 break;
634 #if !ENABLE_WASM_STRIP_CHART
635 // WASM_CHART change
636 case XmlStyleFamily::SCH_CHART_ID:
637 if( ! mxChartImpPropMapper.is() )
639 XMLPropertySetMapper *const pPropMapper = new XMLChartPropertySetMapper(nullptr);
640 mxChartImpPropMapper = new XMLChartImportPropertyMapper( pPropMapper, GetImport() );
642 xMapper = mxChartImpPropMapper;
643 break;
644 #endif
645 case XmlStyleFamily::PAGE_MASTER:
646 if( ! mxPageImpPropMapper.is() )
648 XMLPropertySetMapper *pPropMapper =
649 new XMLPageMasterPropSetMapper();
650 mxPageImpPropMapper =
651 new PageMasterImportPropertyMapper( pPropMapper,
652 const_cast<SvXMLStylesContext*>(this)->GetImport() );
654 xMapper = mxPageImpPropMapper;
655 break;
656 default: break;
659 return xMapper;
662 Reference < XAutoStyleFamily > SvXMLStylesContext::GetAutoStyles( XmlStyleFamily nFamily ) const
664 Reference < XAutoStyleFamily > xAutoStyles;
665 if( XmlStyleFamily::TEXT_TEXT == nFamily || XmlStyleFamily::TEXT_PARAGRAPH == nFamily)
667 bool bPara = XmlStyleFamily::TEXT_PARAGRAPH == nFamily;
668 const Reference<XAutoStyleFamily>& rxAutoStyles = bPara ? mxParaAutoStyles : mxTextAutoStyles;
669 if (!rxAutoStyles)
671 OUString sName(bPara ? gsParagraphStyles : gsCharacterStyles);
672 Reference< XAutoStylesSupplier > xAutoStylesSupp( GetImport().GetModel(), UNO_QUERY );
673 Reference< XAutoStyles > xAutoStyleFamilies = xAutoStylesSupp->getAutoStyles();
674 if (xAutoStyleFamilies->hasByName(sName))
676 Any aAny = xAutoStyleFamilies->getByName( sName );
677 aAny >>= const_cast<Reference<XAutoStyleFamily>&>(rxAutoStyles);
680 xAutoStyles = rxAutoStyles;
682 return xAutoStyles;
685 Reference < XNameContainer > SvXMLStylesContext::GetStylesContainer(
686 XmlStyleFamily nFamily ) const
688 Reference < XNameContainer > xStyles;
689 if (XmlStyleFamily::TEXT_TEXT == nFamily || XmlStyleFamily::TEXT_PARAGRAPH == nFamily)
691 bool bPara = XmlStyleFamily::TEXT_PARAGRAPH == nFamily;
692 const Reference<XNameContainer>& rxStyles = bPara ? mxParaStyles : mxTextStyles;
693 if (!rxStyles)
695 OUString sName(bPara ? gsParagraphStyles : gsCharacterStyles);
696 Reference<XStyleFamiliesSupplier> xFamiliesSupp(GetImport().GetModel(), UNO_QUERY);
697 if (xFamiliesSupp.is())
699 Reference<XNameAccess> xFamilies = xFamiliesSupp->getStyleFamilies();
700 if (xFamilies->hasByName(sName))
702 Any aAny = xFamilies->getByName(sName);
703 aAny >>= const_cast<Reference<XNameContainer>&>(rxStyles);
707 xStyles = rxStyles;
710 return xStyles;
713 OUString SvXMLStylesContext::GetServiceName( XmlStyleFamily nFamily ) const
715 OUString sServiceName;
716 switch( nFamily )
718 case XmlStyleFamily::TEXT_PARAGRAPH:
719 sServiceName = gsParaStyleServiceName;
720 break;
721 case XmlStyleFamily::TEXT_TEXT:
722 sServiceName = gsTextStyleServiceName;
723 break;
724 default: break;
727 return sServiceName;
730 SvXMLStylesContext::SvXMLStylesContext( SvXMLImport& rImport, bool bAuto ) :
731 SvXMLImportContext( rImport ),
732 mpImpl( new SvXMLStylesContext_Impl( bAuto ) )
736 SvXMLStylesContext::~SvXMLStylesContext()
740 css::uno::Reference< css::xml::sax::XFastContextHandler > SvXMLStylesContext::createFastChildContext(
741 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
743 if (nElement == XML_ELEMENT(LO_EXT, XML_THEME))
745 if (auto xImportInfo = GetImport().getImportInfo())
749 if (auto xPropertySetInfo = xImportInfo->getPropertySetInfo())
751 if (xPropertySetInfo->hasPropertyByName(u"IsInPaste"_ustr))
753 css::uno::Any value = xImportInfo->getPropertyValue(u"IsInPaste"_ustr);
754 if (bool b; (value >>= b) && b)
755 return nullptr; // do not import themes in paste mode
759 catch (const css::uno::Exception&)
761 DBG_UNHANDLED_EXCEPTION("xmloff");
765 uno::Reference<uno::XInterface> xObject(GetImport().GetModel(), uno::UNO_QUERY);
766 uno::Reference<drawing::XDrawPageSupplier> const xDrawPageSupplier(GetImport().GetModel(), uno::UNO_QUERY);
767 if (xDrawPageSupplier.is())
769 uno::Reference<drawing::XDrawPage> xPage = xDrawPageSupplier->getDrawPage();
770 if (xPage.is())
771 xObject = xPage;
774 return new XMLThemeContext(GetImport(), xAttrList, xObject);
777 SvXMLStyleContext* pStyle = CreateStyleChildContext( nElement, xAttrList );
778 if (pStyle)
780 if (!pStyle->IsTransient())
781 mpImpl->AddStyle(pStyle);
782 return pStyle;
785 return nullptr;
788 void SvXMLStylesContext::AddStyle(SvXMLStyleContext& rNew)
790 mpImpl->AddStyle( &rNew );
793 void SvXMLStylesContext::dispose()
795 mpImpl->dispose();
798 void SvXMLStylesContext::CopyAutoStylesToDoc()
800 sal_uInt32 nCount = GetStyleCount();
801 sal_uInt32 i;
802 for( i = 0; i < nCount; i++ )
804 SvXMLStyleContext *pStyle = GetStyle( i );
805 if( !pStyle || ( pStyle->GetFamily() != XmlStyleFamily::TEXT_TEXT &&
806 pStyle->GetFamily() != XmlStyleFamily::TEXT_PARAGRAPH &&
807 pStyle->GetFamily() != XmlStyleFamily::TABLE_CELL ) )
808 continue;
809 pStyle->CreateAndInsert( false );
813 void SvXMLStylesContext::CopyStylesToDoc( bool bOverwrite,
814 bool bFinish )
816 // pass 1: create text, paragraph and frame styles
817 sal_uInt32 nCount = GetStyleCount();
818 sal_uInt32 i;
820 for( i = 0; i < nCount; i++ )
822 SvXMLStyleContext *pStyle = GetStyle( i );
823 if( !pStyle )
824 continue;
826 if (pStyle->IsDefaultStyle())
828 if (bOverwrite) pStyle->SetDefaults();
830 else if( InsertStyleFamily( pStyle->GetFamily() ) )
831 pStyle->CreateAndInsert( bOverwrite );
834 // pass 2: create list styles (they require char styles)
835 for( i=0; i<nCount; i++ )
837 SvXMLStyleContext *pStyle = GetStyle( i );
838 if( !pStyle || pStyle->IsDefaultStyle())
839 continue;
841 if( InsertStyleFamily( pStyle->GetFamily() ) )
842 pStyle->CreateAndInsertLate( bOverwrite );
845 // pass3: finish creation of styles
846 if( bFinish )
847 FinishStyles( bOverwrite );
850 void SvXMLStylesContext::FinishStyles( bool bOverwrite )
852 sal_uInt32 nCount = GetStyleCount();
853 for( sal_uInt32 i=0; i<nCount; i++ )
855 SvXMLStyleContext *pStyle = GetStyle( i );
856 if( !pStyle || !pStyle->IsValid() || pStyle->IsDefaultStyle() )
857 continue;
859 if( InsertStyleFamily( pStyle->GetFamily() ) )
860 pStyle->Finish( bOverwrite );
864 const SvXMLStyleContext *SvXMLStylesContext::FindStyleChildContext(
865 XmlStyleFamily nFamily,
866 const OUString& rName,
867 bool bCreateIndex ) const
869 return mpImpl->FindStyleChildContext( nFamily, rName, bCreateIndex );
872 std::pair<SvXMLStylesContext::StyleIndex::const_iterator, SvXMLStylesContext::StyleIndex::const_iterator>
873 SvXMLStylesContext::FindStyleChildContextByDisplayNamePrefix(
874 XmlStyleFamily nFamily,
875 const OUString& rNamePrefix ) const
877 return mpImpl->FindStyleChildContextByDisplayNamePrefix( nFamily, rNamePrefix );
880 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */