Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / xmloff / source / style / prstylei.cxx
bloba0277bf8a752b3d2f8554d98d29f3b68f42b0752
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 <sal/config.h>
22 #include <o3tl/any.hxx>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
25 #include <set>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmlprcon.hxx>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/style/XStyle.hpp>
31 #include <com/sun/star/style/XAutoStyleFamily.hpp>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/beans/XPropertyState.hpp>
35 #include <com/sun/star/beans/XMultiPropertyStates.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <xmloff/xmlimp.hxx>
38 #include <xmloff/prstylei.hxx>
39 #include <xmloff/xmlerror.hxx>
40 #include <xmloff/xmltypes.hxx>
41 #include <xmloff/maptype.hxx>
42 #include <xmloff/xmlimppr.hxx>
43 #include <xmloff/xmlprmap.hxx>
44 #include <comphelper/sequence.hxx>
45 #include <com/sun/star/drawing/FillStyle.hpp>
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::xml::sax;
50 using namespace ::com::sun::star::style;
51 using namespace ::com::sun::star::container;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::lang;
54 using namespace ::xmloff::token;
55 using namespace com::sun::star::drawing;
57 void XMLPropStyleContext::SetAttribute( sal_Int32 nElement,
58 const OUString& rValue )
60 if( nElement == XML_ELEMENT(STYLE, XML_FAMILY) )
62 SAL_WARN_IF( GetFamily() != SvXMLStylesContext::GetFamily( rValue ), "xmloff", "unexpected style family" );
64 else
66 SvXMLStyleContext::SetAttribute( nElement, rValue );
71 namespace
73 struct theStandardSet :
74 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theStandardSet>
76 OldFillStyleDefinitionSet operator () ()
78 OldFillStyleDefinitionSet aSet;
79 aSet.insert("BackColorRGB");
80 aSet.insert("BackTransparent");
81 aSet.insert("BackColorTransparency");
82 aSet.insert("BackGraphic");
83 aSet.insert("BackGraphicFilter");
84 aSet.insert("BackGraphicLocation");
85 aSet.insert("BackGraphicTransparency");
86 return aSet;
89 struct theHeaderSet :
90 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theHeaderSet>
92 OldFillStyleDefinitionSet operator () ()
94 OldFillStyleDefinitionSet aSet;
95 aSet.insert("HeaderBackColorRGB");
96 aSet.insert("HeaderBackTransparent");
97 aSet.insert("HeaderBackColorTransparency");
98 aSet.insert("HeaderBackGraphic");
99 aSet.insert("HeaderBackGraphicFilter");
100 aSet.insert("HeaderBackGraphicLocation");
101 aSet.insert("HeaderBackGraphicTransparency");
102 return aSet;
105 struct theFooterSet :
106 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theFooterSet>
108 OldFillStyleDefinitionSet operator () ()
110 OldFillStyleDefinitionSet aSet;
111 aSet.insert("FooterBackColorRGB");
112 aSet.insert("FooterBackTransparent");
113 aSet.insert("FooterBackColorTransparency");
114 aSet.insert("FooterBackGraphic");
115 aSet.insert("FooterBackGraphicFilter");
116 aSet.insert("FooterBackGraphicLocation");
117 aSet.insert("FooterBackGraphicTransparency");
118 return aSet;
121 struct theParaSet :
122 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theParaSet>
124 OldFillStyleDefinitionSet operator () ()
126 OldFillStyleDefinitionSet aSet;
127 // Caution: here it is *not* 'ParaBackColorRGB' as it should be, but indeed
128 // 'ParaBackColor' is used, see aXMLParaPropMap definition (line 313)
129 aSet.insert("ParaBackColor");
130 aSet.insert("ParaBackTransparent");
131 aSet.insert("ParaBackGraphicLocation");
132 aSet.insert("ParaBackGraphicFilter");
133 aSet.insert("ParaBackGraphic");
135 // These are not used in aXMLParaPropMap definition, thus not needed here
136 // aSet.insert("ParaBackColorTransparency");
137 // aSet.insert("ParaBackGraphicTransparency");
138 return aSet;
145 constexpr OUStringLiteral gsIsPhysical( u"IsPhysical" );
146 constexpr OUStringLiteral gsFollowStyle( u"FollowStyle" );
148 XMLPropStyleContext::XMLPropStyleContext( SvXMLImport& rImport,
149 SvXMLStylesContext& rStyles, XmlStyleFamily nFamily,
150 bool bDefault )
151 : SvXMLStyleContext( rImport, nFamily, bDefault )
152 , mxStyles( &rStyles )
156 XMLPropStyleContext::~XMLPropStyleContext()
160 const OldFillStyleDefinitionSet& XMLPropStyleContext::getStandardSet()
162 return theStandardSet::get();
165 const OldFillStyleDefinitionSet& XMLPropStyleContext::getHeaderSet()
167 return theHeaderSet::get();
170 const OldFillStyleDefinitionSet& XMLPropStyleContext::getFooterSet()
172 return theFooterSet::get();
175 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLPropStyleContext::createFastChildContext(
176 sal_Int32 nElement,
177 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
179 sal_uInt32 nFamily = 0;
180 if( IsTokenInNamespace(nElement, XML_NAMESPACE_STYLE) ||
181 IsTokenInNamespace(nElement, XML_NAMESPACE_LO_EXT) )
183 sal_Int32 nLocalName = nElement & TOKEN_MASK;
184 if( nLocalName == XML_GRAPHIC_PROPERTIES )
185 nFamily = XML_TYPE_PROP_GRAPHIC;
186 else if( nLocalName == XML_DRAWING_PAGE_PROPERTIES )
187 nFamily = XML_TYPE_PROP_DRAWING_PAGE;
188 else if( nLocalName == XML_TEXT_PROPERTIES )
189 nFamily = XML_TYPE_PROP_TEXT;
190 else if( nLocalName == XML_PARAGRAPH_PROPERTIES )
191 nFamily = XML_TYPE_PROP_PARAGRAPH;
192 else if( nLocalName == XML_RUBY_PROPERTIES )
193 nFamily = XML_TYPE_PROP_RUBY;
194 else if( nLocalName == XML_SECTION_PROPERTIES )
195 nFamily = XML_TYPE_PROP_SECTION;
196 else if( nLocalName == XML_TABLE_PROPERTIES )
197 nFamily = XML_TYPE_PROP_TABLE;
198 else if( nLocalName == XML_TABLE_COLUMN_PROPERTIES )
199 nFamily = XML_TYPE_PROP_TABLE_COLUMN;
200 else if( nLocalName ==XML_TABLE_ROW_PROPERTIES )
201 nFamily = XML_TYPE_PROP_TABLE_ROW;
202 else if( nLocalName == XML_TABLE_CELL_PROPERTIES )
203 nFamily = XML_TYPE_PROP_TABLE_CELL;
204 else if( nLocalName == XML_CHART_PROPERTIES )
205 nFamily = XML_TYPE_PROP_CHART;
207 if( nFamily )
209 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
210 mxStyles->GetImportPropertyMapper( GetFamily() );
211 if( xImpPrMap.is() )
212 return new SvXMLPropertySetContext( GetImport(), nElement,
213 xAttrList,
214 nFamily,
215 maProperties,
216 xImpPrMap );
218 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
219 return nullptr;
222 void XMLPropStyleContext::FillPropertySet(
223 const Reference< XPropertySet > & rPropSet )
225 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
226 mxStyles->GetImportPropertyMapper( GetFamily() );
227 SAL_WARN_IF( !xImpPrMap.is(), "xmloff", "There is the import prop mapper" );
228 if( xImpPrMap.is() )
229 xImpPrMap->FillPropertySet( maProperties, rPropSet );
232 void XMLPropStyleContext::SetDefaults()
236 Reference < XStyle > XMLPropStyleContext::Create()
238 Reference < XStyle > xNewStyle;
240 OUString sServiceName = mxStyles->GetServiceName( GetFamily() );
241 if( !sServiceName.isEmpty() )
243 Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
244 UNO_QUERY );
245 if( xFactory.is() )
247 Reference < XInterface > xIfc =
248 xFactory->createInstance( sServiceName );
249 if( xIfc.is() )
250 xNewStyle.set( xIfc, UNO_QUERY );
254 return xNewStyle;
257 void XMLPropStyleContext::CreateAndInsert( bool bOverwrite )
259 SvXMLStylesContext* pSvXMLStylesContext = mxStyles.get();
260 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap = pSvXMLStylesContext->GetImportPropertyMapper(GetFamily());
261 OSL_ENSURE(xImpPrMap.is(), "There is no import prop mapper");
263 // need to filter out old fill definitions when the new ones are used. The new
264 // ones are used when a FillStyle is defined
265 const bool bTakeCareOfDrawingLayerFillStyle(xImpPrMap.is() && GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH);
266 bool bDrawingLayerFillStylesUsed(false);
268 if(bTakeCareOfDrawingLayerFillStyle)
270 // check if new FillStyles are used and if so mark old ones with -1
271 static OUString s_FillStyle("FillStyle");
273 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle))
275 deactivateOldFillStyleDefinitions(theParaSet::get());
276 bDrawingLayerFillStylesUsed = true;
280 if( pSvXMLStylesContext->IsAutomaticStyle()
281 && ( GetFamily() == XmlStyleFamily::TEXT_TEXT || GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH ) )
283 // Need to translate StyleName from temp MapNames to names
284 // used in already imported items (already exist in the pool). This
285 // is required for AutomaticStyles since these do *not* use FillPropertySet
286 // and thus just trigger CheckSpecialContext in XMLTextStyleContext::FillPropertySet
287 // (which may be double action anyways). The mechanism there to use _ContextID_Index_Pair
288 // is not working for AutomaticStyles and is already too late, too (this
289 // method is already called before XMLTextStyleContext::FillPropertySet gets called)
290 if(bDrawingLayerFillStylesUsed)
292 translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames();
295 Reference < XAutoStyleFamily > xAutoFamily = pSvXMLStylesContext->GetAutoStyles( GetFamily() );
296 if( !xAutoFamily.is() )
297 return;
298 if( xImpPrMap.is() )
300 Sequence< PropertyValue > aValues;
301 xImpPrMap->FillPropertySequence( maProperties, aValues );
303 sal_Int32 nLen = aValues.getLength();
304 if( nLen )
306 if( GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH )
308 aValues.realloc( nLen + 2 );
309 PropertyValue *pProps = aValues.getArray() + nLen;
310 pProps->Name = "ParaStyleName";
311 OUString sParent( GetParentName() );
312 if( !sParent.isEmpty() )
314 sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
315 Reference < XNameContainer > xFamilies = pSvXMLStylesContext->GetStylesContainer( GetFamily() );
316 if(xFamilies.is() && xFamilies->hasByName( sParent ) )
318 css::uno::Reference< css::style::XStyle > xStyle;
319 Any aAny = xFamilies->getByName( sParent );
320 aAny >>= xStyle;
321 sParent = xStyle->getName() ;
324 else
325 sParent = "Standard";
326 pProps->Value <<= sParent;
327 ++pProps;
328 pProps->Name = "ParaConditionalStyleName";
329 pProps->Value <<= sParent;
332 Reference < XAutoStyle > xAutoStyle = xAutoFamily->insertStyle( aValues );
333 if( xAutoStyle.is() )
335 Sequence< OUString > aPropNames
337 (GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH)?
338 OUString("ParaAutoStyleName"):
339 OUString("CharAutoStyleName")
341 Sequence< Any > aAny = xAutoStyle->getPropertyValues( aPropNames );
342 if( aAny.hasElements() )
344 OUString aName;
345 aAny[0] >>= aName;
346 SetAutoName( aName );
352 else
354 const OUString& rName = GetDisplayName();
355 if( rName.isEmpty() || IsDefaultStyle() )
356 return;
358 Reference < XNameContainer > xFamilies = pSvXMLStylesContext->GetStylesContainer( GetFamily() );
359 if( !xFamilies.is() )
361 SAL_WARN("xmloff", "no styles container for family " << static_cast<int>(GetFamily()));
362 return;
365 bool bNew = false;
366 if( xFamilies->hasByName( rName ) )
368 Any aAny = xFamilies->getByName( rName );
369 aAny >>= mxStyle;
371 else
373 mxStyle = Create();
374 if( !mxStyle.is() )
375 return;
377 xFamilies->insertByName( rName, Any(mxStyle) );
378 bNew = true;
381 Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
382 Reference< XPropertySetInfo > xPropSetInfo =
383 xPropSet->getPropertySetInfo();
384 if( !bNew && xPropSetInfo->hasPropertyByName( gsIsPhysical ) )
386 Any aAny = xPropSet->getPropertyValue( gsIsPhysical );
387 bNew = !*o3tl::doAccess<bool>(aAny);
389 SetNew( bNew );
390 if( rName != GetName() )
391 GetImport().AddStyleDisplayName( GetFamily(), GetName(), rName );
394 if( bOverwrite || bNew )
396 rtl::Reference < XMLPropertySetMapper > xPrMap;
397 if( xImpPrMap.is() )
398 xPrMap = xImpPrMap->getPropertySetMapper();
399 if( xPrMap.is() )
401 Reference < XMultiPropertyStates > xMultiStates( xPropSet,
402 UNO_QUERY );
403 if( xMultiStates.is() )
405 xMultiStates->setAllPropertiesToDefault();
407 else
409 std::set < OUString > aNameSet;
410 sal_Int32 nCount = xPrMap->GetEntryCount();
411 sal_Int32 i;
412 for( i = 0; i < nCount; i++ )
414 const OUString& rPrName = xPrMap->GetEntryAPIName( i );
415 if( xPropSetInfo->hasPropertyByName( rPrName ) )
416 aNameSet.insert( rPrName );
418 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
419 if (xPropState.is())
421 nCount = aNameSet.size();
422 Sequence<OUString> aNames( comphelper::containerToSequence(aNameSet) );
423 Sequence < PropertyState > aStates( xPropState->getPropertyStates(aNames) );
424 const PropertyState *pStates = aStates.getConstArray();
425 OUString* pNames = aNames.getArray();
427 for( i = 0; i < nCount; i++ )
429 if( PropertyState_DIRECT_VALUE == *pStates++ )
430 xPropState->setPropertyToDefault( pNames[i] );
436 if (mxStyle.is())
437 mxStyle->setParentStyle(OUString());
439 FillPropertySet( xPropSet );
441 else
443 SetValid( false );
448 void XMLPropStyleContext::Finish( bool bOverwrite )
450 if( !mxStyle.is() || !(IsNew() || bOverwrite) )
451 return;
453 // The families container must exist
454 Reference < XNameContainer > xFamilies = mxStyles->GetStylesContainer( GetFamily() );
455 SAL_WARN_IF( !xFamilies.is(), "xmloff", "Families lost" );
456 if( !xFamilies.is() )
457 return;
459 // connect parent
460 OUString sParent( GetParentName() );
461 if( !sParent.isEmpty() )
462 sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
463 if( !sParent.isEmpty() && !xFamilies->hasByName( sParent ) )
464 sParent.clear();
466 if( sParent != mxStyle->getParentStyle() )
468 // this may except if setting the parent style forms a
469 // circle in the style dependencies; especially if the parent
470 // style is the same as the current style
473 mxStyle->setParentStyle( sParent );
475 catch(const uno::Exception& e)
477 // according to the API definition, I would expect a
478 // container::NoSuchElementException. But it throws an
479 // uno::RuntimeException instead. I catch
480 // uno::Exception in order to process both of them.
482 // We can't set the parent style. For a proper
483 // Error-Message, we should pass in the name of the
484 // style, as well as the desired parent style.
486 // getName() throws no non-Runtime exception:
487 GetImport().SetError(
488 XMLERROR_FLAG_ERROR | XMLERROR_PARENT_STYLE_NOT_ALLOWED,
489 { mxStyle->getName(), sParent }, e.Message, nullptr );
493 // connect follow
494 OUString sFollow( GetFollow() );
495 if( !sFollow.isEmpty() )
496 sFollow = GetImport().GetStyleDisplayName( GetFamily(), sFollow );
497 if( sFollow.isEmpty() || !xFamilies->hasByName( sFollow ) )
498 sFollow = mxStyle->getName();
500 Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
501 Reference< XPropertySetInfo > xPropSetInfo =
502 xPropSet->getPropertySetInfo();
503 if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
505 Any aAny = xPropSet->getPropertyValue( gsFollowStyle );
506 OUString sCurrFollow;
507 aAny >>= sCurrFollow;
508 if( sCurrFollow != sFollow )
510 xPropSet->setPropertyValue( gsFollowStyle, Any(sFollow) );
514 // Connect linked style.
515 OUString aLinked(GetLinked());
516 if (!aLinked.isEmpty())
518 if (GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH)
520 aLinked = GetImport().GetStyleDisplayName(XmlStyleFamily::TEXT_TEXT, aLinked);
522 else if (GetFamily() == XmlStyleFamily::TEXT_TEXT)
524 aLinked = GetImport().GetStyleDisplayName(XmlStyleFamily::TEXT_PARAGRAPH, aLinked);
527 if (!aLinked.isEmpty() && xPropSetInfo->hasPropertyByName("LinkStyle"))
529 uno::Any aAny = xPropSet->getPropertyValue("LinkStyle");
530 OUString aCurrentLinked;
531 aAny >>= aCurrentLinked;
532 if (aCurrentLinked != aLinked)
534 xPropSet->setPropertyValue("LinkStyle", uno::Any(aLinked));
538 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
540 xPropSet->setPropertyValue( "Hidden", uno::Any( IsHidden( ) ) );
545 bool XMLPropStyleContext::doNewDrawingLayerFillStyleDefinitionsExist(
546 std::u16string_view rFillStyleTag) const
548 if(!maProperties.empty() && !rFillStyleTag.empty())
550 // no & to avoid non-obvious UAF due to the 2nd temp Reference
551 const rtl::Reference<XMLPropertySetMapper> rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
553 if(rMapper.is())
555 for(const auto& a : maProperties)
557 if(a.mnIndex != -1)
559 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
561 if(rPropName == rFillStyleTag)
563 FillStyle eFillStyle(FillStyle_NONE);
565 if(a.maValue >>= eFillStyle)
567 // okay, type was good, FillStyle is set
569 else
571 // also try an int (see XFillStyleItem::PutValue)
572 sal_Int32 nFillStyle(0);
574 if(a.maValue >>= nFillStyle)
576 eFillStyle = static_cast< FillStyle >(nFillStyle);
580 // we found the entry, check it
581 return FillStyle_NONE != eFillStyle;
588 return false;
591 void XMLPropStyleContext::deactivateOldFillStyleDefinitions(
592 const OldFillStyleDefinitionSet& rHashSetOfTags)
594 if(rHashSetOfTags.empty() || maProperties.empty())
595 return;
597 const rtl::Reference< XMLPropertySetMapper >& rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
599 if(!rMapper.is())
600 return;
602 for(auto& a : maProperties)
604 if(a.mnIndex != -1)
606 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
608 if(rHashSetOfTags.find(rPropName) != rHashSetOfTags.end())
610 // mark entry as inactive
611 a.mnIndex = -1;
617 void XMLPropStyleContext::translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames()
619 if(maProperties.empty())
620 return;
622 const rtl::Reference< XMLPropertySetMapper >& rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
624 if(!rMapper.is())
625 return;
627 static constexpr OUStringLiteral s_FillGradientName(u"FillGradientName");
628 static constexpr OUStringLiteral s_FillHatchName(u"FillHatchName");
629 static constexpr OUStringLiteral s_FillBitmapName(u"FillBitmapName");
630 static constexpr OUStringLiteral s_FillTransparenceGradientName(u"FillTransparenceGradientName");
632 for(auto& a : maProperties)
634 if(a.mnIndex != -1)
636 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
637 XmlStyleFamily aStyleFamily(XmlStyleFamily::DATA_STYLE);
639 if(rPropName == s_FillGradientName || rPropName == s_FillTransparenceGradientName)
641 aStyleFamily = XmlStyleFamily::SD_GRADIENT_ID;
643 else if(rPropName == s_FillHatchName)
645 aStyleFamily = XmlStyleFamily::SD_HATCH_ID;
647 else if(rPropName == s_FillBitmapName)
649 aStyleFamily = XmlStyleFamily::SD_FILL_IMAGE_ID;
652 if(aStyleFamily != XmlStyleFamily::DATA_STYLE)
654 OUString sStyleName;
656 a.maValue >>= sStyleName;
657 sStyleName = GetImport().GetStyleDisplayName( aStyleFamily, sStyleName );
658 a.maValue <<= sStyleName;
664 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */