1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/xml/sax/XAttributeList.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
26 #include <com/sun/star/style/XAutoStylesSupplier.hpp>
27 #include <com/sun/star/style/XAutoStyleFamily.hpp>
28 #include <PageMasterPropMapper.hxx>
29 #include <sal/log.hxx>
30 #include <svl/style.hxx>
31 #include <xmloff/namespacemap.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/xmltoken.hxx>
34 
35 #include <xmloff/families.hxx>
36 #include <xmloff/xmlimp.hxx>
37 #include <xmloff/xmlnumi.hxx>
38 #include <xmloff/xmlimppr.hxx>
39 #include <xmloff/xmlstyle.hxx>
40 #include <xmloff/txtstyli.hxx>
41 #include <xmloff/xmlnumfi.hxx>
42 #include <XMLChartStyleContext.hxx>
43 #include <XMLChartPropertySetMapper.hxx>
44 #include <xmloff/XMLShapeStyleContext.hxx>
45 #include "FillStyleContext.hxx"
46 #include <XMLFootnoteConfigurationImportContext.hxx>
47 #include <XMLIndexBibliographyConfigurationContext.hxx>
48 #include <XMLLineNumberingImportContext.hxx>
49 #include <PageMasterImportContext.hxx>
50 #include "PageMasterImportPropMapper.hxx"
51 
52 #include <memory>
53 #include <set>
54 #include <string_view>
55 #include <vector>
56 
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::container;
60 using namespace ::com::sun::star::style;
61 using namespace ::xmloff::token;
62 
63 constexpr OUStringLiteral gsParaStyleServiceName( u"com.sun.star.style.ParagraphStyle" );
64 constexpr OUStringLiteral gsTextStyleServiceName( u"com.sun.star.style.CharacterStyle" );
65 
SetAttribute(sal_Int32 nElement,const OUString & rValue)66 void SvXMLStyleContext::SetAttribute( sal_Int32 nElement,
67                                       const OUString& rValue )
68 {
69     switch (nElement)
70     {
71         case XML_ELEMENT(STYLE, XML_FAMILY):
72         {
73             if( IsXMLToken( rValue, XML_PARAGRAPH ) )
74                 mnFamily = XmlStyleFamily(SfxStyleFamily::Para);
75             else if( IsXMLToken( rValue, XML_TEXT ) )
76                 mnFamily = XmlStyleFamily(SfxStyleFamily::Char);
77             break;
78         }
79         case XML_ELEMENT(STYLE, XML_NAME):
80             maName = rValue;
81             break;
82         case XML_ELEMENT(STYLE, XML_DISPLAY_NAME):
83             maDisplayName = rValue;
84             break;
85         case XML_ELEMENT(STYLE, XML_PARENT_STYLE_NAME):
86             maParentName = rValue;
87             break;
88         case XML_ELEMENT(STYLE, XML_NEXT_STYLE_NAME):
89             maFollow = rValue;
90             break;
91         case XML_ELEMENT(STYLE, XML_HIDDEN):
92             mbHidden = rValue.toBoolean();
93             break;
94         case XML_ELEMENT(LO_EXT, XML_HIDDEN):
95             mbHidden = rValue.toBoolean();
96             break;
97     }
98 }
99 
100 
SvXMLStyleContext(SvXMLImport & rImp,XmlStyleFamily nFam,bool bDefault)101 SvXMLStyleContext::SvXMLStyleContext(
102         SvXMLImport& rImp,
103         XmlStyleFamily nFam, bool bDefault ) :
104     SvXMLImportContext( rImp ),
105     mbHidden( false ),
106     mnFamily( nFam ),
107     mbValid( true ),
108     mbNew( true ),
109     mbDefaultStyle( bDefault )
110 {
111 }
112 
~SvXMLStyleContext()113 SvXMLStyleContext::~SvXMLStyleContext()
114 {
115 }
116 
startFastElement(sal_Int32,const css::uno::Reference<css::xml::sax::XFastAttributeList> & xAttrList)117 void SvXMLStyleContext::startFastElement(
118     sal_Int32 /*nElement*/,
119     const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
120 {
121     for( auto &it : sax_fastparser::castToFastAttributeList( xAttrList ) )
122         SetAttribute( it.getToken(), it.toString() );
123 }
124 
SetDefaults()125 void SvXMLStyleContext::SetDefaults()
126 {
127 }
128 
CreateAndInsert(bool)129 void SvXMLStyleContext::CreateAndInsert( bool /*bOverwrite*/ )
130 {
131 }
132 
CreateAndInsertLate(bool)133 void SvXMLStyleContext::CreateAndInsertLate( bool /*bOverwrite*/ )
134 {
135 }
136 
Finish(bool)137 void SvXMLStyleContext::Finish( bool /*bOverwrite*/ )
138 {
139 }
140 
IsTransient() const141 bool SvXMLStyleContext::IsTransient() const
142 {
143     return false;
144 }
145 
146 namespace {
147 
148 class SvXMLStyleIndex_Impl
149 {
150     OUString               sName;
151     XmlStyleFamily         nFamily;
152     // we deliberately don't use a reference here, to avoid creating a ref-count-cycle
153     SvXMLStyleContext*     mpStyle;
154 
155 public:
156 
SvXMLStyleIndex_Impl(XmlStyleFamily nFam,const OUString & rName)157     SvXMLStyleIndex_Impl( XmlStyleFamily nFam, const OUString& rName ) :
158         sName( rName ),
159         nFamily( nFam ),
160         mpStyle(nullptr)
161     {
162     }
163 
SvXMLStyleIndex_Impl(const rtl::Reference<SvXMLStyleContext> & rStl)164     SvXMLStyleIndex_Impl( const rtl::Reference<SvXMLStyleContext> &rStl ) :
165         sName( rStl->GetName() ),
166         nFamily( rStl->GetFamily() ),
167         mpStyle ( rStl.get() )
168     {
169     }
170 
GetName() const171     const OUString& GetName() const { return sName; }
GetFamily() const172     XmlStyleFamily GetFamily() const { return nFamily; }
GetStyle() const173     const SvXMLStyleContext *GetStyle() const { return mpStyle; }
174 };
175 
176 struct SvXMLStyleIndexCmp_Impl
177 {
operator ()__anon7e85284b0111::SvXMLStyleIndexCmp_Impl178     bool operator()(const SvXMLStyleIndex_Impl& r1, const SvXMLStyleIndex_Impl& r2) const
179     {
180         sal_Int32 nRet;
181 
182         if( r1.GetFamily() < r2.GetFamily() )
183             nRet = -1;
184         else if( r1.GetFamily() > r2.GetFamily() )
185             nRet = 1;
186         else
187             nRet = r1.GetName().compareTo( r2.GetName() );
188 
189         return nRet < 0;
190     }
191 };
192 
193 }
194 
195 class SvXMLStylesContext_Impl
196 {
197     typedef std::set<SvXMLStyleIndex_Impl, SvXMLStyleIndexCmp_Impl> IndicesType;
198 
199     std::vector<rtl::Reference<SvXMLStyleContext>> aStyles;
200     mutable std::unique_ptr<IndicesType> pIndices;
201     bool bAutomaticStyle;
202 
203 #if OSL_DEBUG_LEVEL > 0
204     mutable sal_uInt32 m_nIndexCreated;
205 #endif
206 
FlushIndex()207     void FlushIndex() { pIndices.reset(); }
208 
209 public:
210     explicit SvXMLStylesContext_Impl( bool bAuto );
211 
GetStyleCount() const212     size_t GetStyleCount() const { return aStyles.size(); }
213 
GetStyle(size_t i)214     SvXMLStyleContext *GetStyle( size_t i )
215     {
216         return i < aStyles.size() ? aStyles[ i ].get() : nullptr;
217     }
218 
219     inline void AddStyle( SvXMLStyleContext *pStyle );
220     void dispose();
221 
222     const SvXMLStyleContext *FindStyleChildContext( XmlStyleFamily nFamily,
223                                                     const OUString& rName,
224                                                     bool bCreateIndex ) const;
IsAutomaticStyle() const225     bool IsAutomaticStyle() const { return bAutomaticStyle; }
226 };
227 
SvXMLStylesContext_Impl(bool bAuto)228 SvXMLStylesContext_Impl::SvXMLStylesContext_Impl( bool bAuto ) :
229     bAutomaticStyle( bAuto )
230 #if OSL_DEBUG_LEVEL > 0
231     , m_nIndexCreated( 0 )
232 #endif
233 {}
234 
AddStyle(SvXMLStyleContext * pStyle)235 inline void SvXMLStylesContext_Impl::AddStyle( SvXMLStyleContext *pStyle )
236 {
237 #if OSL_DEBUG_LEVEL > 0
238 //    for (auto const & xStyle : aStyles)
239 //        if (xStyle->GetFamily() == pStyle->GetFamily() && xStyle->GetName() == pStyle->GetName())
240 //            assert(false && "duplicate style");
241 #endif
242     aStyles.emplace_back(pStyle );
243 
244     FlushIndex();
245 }
246 
dispose()247 void SvXMLStylesContext_Impl::dispose()
248 {
249     FlushIndex();
250     aStyles.clear();
251 }
252 
FindStyleChildContext(XmlStyleFamily nFamily,const OUString & rName,bool bCreateIndex) const253 const SvXMLStyleContext *SvXMLStylesContext_Impl::FindStyleChildContext( XmlStyleFamily nFamily,
254                                                                          const OUString& rName,
255                                                                          bool bCreateIndex ) const
256 {
257     const SvXMLStyleContext *pStyle = nullptr;
258 
259     if( !pIndices && bCreateIndex && !aStyles.empty() )
260     {
261         pIndices = std::make_unique<IndicesType>(aStyles.begin(), aStyles.end());
262         SAL_WARN_IF(pIndices->size() != aStyles.size(), "xmloff.style", "Here is a duplicate Style");
263 #if OSL_DEBUG_LEVEL > 0
264         SAL_WARN_IF(0 != m_nIndexCreated, "xmloff.style",
265                     "Performance warning: sdbcx::Index created multiple times");
266         ++m_nIndexCreated;
267 #endif
268     }
269 
270     if( pIndices )
271     {
272         SvXMLStyleIndex_Impl aIndex( nFamily, rName );
273         IndicesType::iterator aFind = pIndices->find(aIndex);
274         if( aFind != pIndices->end() )
275             pStyle = aFind->GetStyle();
276    }
277     else
278     {
279         for( size_t i = 0; !pStyle && i < aStyles.size(); i++ )
280         {
281             const SvXMLStyleContext *pS = aStyles[ i ].get();
282             if( pS->GetFamily() == nFamily &&
283                 pS->GetName() == rName )
284                 pStyle = pS;
285         }
286     }
287     return pStyle;
288 }
289 
290 
GetStyleCount() const291 sal_uInt32 SvXMLStylesContext::GetStyleCount() const
292 {
293     return mpImpl->GetStyleCount();
294 }
295 
GetStyle(sal_uInt32 i)296 SvXMLStyleContext *SvXMLStylesContext::GetStyle( sal_uInt32 i )
297 {
298     return mpImpl->GetStyle( i );
299 }
300 
GetStyle(sal_uInt32 i) const301 const SvXMLStyleContext *SvXMLStylesContext::GetStyle( sal_uInt32 i ) const
302 {
303     return mpImpl->GetStyle( i );
304 }
305 
IsAutomaticStyle() const306 bool SvXMLStylesContext::IsAutomaticStyle() const
307 {
308     return mpImpl->IsAutomaticStyle();
309 }
310 
CreateStyleChildContext(sal_Int32 nElement,const css::uno::Reference<css::xml::sax::XFastAttributeList> & xAttrList)311 SvXMLStyleContext *SvXMLStylesContext::CreateStyleChildContext(
312         sal_Int32 nElement,
313         const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
314 {
315     SvXMLStyleContext *pStyle = nullptr;
316 
317     if(GetImport().GetDataStylesImport())
318     {
319         pStyle = GetImport().GetDataStylesImport()->CreateChildContext(GetImport(), nElement,
320                                                xAttrList, *this);
321         if (pStyle)
322             return pStyle;
323     }
324 
325     switch (nElement)
326     {
327         case XML_ELEMENT(STYLE, XML_STYLE):
328         case XML_ELEMENT(STYLE, XML_DEFAULT_STYLE):
329         {
330             XmlStyleFamily nFamily = XmlStyleFamily::DATA_STYLE;
331             for( auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ) )
332             {
333                 if( aIter.getToken() == XML_ELEMENT(STYLE, XML_FAMILY) )
334                 {
335                     nFamily = GetFamily( aIter.toString() );
336                     break;
337                 }
338             }
339             pStyle = XML_ELEMENT(STYLE, XML_STYLE)==nElement
340                 ? CreateStyleStyleChildContext( nFamily, nElement, xAttrList )
341                 : CreateDefaultStyleStyleChildContext( nFamily, nElement, xAttrList );
342             break;
343         }
344         case XML_ELEMENT(TEXT, XML_BIBLIOGRAPHY_CONFIGURATION):
345             pStyle = new XMLIndexBibliographyConfigurationContext(GetImport());
346             break;
347         case XML_ELEMENT(TEXT, XML_NOTES_CONFIGURATION):
348             pStyle = new XMLFootnoteConfigurationImportContext(
349                 GetImport(), nElement, xAttrList);
350             break;
351         case XML_ELEMENT(TEXT, XML_LINENUMBERING_CONFIGURATION):
352             pStyle = new XMLLineNumberingImportContext(GetImport());
353             break;
354         case XML_ELEMENT(STYLE, XML_PAGE_LAYOUT):
355         case XML_ELEMENT(STYLE, XML_DEFAULT_PAGE_LAYOUT):
356         {
357             //there is not page family in ODF now, so I specify one for it
358             bool bDefaultStyle  = XML_ELEMENT(STYLE, XML_DEFAULT_PAGE_LAYOUT) == nElement;
359             pStyle = new PageStyleContext( GetImport(), *this, bDefaultStyle );
360         }
361         break;
362         case XML_ELEMENT(TEXT, XML_LIST_STYLE):
363             pStyle = new SvxXMLListStyleContext( GetImport() );
364             break;
365         case XML_ELEMENT(TEXT, XML_OUTLINE_STYLE):
366             pStyle = new SvxXMLListStyleContext( GetImport(), true );
367             break;
368 
369         // FillStyles
370 
371         case XML_ELEMENT(DRAW, XML_GRADIENT):
372         {
373             pStyle = new XMLGradientStyleContext( GetImport(), nElement, xAttrList );
374             break;
375         }
376         case XML_ELEMENT(DRAW, XML_HATCH):
377         {
378             pStyle = new XMLHatchStyleContext( GetImport(), nElement, xAttrList );
379             break;
380         }
381         case XML_ELEMENT(DRAW, XML_FILL_IMAGE):
382         {
383             pStyle = new XMLBitmapStyleContext( GetImport(), nElement, xAttrList );
384             break;
385         }
386         case XML_ELEMENT(DRAW, XML_OPACITY):
387         {
388             pStyle = new XMLTransGradientStyleContext( GetImport(), nElement, xAttrList );
389             break;
390         }
391         case XML_ELEMENT(DRAW, XML_MARKER):
392         {
393             pStyle = new XMLMarkerStyleContext( GetImport(), nElement, xAttrList );
394             break;
395         }
396         case XML_ELEMENT(DRAW, XML_STROKE_DASH):
397         {
398             pStyle = new XMLDashStyleContext( GetImport(), nElement, xAttrList );
399             break;
400         }
401     }
402 
403     if (!pStyle)
404         SAL_WARN("xmloff", "Unknown element " << SvXMLImport::getPrefixAndNameFromToken(nElement));
405 
406     return pStyle;
407 }
408 
CreateStyleStyleChildContext(XmlStyleFamily nFamily,sal_Int32,const uno::Reference<xml::sax::XFastAttributeList> &)409 SvXMLStyleContext *SvXMLStylesContext::CreateStyleStyleChildContext(
410         XmlStyleFamily nFamily, sal_Int32 /*nElement*/,
411         const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
412 {
413     SvXMLStyleContext *pStyle = nullptr;
414 
415     switch( nFamily )
416     {
417         case XmlStyleFamily::TEXT_PARAGRAPH:
418         case XmlStyleFamily::TEXT_TEXT:
419         case XmlStyleFamily::TEXT_SECTION:
420             pStyle = new XMLTextStyleContext( GetImport(), *this, nFamily );
421             break;
422 
423         case XmlStyleFamily::TEXT_RUBY:
424             pStyle = new XMLPropStyleContext( GetImport(), *this, nFamily );
425             break;
426         case XmlStyleFamily::SCH_CHART_ID:
427             pStyle = new XMLChartStyleContext( GetImport(), *this, nFamily );
428             break;
429 
430         case XmlStyleFamily::SD_GRAPHICS_ID:
431         case XmlStyleFamily::SD_PRESENTATION_ID:
432         case XmlStyleFamily::SD_POOL_ID:
433             pStyle = new XMLShapeStyleContext( GetImport(), *this, nFamily );
434             break;
435         default: break;
436     }
437 
438     return pStyle;
439 }
440 
CreateDefaultStyleStyleChildContext(XmlStyleFamily,sal_Int32,const uno::Reference<xml::sax::XFastAttributeList> &)441 SvXMLStyleContext *SvXMLStylesContext::CreateDefaultStyleStyleChildContext(
442         XmlStyleFamily /*nFamily*/, sal_Int32 /*nElement*/,
443         const uno::Reference< xml::sax::XFastAttributeList > & )
444 {
445     return nullptr;
446 }
447 
InsertStyleFamily(XmlStyleFamily) const448 bool SvXMLStylesContext::InsertStyleFamily( XmlStyleFamily ) const
449 {
450     return true;
451 }
452 
GetFamily(std::u16string_view rValue)453 XmlStyleFamily SvXMLStylesContext::GetFamily( std::u16string_view rValue )
454 {
455     XmlStyleFamily nFamily = XmlStyleFamily::DATA_STYLE;
456     if( IsXMLToken( rValue, XML_PARAGRAPH ) )
457     {
458         nFamily = XmlStyleFamily::TEXT_PARAGRAPH;
459     }
460     else if( IsXMLToken( rValue, XML_TEXT ) )
461     {
462         nFamily = XmlStyleFamily::TEXT_TEXT;
463     }
464     else if( IsXMLToken( rValue, XML_DATA_STYLE ) )
465     {
466         nFamily = XmlStyleFamily::DATA_STYLE;
467     }
468     else if ( IsXMLToken( rValue, XML_SECTION ) )
469     {
470         nFamily = XmlStyleFamily::TEXT_SECTION;
471     }
472     else if( IsXMLToken( rValue, XML_TABLE ) )
473     {
474         nFamily = XmlStyleFamily::TABLE_TABLE;
475     }
476     else if( IsXMLToken( rValue, XML_TABLE_COLUMN ) )
477         nFamily = XmlStyleFamily::TABLE_COLUMN;
478     else if( IsXMLToken( rValue, XML_TABLE_ROW ) )
479         nFamily = XmlStyleFamily::TABLE_ROW;
480     else if( IsXMLToken( rValue, XML_TABLE_CELL ) )
481         nFamily = XmlStyleFamily::TABLE_CELL;
482     else if ( rValue == XML_STYLE_FAMILY_SD_GRAPHICS_NAME )
483     {
484         nFamily = XmlStyleFamily::SD_GRAPHICS_ID;
485     }
486     else if ( rValue == XML_STYLE_FAMILY_SD_PRESENTATION_NAME )
487     {
488         nFamily = XmlStyleFamily::SD_PRESENTATION_ID;
489     }
490     else if ( rValue == XML_STYLE_FAMILY_SD_POOL_NAME )
491     {
492         nFamily = XmlStyleFamily::SD_POOL_ID;
493     }
494     else if ( rValue == XML_STYLE_FAMILY_SD_DRAWINGPAGE_NAME )
495     {
496         nFamily = XmlStyleFamily::SD_DRAWINGPAGE_ID;
497     }
498     else if ( rValue == XML_STYLE_FAMILY_SCH_CHART_NAME )
499     {
500         nFamily = XmlStyleFamily::SCH_CHART_ID;
501     }
502     else if ( IsXMLToken( rValue, XML_RUBY ) )
503     {
504         nFamily = XmlStyleFamily::TEXT_RUBY;
505     }
506 
507     return nFamily;
508 }
509 
GetImportPropertyMapper(XmlStyleFamily nFamily) const510 rtl::Reference < SvXMLImportPropertyMapper > SvXMLStylesContext::GetImportPropertyMapper(
511                         XmlStyleFamily nFamily ) const
512 {
513     rtl::Reference < SvXMLImportPropertyMapper > xMapper;
514 
515     switch( nFamily )
516     {
517     case XmlStyleFamily::TEXT_PARAGRAPH:
518         if( !mxParaImpPropMapper.is() )
519         {
520             SvXMLStylesContext * pThis = const_cast<SvXMLStylesContext *>(this);
521             pThis->mxParaImpPropMapper =
522                 pThis->GetImport().GetTextImport()
523                      ->GetParaImportPropertySetMapper();
524         }
525         xMapper = mxParaImpPropMapper;
526         break;
527     case XmlStyleFamily::TEXT_TEXT:
528         if( !mxTextImpPropMapper.is() )
529         {
530             SvXMLStylesContext * pThis = const_cast<SvXMLStylesContext *>(this);
531             pThis->mxTextImpPropMapper =
532                 pThis->GetImport().GetTextImport()
533                      ->GetTextImportPropertySetMapper();
534         }
535         xMapper = mxTextImpPropMapper;
536         break;
537 
538     case XmlStyleFamily::TEXT_SECTION:
539         // don't cache section mapper, as it's rarely used
540         // *sigh*, cast to non-const, because this is a const method,
541         // but SvXMLImport::GetTextImport() isn't.
542         xMapper = const_cast<SvXMLStylesContext*>(this)->GetImport().GetTextImport()->
543             GetSectionImportPropertySetMapper();
544         break;
545 
546     case XmlStyleFamily::TEXT_RUBY:
547         // don't cache section mapper, as it's rarely used
548         // *sigh*, cast to non-const, because this is a const method,
549         // but SvXMLImport::GetTextImport() isn't.
550         xMapper = const_cast<SvXMLStylesContext*>(this)->GetImport().GetTextImport()->
551             GetRubyImportPropertySetMapper();
552         break;
553 
554     case XmlStyleFamily::SD_GRAPHICS_ID:
555     case XmlStyleFamily::SD_PRESENTATION_ID:
556     case XmlStyleFamily::SD_POOL_ID:
557         if(!mxShapeImpPropMapper.is())
558         {
559             rtl::Reference< XMLShapeImportHelper > aImpHelper = const_cast<SvXMLImport&>(GetImport()).GetShapeImport();
560             const_cast<SvXMLStylesContext*>(this)->mxShapeImpPropMapper =
561                 aImpHelper->GetPropertySetMapper();
562         }
563         xMapper = mxShapeImpPropMapper;
564         break;
565     case XmlStyleFamily::SCH_CHART_ID:
566         if( ! mxChartImpPropMapper.is() )
567         {
568             XMLPropertySetMapper *const pPropMapper = new XMLChartPropertySetMapper(nullptr);
569             mxChartImpPropMapper = new XMLChartImportPropertyMapper( pPropMapper, GetImport() );
570         }
571         xMapper = mxChartImpPropMapper;
572         break;
573     case XmlStyleFamily::PAGE_MASTER:
574         if( ! mxPageImpPropMapper.is() )
575         {
576             XMLPropertySetMapper *pPropMapper =
577                 new XMLPageMasterPropSetMapper();
578             mxPageImpPropMapper =
579                 new PageMasterImportPropertyMapper( pPropMapper,
580                                     const_cast<SvXMLStylesContext*>(this)->GetImport() );
581         }
582         xMapper = mxPageImpPropMapper;
583         break;
584     default: break;
585     }
586 
587     return xMapper;
588 }
589 
GetAutoStyles(XmlStyleFamily nFamily) const590 Reference < XAutoStyleFamily > SvXMLStylesContext::GetAutoStyles( XmlStyleFamily nFamily ) const
591 {
592     Reference < XAutoStyleFamily > xAutoStyles;
593     if( XmlStyleFamily::TEXT_TEXT == nFamily || XmlStyleFamily::TEXT_PARAGRAPH == nFamily)
594     {
595         bool bPara = XmlStyleFamily::TEXT_PARAGRAPH == nFamily;
596         if( !bPara && mxTextAutoStyles.is() )
597             xAutoStyles = mxTextAutoStyles;
598         else if( bPara && mxParaAutoStyles.is() )
599             xAutoStyles = mxParaAutoStyles;
600         else
601         {
602             OUString sName(bPara ? std::u16string_view( u"ParagraphStyles" ): std::u16string_view( u"CharacterStyles" ));
603             Reference< XAutoStylesSupplier > xAutoStylesSupp(   GetImport().GetModel(), UNO_QUERY );
604             Reference< XAutoStyles > xAutoStyleFamilies = xAutoStylesSupp->getAutoStyles();
605             if (xAutoStyleFamilies->hasByName(sName))
606             {
607                 Any aAny = xAutoStyleFamilies->getByName( sName );
608                 aAny >>= xAutoStyles;
609                 if( bPara )
610                     const_cast<SvXMLStylesContext *>(this)->mxParaAutoStyles = xAutoStyles;
611                 else
612                     const_cast<SvXMLStylesContext *>(this)->mxTextAutoStyles = xAutoStyles;
613             }
614         }
615     }
616     return xAutoStyles;
617 }
618 
GetStylesContainer(XmlStyleFamily nFamily) const619 Reference < XNameContainer > SvXMLStylesContext::GetStylesContainer(
620                                                 XmlStyleFamily nFamily ) const
621 {
622     Reference < XNameContainer > xStyles;
623     OUString sName;
624     switch( nFamily )
625     {
626     case XmlStyleFamily::TEXT_PARAGRAPH:
627         if( mxParaStyles.is() )
628             xStyles = mxParaStyles;
629         else
630             sName = "ParagraphStyles";
631         break;
632 
633     case XmlStyleFamily::TEXT_TEXT:
634         if( mxTextStyles.is() )
635             xStyles = mxTextStyles;
636         else
637             sName = "CharacterStyles";
638         break;
639     default: break;
640     }
641     if( !xStyles.is() && !sName.isEmpty() )
642     {
643         Reference< XStyleFamiliesSupplier > xFamiliesSupp(
644                                         GetImport().GetModel(), UNO_QUERY );
645         if ( xFamiliesSupp.is() )
646         {
647             Reference< XNameAccess > xFamilies = xFamiliesSupp->getStyleFamilies();
648             if (xFamilies->hasByName(sName))
649             {
650                 xStyles.set(xFamilies->getByName( sName ),uno::UNO_QUERY);
651 
652                 switch( nFamily )
653                 {
654                 case XmlStyleFamily::TEXT_PARAGRAPH:
655                     const_cast<SvXMLStylesContext *>(this)->mxParaStyles = xStyles;
656                     break;
657 
658                 case XmlStyleFamily::TEXT_TEXT:
659                     const_cast<SvXMLStylesContext *>(this)->mxTextStyles = xStyles;
660                     break;
661                 default: break;
662                 }
663             }
664         }
665     }
666 
667     return xStyles;
668 }
669 
GetServiceName(XmlStyleFamily nFamily) const670 OUString SvXMLStylesContext::GetServiceName( XmlStyleFamily nFamily ) const
671 {
672     OUString sServiceName;
673     switch( nFamily )
674     {
675     case XmlStyleFamily::TEXT_PARAGRAPH:
676         sServiceName = gsParaStyleServiceName;
677         break;
678     case XmlStyleFamily::TEXT_TEXT:
679         sServiceName = gsTextStyleServiceName;
680         break;
681     default: break;
682     }
683 
684     return sServiceName;
685 }
686 
SvXMLStylesContext(SvXMLImport & rImport,bool bAuto)687 SvXMLStylesContext::SvXMLStylesContext( SvXMLImport& rImport, bool bAuto ) :
688     SvXMLImportContext( rImport ),
689     mpImpl( new SvXMLStylesContext_Impl( bAuto ) )
690 {
691 }
692 
~SvXMLStylesContext()693 SvXMLStylesContext::~SvXMLStylesContext()
694 {
695 }
696 
createFastChildContext(sal_Int32 nElement,const css::uno::Reference<css::xml::sax::XFastAttributeList> & xAttrList)697 css::uno::Reference< css::xml::sax::XFastContextHandler > SvXMLStylesContext::createFastChildContext(
698         sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
699 {
700     SvXMLStyleContext *pStyle =
701         CreateStyleChildContext( nElement, xAttrList );
702     if( pStyle )
703     {
704         if( !pStyle->IsTransient() )
705             mpImpl->AddStyle( pStyle );
706         return pStyle;
707     }
708 
709     return nullptr;
710 }
711 
AddStyle(SvXMLStyleContext & rNew)712 void SvXMLStylesContext::AddStyle(SvXMLStyleContext& rNew)
713 {
714     mpImpl->AddStyle( &rNew );
715 }
716 
dispose()717 void SvXMLStylesContext::dispose()
718 {
719     mpImpl->dispose();
720 }
721 
CopyAutoStylesToDoc()722 void SvXMLStylesContext::CopyAutoStylesToDoc()
723 {
724     sal_uInt32 nCount = GetStyleCount();
725     sal_uInt32 i;
726     for( i = 0; i < nCount; i++ )
727     {
728         SvXMLStyleContext *pStyle = GetStyle( i );
729         if( !pStyle || ( pStyle->GetFamily() != XmlStyleFamily::TEXT_TEXT &&
730             pStyle->GetFamily() != XmlStyleFamily::TEXT_PARAGRAPH  &&
731             pStyle->GetFamily() != XmlStyleFamily::TABLE_CELL ) )
732             continue;
733         pStyle->CreateAndInsert( false );
734     }
735 }
736 
CopyStylesToDoc(bool bOverwrite,bool bFinish)737 void SvXMLStylesContext::CopyStylesToDoc( bool bOverwrite,
738                                           bool bFinish )
739 {
740     // pass 1: create text, paragraph and frame styles
741     sal_uInt32 nCount = GetStyleCount();
742     sal_uInt32 i;
743 
744     for( i = 0; i < nCount; i++ )
745     {
746         SvXMLStyleContext *pStyle = GetStyle( i );
747         if( !pStyle )
748             continue;
749 
750         if (pStyle->IsDefaultStyle())
751         {
752             if (bOverwrite) pStyle->SetDefaults();
753         }
754         else if( InsertStyleFamily( pStyle->GetFamily() ) )
755             pStyle->CreateAndInsert( bOverwrite );
756     }
757 
758     // pass 2: create list styles (they require char styles)
759     for( i=0; i<nCount; i++ )
760     {
761         SvXMLStyleContext *pStyle = GetStyle( i );
762         if( !pStyle || pStyle->IsDefaultStyle())
763             continue;
764 
765         if( InsertStyleFamily( pStyle->GetFamily() ) )
766             pStyle->CreateAndInsertLate( bOverwrite );
767     }
768 
769     // pass3: finish creation of styles
770     if( bFinish )
771         FinishStyles( bOverwrite );
772 }
773 
FinishStyles(bool bOverwrite)774 void SvXMLStylesContext::FinishStyles( bool bOverwrite )
775 {
776     sal_uInt32 nCount = GetStyleCount();
777     for( sal_uInt32 i=0; i<nCount; i++ )
778     {
779         SvXMLStyleContext *pStyle = GetStyle( i );
780         if( !pStyle || !pStyle->IsValid() || pStyle->IsDefaultStyle() )
781             continue;
782 
783         if( InsertStyleFamily( pStyle->GetFamily() ) )
784             pStyle->Finish( bOverwrite );
785     }
786 }
787 
FindStyleChildContext(XmlStyleFamily nFamily,const OUString & rName,bool bCreateIndex) const788 const SvXMLStyleContext *SvXMLStylesContext::FindStyleChildContext(
789                                   XmlStyleFamily nFamily,
790                                   const OUString& rName,
791                                   bool bCreateIndex ) const
792 {
793     return mpImpl->FindStyleChildContext( nFamily, rName, bCreateIndex );
794 }
795 
796 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
797