LibreOffice Module xmloff (master) 1
XMLTextMasterPageContext.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 <com/sun/star/frame/XModel.hpp>
23#include <com/sun/star/style/XStyle.hpp>
24#include <com/sun/star/lang/XMultiServiceFactory.hpp>
25#include <com/sun/star/beans/XMultiPropertyStates.hpp>
26#include <com/sun/star/beans/XPropertySet.hpp>
27#include <com/sun/star/container/XNameContainer.hpp>
28#include <o3tl/any.hxx>
29#include <sal/log.hxx>
30#include <osl/diagnose.h>
33#include <xmloff/prstylei.hxx>
34#include <xmloff/xmltoken.hxx>
38#include <xmloff/xmlimp.hxx>
39
40
41using namespace ::com::sun::star;
42using namespace ::com::sun::star::uno;
43using namespace ::com::sun::star::xml::sax;
44using namespace ::com::sun::star::style;
45using namespace ::com::sun::star::text;
46using namespace ::com::sun::star::beans;
47using namespace ::com::sun::star::container;
48using namespace ::com::sun::star::lang;
49using namespace ::xmloff::token;
50
52{
53 Reference < XStyle > xNewStyle;
54
55 Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
56 UNO_QUERY );
57 if( xFactory.is() )
58 {
59 Reference < XInterface > xIfc =
60 xFactory->createInstance("com.sun.star.style.PageStyle");
61 if( xIfc.is() )
62 xNewStyle.set( xIfc, UNO_QUERY );
63 }
64
65 return xNewStyle;
66}
67
68constexpr OUStringLiteral gsFollowStyle( u"FollowStyle" );
69
71 sal_Int32 /*nElement*/,
72 const Reference< XFastAttributeList > & xAttrList,
73 bool bOverwrite )
75, bInsertHeader( false )
76, bInsertFooter( false )
77, bInsertHeaderLeft( false )
78, bInsertFooterLeft( false )
79, bInsertHeaderFirst( false )
80, bInsertFooterFirst( false )
81, bHeaderInserted( false )
82, bFooterInserted( false )
83{
84 OUString sName, sDisplayName;
85 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
86 {
87 const OUString aValue = aIter.toString();
88 switch (aIter.getToken())
89 {
91 sName = aValue;
92 break;
94 sDisplayName = aValue;
95 break;
97 sFollow = aValue;
98 break;
100 sPageMasterName = aValue;
101 break;
103 m_sDrawingPageStyle = aValue;
104 break;
105 default:
106 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
107 }
108 }
109
110 if( !sDisplayName.isEmpty() )
111 {
113 sDisplayName );
114 }
115 else
116 {
118 }
119
120 if( sDisplayName.isEmpty() )
121 return;
122
123 Reference < XNameContainer > xPageStyles =
124 GetImport().GetTextImport()->GetPageStyles();
125 if( !xPageStyles.is() )
126 return;
127
128 Any aAny;
129 bool bNew = false;
130 if( xPageStyles->hasByName( sDisplayName ) )
131 {
132 aAny = xPageStyles->getByName( sDisplayName );
133 aAny >>= xStyle;
134 }
135 else
136 {
137 xStyle = Create();
138 if( !xStyle.is() )
139 return;
140
141 xPageStyles->insertByName( sDisplayName, Any(xStyle) );
142 bNew = true;
143 }
144
145 Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
146 Reference< XPropertySetInfo > xPropSetInfo =
147 xPropSet->getPropertySetInfo();
148 OUString sIsPhysical( "IsPhysical" );
149 if( !bNew && xPropSetInfo->hasPropertyByName( sIsPhysical ) )
150 {
151 aAny = xPropSet->getPropertyValue( sIsPhysical );
152 bNew = !*o3tl::doAccess<bool>(aAny);
153 }
154 SetNew( bNew );
155
156 if( !(bOverwrite || bNew) )
157 return;
158
159 Reference < XMultiPropertyStates > xMultiStates( xPropSet,
160 UNO_QUERY );
161 OSL_ENSURE( xMultiStates.is(),
162 "text page style does not support multi property set" );
163 if( xMultiStates.is() )
164 xMultiStates->setAllPropertiesToDefault();
165
166 if ( xPropSetInfo->hasPropertyByName( "GridDisplay" ) )
167 xPropSet->setPropertyValue( "GridDisplay", Any(false) );
168
169 if ( xPropSetInfo->hasPropertyByName( "GridPrint" ) )
170 xPropSet->setPropertyValue( "GridPrint", Any(false) );
171
175}
176
178{
179}
180
181css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextMasterPageContext::createFastChildContext(
182 sal_Int32 nElement,
183 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
184{
185 SvXMLImportContextRef xContext;
186
187 bool bInsert = false, bFooter = false, bLeft = false, bFirst = false;
188 switch( nElement )
189 {
192 {
193 bInsert = true;
194 bHeaderInserted = true;
195 }
196 break;
199 {
200 bInsert = bFooter = true;
201 bFooterInserted = true;
202 }
203 break;
206 bInsert = bLeft = true;
207 break;
210 bInsert = bFooter = bLeft = true;
211 break;
212 case XML_ELEMENT(LO_EXT, XML_HEADER_FIRST):
215 bInsert = bFirst = true;
216 break;
217 case XML_ELEMENT(LO_EXT, XML_FOOTER_FIRST):
220 bInsert = bFooter = bFirst = true;
221 break;
222 }
223
224 if( bInsert && xStyle.is() )
225 {
226 xContext = CreateHeaderFooterContext( nElement, xAttrList,
227 bFooter, bLeft, bFirst );
228 }
229
230 return xContext;
231}
232
234 sal_Int32 /*nElement*/,
235 const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/,
236 const bool bFooter,
237 const bool bLeft,
238 const bool bFirst )
239{
240 Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
241 return new XMLTextHeaderFooterContext( GetImport(), xPropSet, bFooter, bLeft, bFirst );
242}
243
244void XMLTextMasterPageContext::Finish( bool bOverwrite )
245{
246 if( !(xStyle.is() && (IsNew() || bOverwrite)) )
247 return;
248
249 Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
250 XMLPropStyleContext * pDrawingPageStyle(nullptr);
251 if (!m_sDrawingPageStyle.isEmpty())
252 {
253 pDrawingPageStyle = GetImport().GetTextImport()->FindDrawingPage(m_sDrawingPageStyle);
254 }
255 PageStyleContext * pPageLayout(nullptr);
256 if( !sPageMasterName.isEmpty() )
257 {
258 pPageLayout = static_cast<PageStyleContext *>(GetImport().GetTextImport()->FindPageMaster(sPageMasterName));
259 }
260 if (pPageLayout)
261 {
262 pPageLayout->FillPropertySet_PageStyle(xPropSet, pDrawingPageStyle);
263 }
264 else if (pDrawingPageStyle)
265 {
266 // don't need to care about old background attributes in this case
267 pDrawingPageStyle->FillPropertySet(xPropSet);
268 }
269
270 Reference < XNameContainer > xPageStyles =
271 GetImport().GetTextImport()->GetPageStyles();
272 if( !xPageStyles.is() )
273 return;
274
275 Reference< XPropertySetInfo > xPropSetInfo =
276 xPropSet->getPropertySetInfo();
277 if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
278 {
279 OUString sDisplayFollow(
280 GetImport().GetStyleDisplayName(
282 if( sDisplayFollow.isEmpty() ||
283 !xPageStyles->hasByName( sDisplayFollow ) )
284 sDisplayFollow = xStyle->getName();
285
286 Any aAny = xPropSet->getPropertyValue( gsFollowStyle );
287 OUString sCurrFollow;
288 aAny >>= sCurrFollow;
289 if( sCurrFollow != sDisplayFollow )
290 {
291 xPropSet->setPropertyValue( gsFollowStyle, Any(sDisplayFollow) );
292 }
293 }
294
295 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
296 {
297 xPropSet->setPropertyValue( "Hidden", uno::Any( IsHidden( ) ) );
298 }
299}
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral gsFollowStyle(u"FollowStyle")
void FillPropertySet_PageStyle(const css::uno::Reference< css::beans::XPropertySet > &rPropSet, XMLPropStyleContext *pDrawingPageStyle)
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
void AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1391
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:602
bool IsHidden() const
Definition: xmlstyle.hxx:93
void SetNew(bool b)
Definition: xmlstyle.hxx:91
bool IsNew() const
Definition: xmlstyle.hxx:90
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:222
SAL_DLLPRIVATE css::uno::Reference< css::style::XStyle > Create()
css::uno::Reference< css::style::XStyle > xStyle
virtual SvXMLImportContext * CreateHeaderFooterContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const bool bFooter, const bool bLeft, const bool bFirst)
virtual void Finish(bool bOverwrite) override
XMLTextMasterPageContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, bool bOverwrite)
virtual ~XMLTextMasterPageContext() override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
Reference< XSingleServiceFactory > xFactory
XmlStyleFamily
Definition: families.hxx:50
DRAW
OUString sDisplayName
const char * sName
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:98
constexpr OUStringLiteral sIsPhysical(u"IsPhysical")