bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / XMLPageExport.cxx
blob7713b664cefa4ef254b7b88ed65650155aa53e04
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 <xmloff/XMLPageExport.hxx>
21 #include <tools/debug.hxx>
22 #include <xmloff/xmlnmspe.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
25 #include <com/sun/star/style/XStyle.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/container/XIndexReplace.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <xmloff/families.hxx>
31 #include <xmloff/xmlexp.hxx>
32 #include "PageMasterPropHdlFactory.hxx"
33 #include <xmloff/PageMasterStyleMap.hxx>
34 #include "PageMasterPropMapper.hxx"
35 #include "PageMasterExportPropMapper.hxx"
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::style;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::beans;
42 using namespace ::xmloff::token;
44 bool XMLPageExport::findPageMasterName( const OUString& rStyleName, OUString& rPMName ) const
46 for( ::std::vector< XMLPageExportNameEntry >::const_iterator pEntry = aNameVector.begin();
47 pEntry != aNameVector.end(); ++pEntry )
49 if( pEntry->sStyleName == rStyleName )
51 rPMName = pEntry->sPageMasterName;
52 return true;
55 return false;
58 void XMLPageExport::collectPageMasterAutoStyle(
59 const Reference < XPropertySet > & rPropSet,
60 OUString& rPageMasterName )
62 DBG_ASSERT( xPageMasterPropSetMapper.is(), "page master family/XMLPageMasterPropSetMapper not found" );
63 if( xPageMasterPropSetMapper.is() )
65 ::std::vector<XMLPropertyState> xPropStates = xPageMasterExportPropMapper->Filter( rPropSet );
66 if( !xPropStates.empty())
68 OUString sParent;
69 rPageMasterName = rExport.GetAutoStylePool()->Find( XML_STYLE_FAMILY_PAGE_MASTER, sParent, xPropStates );
70 if (rPageMasterName.isEmpty())
71 rPageMasterName = rExport.GetAutoStylePool()->Add(XML_STYLE_FAMILY_PAGE_MASTER, sParent, xPropStates);
76 void XMLPageExport::exportMasterPageContent(
77 const Reference < XPropertySet > &,
78 bool /*bAutoStyles*/ )
83 bool XMLPageExport::exportStyle(
84 const Reference< XStyle >& rStyle,
85 bool bAutoStyles )
87 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
88 Reference< XPropertySetInfo > xPropSetInfo = xPropSet->getPropertySetInfo();
90 // Don't export styles that aren't existing really. This may be the
91 // case for StarOffice Writer's pool styles.
92 if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
94 Any aAny = xPropSet->getPropertyValue( sIsPhysical );
95 if( !*static_cast<sal_Bool const *>(aAny.getValue()) )
96 return false;
99 if( bAutoStyles )
101 XMLPageExportNameEntry aEntry;
102 collectPageMasterAutoStyle( xPropSet, aEntry.sPageMasterName );
103 aEntry.sStyleName = rStyle->getName();
104 aNameVector.push_back( aEntry );
106 exportMasterPageContent( xPropSet, true );
108 else
110 OUString sName( rStyle->getName() );
111 bool bEncoded = false;
112 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME,
113 GetExport().EncodeStyleName( sName, &bEncoded ) );
115 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
117 uno::Any aValue = xPropSet->getPropertyValue( "Hidden" );
118 bool bHidden = false;
119 if ( ( aValue >>= bHidden ) && bHidden && GetExport( ).getDefaultVersion( ) == SvtSaveOptions::ODFVER_LATEST )
120 GetExport( ).AddAttribute( XML_NAMESPACE_STYLE, XML_HIDDEN, "true" );
123 if( bEncoded )
124 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
125 sName);
127 OUString sPMName;
128 if( findPageMasterName( sName, sPMName ) )
129 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PAGE_LAYOUT_NAME, GetExport().EncodeStyleName( sPMName ) );
131 Reference<XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
132 if ( xInfo.is() && xInfo->hasPropertyByName(sFollowStyle) )
134 OUString sNextName;
135 xPropSet->getPropertyValue( sFollowStyle ) >>= sNextName;
137 if( sName != sNextName && !sNextName.isEmpty() )
139 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
140 GetExport().EncodeStyleName( sNextName ) );
144 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
145 XML_MASTER_PAGE, true, true );
147 exportMasterPageContent( xPropSet, false );
150 return true;
153 XMLPageExport::XMLPageExport( SvXMLExport& rExp ) :
154 rExport( rExp ),
155 sIsPhysical( "IsPhysical" ),
156 sFollowStyle( "FollowStyle" )
158 xPageMasterPropHdlFactory = new XMLPageMasterPropHdlFactory;
159 xPageMasterPropSetMapper = new XMLPageMasterPropSetMapper(
160 aXMLPageMasterStyleMap,
161 xPageMasterPropHdlFactory, true );
162 xPageMasterExportPropMapper = new XMLPageMasterExportPropMapper(
163 xPageMasterPropSetMapper, rExp);
165 rExport.GetAutoStylePool()->AddFamily( XML_STYLE_FAMILY_PAGE_MASTER, OUString( XML_STYLE_FAMILY_PAGE_MASTER_NAME ),
166 xPageMasterExportPropMapper, OUString( XML_STYLE_FAMILY_PAGE_MASTER_PREFIX ), false );
168 Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(),
169 UNO_QUERY );
170 DBG_ASSERT( xFamiliesSupp.is(),
171 "No XStyleFamiliesSupplier from XModel for export!" );
172 if( xFamiliesSupp.is() )
174 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
175 DBG_ASSERT( xFamiliesSupp.is(),
176 "getStyleFamilies() from XModel failed for export!" );
177 if( xFamilies.is() )
179 const OUString aPageStyleName("PageStyles");
181 if( xFamilies->hasByName( aPageStyleName ) )
183 xPageStyles.set(xFamilies->getByName( aPageStyleName ),uno::UNO_QUERY);
185 DBG_ASSERT( xPageStyles.is(),
186 "Page Styles not found for export!" );
192 XMLPageExport::~XMLPageExport()
196 void XMLPageExport::exportStyles( bool bUsed, bool bAutoStyles )
198 if( xPageStyles.is() )
200 uno::Sequence< OUString> aSeq = xPageStyles->getElementNames();
201 const OUString* pIter = aSeq.getConstArray();
202 const OUString* pEnd = pIter + aSeq.getLength();
203 for(;pIter != pEnd;++pIter)
205 Reference< XStyle > xStyle(xPageStyles->getByName( *pIter ),uno::UNO_QUERY);
206 if( !bUsed || xStyle->isInUse() )
207 exportStyle( xStyle, bAutoStyles );
212 void XMLPageExport::exportAutoStyles()
214 rExport.GetAutoStylePool()->exportXML(XML_STYLE_FAMILY_PAGE_MASTER
215 , rExport.GetDocHandler(), rExport.GetMM100UnitConverter(),
216 rExport.GetNamespaceMap()
220 void XMLPageExport::exportDefaultStyle()
222 Reference < lang::XMultiServiceFactory > xFactory (GetExport().GetModel(), UNO_QUERY);
223 if (xFactory.is())
225 OUString sTextDefaults ( "com.sun.star.text.Defaults" );
226 Reference < XPropertySet > xPropSet (xFactory->createInstance ( sTextDefaults ), UNO_QUERY);
227 if (xPropSet.is())
229 // <style:default-style ...>
230 GetExport().CheckAttrList();
232 ::std::vector< XMLPropertyState > xPropStates =
233 xPageMasterExportPropMapper->FilterDefaults( xPropSet );
235 bool bExport = false;
236 rtl::Reference < XMLPropertySetMapper > aPropMapper(xPageMasterExportPropMapper->getPropertySetMapper());
237 for( ::std::vector< XMLPropertyState >::iterator aIter = xPropStates.begin(); aIter != xPropStates.end(); ++aIter )
239 XMLPropertyState *pProp = &(*aIter);
240 sal_Int16 nContextId = aPropMapper->GetEntryContextId( pProp->mnIndex );
241 if( nContextId == CTF_PM_STANDARD_MODE )
243 bExport = true;
244 break;
248 if( bExport )
250 assert(GetExport().getDefaultVersion()
251 >= SvtSaveOptions::ODFVER_012);
253 //<style:default-page-layout>
254 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
255 XML_DEFAULT_PAGE_LAYOUT,
256 true, true );
258 xPageMasterExportPropMapper->exportXML( GetExport(), xPropStates,
259 SvXmlExportFlags::IGN_WS );
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */