bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / styleexp.cxx
blob8ca89b6f65078f09647b57e961fd14b8eec65203
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 <tools/debug.hxx>
21 #include <xmloff/nmspmap.hxx>
22 #include <xmloff/xmlnmspe.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <xmloff/xmluconv.hxx>
25 #include <xmloff/attrlist.hxx>
26 #include <xmloff/xmlprmap.hxx>
27 #include <xmloff/xmlexppr.hxx>
28 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
31 #include <com/sun/star/style/XStyle.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/document/XEventsSupplier.hpp>
36 #include <com/sun/star/text/XChapterNumberingSupplier.hpp>
37 #include <xmloff/xmlaustp.hxx>
38 #include <xmloff/styleexp.hxx>
39 #include <xmloff/xmlexp.hxx>
40 #include <xmloff/XMLEventExport.hxx>
41 #include <xmloff/maptype.hxx>
42 #include <set>
43 #include <boost/scoped_ptr.hpp>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::style;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::text;
51 using namespace ::xmloff::token;
53 using ::com::sun::star::document::XEventsSupplier;
55 XMLStyleExport::XMLStyleExport(
56 SvXMLExport& rExp,
57 const OUString& rPoolStyleName,
58 SvXMLAutoStylePoolP *pAutoStyleP ) :
59 rExport( rExp ),
60 sIsPhysical( "IsPhysical" ),
61 sIsAutoUpdate( "IsAutoUpdate" ),
62 sFollowStyle( "FollowStyle" ),
63 sNumberingStyleName( "NumberingStyleName" ),
64 sOutlineLevel( "OutlineLevel" ),
65 sPoolStyleName( rPoolStyleName ),
66 pAutoStylePool( pAutoStyleP )
70 XMLStyleExport::~XMLStyleExport()
74 void XMLStyleExport::exportStyleAttributes( const Reference< XStyle >& )
78 void XMLStyleExport::exportStyleContent( const Reference< XStyle >& )
82 bool XMLStyleExport::exportStyle(
83 const Reference< XStyle >& rStyle,
84 const OUString& rXMLFamily,
85 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
86 const Reference< XNameAccess >& xStyles,
87 const OUString* pPrefix )
89 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
90 Reference< XPropertySetInfo > xPropSetInfo =
91 xPropSet->getPropertySetInfo();
92 Any aAny;
94 // Don't export styles that aren't existing really. This may be the
95 // case for StarOffice Writer's pool styles.
96 if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
98 aAny = xPropSet->getPropertyValue( sIsPhysical );
99 if( !*static_cast<sal_Bool const *>(aAny.getValue()) )
100 return false;
103 // <style:style ...>
104 GetExport().CheckAttrList();
106 // style:name="..."
107 OUString sName;
109 if(pPrefix)
110 sName = *pPrefix;
111 sName += rStyle->getName();
113 bool bEncoded = false;
114 const OUString sEncodedStyleName(GetExport().EncodeStyleName( sName, &bEncoded ));
115 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, sEncodedStyleName );
117 if( bEncoded )
118 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
119 sName);
121 // style:family="..."
122 if( !rXMLFamily.isEmpty() )
123 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY, rXMLFamily);
125 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
127 aAny = xPropSet->getPropertyValue( "Hidden" );
128 bool bHidden = false;
129 if ( ( aAny >>= bHidden ) && bHidden && GetExport( ).getDefaultVersion( ) == SvtSaveOptions::ODFVER_LATEST )
130 GetExport( ).AddAttribute( XML_NAMESPACE_STYLE, XML_HIDDEN, "true" );
133 // style:parent-style-name="..."
134 OUString sParentString(rStyle->getParentStyle());
135 OUString sParent;
137 if(!sParentString.isEmpty())
139 if(pPrefix)
140 sParent = *pPrefix;
141 sParent += sParentString;
143 else
144 sParent = sPoolStyleName;
146 if( !sParent.isEmpty() )
147 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PARENT_STYLE_NAME,
148 GetExport().EncodeStyleName( sParent ) );
150 // style:next-style-name="..." (paragraph styles only)
151 if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
153 aAny = xPropSet->getPropertyValue( sFollowStyle );
154 OUString sNextName;
155 aAny >>= sNextName;
156 if( sName != sNextName )
158 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
159 GetExport().EncodeStyleName( sNextName ) );
163 // style:auto-update="..." (SW only)
164 if( xPropSetInfo->hasPropertyByName( sIsAutoUpdate ) )
166 aAny = xPropSet->getPropertyValue( sIsAutoUpdate );
167 if( *static_cast<sal_Bool const *>(aAny.getValue()) )
168 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_AUTO_UPDATE,
169 XML_TRUE );
172 // style:default-outline-level"..."
173 sal_Int32 nOutlineLevel = 0;
174 if( xPropSetInfo->hasPropertyByName( sOutlineLevel ) )
176 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
177 if( PropertyState_DIRECT_VALUE == xPropState->getPropertyState( sOutlineLevel ) )
179 aAny = xPropSet->getPropertyValue( sOutlineLevel );
180 aAny >>= nOutlineLevel;
181 if( nOutlineLevel > 0 )
183 OUStringBuffer sTmp;
184 sTmp.append( static_cast<sal_Int32>(nOutlineLevel));
185 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
186 XML_DEFAULT_OUTLINE_LEVEL,
187 sTmp.makeStringAndClear() );
189 else
191 /* Empty value for style:default-outline-level does exist
192 since ODF 1.2. Thus, suppress its export for former versions. (#i104889#)
194 if ( ( GetExport().getExportFlags() & SvXMLExportFlags::OASIS ) &&
195 GetExport().getDefaultVersion() >= SvtSaveOptions::ODFVER_012 )
197 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
198 XML_DEFAULT_OUTLINE_LEVEL,
199 OUString( "" ));
205 // style:list-style-name="..." (SW paragarph styles only)
206 if( xPropSetInfo->hasPropertyByName( sNumberingStyleName ) )
208 Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
209 if( PropertyState_DIRECT_VALUE ==
210 xPropState->getPropertyState( sNumberingStyleName ) )
212 aAny = xPropSet->getPropertyValue( sNumberingStyleName );
213 if( aAny.hasValue() )
215 OUString sListName;
216 aAny >>= sListName;
218 /* An direct set empty list style has to be written. Otherwise,
219 this information is lost and causes an error, if the parent
220 style has a list style set. (#i69523#)
222 if ( sListName.isEmpty() )
224 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
225 XML_LIST_STYLE_NAME,
226 sListName /* empty string */);
228 else
230 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
231 bool bSuppressListStyle( false );
233 if ( !GetExport().writeOutlineStyleAsNormalListStyle() )
235 Reference< XChapterNumberingSupplier > xCNSupplier
236 (GetExport().GetModel(), UNO_QUERY);
238 OUString sOutlineName;
239 if (xCNSupplier.is())
241 Reference< XIndexReplace > xNumRule
242 ( xCNSupplier->getChapterNumberingRules() );
243 assert(xNumRule.is());
245 Reference< XPropertySet > xNumRulePropSet
246 (xNumRule, UNO_QUERY);
247 xNumRulePropSet->getPropertyValue("Name")
248 >>= sOutlineName;
249 bSuppressListStyle = sListName == sOutlineName;
254 if ( !sListName.isEmpty() && !bSuppressListStyle )
256 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
257 XML_LIST_STYLE_NAME,
258 GetExport().EncodeStyleName( sListName ) );
263 else if( nOutlineLevel > 0 )
266 bool bNoInheritedListStyle( true );
268 Reference<XStyle> xStyle( xPropState, UNO_QUERY );
269 while ( xStyle.is() )
271 OUString aParentStyle( xStyle->getParentStyle() );
272 if ( aParentStyle.isEmpty() || !xStyles->hasByName( aParentStyle ) )
274 break;
276 else
278 xPropState = Reference< XPropertyState >( xStyles->getByName( aParentStyle ), UNO_QUERY );
279 if ( !xPropState.is() )
281 break;
283 if ( xPropState->getPropertyState( sNumberingStyleName ) == PropertyState_DIRECT_VALUE )
285 bNoInheritedListStyle = false;
286 break;
288 else
290 xStyle = Reference<XStyle>( xPropState, UNO_QUERY );
294 if ( bNoInheritedListStyle )
295 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
296 XML_LIST_STYLE_NAME,
297 OUString( "" ));
301 // style:pool-id="..." is not required any longer since we use
302 // english style names only
303 exportStyleAttributes( rStyle );
305 // TODO: style:help-file-name="..." and style:help-id="..." can neither
306 // be modified by UI nor by API and that for, have not to be exported
307 // currently.
310 // <style:style>
311 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_STYLE,
312 true, true );
314 rPropMapper->SetStyleName( sName );
316 // <style:properties>
317 ::std::vector< XMLPropertyState > xPropStates =
318 rPropMapper->Filter( xPropSet, true );
319 bool const bUseExtensionNamespaceForGraphicProperties(
320 rXMLFamily != "drawing-page" &&
321 rXMLFamily != "graphic" &&
322 rXMLFamily != "presentation" &&
323 rXMLFamily != "chart");
324 rPropMapper->exportXML( GetExport(), xPropStates,
325 SvXmlExportFlags::IGN_WS,
326 bUseExtensionNamespaceForGraphicProperties );
328 rPropMapper->SetStyleName( OUString() );
330 exportStyleContent( rStyle );
332 // <script:events>, if they are supported by this style
333 Reference<XEventsSupplier> xEventsSupp(rStyle, UNO_QUERY);
334 GetExport().GetEventExport().Export(xEventsSupp);
336 return true;
339 bool XMLStyleExport::exportDefaultStyle(
340 const Reference< XPropertySet >& xPropSet,
341 const OUString& rXMLFamily,
342 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper )
344 Reference< XPropertySetInfo > xPropSetInfo =
345 xPropSet->getPropertySetInfo();
347 // <style:default-style ...>
348 GetExport().CheckAttrList();
351 // style:family="..."
352 if( !rXMLFamily.isEmpty() )
353 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY,
354 rXMLFamily );
355 // <style:style>
356 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
357 XML_DEFAULT_STYLE,
358 true, true );
359 // <style:properties>
360 ::std::vector< XMLPropertyState > xPropStates =
361 rPropMapper->FilterDefaults( xPropSet );
362 rPropMapper->exportXML( GetExport(), xPropStates,
363 SvXmlExportFlags::IGN_WS );
365 return true;
368 void XMLStyleExport::exportStyleFamily(
369 const sal_Char *pFamily,
370 const OUString& rXMLFamily,
371 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
372 bool bUsed, sal_uInt16 nFamily, const OUString* pPrefix)
374 const OUString sFamily(OUString::createFromAscii(pFamily ));
375 exportStyleFamily( sFamily, rXMLFamily, rPropMapper, bUsed, nFamily,
376 pPrefix);
379 void XMLStyleExport::exportStyleFamily(
380 const OUString& rFamily, const OUString& rXMLFamily,
381 const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
382 bool bUsed, sal_uInt16 nFamily, const OUString* pPrefix)
384 assert(GetExport().GetModel().is());
385 Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(), UNO_QUERY );
386 if( !xFamiliesSupp.is() )
387 return; // family not available in current model
389 Reference< XNameAccess > xStyleCont;
391 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
392 if( xFamilies->hasByName( rFamily ) )
393 xFamilies->getByName( rFamily ) >>= xStyleCont;
395 if( !xStyleCont.is() )
396 return;
398 // If next styles are supported and used styles should be exported only,
399 // the next style may be unused but has to be exported, too. In this case
400 // the names of all exported styles are remembered.
401 boost::scoped_ptr<std::set<OUString> > pExportedStyles(0);
402 bool bFirstStyle = true;
404 const uno::Sequence< OUString> aSeq = xStyleCont->getElementNames();
405 const OUString* pIter = aSeq.getConstArray();
406 const OUString* pEnd = pIter + aSeq.getLength();
407 for(;pIter != pEnd;++pIter)
409 Reference< XStyle > xStyle;
412 xStyleCont->getByName( *pIter ) >>= xStyle;
414 catch(const lang::IndexOutOfBoundsException&)
416 // due to bugs in prior versions it is possible that
417 // a binary file is missing some critical styles.
418 // The only possible way to deal with this is to
419 // not export them here and remain silent.
420 continue;
422 catch(css::container::NoSuchElementException&)
424 continue;
427 assert(xStyle.is());
428 if (!bUsed || xStyle->isInUse())
430 bool bExported = exportStyle( xStyle, rXMLFamily, rPropMapper,
431 xStyleCont,pPrefix );
432 if (bUsed && bFirstStyle && bExported)
434 // If this is the first style, find out whether next styles
435 // are supported.
436 Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
437 Reference< XPropertySetInfo > xPropSetInfo =
438 xPropSet->getPropertySetInfo();
440 if (xPropSetInfo->hasPropertyByName( sFollowStyle ))
441 pExportedStyles.reset(new std::set<OUString>());
442 bFirstStyle = false;
445 if (pExportedStyles && bExported)
447 // If next styles are supported, remember this style's name.
448 pExportedStyles->insert( xStyle->getName() );
452 // if an auto style pool is given, remember this style's name as a
453 // style name that must not be used by automatic styles.
454 if (pAutoStylePool)
455 pAutoStylePool->RegisterName( nFamily, xStyle->getName() );
458 if( pExportedStyles )
460 // if next styles are supported, export all next styles that are
461 // unused and that for, haven't been exported in the first loop.
462 pIter = aSeq.getConstArray();
463 for(;pIter != pEnd;++pIter)
465 Reference< XStyle > xStyle;
466 xStyleCont->getByName( *pIter ) >>= xStyle;
468 assert(xStyle.is());
470 Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
471 Reference< XPropertySetInfo > xPropSetInfo( xPropSet->getPropertySetInfo() );
473 // styles that aren't existing really are ignored.
474 if (xPropSetInfo->hasPropertyByName( sIsPhysical ))
476 Any aAny( xPropSet->getPropertyValue( sIsPhysical ) );
477 if (!*static_cast<sal_Bool const *>(aAny.getValue()))
478 continue;
481 if (!xStyle->isInUse())
482 continue;
484 if (!xPropSetInfo->hasPropertyByName( sFollowStyle ))
486 continue;
489 OUString sNextName;
490 xPropSet->getPropertyValue( sFollowStyle ) >>= sNextName;
491 OUString sTmp( sNextName );
492 // if the next style hasn't been exported by now, export it now
493 // and remember its name.
494 if (xStyle->getName() != sNextName &&
495 0 == pExportedStyles->count( sTmp ))
497 xStyleCont->getByName( sNextName ) >>= xStyle;
498 assert(xStyle.is());
500 if (exportStyle(xStyle, rXMLFamily, rPropMapper, xStyleCont, pPrefix))
501 pExportedStyles->insert( sTmp );
507 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */