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_uInt16 nPrefixKey,
48  const OUString& rLocalName,
49  const OUString& rValue )
50 {
51  // TODO: use a map here
52  if( XML_NAMESPACE_STYLE == nPrefixKey && IsXMLToken( rLocalName, XML_PAGE_USAGE ) )
53  {
55  }
56  else
57  {
58  XMLPropStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
59  }
60 }
61 
62 
64  SvXMLStylesContext& rStyles,
65  bool bDefaultStyle) :
66  XMLPropStyleContext( rImport, rStyles, XmlStyleFamily::PAGE_MASTER, bDefaultStyle),
67  sPageUsage(),
68  m_bIsFillStyleAlreadyConverted(false) //
69 {
70 }
71 
73 {
74 }
75 
76 css::uno::Reference< css::xml::sax::XFastContextHandler > PageStyleContext::createFastChildContext(
77  sal_Int32 nElement,
78  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
79 {
80  if( nElement == XML_ELEMENT(STYLE, XML_HEADER_STYLE) ||
81  nElement == XML_ELEMENT(STYLE, XML_FOOTER_STYLE) )
82  {
83  bool bHeader = nElement == XML_ELEMENT(STYLE, XML_HEADER_STYLE);
86  if( xImpPrMap.is() )
87  {
88  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
89  sal_Int32 nFlag;
90  if (bHeader)
91  nFlag = CTF_PM_HEADERFLAG;
92  else
93  nFlag = CTF_PM_FOOTERFLAG;
94  sal_Int32 nStartIndex (-1);
95  sal_Int32 nEndIndex (-1);
96  bool bFirst(false);
97  bool bEnd(false);
98  sal_Int32 nIndex = 0;
99  while ( nIndex < rMapper->GetEntryCount() && !bEnd)
100  {
101  if ((rMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK) == nFlag)
102  {
103  if (!bFirst)
104  {
105  bFirst = true;
106  nStartIndex = nIndex;
107  }
108  }
109  else if (bFirst)
110  {
111  bEnd = true;
112  nEndIndex = nIndex;
113  }
114  nIndex++;
115  }
116  if (!bEnd)
117  nEndIndex = nIndex;
118  return new PageHeaderFooterContext(GetImport(),
119  GetProperties(), xImpPrMap, nStartIndex, nEndIndex, bHeader);
120  }
121  }
122 
123  if( nElement == XML_ELEMENT(STYLE, XML_PAGE_LAYOUT_PROPERTIES) )
124  {
127  if( xImpPrMap.is() )
128  {
129  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
130  sal_Int32 nEndIndex (-1);
131  bool bEnd(false);
132  sal_Int32 nIndex = 0;
133  sal_Int16 nContextID;
134  while ( nIndex < rMapper->GetEntryCount() && !bEnd)
135  {
136  nContextID = rMapper->GetEntryContextId( nIndex );
137  if (nContextID && ((nContextID & CTF_PM_FLAGMASK) != XML_PM_CTF_START))
138  {
139  nEndIndex = nIndex;
140  bEnd = true;
141  }
142  nIndex++;
143  }
144  if (!bEnd)
145  nEndIndex = nIndex;
146  return new PagePropertySetContext( GetImport(), nElement,
147  xAttrList,
149  GetProperties(),
150  xImpPrMap, 0, nEndIndex, Page);
151  }
152  }
153 
154  return XMLPropStyleContext::createFastChildContext(nElement, xAttrList);
155 }
156 
157 void PageStyleContext::FillPropertySet(const uno::Reference<beans::XPropertySet > &)
158 {
159  assert(false); // don't call this virtual, call function below
160 }
161 
163  const uno::Reference<beans::XPropertySet> & rPropSet,
164  XMLPropStyleContext *const pDrawingPageStyle)
165 {
166  // need to filter out old fill definitions when the new ones are used. The new
167  // ones are used when a FillStyle is defined
169  {
170  static OUString s_FillStyle("FillStyle");
171  static OUString s_HeaderFillStyle("HeaderFillStyle");
172  static OUString s_FooterFillStyle("FooterFillStyle");
173 
174  // note: the function must only check by property name, not any id/flag!
176  || (pDrawingPageStyle && pDrawingPageStyle->doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle)))
177  {
179  }
180 
181  if(doNewDrawingLayerFillStyleDefinitionsExist(s_HeaderFillStyle))
182  {
184  }
185 
186  if(doNewDrawingLayerFillStyleDefinitionsExist(s_FooterFillStyle))
187  {
189  }
190 
192  }
193 
194  // do not use XMLPropStyleContext::FillPropertySet, we need to handle this ourselves since
195  // we have properties which use the MID_FLAG_NO_PROPERTY_IMPORT flag since they need some special
196  // handling
198 
199  if(xImpPrMap.is())
200  {
201  // properties that need special handling because they need the used name to be translated first
202  struct ContextID_Index_Pair aContextIDs[] =
203  {
204  { CTF_PM_FILLGRADIENTNAME, -1 },
205  { CTF_PM_FILLTRANSNAME, -1 },
206  { CTF_PM_FILLHATCHNAME, -1 },
207  { CTF_PM_FILLBITMAPNAME, -1 },
208 
209  // also need to special handling for header entries
214 
215  // also need to special handling for footer entries
220 
221  {-1, -1}
222  };
223 
224  // the style families associated with the same index modulo 4
225  static const XmlStyleFamily aFamilies[] =
226  {
231  };
232 
233  // Fill PropertySet, but let it handle special properties not itself
234  xImpPrMap->FillPropertySet(GetProperties(), rPropSet, aContextIDs);
235 
236  // get property set mapper
237  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
238  Reference< XPropertySetInfo > xInfo;
239 
240  // handle special attributes which have MID_FLAG_NO_PROPERTY_IMPORT set
241  for(sal_uInt16 i = 0; aContextIDs[i].nContextID != -1; i++)
242  {
243  sal_Int32 nIndex = aContextIDs[i].nIndex;
244 
245  if(nIndex != -1)
246  {
247  switch(aContextIDs[i].nContextID)
248  {
253 
258 
263  {
264  struct XMLPropertyState& rState = GetProperties()[nIndex];
265  OUString sStyleName;
266  rState.maValue >>= sStyleName;
267 
268  // translate the used name from ODF intern to the name used in the Model
269  sStyleName = GetImport().GetStyleDisplayName(aFamilies[i%4], sStyleName);
270 
271  try
272  {
273  // set property
274  const OUString& rPropertyName = rMapper->GetEntryAPIName(rState.mnIndex);
275 
276  if(!xInfo.is())
277  {
278  xInfo = rPropSet->getPropertySetInfo();
279  }
280 
281  if(xInfo->hasPropertyByName(rPropertyName))
282  {
283  rPropSet->setPropertyValue(rPropertyName,Any(sStyleName));
284  }
285  }
286  catch(css::lang::IllegalArgumentException& e)
287  {
288  Sequence<OUString> aSeq { sStyleName };
291  aSeq,e.Message,nullptr);
292  }
293  break;
294  }
295  }
296  }
297  }
298  }
299  else
300  {
301  OSL_ENSURE(xImpPrMap.is(), "Got no SvXMLImportPropertyMapper (!)");
302  }
303 
304  // pDrawingPageStyle overrides this
305  if (pDrawingPageStyle)
306  {
307  pDrawingPageStyle->FillPropertySet(rPropSet);
308  }
309 
310  // old code, replaced by above stuff
311  // XMLPropStyleContext::FillPropertySet(rPropSet);
312 
313  if (!sPageUsage.isEmpty())
314  {
315  uno::Any aPageUsage;
316  XMLPMPropHdl_PageStyleLayout aPageUsageHdl;
317  if (aPageUsageHdl.importXML(sPageUsage, aPageUsage, GetImport().GetMM100UnitConverter()))
318  rPropSet->setPropertyValue("PageStyleLayout", aPageUsage);
319  }
320 }
321 
323 {
324  { CTF_PM_FILLGRADIENTNAME, -1 },
325  { CTF_PM_FILLTRANSNAME, -1 },
326  { CTF_PM_FILLHATCHNAME, -1 },
327  { CTF_PM_FILLBITMAPNAME, -1 },
328 
329  {-1, -1}
330 };
331 
332 extern XmlStyleFamily const g_MasterPageFamilies[] =
333 {
338 };
339 
340 // text grid enhancement for better CJK support
341 //set default page layout style
343 {
344  Reference < XMultiServiceFactory > xFactory ( GetImport().GetModel(), UNO_QUERY);
345  if (xFactory.is())
346  {
347  Reference < XInterface > xInt = xFactory->createInstance( "com.sun.star.text.Defaults" );
348  Reference < beans::XPropertySet > xProperties ( xInt, UNO_QUERY );
349  if ( xProperties.is() )
350  FillPropertySet_PageStyle(xProperties, nullptr);
351  }
352 }
353 
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 nContextID
Definition: xmlimppr.hxx:58
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:104
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:176
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
#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:161
#define CTF_PM_HEADERFILLHATCHNAME
#define CTF_PM_HEADERFILLBITMAPNAME
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
Definition: prstylei.cxx:57
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:524
#define CTF_PM_FILLBITMAPNAME
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
void deactivateOldFillStyleDefinitions(const OldFillStyleDefinitionSet &rHashSetOfTags)
Definition: prstylei.cxx:570
XmlStyleFamily const g_MasterPageFamilies[]
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:530
#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:223
#define CTF_PM_HEADERFILLGRADIENTNAME
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:81
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:166
static const OldFillStyleDefinitionSet & getFooterSet()
Definition: prstylei.cxx:171
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:1469
ContextID_Index_Pair const g_MasterPageContextIDs[]
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
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:80
This struct is used as an optional parameter to the static _FillPropertySet() methods.
Definition: xmlimppr.hxx:56
Reference< XSingleServiceFactory > xFactory
#define CTF_PM_FOOTERFLAG
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
#define CTF_PM_FILLTRANSNAME
virtual void SetDefaults() override
constexpr sal_uInt16 XML_NAMESPACE_STYLE
virtual ~PageStyleContext() override