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 <osl/diagnose.h>
30 #include <xmloff/nmspmap.hxx>
31 #include <xmloff/xmlnmspe.hxx>
32 #include <xmloff/prstylei.hxx>
33 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlimp.hxx>
37 
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::xml::sax;
42 using namespace ::com::sun::star::style;
43 using namespace ::com::sun::star::text;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::lang;
47 using namespace ::xmloff::token;
48 
49 Reference < XStyle > XMLTextMasterPageContext::Create()
50 {
51  Reference < XStyle > xNewStyle;
52 
53  Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
54  UNO_QUERY );
55  if( xFactory.is() )
56  {
58  xFactory->createInstance("com.sun.star.style.PageStyle");
59  if( xIfc.is() )
60  xNewStyle.set( xIfc, UNO_QUERY );
61  }
62 
63  return xNewStyle;
64 }
65 
66 static const OUStringLiteral gsFollowStyle( "FollowStyle" );
67 
69  sal_uInt16 nPrfx, const OUString& rLName,
70  const Reference< XAttributeList > & xAttrList,
71  bool bOverwrite )
72 : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XmlStyleFamily::MASTER_PAGE )
73 , bInsertHeader( false )
74 , bInsertFooter( false )
75 , bInsertHeaderLeft( false )
76 , bInsertFooterLeft( false )
77 , bInsertHeaderFirst( false )
78 , bInsertFooterFirst( false )
79 , bHeaderInserted( false )
80 , bFooterInserted( false )
81 {
82  OUString sName, sDisplayName;
83  sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
84  for( sal_Int16 i=0; i < nAttrCount; i++ )
85  {
86  const OUString& rAttrName = xAttrList->getNameByIndex( i );
87  OUString aLocalName;
88  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
89  if( XML_NAMESPACE_STYLE == nPrefix )
90  {
91  if( IsXMLToken( aLocalName, XML_NAME ) )
92  {
93  sName = xAttrList->getValueByIndex( i );
94  }
95  else if( IsXMLToken( aLocalName, XML_DISPLAY_NAME ) )
96  {
97  sDisplayName = xAttrList->getValueByIndex( i );
98  }
99  else if( IsXMLToken( aLocalName, XML_NEXT_STYLE_NAME ) )
100  {
101  sFollow = xAttrList->getValueByIndex( i );
102  }
103  else if( IsXMLToken( aLocalName, XML_PAGE_LAYOUT_NAME ) )
104  {
105  sPageMasterName = xAttrList->getValueByIndex( i );
106  }
107  }
108  }
109 
110  if( !sDisplayName.isEmpty() )
111  {
113  sDisplayName );
114  }
115  else
116  {
117  sDisplayName = sName;
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  {
158  Reference < XMultiPropertyStates > xMultiStates( xPropSet,
159  UNO_QUERY );
160  OSL_ENSURE( xMultiStates.is(),
161  "text page style does not support multi property set" );
162  if( xMultiStates.is() )
163  xMultiStates->setAllPropertiesToDefault();
164 
165  if ( xPropSetInfo->hasPropertyByName( "GridDisplay" ) )
166  xPropSet->setPropertyValue( "GridDisplay", Any(false) );
167 
168  if ( xPropSetInfo->hasPropertyByName( "GridPrint" ) )
169  xPropSet->setPropertyValue( "GridPrint", Any(false) );
170 
171  bInsertHeader = bInsertFooter = true;
174  }
175 }
176 
178 {
179 }
180 
182  sal_uInt16 nPrefix,
183  const OUString& rLocalName,
184  const Reference< XAttributeList > & xAttrList )
185 {
186  SvXMLImportContextRef xContext;
187 
188  const SvXMLTokenMap& rTokenMap =
189  GetImport().GetTextImport()->GetTextMasterPageElemTokenMap();
190 
191  bool bInsert = false, bFooter = false, bLeft = false, bFirst = false;
192  switch( rTokenMap.Get( nPrefix, rLocalName ) )
193  {
196  {
197  bInsert = true;
198  bHeaderInserted = true;
199  }
200  break;
203  {
204  bInsert = bFooter = true;
205  bFooterInserted = true;
206  }
207  break;
210  bInsert = bLeft = true;
211  break;
214  bInsert = bFooter = bLeft = true;
215  break;
218  bInsert = bFirst = true;
219  break;
222  bInsert = bFooter = bFirst = true;
223  break;
224  }
225 
226  if( bInsert && xStyle.is() )
227  {
228  xContext = CreateHeaderFooterContext( nPrefix, rLocalName,
229  xAttrList,
230  bFooter, bLeft, bFirst );
231  }
232  else
233  {
234  xContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
235  xAttrList );
236  }
237 
238  return xContext;
239 }
240 
242  sal_uInt16 nPrefix,
243  const OUString& rLocalName,
244  const css::uno::Reference< css::xml::sax::XAttributeList > & xAttrList,
245  const bool bFooter,
246  const bool bLeft,
247  const bool bFirst )
248 {
249  Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
251  nPrefix, rLocalName,
252  xAttrList,
253  xPropSet,
254  bFooter, bLeft, bFirst );
255 }
256 
257 void XMLTextMasterPageContext::Finish( bool bOverwrite )
258 {
259  if( xStyle.is() && (IsNew() || bOverwrite) )
260  {
261  Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
262  if( !sPageMasterName.isEmpty() )
263  {
264  XMLPropStyleContext* pStyle =
265  GetImport().GetTextImport()->FindPageMaster( sPageMasterName );
266  if (pStyle)
267  {
268  pStyle->FillPropertySet(xPropSet);
269  }
270  }
271 
272  Reference < XNameContainer > xPageStyles =
273  GetImport().GetTextImport()->GetPageStyles();
274  if( !xPageStyles.is() )
275  return;
276 
277  Reference< XPropertySetInfo > xPropSetInfo =
278  xPropSet->getPropertySetInfo();
279  if( xPropSetInfo->hasPropertyByName( gsFollowStyle ) )
280  {
281  OUString sDisplayFollow(
282  GetImport().GetStyleDisplayName(
284  if( sDisplayFollow.isEmpty() ||
285  !xPageStyles->hasByName( sDisplayFollow ) )
286  sDisplayFollow = xStyle->getName();
287 
288  Any aAny = xPropSet->getPropertyValue( gsFollowStyle );
289  OUString sCurrFollow;
290  aAny >>= sCurrFollow;
291  if( sCurrFollow != sDisplayFollow )
292  {
293  xPropSet->setPropertyValue( gsFollowStyle, Any(sDisplayFollow) );
294  }
295  }
296 
297  if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
298  {
299  xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
300  }
301  }
302 }
303 
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
constexpr sal_uInt16 XML_NAMESPACE_STYLE
Definition: xmlnmspe.hxx:30
void SetNew(bool b)
Definition: xmlstyle.hxx:118
virtual SvXMLImportContext * CreateHeaderFooterContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, const bool bFooter, const bool bLeft, const bool bFirst)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
static const OUStringLiteral gsFollowStyle("FollowStyle")
bool IsNew() const
Definition: xmlstyle.hxx:117
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:594
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
Definition: nmspmap.cxx:437
XmlStyleFamily
Definition: families.hxx:47
const char * sName
bool IsHidden() const
Definition: xmlstyle.hxx:120
XMLTextMasterPageContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, bool bOverwrite)
int i
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:231
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:93
virtual ~XMLTextMasterPageContext() override
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
static const OUStringLiteral sIsPhysical("IsPhysical")
Handling of tokens in XML:
virtual void Finish(bool bOverwrite) override
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
Create a children element context.
Definition: xmlictxt.cxx:51
void AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1460
Reference< XSingleServiceFactory > xFactory
css::uno::Reference< css::style::XStyle > xStyle
SAL_DLLPRIVATE css::uno::Reference< css::style::XStyle > Create()
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
OUString sDisplayName