LibreOffice Module xmloff (master)  1
PageMasterImportContext.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 
21 #include <xmloff/xmlimppr.hxx>
22 #include <xmloff/xmlnamespace.hxx>
23 #include <xmloff/xmlprmap.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include "PageMasterPropHdl.hxx"
28 #include <PageMasterStyleMap.hxx>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <osl/diagnose.h>
32 
33 //
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/beans/XPropertySetInfo.hpp>
36 #include <xmloff/xmlerror.hxx>
38 
39 using namespace ::com::sun::star;
40 using namespace ::xmloff::token;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 
44 //
45 using namespace ::com::sun::star::beans;
46 
47 void PageStyleContext::SetAttribute( sal_Int32 nElement,
48  const OUString& rValue )
49 {
50  if( nElement == XML_ELEMENT(STYLE, XML_PAGE_USAGE) )
51  {
53  }
54  else
55  {
56  XMLPropStyleContext::SetAttribute( nElement, rValue );
57  }
58 }
59 
60 
62  SvXMLStylesContext& rStyles,
63  bool bDefaultStyle) :
64  XMLPropStyleContext( rImport, rStyles, XmlStyleFamily::PAGE_MASTER, bDefaultStyle),
65  sPageUsage(),
66  m_bIsFillStyleAlreadyConverted(false) //
67 {
68 }
69 
71 {
72 }
73 
74 css::uno::Reference< css::xml::sax::XFastContextHandler > PageStyleContext::createFastChildContext(
75  sal_Int32 nElement,
76  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
77 {
78  if( nElement == XML_ELEMENT(STYLE, XML_HEADER_STYLE) ||
79  nElement == XML_ELEMENT(STYLE, XML_FOOTER_STYLE) )
80  {
81  bool bHeader = nElement == XML_ELEMENT(STYLE, XML_HEADER_STYLE);
84  if( xImpPrMap.is() )
85  {
86  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
87  sal_Int32 nFlag;
88  if (bHeader)
89  nFlag = CTF_PM_HEADERFLAG;
90  else
91  nFlag = CTF_PM_FOOTERFLAG;
92  sal_Int32 nStartIndex (-1);
93  sal_Int32 nEndIndex (-1);
94  bool bFirst(false);
95  bool bEnd(false);
96  sal_Int32 nIndex = 0;
97  while ( nIndex < rMapper->GetEntryCount() && !bEnd)
98  {
99  if ((rMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK) == nFlag)
100  {
101  if (!bFirst)
102  {
103  bFirst = true;
104  nStartIndex = nIndex;
105  }
106  }
107  else if (bFirst)
108  {
109  bEnd = true;
110  nEndIndex = nIndex;
111  }
112  nIndex++;
113  }
114  if (!bEnd)
115  nEndIndex = nIndex;
116  return new PageHeaderFooterContext(GetImport(),
117  GetProperties(), xImpPrMap, nStartIndex, nEndIndex, bHeader);
118  }
119  }
120 
121  if( nElement == XML_ELEMENT(STYLE, XML_PAGE_LAYOUT_PROPERTIES) )
122  {
125  if( xImpPrMap.is() )
126  {
127  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
128  sal_Int32 nEndIndex (-1);
129  bool bEnd(false);
130  sal_Int32 nIndex = 0;
131  sal_Int16 nContextID;
132  while ( nIndex < rMapper->GetEntryCount() && !bEnd)
133  {
134  nContextID = rMapper->GetEntryContextId( nIndex );
135  if (nContextID && ((nContextID & CTF_PM_FLAGMASK) != XML_PM_CTF_START))
136  {
137  nEndIndex = nIndex;
138  bEnd = true;
139  }
140  nIndex++;
141  }
142  if (!bEnd)
143  nEndIndex = nIndex;
144  return new PagePropertySetContext( GetImport(), nElement,
145  xAttrList,
147  GetProperties(),
148  xImpPrMap, 0, nEndIndex, Page);
149  }
150  }
151 
152  return XMLPropStyleContext::createFastChildContext(nElement, xAttrList);
153 }
154 
155 void PageStyleContext::FillPropertySet(const uno::Reference<beans::XPropertySet > &)
156 {
157  assert(false); // don't call this virtual, call function below
158 }
159 
161  const uno::Reference<beans::XPropertySet> & rPropSet,
162  XMLPropStyleContext *const pDrawingPageStyle)
163 {
164  // need to filter out old fill definitions when the new ones are used. The new
165  // ones are used when a FillStyle is defined
167  {
168  static OUString s_FillStyle("FillStyle");
169  static OUString s_HeaderFillStyle("HeaderFillStyle");
170  static OUString s_FooterFillStyle("FooterFillStyle");
171 
172  // note: the function must only check by property name, not any id/flag!
174  || (pDrawingPageStyle && pDrawingPageStyle->doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle)))
175  {
177  }
178 
179  if(doNewDrawingLayerFillStyleDefinitionsExist(s_HeaderFillStyle))
180  {
182  }
183 
184  if(doNewDrawingLayerFillStyleDefinitionsExist(s_FooterFillStyle))
185  {
187  }
188 
190  }
191 
192  // do not use XMLPropStyleContext::FillPropertySet, we need to handle this ourselves since
193  // we have properties which use the MID_FLAG_NO_PROPERTY_IMPORT flag since they need some special
194  // handling
196 
197  if(xImpPrMap.is())
198  {
199  // properties that need special handling because they need the used name to be translated first
200  struct ContextID_Index_Pair aContextIDs[] =
201  {
202  { CTF_PM_FILLGRADIENTNAME, -1 },
203  { CTF_PM_FILLTRANSNAME, -1 },
204  { CTF_PM_FILLHATCHNAME, -1 },
205  { CTF_PM_FILLBITMAPNAME, -1 },
206 
207  // also need to special handling for header entries
212 
213  // also need to special handling for footer entries
218 
219  {-1, -1}
220  };
221 
222  // the style families associated with the same index modulo 4
223  static const XmlStyleFamily aFamilies[] =
224  {
229  };
230 
231  // Fill PropertySet, but let it handle special properties not itself
232  xImpPrMap->FillPropertySet(GetProperties(), rPropSet, aContextIDs);
233 
234  // get property set mapper
235  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
236  Reference< XPropertySetInfo > xInfo;
237 
238  // handle special attributes which have MID_FLAG_NO_PROPERTY_IMPORT set
239  for(sal_uInt16 i = 0; aContextIDs[i].nContextID != -1; i++)
240  {
241  sal_Int32 nIndex = aContextIDs[i].nIndex;
242 
243  if(nIndex != -1)
244  {
245  switch(aContextIDs[i].nContextID)
246  {
251 
256 
261  {
262  struct XMLPropertyState& rState = GetProperties()[nIndex];
263  OUString sStyleName;
264  rState.maValue >>= sStyleName;
265 
266  // translate the used name from ODF intern to the name used in the Model
267  sStyleName = GetImport().GetStyleDisplayName(aFamilies[i%4], sStyleName);
268 
269  try
270  {
271  // set property
272  const OUString& rPropertyName = rMapper->GetEntryAPIName(rState.mnIndex);
273 
274  if(!xInfo.is())
275  {
276  xInfo = rPropSet->getPropertySetInfo();
277  }
278 
279  if(xInfo->hasPropertyByName(rPropertyName))
280  {
281  rPropSet->setPropertyValue(rPropertyName,Any(sStyleName));
282  }
283  }
284  catch(css::lang::IllegalArgumentException& e)
285  {
286  Sequence<OUString> aSeq { sStyleName };
289  aSeq,e.Message,nullptr);
290  }
291  break;
292  }
293  }
294  }
295  }
296  }
297  else
298  {
299  OSL_ENSURE(xImpPrMap.is(), "Got no SvXMLImportPropertyMapper (!)");
300  }
301 
302  // pDrawingPageStyle overrides this
303  if (pDrawingPageStyle)
304  {
305  pDrawingPageStyle->FillPropertySet(rPropSet);
306  }
307 
308  // old code, replaced by above stuff
309  // XMLPropStyleContext::FillPropertySet(rPropSet);
310 
311  if (!sPageUsage.isEmpty())
312  {
313  uno::Any aPageUsage;
314  XMLPMPropHdl_PageStyleLayout aPageUsageHdl;
315  if (aPageUsageHdl.importXML(sPageUsage, aPageUsage, GetImport().GetMM100UnitConverter()))
316  rPropSet->setPropertyValue("PageStyleLayout", aPageUsage);
317  }
318 }
319 
321 {
322  { CTF_PM_FILLGRADIENTNAME, -1 },
323  { CTF_PM_FILLTRANSNAME, -1 },
324  { CTF_PM_FILLHATCHNAME, -1 },
325  { CTF_PM_FILLBITMAPNAME, -1 },
326 
327  {-1, -1}
328 };
329 
330 extern XmlStyleFamily const g_MasterPageFamilies[] =
331 {
336 };
337 
338 // text grid enhancement for better CJK support
339 //set default page layout style
341 {
342  Reference < XMultiServiceFactory > xFactory ( GetImport().GetModel(), UNO_QUERY);
343  if (xFactory.is())
344  {
345  Reference < XInterface > xInt = xFactory->createInstance( "com.sun.star.text.Defaults" );
346  Reference < beans::XPropertySet > xProperties ( xInt, UNO_QUERY );
347  if ( xProperties.is() )
348  FillPropertySet_PageStyle(xProperties, nullptr);
349  }
350 }
351 
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 nContextID
Definition: xmlimppr.hxx:58
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:83
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
Definition: prstylei.cxx:175
#define XML_TYPE_PROP_PAGE_LAYOUT
Definition: xmltypes.hxx:96
#define CTF_PM_HEADERFILLTRANSNAME
#define CTF_PM_FLAGMASK
sal_Int32 nIndex
#define CTF_PM_FILLGRADIENTNAME
static const OldFillStyleDefinitionSet & getStandardSet()
Definition: prstylei.cxx:160
#define CTF_PM_HEADERFILLHATCHNAME
#define CTF_PM_HEADERFILLBITMAPNAME
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:56
sal_Int32 mnIndex
Definition: maptype.hxx:124
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
XmlStyleFamily
Definition: families.hxx:47
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
PageStyleContext(SvXMLImport &rImport, SvXMLStylesContext &rStyles, bool bDefaultStyle)
#define CTF_PM_FOOTERFILLBITMAPNAME
#define XML_PM_CTF_START
Definition: contextid.hxx:30
#define CTF_PM_FOOTERFILLGRADIENTNAME
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
bool doNewDrawingLayerFillStyleDefinitionsExist(const OUString &rFillStyleTag) const
Definition: prstylei.cxx:523
#define CTF_PM_FILLBITMAPNAME
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
void deactivateOldFillStyleDefinitions(const OldFillStyleDefinitionSet &rHashSetOfTags)
Definition: prstylei.cxx:569
XmlStyleFamily const g_MasterPageFamilies[]
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:510
#define XMLERROR_STYLE_PROP_VALUE
Definition: xmlerror.hxx:68
int i
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:222
#define CTF_PM_HEADERFILLGRADIENTNAME
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:79
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
#define CTF_PM_HEADERFLAG
#define CTF_PM_FILLHATCHNAME
#define CTF_PM_FOOTERFILLTRANSNAME
#define CTF_PM_FOOTERFILLHATCHNAME
static const OldFillStyleDefinitionSet & getHeaderSet()
Definition: prstylei.cxx:165
static const OldFillStyleDefinitionSet & getFooterSet()
Definition: prstylei.cxx:170
void FillPropertySet_PageStyle(const css::uno::Reference< css::beans::XPropertySet > &rPropSet, XMLPropStyleContext *pDrawingPageStyle)
css::uno::Any maValue
Definition: maptype.hxx:125
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1393
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
Definition: prstylei.cxx:57
ContextID_Index_Pair const g_MasterPageContextIDs[]
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:122
void SetError(sal_Int32 nId, const css::uno::Sequence< OUString > &rMsgParams, const OUString &rExceptionMessage, const css::uno::Reference< css::xml::sax::XLocator > &rLocator)
Record an error condition that occurred during import.
Sequence< sal_Int8 > aSeq
SvXMLStylesContext * GetStyles()
Definition: prstylei.hxx:78
This struct is used as an optional parameter to the static _FillPropertySet() methods.
Definition: xmlimppr.hxx:56
Reference< XSingleServiceFactory > xFactory
#define CTF_PM_FOOTERFLAG
#define CTF_PM_FILLTRANSNAME
virtual void SetDefaults() override
virtual ~PageStyleContext() override