1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
46 #include "StylePropertiesContext.hxx"
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::xml::sax
;
51 using namespace ::com::sun::star::style
;
52 using namespace ::com::sun::star::container
;
53 using namespace ::com::sun::star::beans
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::xmloff::token
;
56 using namespace com::sun::star::drawing
;
58 void XMLPropStyleContext::SetAttribute( sal_Int32 nElement
,
59 const OUString
& rValue
)
61 if( nElement
== XML_ELEMENT(STYLE
, XML_FAMILY
) )
63 SAL_WARN_IF( GetFamily() != SvXMLStylesContext::GetFamily( rValue
), "xmloff", "unexpected style family" );
67 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
74 const OldFillStyleDefinitionSet
& theStandardSet()
76 static const OldFillStyleDefinitionSet theSet
= []()
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");
90 const OldFillStyleDefinitionSet
& theHeaderSet()
92 static const OldFillStyleDefinitionSet theSet
= []()
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");
106 const OldFillStyleDefinitionSet
& theFooterSet()
108 static const OldFillStyleDefinitionSet theSet
= []()
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");
122 const OldFillStyleDefinitionSet
& theParaSet()
124 static const OldFillStyleDefinitionSet theSet
= []()
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");
146 constexpr OUStringLiteral
gsIsPhysical( u
"IsPhysical" );
147 constexpr OUStringLiteral
gsFollowStyle( u
"FollowStyle" );
149 XMLPropStyleContext::XMLPropStyleContext( SvXMLImport
& rImport
,
150 SvXMLStylesContext
& rStyles
, XmlStyleFamily nFamily
,
152 : SvXMLStyleContext( rImport
, nFamily
, bDefault
)
153 , mxStyles( &rStyles
)
157 XMLPropStyleContext::~XMLPropStyleContext()
161 const OldFillStyleDefinitionSet
& XMLPropStyleContext::getStandardSet()
163 return theStandardSet();
166 const OldFillStyleDefinitionSet
& XMLPropStyleContext::getHeaderSet()
168 return theHeaderSet();
171 const OldFillStyleDefinitionSet
& XMLPropStyleContext::getFooterSet()
173 return theFooterSet();
176 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLPropStyleContext::createFastChildContext(
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
;
210 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
=
211 mxStyles
->GetImportPropertyMapper( GetFamily() );
214 return new StylePropertiesContext(GetImport(), nElement
, xAttrList
, nFamily
, maProperties
, xImpPrMap
);
217 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
221 void XMLPropStyleContext::FillPropertySet(
222 const Reference
< XPropertySet
> & rPropSet
)
224 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
=
225 mxStyles
->GetImportPropertyMapper( GetFamily() );
226 SAL_WARN_IF( !xImpPrMap
.is(), "xmloff", "There is the import prop mapper" );
228 xImpPrMap
->FillPropertySet( maProperties
, rPropSet
);
231 void XMLPropStyleContext::SetDefaults()
235 Reference
< XStyle
> XMLPropStyleContext::Create()
237 Reference
< XStyle
> xNewStyle
;
239 OUString sServiceName
= mxStyles
->GetServiceName( GetFamily() );
240 if( !sServiceName
.isEmpty() )
242 Reference
< XMultiServiceFactory
> xFactory( GetImport().GetModel(),
246 Reference
< XInterface
> xIfc
=
247 xFactory
->createInstance( sServiceName
);
249 xNewStyle
.set( xIfc
, UNO_QUERY
);
256 void XMLPropStyleContext::CreateAndInsert( bool bOverwrite
)
258 SvXMLStylesContext
* pSvXMLStylesContext
= mxStyles
.get();
259 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
= pSvXMLStylesContext
->GetImportPropertyMapper(GetFamily());
260 OSL_ENSURE(xImpPrMap
.is(), "There is no import prop mapper");
262 // need to filter out old fill definitions when the new ones are used. The new
263 // ones are used when a FillStyle is defined
264 const bool bTakeCareOfDrawingLayerFillStyle(xImpPrMap
.is() && GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH
);
265 bool bDrawingLayerFillStylesUsed(false);
267 if(bTakeCareOfDrawingLayerFillStyle
)
269 // check if new FillStyles are used and if so mark old ones with -1
270 static OUString
s_FillStyle("FillStyle");
272 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle
))
274 deactivateOldFillStyleDefinitions(theParaSet());
275 bDrawingLayerFillStylesUsed
= true;
279 if( pSvXMLStylesContext
->IsAutomaticStyle()
280 && ( GetFamily() == XmlStyleFamily::TEXT_TEXT
|| GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH
) )
282 // Need to translate StyleName from temp MapNames to names
283 // used in already imported items (already exist in the pool). This
284 // is required for AutomaticStyles since these do *not* use FillPropertySet
285 // and thus just trigger CheckSpecialContext in XMLTextStyleContext::FillPropertySet
286 // (which may be double action anyways). The mechanism there to use _ContextID_Index_Pair
287 // is not working for AutomaticStyles and is already too late, too (this
288 // method is already called before XMLTextStyleContext::FillPropertySet gets called)
289 if(bDrawingLayerFillStylesUsed
)
291 translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames();
294 Reference
< XAutoStyleFamily
> xAutoFamily
= pSvXMLStylesContext
->GetAutoStyles( GetFamily() );
295 if( !xAutoFamily
.is() )
299 Sequence
< PropertyValue
> aValues
;
300 xImpPrMap
->FillPropertySequence( maProperties
, aValues
);
302 sal_Int32 nLen
= aValues
.getLength();
305 if( GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH
)
307 aValues
.realloc( nLen
+ 2 );
308 PropertyValue
*pProps
= aValues
.getArray() + nLen
;
309 pProps
->Name
= "ParaStyleName";
310 OUString
sParent( GetParentName() );
311 if( !sParent
.isEmpty() )
313 sParent
= GetImport().GetStyleDisplayName( GetFamily(), sParent
);
314 Reference
< XNameContainer
> xFamilies
= pSvXMLStylesContext
->GetStylesContainer( GetFamily() );
315 if(xFamilies
.is() && xFamilies
->hasByName( sParent
) )
317 css::uno::Reference
< css::style::XStyle
> xStyle
;
318 Any aAny
= xFamilies
->getByName( sParent
);
320 sParent
= xStyle
->getName() ;
324 sParent
= "Standard";
325 pProps
->Value
<<= sParent
;
327 pProps
->Name
= "ParaConditionalStyleName";
328 pProps
->Value
<<= sParent
;
331 Reference
< XAutoStyle
> xAutoStyle
= xAutoFamily
->insertStyle( aValues
);
332 if( xAutoStyle
.is() )
334 Sequence
< OUString
> aPropNames
336 (GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH
)?
337 OUString("ParaAutoStyleName"):
338 OUString("CharAutoStyleName")
340 Sequence
< Any
> aAny
= xAutoStyle
->getPropertyValues( aPropNames
);
341 if( aAny
.hasElements() )
343 SetAutoName(aAny
[0]);
351 const OUString
& rName
= GetDisplayName();
352 if( rName
.isEmpty() || IsDefaultStyle() )
355 Reference
< XNameContainer
> xFamilies
= pSvXMLStylesContext
->GetStylesContainer( GetFamily() );
356 if( !xFamilies
.is() )
358 SAL_WARN("xmloff", "no styles container for family " << static_cast<int>(GetFamily()));
363 if( xFamilies
->hasByName( rName
) )
365 Any aAny
= xFamilies
->getByName( rName
);
374 xFamilies
->insertByName( rName
, Any(mxStyle
) );
378 Reference
< XPropertySet
> xPropSet( mxStyle
, UNO_QUERY
);
379 Reference
< XPropertySetInfo
> xPropSetInfo
=
380 xPropSet
->getPropertySetInfo();
381 if( !bNew
&& xPropSetInfo
->hasPropertyByName( gsIsPhysical
) )
383 Any aAny
= xPropSet
->getPropertyValue( gsIsPhysical
);
384 bNew
= !*o3tl::doAccess
<bool>(aAny
);
387 if( rName
!= GetName() )
388 GetImport().AddStyleDisplayName( GetFamily(), GetName(), rName
);
391 if( bOverwrite
|| bNew
)
393 rtl::Reference
< XMLPropertySetMapper
> xPrMap
;
395 xPrMap
= xImpPrMap
->getPropertySetMapper();
398 Reference
< XMultiPropertyStates
> xMultiStates( xPropSet
,
400 if( xMultiStates
.is() )
402 xMultiStates
->setAllPropertiesToDefault();
406 std::set
< OUString
> aNameSet
;
407 sal_Int32 nCount
= xPrMap
->GetEntryCount();
409 for( i
= 0; i
< nCount
; i
++ )
411 const OUString
& rPrName
= xPrMap
->GetEntryAPIName( i
);
412 if( xPropSetInfo
->hasPropertyByName( rPrName
) )
413 aNameSet
.insert( rPrName
);
415 Reference
< XPropertyState
> xPropState( xPropSet
, uno::UNO_QUERY
);
418 nCount
= aNameSet
.size();
419 Sequence
<OUString
> aNames( comphelper::containerToSequence(aNameSet
) );
420 Sequence
< PropertyState
> aStates( xPropState
->getPropertyStates(aNames
) );
421 const PropertyState
*pStates
= aStates
.getConstArray();
422 OUString
* pNames
= aNames
.getArray();
424 for( i
= 0; i
< nCount
; i
++ )
426 if( PropertyState_DIRECT_VALUE
== *pStates
++ )
427 xPropState
->setPropertyToDefault( pNames
[i
] );
434 mxStyle
->setParentStyle(OUString());
436 FillPropertySet( xPropSet
);
445 void XMLPropStyleContext::Finish( bool bOverwrite
)
447 if( !mxStyle
.is() || !(IsNew() || bOverwrite
) )
450 // The families container must exist
451 Reference
< XNameContainer
> xFamilies
= mxStyles
->GetStylesContainer( GetFamily() );
452 SAL_WARN_IF( !xFamilies
.is(), "xmloff", "Families lost" );
453 if( !xFamilies
.is() )
457 OUString
sParent( GetParentName() );
458 if( !sParent
.isEmpty() )
459 sParent
= GetImport().GetStyleDisplayName( GetFamily(), sParent
);
460 if( !sParent
.isEmpty() && !xFamilies
->hasByName( sParent
) )
463 if( sParent
!= mxStyle
->getParentStyle() )
465 // this may except if setting the parent style forms a
466 // circle in the style dependencies; especially if the parent
467 // style is the same as the current style
470 mxStyle
->setParentStyle( sParent
);
472 catch(const uno::Exception
& e
)
474 // according to the API definition, I would expect a
475 // container::NoSuchElementException. But it throws an
476 // uno::RuntimeException instead. I catch
477 // uno::Exception in order to process both of them.
479 // We can't set the parent style. For a proper
480 // Error-Message, we should pass in the name of the
481 // style, as well as the desired parent style.
483 // getName() throws no non-Runtime exception:
484 GetImport().SetError(
485 XMLERROR_FLAG_ERROR
| XMLERROR_PARENT_STYLE_NOT_ALLOWED
,
486 { mxStyle
->getName(), sParent
}, e
.Message
, nullptr );
491 OUString
sFollow( GetFollow() );
492 if( !sFollow
.isEmpty() )
493 sFollow
= GetImport().GetStyleDisplayName( GetFamily(), sFollow
);
494 if( sFollow
.isEmpty() || !xFamilies
->hasByName( sFollow
) )
495 sFollow
= mxStyle
->getName();
497 Reference
< XPropertySet
> xPropSet( mxStyle
, UNO_QUERY
);
498 Reference
< XPropertySetInfo
> xPropSetInfo
=
499 xPropSet
->getPropertySetInfo();
500 if( xPropSetInfo
->hasPropertyByName( gsFollowStyle
) )
502 Any aAny
= xPropSet
->getPropertyValue( gsFollowStyle
);
503 OUString sCurrFollow
;
504 aAny
>>= sCurrFollow
;
505 if( sCurrFollow
!= sFollow
)
507 xPropSet
->setPropertyValue( gsFollowStyle
, Any(sFollow
) );
511 // Connect linked style.
512 OUString
aLinked(GetLinked());
513 if (!aLinked
.isEmpty())
515 if (GetFamily() == XmlStyleFamily::TEXT_PARAGRAPH
)
517 aLinked
= GetImport().GetStyleDisplayName(XmlStyleFamily::TEXT_TEXT
, aLinked
);
519 else if (GetFamily() == XmlStyleFamily::TEXT_TEXT
)
521 aLinked
= GetImport().GetStyleDisplayName(XmlStyleFamily::TEXT_PARAGRAPH
, aLinked
);
524 if (!aLinked
.isEmpty() && xPropSetInfo
->hasPropertyByName("LinkStyle"))
526 uno::Any aAny
= xPropSet
->getPropertyValue("LinkStyle");
527 OUString aCurrentLinked
;
528 aAny
>>= aCurrentLinked
;
529 if (aCurrentLinked
!= aLinked
)
531 xPropSet
->setPropertyValue("LinkStyle", uno::Any(aLinked
));
535 if ( xPropSetInfo
->hasPropertyByName( "Hidden" ) )
537 xPropSet
->setPropertyValue( "Hidden", uno::Any( IsHidden( ) ) );
542 bool XMLPropStyleContext::doNewDrawingLayerFillStyleDefinitionsExist(
543 std::u16string_view rFillStyleTag
) const
545 if(!maProperties
.empty() && !rFillStyleTag
.empty())
547 // no & to avoid non-obvious UAF due to the 2nd temp Reference
548 const rtl::Reference
<XMLPropertySetMapper
> rMapper
= GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
552 for(const auto& a
: maProperties
)
556 const OUString
& rPropName
= rMapper
->GetEntryAPIName(a
.mnIndex
);
558 if(rPropName
== rFillStyleTag
)
560 FillStyle
eFillStyle(FillStyle_NONE
);
562 if(a
.maValue
>>= eFillStyle
)
564 // okay, type was good, FillStyle is set
568 // also try an int (see XFillStyleItem::PutValue)
569 sal_Int32
nFillStyle(0);
571 if(a
.maValue
>>= nFillStyle
)
573 eFillStyle
= static_cast< FillStyle
>(nFillStyle
);
577 // we found the entry, check it
578 return FillStyle_NONE
!= eFillStyle
;
588 void XMLPropStyleContext::deactivateOldFillStyleDefinitions(
589 const OldFillStyleDefinitionSet
& rHashSetOfTags
)
591 if(rHashSetOfTags
.empty() || maProperties
.empty())
594 const rtl::Reference
< XMLPropertySetMapper
>& rMapper
= GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
599 for(auto& a
: maProperties
)
603 const OUString
& rPropName
= rMapper
->GetEntryAPIName(a
.mnIndex
);
605 if(rHashSetOfTags
.find(rPropName
) != rHashSetOfTags
.end())
607 // mark entry as inactive
614 void XMLPropStyleContext::translateNameBasedDrawingLayerFillStyleDefinitionsToStyleDisplayNames()
616 if(maProperties
.empty())
619 const rtl::Reference
< XMLPropertySetMapper
>& rMapper
= GetStyles()->GetImportPropertyMapper(GetFamily())->getPropertySetMapper();
624 static constexpr OUStringLiteral
s_FillGradientName(u
"FillGradientName");
625 static constexpr OUStringLiteral
s_FillHatchName(u
"FillHatchName");
626 static constexpr OUStringLiteral
s_FillBitmapName(u
"FillBitmapName");
627 static constexpr OUStringLiteral
s_FillTransparenceGradientName(u
"FillTransparenceGradientName");
629 for(auto& a
: maProperties
)
633 const OUString
& rPropName
= rMapper
->GetEntryAPIName(a
.mnIndex
);
634 XmlStyleFamily
aStyleFamily(XmlStyleFamily::DATA_STYLE
);
636 if(rPropName
== s_FillGradientName
|| rPropName
== s_FillTransparenceGradientName
)
638 aStyleFamily
= XmlStyleFamily::SD_GRADIENT_ID
;
640 else if(rPropName
== s_FillHatchName
)
642 aStyleFamily
= XmlStyleFamily::SD_HATCH_ID
;
644 else if(rPropName
== s_FillBitmapName
)
646 aStyleFamily
= XmlStyleFamily::SD_FILL_IMAGE_ID
;
649 if(aStyleFamily
!= XmlStyleFamily::DATA_STYLE
)
653 a
.maValue
>>= sStyleName
;
654 sStyleName
= GetImport().GetStyleDisplayName( aStyleFamily
, sStyleName
);
655 a
.maValue
<<= sStyleName
;
661 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */