LibreOffice Module sw (master)  1
xmlitemi.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 <rtl/ustring.hxx>
21 #include <osl/diagnose.h>
22 
23 #include <com/sun/star/util/MeasureUnit.hpp>
24 
25 #include <xmloff/xmluconv.hxx>
26 #include <xmloff/families.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/xmltoken.hxx>
29 
30 #include <editeng/brushitem.hxx>
31 #include <editeng/memberids.h>
32 #include <svl/itemset.hxx>
33 #include <svl/itempool.hxx>
34 
35 #include <hintids.hxx>
36 #include <unomid.h>
37 #include "xmlbrshi.hxx"
38 #include "xmlimp.hxx"
39 #include "xmlitmap.hxx"
40 #include "xmlimpit.hxx"
41 #include "xmlitem.hxx"
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 
46 namespace {
47 
48 class SwXMLImportTableItemMapper_Impl: public SvXMLImportItemMapper
49 {
50 
51 public:
52 
53  explicit SwXMLImportTableItemMapper_Impl(const SvXMLItemMapEntriesRef& rMapEntries);
54 
55  virtual bool handleSpecialItem( const SvXMLItemMapEntry& rEntry,
56  SfxPoolItem& rItem,
57  SfxItemSet& rSet,
58  const OUString& rValue,
59  const SvXMLUnitConverter& rUnitConverter ) override;
60 
61  virtual bool
62  handleNoItem(SvXMLItemMapEntry const& rEntry,
63  SfxItemSet & rSet,
64  OUString const& rValue,
65  SvXMLUnitConverter const& rUnitConverter,
66  SvXMLNamespaceMap const& rNamespaceMap) override;
67 
68  virtual void finished(SfxItemSet & rSet,
69  SvXMLUnitConverter const& rUnitConverter) const override;
70 
71  virtual void setMapEntries( SvXMLItemMapEntriesRef rMapEntries ) override;
72 
73 private:
74  void Reset();
75 
76  OUString m_FoMarginValue;
77  enum { LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3 };
78  bool m_bHaveMargin[4];
79 };
80 
81 }
82 
83 SwXMLImportTableItemMapper_Impl::SwXMLImportTableItemMapper_Impl(
84  const SvXMLItemMapEntriesRef& rMapEntries ) :
85  SvXMLImportItemMapper( rMapEntries )
86 {
87  Reset();
88 }
89 
90 void SwXMLImportTableItemMapper_Impl::Reset()
91 {
92  m_FoMarginValue.clear();
93  for (int i = 0; i < 3; ++i)
94  {
95  m_bHaveMargin[i] = false;
96  }
97 }
98 
99 void SwXMLImportTableItemMapper_Impl::setMapEntries(
100  SvXMLItemMapEntriesRef rMapEntries )
101 {
102  Reset();
104 }
105 
106 bool SwXMLImportTableItemMapper_Impl::handleSpecialItem(
107  const SvXMLItemMapEntry& rEntry,
108  SfxPoolItem& rItem,
109  SfxItemSet& rItemSet,
110  const OUString& rValue,
111  const SvXMLUnitConverter& rUnitConv )
112 {
113  bool bRet = false;
114  sal_uInt16 nMemberId = static_cast< sal_Int16 >(rEntry.nMemberId & MID_SW_FLAG_MASK);
115  switch( rItem.Which() )
116  {
117  case RES_LR_SPACE:
118  switch (nMemberId)
119  {
120  case MID_L_MARGIN:
121  m_bHaveMargin[LEFT] = true;
122  break;
123  case MID_R_MARGIN:
124  m_bHaveMargin[RIGHT] = true;
125  break;
126  }
128  rItem, rValue, nMemberId, rUnitConv);
129  break;
130  case RES_UL_SPACE:
131  switch (nMemberId)
132  {
133  case MID_UP_MARGIN:
134  m_bHaveMargin[TOP] = true;
135  break;
136  case MID_LO_MARGIN:
137  m_bHaveMargin[BOTTOM] = true;
138  break;
139  }
141  rItem, rValue, nMemberId, rUnitConv);
142  break;
143  case RES_FRM_SIZE:
144  switch( nMemberId )
145  {
147  // If the item is existing already, a relative value has been set
148  // already that must be preserved.
149  if( SfxItemState::SET != rItemSet.GetItemState( RES_FRM_SIZE,
150  false ) )
152  rItem, rValue, nMemberId, rUnitConv );
153  break;
154  }
155  }
156 
157  return bRet;
158 }
159 
160 bool SwXMLImportTableItemMapper_Impl::handleNoItem(
161  SvXMLItemMapEntry const& rEntry,
162  SfxItemSet & rSet,
163  OUString const& rValue,
164  SvXMLUnitConverter const& rUnitConverter,
165  SvXMLNamespaceMap const& rNamespaceMap)
166 {
167  if ((XML_NAMESPACE_FO == rEntry.nNameSpace) &&
169  {
170  m_FoMarginValue = rValue;
171  return true;
172  }
173  else
174  {
176  rEntry, rSet, rValue, rUnitConverter, rNamespaceMap);
177  }
178 }
179 
180 void SwXMLImportTableItemMapper_Impl::finished(
181  SfxItemSet & rSet, SvXMLUnitConverter const& rUnitConverter) const
182 {
183  if (m_FoMarginValue.isEmpty())
184  return;
185 
186  sal_uInt16 const Ids[4][2] = {
191  };
192  for (int i = 0; i < 4; ++i)
193  {
194  if (m_bHaveMargin[i])
195  {
196  continue; // already read fo:margin-top etc.
197  }
198  // first get item from itemset
199  SfxPoolItem const* pItem = nullptr;
200  SfxItemState eState =
201  rSet.GetItemState(Ids[i][0], true, &pItem);
202 
203  // if not set, try the pool
204  if ((SfxItemState::SET != eState) && SfxItemPool::IsWhich(Ids[i][0]))
205  {
206  pItem = &rSet.GetPool()->GetDefaultItem(Ids[i][0]);
207  }
208 
209  // do we have an item?
210  if (eState >= SfxItemState::DEFAULT && pItem)
211  {
212  std::unique_ptr<SfxPoolItem> pNewItem(pItem->Clone());
213  bool const bPut = PutXMLValue(
214  *pNewItem, m_FoMarginValue, Ids[i][1], rUnitConverter);
215  if (bPut)
216  {
217  rSet.Put(std::move(pNewItem));
218  }
219  }
220  else
221  {
222  OSL_ENSURE(false, "could not get item");
223  }
224  }
225 }
226 
227 namespace {
228 
229 class SwXMLItemSetContext_Impl : public SvXMLItemSetContext
230 {
231  SvXMLImportContextRef xBackground;
232 
234 
235 public:
236  SwXMLItemSetContext_Impl( SwXMLImport& rImport, sal_uInt16 nPrfx,
237  const OUString& rLName,
238  const Reference< xml::sax::XAttributeList > & xAttrList,
239  SfxItemSet& rItemSet,
240  SvXMLImportItemMapper & rIMapper,
241  const SvXMLUnitConverter& rUnitConv );
242  virtual ~SwXMLItemSetContext_Impl() override;
243 
244  virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix,
245  const OUString& rLocalName,
246  const ::uno::Reference< xml::sax::XAttributeList > & xAttrList,
247  SfxItemSet& rItemSet,
248  const SvXMLItemMapEntry& rEntry,
249  const SvXMLUnitConverter& rUnitConv ) override;
250 };
251 
252 }
253 
254 SwXMLItemSetContext_Impl::SwXMLItemSetContext_Impl(
255  SwXMLImport& rImport, sal_uInt16 nPrfx,
256  const OUString& rLName,
257  const Reference< xml::sax::XAttributeList > & xAttrList,
258  SfxItemSet& _rItemSet,
259  SvXMLImportItemMapper & _rIMapper,
260  const SvXMLUnitConverter& _rUnitConv ) :
261  SvXMLItemSetContext( rImport, nPrfx, rLName, xAttrList,
262  _rItemSet, _rIMapper, _rUnitConv )
263 {
264 }
265 
266 SwXMLItemSetContext_Impl::~SwXMLItemSetContext_Impl()
267 {
268  if( xBackground.is() )
269  {
270  const SvxBrushItem& rItem =
271  static_cast<SwXMLBrushItemImportContext*>(xBackground.get())->GetItem();
272  rItemSet.Put( rItem );
273  }
274 }
275 
276 SvXMLImportContextRef SwXMLItemSetContext_Impl::CreateChildContext(
277  sal_uInt16 nPrefix,
278  const OUString& rLocalName,
279  const Reference< xml::sax::XAttributeList > & xAttrList,
280  SfxItemSet& _rItemSet,
281  const SvXMLItemMapEntry& rEntry,
282  const SvXMLUnitConverter& _rUnitConv )
283 {
284  SvXMLImportContextRef xContext;
285 
286  switch( rEntry.nWhichId )
287  {
288  case RES_BACKGROUND:
289  {
290  const SfxPoolItem *pItem;
291  if( SfxItemState::SET == _rItemSet.GetItemState( RES_BACKGROUND,
292  false, &pItem ) )
293  {
294  xContext = new SwXMLBrushItemImportContext(
295  GetImport(), nPrefix, rLocalName, xAttrList,
296  _rUnitConv, *static_cast<const SvxBrushItem *>(pItem) );
297  }
298  else
299  {
300  xContext = new SwXMLBrushItemImportContext(
301  GetImport(), nPrefix, rLocalName, xAttrList,
302  _rUnitConv, RES_BACKGROUND );
303  }
304  xBackground = xContext;
305  }
306  break;
307  }
308 
309  if (!xContext)
310  xContext = SvXMLItemSetContext::CreateChildContext( nPrefix, rLocalName,
311  xAttrList, _rItemSet,
312  rEntry, _rUnitConv );
313 
314  return xContext;
315 }
316 
318 {
320  util::MeasureUnit::TWIP, util::MeasureUnit::TWIP ) );
321 
326 
327  m_pTableItemMapper.reset( new SwXMLImportTableItemMapper_Impl( m_xTableItemMap ) );
328 }
329 
331 {
332  m_pTableItemMapper.reset();
333  m_pTwipUnitConv.reset();
334 }
335 
337  sal_uInt16 nPrefix,
338  const OUString& rLocalName,
339  const Reference< xml::sax::XAttributeList > & xAttrList,
340  XmlStyleFamily nFamily,
341  SfxItemSet& rItemSet )
342 {
343  SvXMLItemMapEntriesRef xItemMap;
344 
345  switch( nFamily )
346  {
347  case XmlStyleFamily::TABLE_TABLE:
348  xItemMap = m_xTableItemMap;
349  break;
350  case XmlStyleFamily::TABLE_COLUMN:
351  xItemMap = m_xTableColItemMap;
352  break;
353  case XmlStyleFamily::TABLE_ROW:
354  xItemMap = m_xTableRowItemMap;
355  break;
356  case XmlStyleFamily::TABLE_CELL:
357  xItemMap = m_xTableCellItemMap;
358  break;
359  default: break;
360  }
361 
362  m_pTableItemMapper->setMapEntries( xItemMap );
363 
364  return new SwXMLItemSetContext_Impl( *this, nPrefix, rLocalName,
365  xAttrList, rItemSet,
367  *m_pTwipUnitConv );
368 }
369 
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SvXMLUnitConverter > m_pTwipUnitConv
Definition: xmlimp.hxx:64
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
SvXMLItemMapEntry const aXMLTableItemMap[]
Definition: xmlitemm.cxx:43
constexpr TypedWhichId< SwFormatFrameSize > RES_FRM_SIZE(89)
sal_uInt16 nWhichId
Definition: xmlitmap.hxx:43
const SfxPoolItem * GetItem(const SwTextAttr &rAttr, sal_uInt16 nWhich)
Extracts pool item of type nWhich from rAttr.
Definition: atrstck.cxx:157
virtual bool handleNoItem(const SvXMLItemMapEntry &rEntry, SfxItemSet &rSet, const OUString &rValue, const SvXMLUnitConverter &rUnitConverter, const SvXMLNamespaceMap &rNamespaceMap)
this method is called for every item that has the MID_SW_FLAG_NO_ITEM_IMPORT flag set ...
Definition: xmlimpit.cxx:198
SvXMLItemMapEntriesRef m_xTableCellItemMap
Definition: xmlimp.hxx:77
SvXMLItemMapEntry const aXMLTableColItemMap[]
Definition: xmlitemm.cxx:123
const SvXMLImportItemMapper & GetTableItemMapper() const
Definition: xmlimp.hxx:183
SvXMLItemMapEntriesRef m_xTableRowItemMap
Definition: xmlimp.hxx:76
XmlStyleFamily
#define MID_SW_FLAG_MASK
Definition: xmlitmap.hxx:28
this class manages an array of SvXMLItemMapEntry.
Definition: xmlitmap.hxx:65
virtual void setMapEntries(SvXMLItemMapEntriesRef rMapEntries)
Definition: xmlimpit.cxx:72
SvXMLItemMapEntriesRef m_xTableItemMap
Definition: xmlimp.hxx:74
const sal_uInt32 RIGHT
#define MID_L_MARGIN
SvXMLImportContext * CreateTableItemImportContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, XmlStyleFamily nSubFamily, SfxItemSet &rItemSet)
Definition: xmlitemi.cxx:336
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
int i
std::unique_ptr< SvXMLImportItemMapper > m_pTableItemMapper
Definition: xmlimp.hxx:65
void FinitItemImport()
Definition: xmlitemi.cxx:330
enum::xmloff::token::XMLTokenEnum const eLocalName
Definition: xmlitmap.hxx:45
virtual void finished(SfxItemSet &rSet, SvXMLUnitConverter const &rUnitConverter) const
This method is called when all attributes have benn processed.
Definition: xmlimpit.cxx:209
#define MID_R_MARGIN
static bool PutXMLValue(SfxPoolItem &rItem, const OUString &rValue, sal_uInt16 nMemberId, const SvXMLUnitConverter &rUnitConverter)
This method is called for every item that should be set based upon an XML attribute value...
Definition: xmlimpit.cxx:242
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
SfxItemPool * GetPool() const
virtual bool handleSpecialItem(const SvXMLItemMapEntry &rEntry, SfxPoolItem &rItem, SfxItemSet &rSet, const OUString &rValue, const SvXMLUnitConverter &rUnitConverter)
this method is called for every item that has the MID_SW_FLAG_SPECIAL_ITEM_IMPORT flag set ...
Definition: xmlimpit.cxx:186
const sal_uInt32 BOTTOM
sal_uInt32 nMemberId
Definition: xmlitmap.hxx:48
const sal_uInt32 LEFT
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
SfxItemState
constexpr TypedWhichId< SvxBrushItem > RES_BACKGROUND(105)
css::uno::Reference< css::uno::XComponentContext > const & GetComponentContext() const
void InitItemImport()
Definition: xmlitemi.cxx:317
#define MID_LO_MARGIN
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(91)
sal_uInt16 nNameSpace
Definition: xmlitmap.hxx:41
SvXMLItemMapEntriesRef m_xTableColItemMap
Definition: xmlimp.hxx:75
SvXMLItemMapEntry const aXMLTableCellItemMap[]
Definition: xmlitemm.cxx:202
#define MID_FRMSIZE_COL_WIDTH
Definition: unomid.h:81
SvXMLItemMapEntry const aXMLTableRowItemMap[]
Definition: xmlitemm.cxx:130
constexpr TypedWhichId< SvxULSpaceItem > RES_UL_SPACE(92)
sal_uInt16 Which() const
static bool IsWhich(sal_uInt16 nId)
const sal_uInt32 TOP
#define MID_UP_MARGIN