tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / xmloff / source / style / prstylei.cxx
blobf2e8e7e9498379c532ffb9f47cd234ad04ac61f3
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 <com/sun/star/frame/XModel.hpp>
29 #include <com/sun/star/style/XStyle.hpp>
30 #include <com/sun/star/style/XAutoStyleFamily.hpp>
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/XPropertyState.hpp>
34 #include <com/sun/star/beans/XMultiPropertyStates.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <xmloff/xmlimp.hxx>
37 #include <xmloff/prstylei.hxx>
38 #include <xmloff/xmlerror.hxx>
39 #include <xmloff/xmltypes.hxx>
40 #include <xmloff/maptype.hxx>
41 #include <xmloff/xmlimppr.hxx>
42 #include <xmloff/xmlprmap.hxx>
43 #include <comphelper/sequence.hxx>
44 #include <com/sun/star/drawing/FillStyle.hpp>
45 #include "StylePropertiesContext.hxx"
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 const OldFillStyleDefinitionSet & theStandardSet()
75 static const OldFillStyleDefinitionSet theSet = []()
77 OldFillStyleDefinitionSet aSet;
78 aSet.insert(u"BackColorRGB"_ustr);
79 aSet.insert(u"BackTransparent"_ustr);
80 aSet.insert(u"BackColorTransparency"_ustr);
81 aSet.insert(u"BackGraphic"_ustr);
82 aSet.insert(u"BackGraphicFilter"_ustr);
83 aSet.insert(u"BackGraphicLocation"_ustr);
84 aSet.insert(u"BackGraphicTransparency"_ustr);
85 return aSet;
86 }();
87 return theSet;
89 const OldFillStyleDefinitionSet & theHeaderSet()
91 static const OldFillStyleDefinitionSet theSet = []()
93 OldFillStyleDefinitionSet aSet;
94 aSet.insert(u"HeaderBackColorRGB"_ustr);
95 aSet.insert(u"HeaderBackTransparent"_ustr);
96 aSet.insert(u"HeaderBackColorTransparency"_ustr);
97 aSet.insert(u"HeaderBackGraphic"_ustr);
98 aSet.insert(u"HeaderBackGraphicFilter"_ustr);
99 aSet.insert(u"HeaderBackGraphicLocation"_ustr);
100 aSet.insert(u"HeaderBackGraphicTransparency"_ustr);
101 return aSet;
102 }();
103 return theSet;
105 const OldFillStyleDefinitionSet & theFooterSet()
107 static const OldFillStyleDefinitionSet theSet = []()
109 OldFillStyleDefinitionSet aSet;
110 aSet.insert(u"FooterBackColorRGB"_ustr);
111 aSet.insert(u"FooterBackTransparent"_ustr);
112 aSet.insert(u"FooterBackColorTransparency"_ustr);
113 aSet.insert(u"FooterBackGraphic"_ustr);
114 aSet.insert(u"FooterBackGraphicFilter"_ustr);
115 aSet.insert(u"FooterBackGraphicLocation"_ustr);
116 aSet.insert(u"FooterBackGraphicTransparency"_ustr);
117 return aSet;
118 }();
119 return theSet;
121 const OldFillStyleDefinitionSet & theParaSet()
123 static const OldFillStyleDefinitionSet theSet = []()
125 OldFillStyleDefinitionSet aSet;
126 // Caution: here it is *not* 'ParaBackColorRGB' as it should be, but indeed
127 // 'ParaBackColor' is used, see aXMLParaPropMap definition (line 313)
128 aSet.insert(u"ParaBackColor"_ustr);
129 aSet.insert(u"ParaBackTransparent"_ustr);
130 aSet.insert(u"ParaBackGraphicLocation"_ustr);
131 aSet.insert(u"ParaBackGraphicFilter"_ustr);
132 aSet.insert(u"ParaBackGraphic"_ustr);
134 // These are not used in aXMLParaPropMap definition, thus not needed here
135 // aSet.insert("ParaBackColorTransparency");
136 // aSet.insert("ParaBackGraphicTransparency");
137 return aSet;
138 }();
139 return theSet;
145 constexpr OUString gsIsPhysical( u"IsPhysical"_ustr );
146 constexpr OUString gsFollowStyle( u"FollowStyle"_ustr );
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();
165 const OldFillStyleDefinitionSet& XMLPropStyleContext::getHeaderSet()
167 return theHeaderSet();
170 const OldFillStyleDefinitionSet& XMLPropStyleContext::getFooterSet()
172 return theFooterSet();
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())
213 return new StylePropertiesContext(GetImport(), nElement, xAttrList, nFamily, maProperties, xImpPrMap);
216 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
217 return nullptr;
220 void XMLPropStyleContext::FillPropertySet(
221 const Reference< XPropertySet > & rPropSet )
223 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
224 mxStyles->GetImportPropertyMapper( GetFamily() );
225 SAL_WARN_IF( !xImpPrMap.is(), "xmloff", "There is the import prop mapper" );
226 if( xImpPrMap.is() )
227 xImpPrMap->FillPropertySet( maProperties, rPropSet );
230 void XMLPropStyleContext::SetDefaults()
234 Reference < XStyle > XMLPropStyleContext::Create()
236 Reference < XStyle > xNewStyle;
238 OUString sServiceName = mxStyles->GetServiceName( GetFamily() );
239 if( !sServiceName.isEmpty() )
241 Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
242 UNO_QUERY );
243 if( xFactory.is() )
245 Reference < XInterface > xIfc =
246 xFactory->createInstance( sServiceName );
247 if( xIfc.is() )
248 xNewStyle.set( xIfc, UNO_QUERY );
252 return xNewStyle;
255 void XMLPropStyleContext::CreateAndInsert( bool bOverwrite )
257 SvXMLStylesContext* pSvXMLStylesContext = mxStyles.get();
258 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap = pSvXMLStylesContext->GetImportPropertyMapper(GetFamily());
259 OSL_ENSURE(xImpPrMap.is(), "There is no import prop mapper");
261 // need to filter out old fill definitions when the new ones are used. The new
262 // ones are used when a FillStyle is defined
263 const bool bTakeCareOfDrawingLayerFillStyle(xImpPrMap.is() && GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH);
264 bool bDrawingLayerFillStylesUsed(false);
266 if(bTakeCareOfDrawingLayerFillStyle)
268 // check if new FillStyles are used and if so mark old ones with -1
269 static OUString s_FillStyle(u"FillStyle"_ustr);
271 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle))
273 deactivateOldFillStyleDefinitions(theParaSet());
274 bDrawingLayerFillStylesUsed = true;
278 if( pSvXMLStylesContext->IsAutomaticStyle()
279 && ( GetFamily() == XmlStyleFamily::TEXT_TEXT || GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH ) )
281 // Need to translate StyleName from temp MapNames to names
282 // used in already imported items (already exist in the pool). This
283 // is required for AutomaticStyles since these do *not* use FillPropertySet
284 // and thus just trigger CheckSpecialContext in XMLTextStyleContext::FillPropertySet
285 // (which may be double action anyways). The mechanism there to use _ContextID_Index_Pair
286 // is not working for AutomaticStyles and is already too late, too (this
287 // method is already called before XMLTextStyleContext::FillPropertySet gets called)
288 if(bDrawingLayerFillStylesUsed)
290 translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames();
293 Reference < XAutoStyleFamily > xAutoFamily = pSvXMLStylesContext->GetAutoStyles( GetFamily() );
294 if( !xAutoFamily.is() )
295 return;
296 if( xImpPrMap.is() )
298 Sequence< PropertyValue > aValues;
299 xImpPrMap->FillPropertySequence( maProperties, aValues );
301 sal_Int32 nLen = aValues.getLength();
302 if( nLen )
304 if( GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH )
306 aValues.realloc( nLen + 2 );
307 PropertyValue *pProps = aValues.getArray() + nLen;
308 pProps->Name = "ParaStyleName";
309 OUString sParent( GetParentName() );
310 if( !sParent.isEmpty() )
312 sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
313 Reference < XNameContainer > xFamilies = pSvXMLStylesContext->GetStylesContainer( GetFamily() );
314 if(xFamilies.is() && xFamilies->hasByName( sParent ) )
316 css::uno::Reference< css::style::XStyle > xStyle;
317 Any aAny = xFamilies->getByName( sParent );
318 aAny >>= xStyle;
319 sParent = xStyle->getName() ;
322 else
323 sParent = "Standard";
324 pProps->Value <<= sParent;
325 ++pProps;
326 pProps->Name = "ParaConditionalStyleName";
327 pProps->Value <<= sParent;
330 Reference < XAutoStyle > xAutoStyle = xAutoFamily->insertStyle( aValues );
331 if( xAutoStyle.is() )
333 Sequence< OUString > aPropNames
335 (GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH)?
336 u"ParaAutoStyleName"_ustr:
337 u"CharAutoStyleName"_ustr
339 Sequence< Any > aAny = xAutoStyle->getPropertyValues( aPropNames );
340 if( aAny.hasElements() )
342 SetAutoName(aAny[0]);
348 else
350 const OUString& rName = GetDisplayName();
351 if( rName.isEmpty() || IsDefaultStyle() )
352 return;
354 Reference < XNameContainer > xFamilies = pSvXMLStylesContext->GetStylesContainer( GetFamily() );
355 if( !xFamilies.is() )
357 SAL_WARN("xmloff", "no styles container for family " << static_cast<int>(GetFamily()));
358 return;
361 bool bNew = false;
362 if( xFamilies->hasByName( rName ) )
364 Any aAny = xFamilies->getByName( rName );
365 aAny >>= mxStyle;
367 else
369 mxStyle = Create();
370 if( !mxStyle.is() )
371 return;
373 xFamilies->insertByName( rName, Any(mxStyle) );
374 bNew = true;
377 Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
378 Reference< XPropertySetInfo > xPropSetInfo =
379 xPropSet->getPropertySetInfo();
380 if( !bNew && xPropSetInfo->hasPropertyByName( gsIsPhysical ) )
382 Any aAny = xPropSet->getPropertyValue( gsIsPhysical );
383 bNew = !*o3tl::doAccess<bool>(aAny);
385 SetNew( bNew );
386 if( rName != GetName() )
387 GetImport().AddStyleDisplayName( GetFamily(), GetName(), rName );
390 if( bOverwrite || bNew )
392 rtl::Reference < XMLPropertySetMapper > xPrMap;
393 if( xImpPrMap.is() )
394 xPrMap = xImpPrMap->getPropertySetMapper();
395 if( xPrMap.is() )
397 Reference < XMultiPropertyStates > xMultiStates( xPropSet,
398 UNO_QUERY );
399 if( xMultiStates.is() )
401 xMultiStates->setAllPropertiesToDefault();
403 else
405 std::set < OUString > aNameSet;
406 sal_Int32 nCount = xPrMap->GetEntryCount();
407 sal_Int32 i;
408 for( i = 0; i < nCount; i++ )
410 const OUString& rPrName = xPrMap->GetEntryAPIName( i );
411 if( xPropSetInfo->hasPropertyByName( rPrName ) )
412 aNameSet.insert( rPrName );
414 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
415 if (xPropState.is())
417 nCount = aNameSet.size();
418 Sequence<OUString> aNames( comphelper::containerToSequence(aNameSet) );
419 Sequence < PropertyState > aStates( xPropState->getPropertyStates(aNames) );
420 const PropertyState *pStates = aStates.getConstArray();
421 OUString* pNames = aNames.getArray();
423 for( i = 0; i < nCount; i++ )
425 if( PropertyState_DIRECT_VALUE == *pStates++ )
426 xPropState->setPropertyToDefault( pNames[i] );
432 if (mxStyle.is())
433 mxStyle->setParentStyle(OUString());
435 FillPropertySet( xPropSet );
437 else
439 SetValid( false );
444 void XMLPropStyleContext::Finish( bool bOverwrite )
446 if( !mxStyle.is() || !(IsNew() || bOverwrite) )
447 return;
449 // The families container must exist
450 Reference < XNameContainer > xFamilies = mxStyles->GetStylesContainer( GetFamily() );
451 SAL_WARN_IF( !xFamilies.is(), "xmloff", "Families lost" );
452 if( !xFamilies.is() )
453 return;
455 // connect parent
456 OUString sParent( GetParentName() );
457 if( !sParent.isEmpty() )
458 sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
459 if( !sParent.isEmpty() && !xFamilies->hasByName( sParent ) )
460 sParent.clear();
462 if( sParent != mxStyle->getParentStyle() )
464 // this may except if setting the parent style forms a
465 // circle in the style dependencies; especially if the parent
466 // style is the same as the current style
469 mxStyle->setParentStyle( sParent );
471 catch(const uno::Exception& e)
473 // according to the API definition, I would expect a
474 // container::NoSuchElementException. But it throws an
475 // uno::RuntimeException instead. I catch
476 // uno::Exception in order to process both of them.
478 // We can't set the parent style. For a proper
479 // Error-Message, we should pass in the name of the
480 // style, as well as the desired parent style.
482 // getName() throws no non-Runtime exception:
483 GetImport().SetError(
484 XMLERROR_FLAG_ERROR | XMLERROR_PARENT_STYLE_NOT_ALLOWED,
485 { mxStyle->getName(), sParent }, e.Message, nullptr );
489 // connect follow
490 OUString sFollow( GetFollow() );
491 if( !sFollow.isEmpty() )
492 sFollow = GetImport().GetStyleDisplayName( GetFamily(), sFollow );
493 if( sFollow.isEmpty() || !xFamilies->hasByName( sFollow ) )
494 sFollow = mxStyle->getName();
496 Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
497 Reference< XPropertySetInfo > xPropSetInfo =
498 xPropSet->getPropertySetInfo();
499 if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
501 Any aAny = xPropSet->getPropertyValue( gsFollowStyle );
502 OUString sCurrFollow;
503 aAny >>= sCurrFollow;
504 if( sCurrFollow != sFollow )
506 xPropSet->setPropertyValue( gsFollowStyle, Any(sFollow) );
510 // Connect linked style.
511 OUString aLinked(GetLinked());
512 if (!aLinked.isEmpty())
514 if (GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH)
516 aLinked = GetImport().GetStyleDisplayName(XmlStyleFamily::TEXT_TEXT, aLinked);
518 else if (GetFamily() == XmlStyleFamily::TEXT_TEXT)
520 aLinked = GetImport().GetStyleDisplayName(XmlStyleFamily::TEXT_PARAGRAPH, aLinked);
523 if (!aLinked.isEmpty() && xPropSetInfo->hasPropertyByName(u"LinkStyle"_ustr))
525 uno::Any aAny = xPropSet->getPropertyValue(u"LinkStyle"_ustr);
526 OUString aCurrentLinked;
527 aAny >>= aCurrentLinked;
528 if (aCurrentLinked != aLinked)
530 xPropSet->setPropertyValue(u"LinkStyle"_ustr, uno::Any(aLinked));
534 if ( xPropSetInfo->hasPropertyByName( u"Hidden"_ustr ) )
536 xPropSet->setPropertyValue( u"Hidden"_ustr, uno::Any( IsHidden( ) ) );
541 bool XMLPropStyleContext::doNewDrawingLayerFillStyleDefinitionsExist(
542 std::u16string_view rFillStyleTag) const
544 if(!maProperties.empty() && !rFillStyleTag.empty())
546 // no & to avoid non-obvious UAF due to the 2nd temp Reference
547 const rtl::Reference<XMLPropertySetMapper> rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
549 if(rMapper.is())
551 for(const auto& a : maProperties)
553 if(a.mnIndex != -1)
555 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
557 if(rPropName == rFillStyleTag)
559 FillStyle eFillStyle(FillStyle_NONE);
561 if(a.maValue >>= eFillStyle)
563 // okay, type was good, FillStyle is set
565 else
567 // also try an int (see XFillStyleItem::PutValue)
568 sal_Int32 nFillStyle(0);
570 if(a.maValue >>= nFillStyle)
572 eFillStyle = static_cast< FillStyle >(nFillStyle);
576 // we found the entry, check it
577 return FillStyle_NONE != eFillStyle;
584 return false;
587 void XMLPropStyleContext::deactivateOldFillStyleDefinitions(
588 const OldFillStyleDefinitionSet& rHashSetOfTags)
590 if(rHashSetOfTags.empty() || maProperties.empty())
591 return;
593 const rtl::Reference< XMLPropertySetMapper >& rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
595 if(!rMapper.is())
596 return;
598 for(auto& a : maProperties)
600 if(a.mnIndex != -1)
602 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
604 if(rHashSetOfTags.find(rPropName) != rHashSetOfTags.end())
606 // mark entry as inactive
607 a.mnIndex = -1;
613 void XMLPropStyleContext::translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames()
615 if(maProperties.empty())
616 return;
618 const rtl::Reference< XMLPropertySetMapper >& rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
620 if(!rMapper.is())
621 return;
623 for(auto& a : maProperties)
625 if(a.mnIndex != -1)
627 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
628 XmlStyleFamily aStyleFamily(XmlStyleFamily::DATA_STYLE);
630 if(rPropName == u"FillGradientName"
631 || rPropName == u"FillTransparenceGradientName")
633 aStyleFamily = XmlStyleFamily::SD_GRADIENT_ID;
635 else if(rPropName == u"FillHatchName")
637 aStyleFamily = XmlStyleFamily::SD_HATCH_ID;
639 else if(rPropName == u"FillBitmapName")
641 aStyleFamily = XmlStyleFamily::SD_FILL_IMAGE_ID;
644 if(aStyleFamily != XmlStyleFamily::DATA_STYLE)
646 OUString sStyleName;
648 a.maValue >>= sStyleName;
649 sStyleName = GetImport().GetStyleDisplayName( aStyleFamily, sStyleName );
650 a.maValue <<= sStyleName;
656 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */