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/xmlnmspe.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>
37 
38 using namespace ::com::sun::star;
39 using namespace ::xmloff::token;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 
43 //
44 using namespace ::com::sun::star::beans;
45 
46 void PageStyleContext::SetAttribute( sal_uInt16 nPrefixKey,
47  const OUString& rLocalName,
48  const OUString& rValue )
49 {
50  // TODO: use a map here
51  if( XML_NAMESPACE_STYLE == nPrefixKey && IsXMLToken( rLocalName, XML_PAGE_USAGE ) )
52  {
54  }
55  else
56  {
57  XMLPropStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
58  }
59 }
60 
61 
63  sal_uInt16 nPrfx, const OUString& rLName,
64  const uno::Reference< xml::sax::XAttributeList > & xAttrList,
65  SvXMLStylesContext& rStyles,
66  bool bDefaultStyle) :
67  XMLPropStyleContext( rImport, nPrfx, rLName, xAttrList, rStyles, XmlStyleFamily::PAGE_MASTER, bDefaultStyle),
68  sPageUsage(),
69  m_bIsFillStyleAlreadyConverted(false) //
70 {
71 }
72 
74 {
75 }
76 
78  sal_uInt16 nPrefix,
79  const OUString& rLocalName,
80  const uno::Reference< xml::sax::XAttributeList > & xAttrList )
81 {
82  if( XML_NAMESPACE_STYLE == nPrefix &&
83  ((IsXMLToken(rLocalName, XML_HEADER_STYLE )) ||
84  (IsXMLToken(rLocalName, XML_FOOTER_STYLE )) ) )
85  {
86  bool bHeader = IsXMLToken(rLocalName, XML_HEADER_STYLE);
89  if( xImpPrMap.is() )
90  {
91  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
92  sal_Int32 nFlag;
93  if (bHeader)
94  nFlag = CTF_PM_HEADERFLAG;
95  else
96  nFlag = CTF_PM_FOOTERFLAG;
97  sal_Int32 nStartIndex (-1);
98  sal_Int32 nEndIndex (-1);
99  bool bFirst(false);
100  bool bEnd(false);
101  sal_Int32 nIndex = 0;
102  while ( nIndex < rMapper->GetEntryCount() && !bEnd)
103  {
104  if ((rMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK) == nFlag)
105  {
106  if (!bFirst)
107  {
108  bFirst = true;
109  nStartIndex = nIndex;
110  }
111  }
112  else if (bFirst)
113  {
114  bEnd = true;
115  nEndIndex = nIndex;
116  }
117  nIndex++;
118  }
119  if (!bEnd)
120  nEndIndex = nIndex;
121  return new PageHeaderFooterContext(GetImport(), nPrefix, rLocalName,
122  xAttrList, GetProperties(), xImpPrMap, nStartIndex, nEndIndex, bHeader);
123  }
124  }
125 
126  if( XML_NAMESPACE_STYLE == nPrefix &&
128  {
131  if( xImpPrMap.is() )
132  {
133  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
134  sal_Int32 nEndIndex (-1);
135  bool bEnd(false);
136  sal_Int32 nIndex = 0;
137  sal_Int16 nContextID;
138  while ( nIndex < rMapper->GetEntryCount() && !bEnd)
139  {
140  nContextID = rMapper->GetEntryContextId( nIndex );
141  if (nContextID && ((nContextID & CTF_PM_FLAGMASK) != XML_PM_CTF_START))
142  {
143  nEndIndex = nIndex;
144  bEnd = true;
145  }
146  nIndex++;
147  }
148  if (!bEnd)
149  nEndIndex = nIndex;
150  return new PagePropertySetContext( GetImport(), nPrefix,
151  rLocalName, xAttrList,
153  GetProperties(),
154  xImpPrMap, 0, nEndIndex, Page);
155  }
156  }
157 
158  return XMLPropStyleContext::CreateChildContext(nPrefix, rLocalName, xAttrList);
159 }
160 
161 void PageStyleContext::FillPropertySet(const uno::Reference<beans::XPropertySet > & rPropSet)
162 {
163  // need to filter out old fill definitions when the new ones are used. The new
164  // ones are used when a FillStyle is defined
166  {
167  static OUString s_FillStyle("FillStyle");
168  static OUString s_HeaderFillStyle("HeaderFillStyle");
169  static OUString s_FooterFillStyle("FooterFillStyle");
170 
172  {
174  }
175 
176  if(doNewDrawingLayerFillStyleDefinitionsExist(s_HeaderFillStyle))
177  {
179  }
180 
181  if(doNewDrawingLayerFillStyleDefinitionsExist(s_FooterFillStyle))
182  {
184  }
185 
187  }
188 
189  // do not use XMLPropStyleContext::FillPropertySet, we need to handle this ourselves since
190  // we have properties which use the MID_FLAG_NO_PROPERTY_IMPORT flag since they need some special
191  // handling
193 
194  if(xImpPrMap.is())
195  {
196  // properties that need special handling because they need the used name to be translated first
197  struct ContextID_Index_Pair aContextIDs[] =
198  {
199  { CTF_PM_FILLGRADIENTNAME, -1 },
200  { CTF_PM_FILLTRANSNAME, -1 },
201  { CTF_PM_FILLHATCHNAME, -1 },
202  { CTF_PM_FILLBITMAPNAME, -1 },
203 
204  // also need to special handling for header entries
209 
210  // also need to special handling for footer entries
215 
216  {-1, -1}
217  };
218 
219  // the style families associated with the same index modulo 4
220  static const XmlStyleFamily aFamilies[] =
221  {
226  };
227 
228  // Fill PropertySet, but let it handle special properties not itself
229  xImpPrMap->FillPropertySet(GetProperties(), rPropSet, aContextIDs);
230 
231  // get property set mapper
232  const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
233  Reference< XPropertySetInfo > xInfo;
234 
235  // handle special attributes which have MID_FLAG_NO_PROPERTY_IMPORT set
236  for(sal_uInt16 i = 0; aContextIDs[i].nContextID != -1; i++)
237  {
238  sal_Int32 nIndex = aContextIDs[i].nIndex;
239 
240  if(nIndex != -1)
241  {
242  switch(aContextIDs[i].nContextID)
243  {
248 
253 
258  {
259  struct XMLPropertyState& rState = GetProperties()[nIndex];
260  OUString sStyleName;
261  rState.maValue >>= sStyleName;
262 
263  // translate the used name from ODF intern to the name used in the Model
264  sStyleName = GetImport().GetStyleDisplayName(aFamilies[i%4], sStyleName);
265 
266  try
267  {
268  // set property
269  const OUString& rPropertyName = rMapper->GetEntryAPIName(rState.mnIndex);
270 
271  if(!xInfo.is())
272  {
273  xInfo = rPropSet->getPropertySetInfo();
274  }
275 
276  if(xInfo->hasPropertyByName(rPropertyName))
277  {
278  rPropSet->setPropertyValue(rPropertyName,Any(sStyleName));
279  }
280  }
281  catch(css::lang::IllegalArgumentException& e)
282  {
283  Sequence<OUString> aSeq { sStyleName };
286  aSeq,e.Message,nullptr);
287  }
288  break;
289  }
290  }
291  }
292  }
293  }
294  else
295  {
296  OSL_ENSURE(xImpPrMap.is(), "Got no SvXMLImportPropertyMapper (!)");
297  }
298 
299  // old code, replaced by above stuff
300  // XMLPropStyleContext::FillPropertySet(rPropSet);
301 
302  if (!sPageUsage.isEmpty())
303  {
304  uno::Any aPageUsage;
305  XMLPMPropHdl_PageStyleLayout aPageUsageHdl;
306  if (aPageUsageHdl.importXML(sPageUsage, aPageUsage, GetImport().GetMM100UnitConverter()))
307  rPropSet->setPropertyValue("PageStyleLayout", aPageUsage);
308  }
309 }
310 
311 // text grid enhancement for better CJK support
312 //set default page layout style
314 {
315  Reference < XMultiServiceFactory > xFactory ( GetImport().GetModel(), UNO_QUERY);
316  if (xFactory.is())
317  {
318  Reference < XInterface > xInt = xFactory->createInstance( "com.sun.star.text.Defaults" );
319  Reference < beans::XPropertySet > xProperties ( xInt, UNO_QUERY );
320  if ( xProperties.is() )
321  FillPropertySet ( xProperties );
322  }
323 }
324 
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 nContextID
Definition: xmlimppr.hxx:56
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:112
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
#define XML_TYPE_PROP_PAGE_LAYOUT
Definition: xmltypes.hxx:96
constexpr sal_uInt16 XML_NAMESPACE_STYLE
Definition: xmlnmspe.hxx:30
#define CTF_PM_HEADERFILLTRANSNAME
#define CTF_PM_FLAGMASK
sal_Int32 nIndex
#define CTF_PM_FILLGRADIENTNAME
static const OldFillStyleDefinitionSet & getStandardSet()
Definition: prstylei.cxx:163
#define CTF_PM_HEADERFILLHATCHNAME
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
#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:106
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
XmlStyleFamily
Definition: families.hxx:47
#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:535
#define CTF_PM_FILLBITMAPNAME
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
void deactivateOldFillStyleDefinitions(const OldFillStyleDefinitionSet &rHashSetOfTags)
Definition: prstylei.cxx:580
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:580
#define XMLERROR_STYLE_PROP_VALUE
Definition: xmlerror.hxx:68
int i
#define CTF_PM_HEADERFILLGRADIENTNAME
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:79
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
Definition: prstylei.cxx:178
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...
#define CTF_PM_HEADERFLAG
#define CTF_PM_FILLHATCHNAME
#define CTF_PM_FOOTERFILLTRANSNAME
#define CTF_PM_FOOTERFILLHATCHNAME
PageStyleContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, SvXMLStylesContext &rStyles, bool bDefaultStyle)
static const OldFillStyleDefinitionSet & getHeaderSet()
Definition: prstylei.cxx:168
static const OldFillStyleDefinitionSet & getFooterSet()
Definition: prstylei.cxx:173
css::uno::Any maValue
Definition: maptype.hxx:107
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1491
Handling of tokens in XML:
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:104
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:54
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
virtual ~PageStyleContext() override