nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / style / prstylei.cxx
blob5ed852e98e48b8761fc5f9f91f38d2e338bfd172
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_uInt16 nPrefixKey,
58 const OUString& rLocalName,
59 const OUString& rValue )
61 if( XML_NAMESPACE_STYLE == nPrefixKey && IsXMLToken( rLocalName, XML_FAMILY ) )
63 SAL_WARN_IF( GetFamily() != SvXMLStylesContext::GetFamily( rValue ), "xmloff", "unexpected style family" );
65 else
67 SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
72 namespace
74 struct theStandardSet :
75 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theStandardSet>
77 OldFillStyleDefinitionSet operator () ()
79 OldFillStyleDefinitionSet aSet;
80 aSet.insert("BackColorRGB");
81 aSet.insert("BackTransparent");
82 aSet.insert("BackColorTransparency");
83 aSet.insert("BackGraphic");
84 aSet.insert("BackGraphicFilter");
85 aSet.insert("BackGraphicLocation");
86 aSet.insert("BackGraphicTransparency");
87 return aSet;
90 struct theHeaderSet :
91 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theHeaderSet>
93 OldFillStyleDefinitionSet operator () ()
95 OldFillStyleDefinitionSet aSet;
96 aSet.insert("HeaderBackColorRGB");
97 aSet.insert("HeaderBackTransparent");
98 aSet.insert("HeaderBackColorTransparency");
99 aSet.insert("HeaderBackGraphic");
100 aSet.insert("HeaderBackGraphicFilter");
101 aSet.insert("HeaderBackGraphicLocation");
102 aSet.insert("HeaderBackGraphicTransparency");
103 return aSet;
106 struct theFooterSet :
107 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theFooterSet>
109 OldFillStyleDefinitionSet operator () ()
111 OldFillStyleDefinitionSet aSet;
112 aSet.insert("FooterBackColorRGB");
113 aSet.insert("FooterBackTransparent");
114 aSet.insert("FooterBackColorTransparency");
115 aSet.insert("FooterBackGraphic");
116 aSet.insert("FooterBackGraphicFilter");
117 aSet.insert("FooterBackGraphicLocation");
118 aSet.insert("FooterBackGraphicTransparency");
119 return aSet;
122 struct theParaSet :
123 public rtl::StaticWithInit<OldFillStyleDefinitionSet, theParaSet>
125 OldFillStyleDefinitionSet operator () ()
127 OldFillStyleDefinitionSet aSet;
128 // Caution: here it is *not* 'ParaBackColorRGB' as it should be, but indeed
129 // 'ParaBackColor' is used, see aXMLParaPropMap definition (line 313)
130 aSet.insert("ParaBackColor");
131 aSet.insert("ParaBackTransparent");
132 aSet.insert("ParaBackGraphicLocation");
133 aSet.insert("ParaBackGraphicFilter");
134 aSet.insert("ParaBackGraphic");
136 // These are not used in aXMLParaPropMap definition, thus not needed here
137 // aSet.insert("ParaBackColorTransparency");
138 // aSet.insert("ParaBackGraphicTransparency");
139 return aSet;
146 constexpr OUStringLiteral gsIsPhysical( u"IsPhysical" );
147 constexpr OUStringLiteral gsFollowStyle( u"FollowStyle" );
149 XMLPropStyleContext::XMLPropStyleContext( SvXMLImport& rImport,
150 SvXMLStylesContext& rStyles, XmlStyleFamily nFamily,
151 bool bDefault )
152 : SvXMLStyleContext( rImport, nFamily, bDefault )
153 , mxStyles( &rStyles )
157 XMLPropStyleContext::~XMLPropStyleContext()
161 const OldFillStyleDefinitionSet& XMLPropStyleContext::getStandardSet()
163 return theStandardSet::get();
166 const OldFillStyleDefinitionSet& XMLPropStyleContext::getHeaderSet()
168 return theHeaderSet::get();
171 const OldFillStyleDefinitionSet& XMLPropStyleContext::getFooterSet()
173 return theFooterSet::get();
176 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLPropStyleContext::createFastChildContext(
177 sal_Int32 nElement,
178 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
180 sal_uInt32 nFamily = 0;
181 if( IsTokenInNamespace(nElement, XML_NAMESPACE_STYLE) ||
182 IsTokenInNamespace(nElement, XML_NAMESPACE_LO_EXT) )
184 sal_Int32 nLocalName = nElement & TOKEN_MASK;
185 if( nLocalName == XML_GRAPHIC_PROPERTIES )
186 nFamily = XML_TYPE_PROP_GRAPHIC;
187 else if( nLocalName == XML_DRAWING_PAGE_PROPERTIES )
188 nFamily = XML_TYPE_PROP_DRAWING_PAGE;
189 else if( nLocalName == XML_TEXT_PROPERTIES )
190 nFamily = XML_TYPE_PROP_TEXT;
191 else if( nLocalName == XML_PARAGRAPH_PROPERTIES )
192 nFamily = XML_TYPE_PROP_PARAGRAPH;
193 else if( nLocalName == XML_RUBY_PROPERTIES )
194 nFamily = XML_TYPE_PROP_RUBY;
195 else if( nLocalName == XML_SECTION_PROPERTIES )
196 nFamily = XML_TYPE_PROP_SECTION;
197 else if( nLocalName == XML_TABLE_PROPERTIES )
198 nFamily = XML_TYPE_PROP_TABLE;
199 else if( nLocalName == XML_TABLE_COLUMN_PROPERTIES )
200 nFamily = XML_TYPE_PROP_TABLE_COLUMN;
201 else if( nLocalName ==XML_TABLE_ROW_PROPERTIES )
202 nFamily = XML_TYPE_PROP_TABLE_ROW;
203 else if( nLocalName == XML_TABLE_CELL_PROPERTIES )
204 nFamily = XML_TYPE_PROP_TABLE_CELL;
205 else if( nLocalName == XML_CHART_PROPERTIES )
206 nFamily = XML_TYPE_PROP_CHART;
208 if( nFamily )
210 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
211 mxStyles->GetImportPropertyMapper( GetFamily() );
212 if( xImpPrMap.is() )
213 return new SvXMLPropertySetContext( GetImport(), nElement,
214 xAttrList,
215 nFamily,
216 maProperties,
217 xImpPrMap );
219 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
220 return nullptr;
223 void XMLPropStyleContext::FillPropertySet(
224 const Reference< XPropertySet > & rPropSet )
226 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
227 mxStyles->GetImportPropertyMapper( GetFamily() );
228 SAL_WARN_IF( !xImpPrMap.is(), "xmloff", "There is the import prop mapper" );
229 if( xImpPrMap.is() )
230 xImpPrMap->FillPropertySet( maProperties, rPropSet );
233 void XMLPropStyleContext::SetDefaults()
237 Reference < XStyle > XMLPropStyleContext::Create()
239 Reference < XStyle > xNewStyle;
241 OUString sServiceName = mxStyles->GetServiceName( GetFamily() );
242 if( !sServiceName.isEmpty() )
244 Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
245 UNO_QUERY );
246 if( xFactory.is() )
248 Reference < XInterface > xIfc =
249 xFactory->createInstance( sServiceName );
250 if( xIfc.is() )
251 xNewStyle.set( xIfc, UNO_QUERY );
255 return xNewStyle;
258 void XMLPropStyleContext::CreateAndInsert( bool bOverwrite )
260 SvXMLStylesContext* pSvXMLStylesContext = mxStyles.get();
261 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap = pSvXMLStylesContext->GetImportPropertyMapper(GetFamily());
262 OSL_ENSURE(xImpPrMap.is(), "There is no import prop mapper");
264 // need to filter out old fill definitions when the new ones are used. The new
265 // ones are used when a FillStyle is defined
266 const bool bTakeCareOfDrawingLayerFillStyle(xImpPrMap.is() && GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH);
267 bool bDrawingLayerFillStylesUsed(false);
269 if(bTakeCareOfDrawingLayerFillStyle)
271 // check if new FillStyles are used and if so mark old ones with -1
272 static OUString s_FillStyle("FillStyle");
274 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle))
276 deactivateOldFillStyleDefinitions(theParaSet::get());
277 bDrawingLayerFillStylesUsed = true;
281 if( pSvXMLStylesContext->IsAutomaticStyle()
282 && ( GetFamily() == XmlStyleFamily::TEXT_TEXT || GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH ) )
284 // Need to translate StyleName from temp MapNames to names
285 // used in already imported items (already exist in the pool). This
286 // is required for AutomaticStyles since these do *not* use FillPropertySet
287 // and thus just trigger CheckSpecialContext in XMLTextStyleContext::FillPropertySet
288 // (which may be double action anyways). The mechanism there to use _ContextID_Index_Pair
289 // is not working for AutomaticStyles and is already too late, too (this
290 // method is already called before XMLTextStyleContext::FillPropertySet gets called)
291 if(bDrawingLayerFillStylesUsed)
293 translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames();
296 Reference < XAutoStyleFamily > xAutoFamily = pSvXMLStylesContext->GetAutoStyles( GetFamily() );
297 if( !xAutoFamily.is() )
298 return;
299 if( xImpPrMap.is() )
301 Sequence< PropertyValue > aValues;
302 xImpPrMap->FillPropertySequence( maProperties, aValues );
304 sal_Int32 nLen = aValues.getLength();
305 if( nLen )
307 if( GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH )
309 aValues.realloc( nLen + 2 );
310 PropertyValue *pProps = aValues.getArray() + nLen;
311 pProps->Name = "ParaStyleName";
312 OUString sParent( GetParentName() );
313 if( !sParent.isEmpty() )
315 sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
316 Reference < XNameContainer > xFamilies = pSvXMLStylesContext->GetStylesContainer( GetFamily() );
317 if(xFamilies.is() && xFamilies->hasByName( sParent ) )
319 css::uno::Reference< css::style::XStyle > xStyle;
320 Any aAny = xFamilies->getByName( sParent );
321 aAny >>= xStyle;
322 sParent = xStyle->getName() ;
325 else
326 sParent = "Standard";
327 pProps->Value <<= sParent;
328 ++pProps;
329 pProps->Name = "ParaConditionalStyleName";
330 pProps->Value <<= sParent;
333 Reference < XAutoStyle > xAutoStyle = xAutoFamily->insertStyle( aValues );
334 if( xAutoStyle.is() )
336 Sequence< OUString > aPropNames(1);
337 aPropNames[0] = GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH ?
338 OUStringLiteral(u"ParaAutoStyleName") :
339 OUStringLiteral(u"CharAutoStyleName");
340 Sequence< Any > aAny = xAutoStyle->getPropertyValues( aPropNames );
341 if( aAny.hasElements() )
343 OUString aName;
344 aAny[0] >>= aName;
345 SetAutoName( aName );
351 else
353 const OUString& rName = GetDisplayName();
354 if( rName.isEmpty() || IsDefaultStyle() )
355 return;
357 Reference < XNameContainer > xFamilies = pSvXMLStylesContext->GetStylesContainer( GetFamily() );
358 if( !xFamilies.is() )
360 SAL_WARN("xmloff", "no styles container for family " << static_cast<int>(GetFamily()));
361 return;
364 bool bNew = false;
365 if( xFamilies->hasByName( rName ) )
367 Any aAny = xFamilies->getByName( rName );
368 aAny >>= mxStyle;
370 else
372 mxStyle = Create();
373 if( !mxStyle.is() )
374 return;
376 xFamilies->insertByName( rName, Any(mxStyle) );
377 bNew = true;
380 Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
381 Reference< XPropertySetInfo > xPropSetInfo =
382 xPropSet->getPropertySetInfo();
383 if( !bNew && xPropSetInfo->hasPropertyByName( gsIsPhysical ) )
385 Any aAny = xPropSet->getPropertyValue( gsIsPhysical );
386 bNew = !*o3tl::doAccess<bool>(aAny);
388 SetNew( bNew );
389 if( rName != GetName() )
390 GetImport().AddStyleDisplayName( GetFamily(), GetName(), rName );
393 if( bOverwrite || bNew )
395 rtl::Reference < XMLPropertySetMapper > xPrMap;
396 if( xImpPrMap.is() )
397 xPrMap = xImpPrMap->getPropertySetMapper();
398 if( xPrMap.is() )
400 Reference < XMultiPropertyStates > xMultiStates( xPropSet,
401 UNO_QUERY );
402 if( xMultiStates.is() )
404 xMultiStates->setAllPropertiesToDefault();
406 else
408 std::set < OUString > aNameSet;
409 sal_Int32 nCount = xPrMap->GetEntryCount();
410 sal_Int32 i;
411 for( i = 0; i < nCount; i++ )
413 const OUString& rPrName = xPrMap->GetEntryAPIName( i );
414 if( xPropSetInfo->hasPropertyByName( rPrName ) )
415 aNameSet.insert( rPrName );
417 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
418 if (xPropState.is())
420 nCount = aNameSet.size();
421 Sequence<OUString> aNames( comphelper::containerToSequence(aNameSet) );
422 Sequence < PropertyState > aStates( xPropState->getPropertyStates(aNames) );
423 const PropertyState *pStates = aStates.getConstArray();
424 OUString* pNames = aNames.getArray();
426 for( i = 0; i < nCount; i++ )
428 if( PropertyState_DIRECT_VALUE == *pStates++ )
429 xPropState->setPropertyToDefault( pNames[i] );
435 if (mxStyle.is())
436 mxStyle->setParentStyle(OUString());
438 FillPropertySet( xPropSet );
440 else
442 SetValid( false );
447 void XMLPropStyleContext::Finish( bool bOverwrite )
449 if( !mxStyle.is() || !(IsNew() || bOverwrite) )
450 return;
452 // The families container must exist
453 Reference < XNameContainer > xFamilies = mxStyles->GetStylesContainer( GetFamily() );
454 SAL_WARN_IF( !xFamilies.is(), "xmloff", "Families lost" );
455 if( !xFamilies.is() )
456 return;
458 // connect parent
459 OUString sParent( GetParentName() );
460 if( !sParent.isEmpty() )
461 sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
462 if( !sParent.isEmpty() && !xFamilies->hasByName( sParent ) )
463 sParent.clear();
465 if( sParent != mxStyle->getParentStyle() )
467 // this may except if setting the parent style forms a
468 // circle in the style dependencies; especially if the parent
469 // style is the same as the current style
472 mxStyle->setParentStyle( sParent );
474 catch(const uno::Exception& e)
476 // according to the API definition, I would expect a
477 // container::NoSuchElementException. But it throws an
478 // uno::RuntimeException instead. I catch
479 // uno::Exception in order to process both of them.
481 // We can't set the parent style. For a proper
482 // Error-Message, we should pass in the name of the
483 // style, as well as the desired parent style.
484 Sequence<OUString> aSequence(2);
486 // getName() throws no non-Runtime exception:
487 aSequence[0] = mxStyle->getName();
488 aSequence[1] = sParent;
490 GetImport().SetError(
491 XMLERROR_FLAG_ERROR | XMLERROR_PARENT_STYLE_NOT_ALLOWED,
492 aSequence, e.Message, nullptr );
496 // connect follow
497 OUString sFollow( GetFollow() );
498 if( !sFollow.isEmpty() )
499 sFollow = GetImport().GetStyleDisplayName( GetFamily(), sFollow );
500 if( sFollow.isEmpty() || !xFamilies->hasByName( sFollow ) )
501 sFollow = mxStyle->getName();
503 Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
504 Reference< XPropertySetInfo > xPropSetInfo =
505 xPropSet->getPropertySetInfo();
506 if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
508 Any aAny = xPropSet->getPropertyValue( gsFollowStyle );
509 OUString sCurrFollow;
510 aAny >>= sCurrFollow;
511 if( sCurrFollow != sFollow )
513 xPropSet->setPropertyValue( gsFollowStyle, Any(sFollow) );
517 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
519 xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
524 bool XMLPropStyleContext::doNewDrawingLayerFillStyleDefinitionsExist(
525 const OUString& rFillStyleTag) const
527 if(!maProperties.empty() && rFillStyleTag.getLength())
529 // no & to avoid non-obvious UAF due to the 2nd temp Reference
530 const rtl::Reference<XMLPropertySetMapper> rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
532 if(rMapper.is())
534 for(const auto& a : maProperties)
536 if(a.mnIndex != -1)
538 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
540 if(rPropName == rFillStyleTag)
542 FillStyle eFillStyle(FillStyle_NONE);
544 if(a.maValue >>= eFillStyle)
546 // okay, type was good, FillStyle is set
548 else
550 // also try an int (see XFillStyleItem::PutValue)
551 sal_Int32 nFillStyle(0);
553 if(a.maValue >>= nFillStyle)
555 eFillStyle = static_cast< FillStyle >(nFillStyle);
559 // we found the entry, check it
560 return FillStyle_NONE != eFillStyle;
567 return false;
570 void XMLPropStyleContext::deactivateOldFillStyleDefinitions(
571 const OldFillStyleDefinitionSet& rHashSetOfTags)
573 if(rHashSetOfTags.empty() || maProperties.empty())
574 return;
576 const rtl::Reference< XMLPropertySetMapper >& rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
578 if(!rMapper.is())
579 return;
581 for(auto& a : maProperties)
583 if(a.mnIndex != -1)
585 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
587 if(rHashSetOfTags.find(rPropName) != rHashSetOfTags.end())
589 // mark entry as inactive
590 a.mnIndex = -1;
596 void XMLPropStyleContext::translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames()
598 if(maProperties.empty())
599 return;
601 const rtl::Reference< XMLPropertySetMapper >& rMapper = GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
603 if(!rMapper.is())
604 return;
606 static OUString s_FillGradientName("FillGradientName");
607 static OUString s_FillHatchName("FillHatchName");
608 static OUString s_FillBitmapName("FillBitmapName");
609 static OUString s_FillTransparenceGradientName("FillTransparenceGradientName");
611 for(auto& a : maProperties)
613 if(a.mnIndex != -1)
615 const OUString& rPropName = rMapper->GetEntryAPIName(a.mnIndex);
616 XmlStyleFamily aStyleFamily(XmlStyleFamily::DATA_STYLE);
618 if(rPropName == s_FillGradientName || rPropName == s_FillTransparenceGradientName)
620 aStyleFamily = XmlStyleFamily::SD_GRADIENT_ID;
622 else if(rPropName == s_FillHatchName)
624 aStyleFamily = XmlStyleFamily::SD_HATCH_ID;
626 else if(rPropName == s_FillBitmapName)
628 aStyleFamily = XmlStyleFamily::SD_FILL_IMAGE_ID;
631 if(aStyleFamily != XmlStyleFamily::DATA_STYLE)
633 OUString sStyleName;
635 a.maValue >>= sStyleName;
636 sStyleName = GetImport().GetStyleDisplayName( aStyleFamily, sStyleName );
637 a.maValue <<= sStyleName;
643 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */