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 <xmloff/xmlnamespace.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmluconv.hxx>
26 #include <xmloff/xmlexppr.hxx>
27 #include <com/sun/star/frame/XModel.hpp>
28 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
29 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
30 #include <com/sun/star/style/XStyle.hpp>
31 #include <com/sun/star/beans/NamedValue.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/XPropertyState.hpp>
34 #include <com/sun/star/document/XEventsSupplier.hpp>
35 #include <com/sun/star/text/XChapterNumberingSupplier.hpp>
36 #include <xmloff/xmlaustp.hxx>
37 #include <xmloff/styleexp.hxx>
38 #include <xmloff/xmlexp.hxx>
39 #include <xmloff/XMLEventExport.hxx>
40 #include <xmloff/maptype.hxx>
43 #include <prstylecond.hxx>
44 #include <sal/log.hxx>
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::style
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::com::sun::star::beans
;
51 using namespace ::com::sun::star::text
;
52 using namespace ::xmloff::token
;
54 using ::com::sun::star::document::XEventsSupplier
;
56 constexpr OUStringLiteral
gsIsPhysical( u
"IsPhysical" );
57 constexpr OUStringLiteral
gsIsAutoUpdate( u
"IsAutoUpdate" );
58 constexpr OUStringLiteral
gsFollowStyle( u
"FollowStyle" );
59 constexpr OUStringLiteral
gsNumberingStyleName( u
"NumberingStyleName" );
60 constexpr OUStringLiteral
gsOutlineLevel( u
"OutlineLevel" );
62 XMLStyleExport::XMLStyleExport(
64 SvXMLAutoStylePoolP
*pAutoStyleP
) :
66 m_pAutoStylePool( pAutoStyleP
)
70 XMLStyleExport::~XMLStyleExport()
74 void XMLStyleExport::exportStyleAttributes( const Reference
< XStyle
>& )
78 void XMLStyleExport::exportStyleContent( const Reference
< XStyle
>& rStyle
)
80 Reference
< XPropertySet
> xPropSet( rStyle
, UNO_QUERY
);
81 assert(xPropSet
.is());
85 uno::Any aProperty
= xPropSet
->getPropertyValue( "ParaStyleConditions" );
86 uno::Sequence
< beans::NamedValue
> aSeq
;
90 for (beans::NamedValue
const& rNamedCond
: std::as_const(aSeq
))
94 if (rNamedCond
.Value
>>= aStyleName
)
96 if (!aStyleName
.isEmpty())
98 OUString aExternal
= GetParaStyleCondExternal(rNamedCond
.Name
);
100 if (!aExternal
.isEmpty())
104 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
107 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
108 XML_APPLY_STYLE_NAME
,
109 GetExport().EncodeStyleName( aStyleName
,
111 SvXMLElementExport
aElem( GetExport(),
121 catch( const beans::UnknownPropertyException
& )
128 /// Writes <style:style style:list-level="..."> for Writer paragraph styles.
129 void ExportStyleListlevel(const uno::Reference
<beans::XPropertySetInfo
>& xPropSetInfo
,
130 const uno::Reference
<beans::XPropertyState
>& xPropState
,
131 const uno::Reference
<beans::XPropertySet
>& xPropSet
, SvXMLExport
& rExport
)
133 if (!xPropSetInfo
->hasPropertyByName("NumberingLevel"))
135 SAL_WARN("xmloff", "ExportStyleListlevel: no NumberingLevel for a Writer paragraph style");
139 if (xPropState
->getPropertyState("NumberingLevel") != beans::PropertyState_DIRECT_VALUE
)
144 sal_Int16 nNumberingLevel
{};
145 if (!(xPropSet
->getPropertyValue("NumberingLevel") >>= nNumberingLevel
))
150 // The spec is positiveInteger (1-based), but the implementation is 0-based.
151 rExport
.AddAttribute(XML_NAMESPACE_STYLE
, XML_LIST_LEVEL
, OUString::number(++nNumberingLevel
));
155 bool XMLStyleExport::exportStyle(
156 const Reference
< XStyle
>& rStyle
,
157 const OUString
& rXMLFamily
,
158 const rtl::Reference
< SvXMLExportPropertyMapper
>& rPropMapper
,
159 const Reference
< XNameAccess
>& xStyles
,
160 const OUString
* pPrefix
)
162 Reference
< XPropertySet
> xPropSet( rStyle
, UNO_QUERY
);
166 Reference
< XPropertySetInfo
> xPropSetInfo
=
167 xPropSet
->getPropertySetInfo();
170 // Don't export styles that aren't existing really. This may be the
171 // case for StarOffice Writer's pool styles.
172 if( xPropSetInfo
->hasPropertyByName( gsIsPhysical
) )
174 aAny
= xPropSet
->getPropertyValue( gsIsPhysical
);
175 if( !*o3tl::doAccess
<bool>(aAny
) )
180 GetExport().CheckAttrList();
187 sName
+= rStyle
->getName();
189 bool bEncoded
= false;
190 const OUString
sEncodedStyleName(GetExport().EncodeStyleName( sName
, &bEncoded
));
191 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_NAME
, sEncodedStyleName
);
194 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_DISPLAY_NAME
,
197 // style:family="..."
198 if( !rXMLFamily
.isEmpty() )
199 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_FAMILY
, rXMLFamily
);
201 if ( xPropSetInfo
->hasPropertyByName( "Hidden" ) )
203 aAny
= xPropSet
->getPropertyValue( "Hidden" );
204 bool bHidden
= false;
205 if ((aAny
>>= bHidden
) && bHidden
206 && GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED
)
208 GetExport().AddAttribute(XML_NAMESPACE_LO_EXT
, XML_HIDDEN
, "true");
209 GetExport().AddAttribute(XML_NAMESPACE_STYLE
, XML_HIDDEN
, "true"); // FIXME for compatibility
213 // style:parent-style-name="..."
214 OUString
sParentString(rStyle
->getParentStyle());
217 if(!sParentString
.isEmpty())
221 sParent
+= sParentString
;
224 if( !sParent
.isEmpty() )
225 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_PARENT_STYLE_NAME
,
226 GetExport().EncodeStyleName( sParent
) );
228 // style:next-style-name="..." (paragraph styles only)
229 if( xPropSetInfo
->hasPropertyByName( gsFollowStyle
) )
231 aAny
= xPropSet
->getPropertyValue( gsFollowStyle
);
234 if( sName
!= sNextName
)
236 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_NEXT_STYLE_NAME
,
237 GetExport().EncodeStyleName( sNextName
) );
241 // style:linked-style-name="..." (SW paragraph and character styles only)
242 if (xPropSetInfo
->hasPropertyByName("LinkStyle"))
244 aAny
= xPropSet
->getPropertyValue("LinkStyle");
247 if (!sLinkName
.isEmpty()
248 && (GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED
))
250 GetExport().AddAttribute(XML_NAMESPACE_LO_EXT
, XML_LINKED_STYLE_NAME
,
251 GetExport().EncodeStyleName(sLinkName
));
255 // style:auto-update="..." (SW only)
256 if( xPropSetInfo
->hasPropertyByName( gsIsAutoUpdate
) )
258 aAny
= xPropSet
->getPropertyValue( gsIsAutoUpdate
);
259 if( *o3tl::doAccess
<bool>(aAny
) )
260 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_AUTO_UPDATE
,
264 // style:default-outline-level"..."
265 sal_Int32 nOutlineLevel
= 0;
266 if( xPropSetInfo
->hasPropertyByName( gsOutlineLevel
) )
268 Reference
< XPropertyState
> xPropState( xPropSet
, uno::UNO_QUERY
);
269 if( PropertyState_DIRECT_VALUE
== xPropState
->getPropertyState( gsOutlineLevel
) )
271 aAny
= xPropSet
->getPropertyValue( gsOutlineLevel
);
272 aAny
>>= nOutlineLevel
;
273 if( nOutlineLevel
> 0 )
275 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
276 XML_DEFAULT_OUTLINE_LEVEL
,
277 OUString::number(nOutlineLevel
) );
281 /* Empty value for style:default-outline-level does exist
282 since ODF 1.2. Thus, suppress its export for former versions. (#i104889#)
284 if ( ( GetExport().getExportFlags() & SvXMLExportFlags::OASIS
) &&
285 GetExport().getSaneDefaultVersion() >= SvtSaveOptions::ODFSVER_012
)
287 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
288 XML_DEFAULT_OUTLINE_LEVEL
,
295 // style:list-style-name="..." (SW paragraph styles only)
296 if( xPropSetInfo
->hasPropertyByName( gsNumberingStyleName
) )
298 Reference
< XPropertyState
> xPropState( xPropSet
, uno::UNO_QUERY
);
299 if( PropertyState_DIRECT_VALUE
==
300 xPropState
->getPropertyState( gsNumberingStyleName
) )
302 aAny
= xPropSet
->getPropertyValue( gsNumberingStyleName
);
303 if( aAny
.hasValue() )
308 /* A direct set empty list style has to be written. Otherwise,
309 this information is lost and causes an error, if the parent
310 style has a list style set. (#i69523#)
312 if ( sListName
.isEmpty() )
314 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
316 sListName
/* empty string */);
320 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
321 bool bSuppressListStyle( false );
323 if ( !GetExport().writeOutlineStyleAsNormalListStyle() )
325 Reference
< XChapterNumberingSupplier
> xCNSupplier
326 (GetExport().GetModel(), UNO_QUERY
);
328 if (xCNSupplier
.is())
330 Reference
< XIndexReplace
> xNumRule
331 ( xCNSupplier
->getChapterNumberingRules() );
332 assert(xNumRule
.is());
334 Reference
< XPropertySet
> xNumRulePropSet
335 (xNumRule
, UNO_QUERY
);
336 OUString sOutlineName
;
337 xNumRulePropSet
->getPropertyValue("Name")
339 bSuppressListStyle
= sListName
== sOutlineName
;
344 if ( !sListName
.isEmpty() && !bSuppressListStyle
)
346 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
348 GetExport().EncodeStyleName( sListName
) );
350 ExportStyleListlevel(xPropSetInfo
, xPropState
, xPropSet
, GetExport());
355 else if( nOutlineLevel
> 0 )
358 bool bNoInheritedListStyle( true );
360 Reference
<XStyle
> xStyle( xPropState
, UNO_QUERY
);
361 while ( xStyle
.is() )
363 OUString
aParentStyle( xStyle
->getParentStyle() );
364 if ( aParentStyle
.isEmpty() || !xStyles
->hasByName( aParentStyle
) )
370 xPropState
.set( xStyles
->getByName( aParentStyle
), UNO_QUERY
);
371 if ( !xPropState
.is() )
375 if ( xPropState
->getPropertyState( gsNumberingStyleName
) == PropertyState_DIRECT_VALUE
)
377 bNoInheritedListStyle
= false;
382 xStyle
.set( xPropState
, UNO_QUERY
);
386 if ( bNoInheritedListStyle
)
387 GetExport().AddAttribute( XML_NAMESPACE_STYLE
,
393 // style:pool-id="..." is not required any longer since we use
394 // english style names only
395 exportStyleAttributes( rStyle
);
397 // TODO: style:help-file-name="..." and style:help-id="..." can neither
398 // be modified by UI nor by API and that for, have not to be exported
403 SvXMLElementExport
aElem( GetExport(), XML_NAMESPACE_STYLE
, XML_STYLE
,
406 rPropMapper
->SetStyleName( sName
);
408 // <style:properties>
409 ::std::vector
< XMLPropertyState
> aPropStates
=
410 rPropMapper
->Filter(GetExport(), xPropSet
, true);
411 bool const bUseExtensionNamespaceForGraphicProperties(
412 rXMLFamily
!= "drawing-page" &&
413 rXMLFamily
!= "graphic" &&
414 rXMLFamily
!= "presentation" &&
415 rXMLFamily
!= "chart");
416 rPropMapper
->exportXML( GetExport(), aPropStates
,
417 SvXmlExportFlags::IGN_WS
,
418 bUseExtensionNamespaceForGraphicProperties
);
420 rPropMapper
->SetStyleName( OUString() );
422 exportStyleContent( rStyle
);
424 // <script:events>, if they are supported by this style
425 Reference
<XEventsSupplier
> xEventsSupp(rStyle
, UNO_QUERY
);
426 GetExport().GetEventExport().Export(xEventsSupp
);
431 void XMLStyleExport::exportDefaultStyle(
432 const Reference
< XPropertySet
>& xPropSet
,
433 const OUString
& rXMLFamily
,
434 const rtl::Reference
< SvXMLExportPropertyMapper
>& rPropMapper
)
436 // <style:default-style ...>
437 GetExport().CheckAttrList();
440 // style:family="..."
441 if( !rXMLFamily
.isEmpty() )
442 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_FAMILY
,
445 SvXMLElementExport
aElem( GetExport(), XML_NAMESPACE_STYLE
,
448 // <style:properties>
449 ::std::vector
< XMLPropertyState
> aPropStates
=
450 rPropMapper
->FilterDefaults(GetExport(), xPropSet
);
451 rPropMapper
->exportXML( GetExport(), aPropStates
,
452 SvXmlExportFlags::IGN_WS
);
456 void XMLStyleExport::exportStyleFamily(
457 const OUString
& rFamily
, const OUString
& rXMLFamily
,
458 const rtl::Reference
< SvXMLExportPropertyMapper
>& rPropMapper
,
459 bool bUsed
, XmlStyleFamily nFamily
, const OUString
* pPrefix
)
461 assert(GetExport().GetModel().is());
462 Reference
< XStyleFamiliesSupplier
> xFamiliesSupp( GetExport().GetModel(), UNO_QUERY
);
463 if( !xFamiliesSupp
.is() )
464 return; // family not available in current model
466 Reference
< XNameAccess
> xStyleCont
;
468 Reference
< XNameAccess
> xFamilies( xFamiliesSupp
->getStyleFamilies() );
469 if( xFamilies
->hasByName( rFamily
) )
470 xFamilies
->getByName( rFamily
) >>= xStyleCont
;
472 if( !xStyleCont
.is() )
475 // If next styles are supported and used styles should be exported only,
476 // the next style may be unused but has to be exported, too. In this case
477 // the names of all exported styles are remembered.
478 std::optional
<std::set
<OUString
> > xExportedStyles
;
479 bool bFirstStyle
= true;
481 const uno::Sequence
< OUString
> aSeq
= xStyleCont
->getElementNames();
482 for(const auto& rName
: aSeq
)
484 Reference
< XStyle
> xStyle
;
487 xStyleCont
->getByName( rName
) >>= xStyle
;
489 catch(const lang::IndexOutOfBoundsException
&)
491 // due to bugs in prior versions it is possible that
492 // a binary file is missing some critical styles.
493 // The only possible way to deal with this is to
494 // not export them here and remain silent.
497 catch(css::container::NoSuchElementException
&)
503 if (!bUsed
|| xStyle
->isInUse())
505 bool bExported
= exportStyle( xStyle
, rXMLFamily
, rPropMapper
,
506 xStyleCont
,pPrefix
);
507 if (bUsed
&& bFirstStyle
&& bExported
)
509 // If this is the first style, find out whether next styles
511 Reference
< XPropertySet
> xPropSet( xStyle
, UNO_QUERY
);
512 Reference
< XPropertySetInfo
> xPropSetInfo
=
513 xPropSet
->getPropertySetInfo();
515 if (xPropSetInfo
->hasPropertyByName( gsFollowStyle
))
516 xExportedStyles
.emplace();
520 if (xExportedStyles
&& bExported
)
522 // If next styles are supported, remember this style's name.
523 xExportedStyles
->insert( xStyle
->getName() );
527 // if an auto style pool is given, remember this style's name as a
528 // style name that must not be used by automatic styles.
529 if (m_pAutoStylePool
)
530 m_pAutoStylePool
->RegisterName( nFamily
, xStyle
->getName() );
533 if( !xExportedStyles
)
536 // if next styles are supported, export all next styles that are
537 // unused and that for, haven't been exported in the first loop.
538 for(const auto& rName
: aSeq
)
540 Reference
< XStyle
> xStyle
;
541 xStyleCont
->getByName( rName
) >>= xStyle
;
545 Reference
< XPropertySet
> xPropSet( xStyle
, UNO_QUERY
);
546 Reference
< XPropertySetInfo
> xPropSetInfo( xPropSet
->getPropertySetInfo() );
548 // styles that aren't existing really are ignored.
549 if (xPropSetInfo
->hasPropertyByName( gsIsPhysical
))
551 Any
aAny( xPropSet
->getPropertyValue( gsIsPhysical
) );
552 if (!*o3tl::doAccess
<bool>(aAny
))
556 if (!xStyle
->isInUse())
559 if (!xPropSetInfo
->hasPropertyByName( gsFollowStyle
))
565 xPropSet
->getPropertyValue( gsFollowStyle
) >>= sNextName
;
566 OUString
sTmp( sNextName
);
567 // if the next style hasn't been exported by now, export it now
568 // and remember its name.
569 if (xStyle
->getName() != sNextName
&&
570 0 == xExportedStyles
->count( sTmp
))
572 xStyleCont
->getByName( sNextName
) >>= xStyle
;
575 if (exportStyle(xStyle
, rXMLFamily
, rPropMapper
, xStyleCont
, pPrefix
))
576 xExportedStyles
->insert( sTmp
);
581 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */