LibreOffice Module xmloff (master) 1
styleexp.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <o3tl/any.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>
40#include <xmloff/maptype.hxx>
41#include <memory>
42#include <set>
43#include <prstylecond.hxx>
44#include <sal/log.hxx>
45
46using namespace ::com::sun::star;
47using namespace ::com::sun::star::uno;
48using namespace ::com::sun::star::style;
49using namespace ::com::sun::star::container;
50using namespace ::com::sun::star::beans;
51using namespace ::com::sun::star::text;
52using namespace ::xmloff::token;
53
54using ::com::sun::star::document::XEventsSupplier;
55
56constexpr OUStringLiteral gsIsPhysical( u"IsPhysical" );
57constexpr OUStringLiteral gsIsAutoUpdate( u"IsAutoUpdate" );
58constexpr OUStringLiteral gsFollowStyle( u"FollowStyle" );
59constexpr OUStringLiteral gsNumberingStyleName( u"NumberingStyleName" );
60constexpr OUStringLiteral gsOutlineLevel( u"OutlineLevel" );
61
63 SvXMLExport& rExp,
64 SvXMLAutoStylePoolP *pAutoStyleP ) :
65 m_rExport( rExp ),
66 m_pAutoStylePool( pAutoStyleP )
67{
68}
69
71{
72}
73
74void XMLStyleExport::exportStyleAttributes( const Reference< XStyle >& )
76}
77
78void XMLStyleExport::exportStyleContent( const Reference< XStyle >& rStyle )
79{
80 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
81 assert(xPropSet.is());
82
83 try
84 {
85 uno::Any aProperty = xPropSet->getPropertyValue( "ParaStyleConditions" );
86 uno::Sequence< beans::NamedValue > aSeq;
87
88 aProperty >>= aSeq;
89
90 for (beans::NamedValue const& rNamedCond : std::as_const(aSeq))
91 {
92 OUString aStyleName;
93
94 if (rNamedCond.Value >>= aStyleName)
95 {
96 if (!aStyleName.isEmpty())
97 {
98 OUString aExternal = GetParaStyleCondExternal(rNamedCond.Name);
99
100 if (!aExternal.isEmpty())
101 {
102 bool bEncoded;
103
106 aExternal);
109 GetExport().EncodeStyleName( aStyleName,
110 &bEncoded ) );
113 XML_MAP,
114 true,
115 true );
116 }
117 }
118 }
119 }
120 }
121 catch( const beans::UnknownPropertyException& )
122 {
123 }
124}
125
126namespace
127{
129void ExportStyleListlevel(const uno::Reference<beans::XPropertySetInfo>& xPropSetInfo,
130 const uno::Reference<beans::XPropertyState>& xPropState,
131 const uno::Reference<beans::XPropertySet>& xPropSet, SvXMLExport& rExport)
132{
133 if (!xPropSetInfo->hasPropertyByName("NumberingLevel"))
134 {
135 SAL_WARN("xmloff", "ExportStyleListlevel: no NumberingLevel for a Writer paragraph style");
136 return;
137 }
138
139 if (xPropState->getPropertyState("NumberingLevel") != beans::PropertyState_DIRECT_VALUE)
140 {
141 return;
142 }
143
144 sal_Int16 nNumberingLevel{};
145 if (!(xPropSet->getPropertyValue("NumberingLevel") >>= nNumberingLevel))
146 {
147 return;
148 }
149
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));
152}
153}
154
156 const Reference< XStyle >& rStyle,
157 const OUString& rXMLFamily,
159 const Reference< XNameAccess >& xStyles,
160 const OUString* pPrefix )
161{
162 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
163 if (!xPropSet)
164 return false;
165
166 Reference< XPropertySetInfo > xPropSetInfo =
167 xPropSet->getPropertySetInfo();
168 Any aAny;
169
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 ) )
173 {
174 aAny = xPropSet->getPropertyValue( gsIsPhysical );
175 if( !*o3tl::doAccess<bool>(aAny) )
176 return false;
177 }
178
179 // <style:style ...>
181
182 // style:name="..."
183 OUString sName;
184
185 if(pPrefix)
186 sName = *pPrefix;
187 sName += rStyle->getName();
188
189 bool bEncoded = false;
190 const OUString sEncodedStyleName(GetExport().EncodeStyleName( sName, &bEncoded ));
191 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, sEncodedStyleName );
192
193 if( bEncoded )
195 sName);
196
197 // style:family="..."
198 if( !rXMLFamily.isEmpty() )
200
201 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
202 {
203 aAny = xPropSet->getPropertyValue( "Hidden" );
204 bool bHidden = false;
205 if ((aAny >>= bHidden) && bHidden
206 && GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED)
207 {
209 GetExport().AddAttribute(XML_NAMESPACE_STYLE, XML_HIDDEN, "true"); // FIXME for compatibility
210 }
211 }
212
213 // style:parent-style-name="..."
214 OUString sParentString(rStyle->getParentStyle());
215 OUString sParent;
216
217 if(!sParentString.isEmpty())
218 {
219 if(pPrefix)
220 sParent = *pPrefix;
221 sParent += sParentString;
222 }
223
224 if( !sParent.isEmpty() )
226 GetExport().EncodeStyleName( sParent ) );
227
228 // style:next-style-name="..." (paragraph styles only)
229 if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
230 {
231 aAny = xPropSet->getPropertyValue( gsFollowStyle );
232 OUString sNextName;
233 aAny >>= sNextName;
234 if( sName != sNextName )
235 {
237 GetExport().EncodeStyleName( sNextName ) );
238 }
239 }
240
241 // style:linked-style-name="..." (SW paragraph and character styles only)
242 if (xPropSetInfo->hasPropertyByName("LinkStyle"))
243 {
244 aAny = xPropSet->getPropertyValue("LinkStyle");
245 OUString sLinkName;
246 aAny >>= sLinkName;
247 if (!sLinkName.isEmpty()
248 && (GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED))
249 {
251 GetExport().EncodeStyleName(sLinkName));
252 }
253 }
254
255 // style:auto-update="..." (SW only)
256 if( xPropSetInfo->hasPropertyByName( gsIsAutoUpdate ) )
257 {
258 aAny = xPropSet->getPropertyValue( gsIsAutoUpdate );
259 if( *o3tl::doAccess<bool>(aAny) )
261 XML_TRUE );
262 }
263
264 // style:default-outline-level"..."
265 sal_Int32 nOutlineLevel = 0;
266 if( xPropSetInfo->hasPropertyByName( gsOutlineLevel ) )
267 {
268 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
269 if( PropertyState_DIRECT_VALUE == xPropState->getPropertyState( gsOutlineLevel ) )
270 {
271 aAny = xPropSet->getPropertyValue( gsOutlineLevel );
272 aAny >>= nOutlineLevel;
273 if( nOutlineLevel > 0 )
274 {
277 OUString::number(nOutlineLevel) );
278 }
279 else
280 {
281 /* Empty value for style:default-outline-level does exist
282 since ODF 1.2. Thus, suppress its export for former versions. (#i104889#)
283 */
284 if ( ( GetExport().getExportFlags() & SvXMLExportFlags::OASIS ) &&
285 GetExport().getSaneDefaultVersion() >= SvtSaveOptions::ODFSVER_012)
286 {
289 OUString( "" ));
290 }
291 }
292 }
293 }
294
295 // style:list-style-name="..." (SW paragraph styles only)
296 if( xPropSetInfo->hasPropertyByName( gsNumberingStyleName ) )
297 {
298 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
299 if( PropertyState_DIRECT_VALUE ==
300 xPropState->getPropertyState( gsNumberingStyleName ) )
301 {
302 aAny = xPropSet->getPropertyValue( gsNumberingStyleName );
303 if( aAny.hasValue() )
304 {
305 OUString sListName;
306 aAny >>= sListName;
307
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#)
311 */
312 if ( sListName.isEmpty() )
313 {
316 sListName /* empty string */);
317 }
318 else
319 {
320 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
321 bool bSuppressListStyle( false );
322 {
323 if ( !GetExport().writeOutlineStyleAsNormalListStyle() )
324 {
325 Reference< XChapterNumberingSupplier > xCNSupplier
326 (GetExport().GetModel(), UNO_QUERY);
327
328 if (xCNSupplier.is())
329 {
330 Reference< XIndexReplace > xNumRule
331 ( xCNSupplier->getChapterNumberingRules() );
332 assert(xNumRule.is());
333
334 Reference< XPropertySet > xNumRulePropSet
335 (xNumRule, UNO_QUERY);
336 OUString sOutlineName;
337 xNumRulePropSet->getPropertyValue("Name")
338 >>= sOutlineName;
339 bSuppressListStyle = sListName == sOutlineName;
340 }
341 }
342 }
343
344 if ( !sListName.isEmpty() && !bSuppressListStyle )
345 {
348 GetExport().EncodeStyleName( sListName ) );
349
350 ExportStyleListlevel(xPropSetInfo, xPropState, xPropSet, GetExport());
351 }
352 }
353 }
354 }
355 else if( nOutlineLevel > 0 )
356 {
357
358 bool bNoInheritedListStyle( true );
359
360 Reference<XStyle> xStyle( xPropState, UNO_QUERY );
361 while ( xStyle.is() )
362 {
363 OUString aParentStyle( xStyle->getParentStyle() );
364 if ( aParentStyle.isEmpty() || !xStyles->hasByName( aParentStyle ) )
365 {
366 break;
367 }
368 else
369 {
370 xPropState.set( xStyles->getByName( aParentStyle ), UNO_QUERY );
371 if ( !xPropState.is() )
372 {
373 break;
374 }
375 if ( xPropState->getPropertyState( gsNumberingStyleName ) == PropertyState_DIRECT_VALUE )
376 {
377 bNoInheritedListStyle = false;
378 break;
379 }
380 else
381 {
382 xStyle.set( xPropState, UNO_QUERY );
383 }
384 }
385 }
386 if ( bNoInheritedListStyle )
389 OUString( "" ));
390 }
391 }
392
393 // style:pool-id="..." is not required any longer since we use
394 // english style names only
395 exportStyleAttributes( rStyle );
396
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
399 // currently.
400
401 {
402 // <style:style>
404 true, true );
405
406 rPropMapper->SetStyleName( sName );
407
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,
418 bUseExtensionNamespaceForGraphicProperties );
419
420 rPropMapper->SetStyleName( OUString() );
421
422 exportStyleContent( rStyle );
423
424 // <script:events>, if they are supported by this style
425 Reference<XEventsSupplier> xEventsSupp(rStyle, UNO_QUERY);
426 GetExport().GetEventExport().Export(xEventsSupp);
427 }
428 return true;
429}
430
432 const Reference< XPropertySet >& xPropSet,
433 const OUString& rXMLFamily,
435{
436 // <style:default-style ...>
438
439 {
440 // style:family="..."
441 if( !rXMLFamily.isEmpty() )
443 rXMLFamily );
444 // <style:style>
447 true, true );
448 // <style:properties>
449 ::std::vector< XMLPropertyState > aPropStates =
450 rPropMapper->FilterDefaults(GetExport(), xPropSet);
451 rPropMapper->exportXML( GetExport(), aPropStates,
453 }
454}
455
457 const OUString& rFamily, const OUString& rXMLFamily,
459 bool bUsed, XmlStyleFamily nFamily, const OUString* pPrefix)
460{
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
465
466 Reference< XNameAccess > xStyleCont;
467
468 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
469 if( xFamilies->hasByName( rFamily ) )
470 xFamilies->getByName( rFamily ) >>= xStyleCont;
471
472 if( !xStyleCont.is() )
473 return;
474
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;
480
481 const uno::Sequence< OUString> aSeq = xStyleCont->getElementNames();
482 for(const auto& rName : aSeq)
483 {
484 Reference< XStyle > xStyle;
485 try
486 {
487 xStyleCont->getByName( rName ) >>= xStyle;
488 }
489 catch(const lang::IndexOutOfBoundsException&)
490 {
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.
495 continue;
496 }
497 catch(css::container::NoSuchElementException&)
498 {
499 continue;
500 }
501
502 assert(xStyle.is());
503 if (!bUsed || xStyle->isInUse())
504 {
505 bool bExported = exportStyle( xStyle, rXMLFamily, rPropMapper,
506 xStyleCont,pPrefix );
507 if (bUsed && bFirstStyle && bExported)
508 {
509 // If this is the first style, find out whether next styles
510 // are supported.
511 Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
512 Reference< XPropertySetInfo > xPropSetInfo =
513 xPropSet->getPropertySetInfo();
514
515 if (xPropSetInfo->hasPropertyByName( gsFollowStyle ))
516 xExportedStyles.emplace();
517 bFirstStyle = false;
518 }
519
520 if (xExportedStyles && bExported)
521 {
522 // If next styles are supported, remember this style's name.
523 xExportedStyles->insert( xStyle->getName() );
524 }
525 }
526
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.
530 m_pAutoStylePool->RegisterName( nFamily, xStyle->getName() );
531 }
532
533 if( !xExportedStyles )
534 return;
535
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)
539 {
540 Reference< XStyle > xStyle;
541 xStyleCont->getByName( rName ) >>= xStyle;
542
543 assert(xStyle.is());
544
545 Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
546 Reference< XPropertySetInfo > xPropSetInfo( xPropSet->getPropertySetInfo() );
547
548 // styles that aren't existing really are ignored.
549 if (xPropSetInfo->hasPropertyByName( gsIsPhysical ))
550 {
551 Any aAny( xPropSet->getPropertyValue( gsIsPhysical ) );
552 if (!*o3tl::doAccess<bool>(aAny))
553 continue;
554 }
555
556 if (!xStyle->isInUse())
557 continue;
558
559 if (!xPropSetInfo->hasPropertyByName( gsFollowStyle ))
560 {
561 continue;
562 }
563
564 OUString sNextName;
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 ))
571 {
572 xStyleCont->getByName( sNextName ) >>= xStyle;
573 assert(xStyle.is());
574
575 if (exportStyle(xStyle, rXMLFamily, rPropMapper, xStyleCont, pPrefix))
576 xExportedStyles->insert( sTmp );
577 }
578 }
579}
580
581/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void RegisterName(XmlStyleFamily nFamily, const OUString &rName)
Register a name that must not be used as a generated name.
Definition: xmlaustp.cxx:298
XMLEventExport & GetEventExport()
get Event export, with handlers for script types "None" and "StarBasic" already registered; other han...
Definition: xmlexp.cxx:1989
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
Definition: xmlexp.cxx:907
void CheckAttrList()
Definition: xmlexp.cxx:1012
void Export(css::uno::Reference< css::document::XEventsSupplier > const &xAccess, bool bUseWhitespace=true)
export the events (calls EventExport::Export(Reference<XNameAccess>) )
bool exportStyle(const css::uno::Reference< css::style::XStyle > &rStyle, const OUString &rXMLFamily, const rtl::Reference< SvXMLExportPropertyMapper > &rPropMapper, const css::uno::Reference< css::container::XNameAccess > &xStyles, const OUString *pPrefix)
Definition: styleexp.cxx:155
virtual void exportStyleContent(const css::uno::Reference< css::style::XStyle > &rStyle)
Definition: styleexp.cxx:78
SvXMLAutoStylePoolP * m_pAutoStylePool
Definition: styleexp.hxx:56
virtual ~XMLStyleExport() override
Definition: styleexp.cxx:70
virtual void exportStyleAttributes(const css::uno::Reference< css::style::XStyle > &rStyle)
Definition: styleexp.cxx:74
void exportStyleFamily(const OUString &rFamily, const OUString &rXMLFamily, const rtl::Reference< SvXMLExportPropertyMapper > &rPropMapper, bool bUsed, XmlStyleFamily nFamily, const OUString *pPrefix=nullptr)
Definition: styleexp.cxx:456
XMLStyleExport(SvXMLExport &rExp, SvXMLAutoStylePoolP *pAutoStyleP=nullptr)
Definition: styleexp.cxx:62
void exportDefaultStyle(const css::uno::Reference< css::beans::XPropertySet > &xPropSet, const OUString &rXMLFamily, const rtl::Reference< SvXMLExportPropertyMapper > &rPropMapper)
Definition: styleexp.cxx:431
SvXMLExport & GetExport()
Definition: styleexp.hxx:59
XmlStyleFamily
Definition: families.hxx:50
OUString sName
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
Handling of tokens in XML:
@ XML_DEFAULT_OUTLINE_LEVEL
Definition: xmltoken.hxx:2791
@ XML_APPLY_STYLE_NAME
Definition: xmltoken.hxx:269
OUString GetParaStyleCondExternal(std::u16string_view internal)
Definition: prstylecond.cxx:71
constexpr OUStringLiteral gsIsPhysical(u"IsPhysical")
constexpr OUStringLiteral gsIsAutoUpdate(u"IsAutoUpdate")
constexpr OUStringLiteral gsNumberingStyleName(u"NumberingStyleName")
constexpr OUStringLiteral gsOutlineLevel(u"OutlineLevel")
constexpr OUStringLiteral gsFollowStyle(u"FollowStyle")
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
constexpr sal_uInt16 XML_NAMESPACE_STYLE