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/memberids.h>
31 #include <svl/itemset.hxx>
32 #include <svl/itempool.hxx>
33 
34 #include <hintids.hxx>
35 #include <unomid.h>
36 #include "xmlimp.hxx"
37 #include "xmlitmap.hxx"
38 #include "xmlimpit.hxx"
39 #include "xmlitem.hxx"
40 
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::uno;
43 
44 namespace {
45 
46 class SwXMLImportTableItemMapper_Impl: public SvXMLImportItemMapper
47 {
48 
49 public:
50 
51  explicit SwXMLImportTableItemMapper_Impl(const SvXMLItemMapEntriesRef& rMapEntries);
52 
53  virtual bool handleSpecialItem( const SvXMLItemMapEntry& rEntry,
54  SfxPoolItem& rItem,
55  SfxItemSet& rSet,
56  const OUString& rValue,
57  const SvXMLUnitConverter& rUnitConverter ) override;
58 
59  virtual bool
60  handleNoItem(SvXMLItemMapEntry const& rEntry,
61  SfxItemSet & rSet,
62  OUString const& rValue,
63  SvXMLUnitConverter const& rUnitConverter,
64  SvXMLNamespaceMap const& rNamespaceMap) override;
65 
66  virtual void finished(SfxItemSet & rSet,
67  SvXMLUnitConverter const& rUnitConverter) const override;
68 
69  virtual void setMapEntries( SvXMLItemMapEntriesRef rMapEntries ) override;
70 
71 private:
72  void Reset();
73 
74  OUString m_FoMarginValue;
75  enum { LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3 };
76  bool m_bHaveMargin[4];
77 };
78 
79 }
80 
81 SwXMLImportTableItemMapper_Impl::SwXMLImportTableItemMapper_Impl(
82  const SvXMLItemMapEntriesRef& rMapEntries ) :
83  SvXMLImportItemMapper( rMapEntries )
84 {
85  Reset();
86 }
87 
88 void SwXMLImportTableItemMapper_Impl::Reset()
89 {
90  m_FoMarginValue.clear();
91  for (int i = 0; i < 3; ++i)
92  {
93  m_bHaveMargin[i] = false;
94  }
95 }
96 
97 void SwXMLImportTableItemMapper_Impl::setMapEntries(
98  SvXMLItemMapEntriesRef rMapEntries )
99 {
100  Reset();
102 }
103 
104 bool SwXMLImportTableItemMapper_Impl::handleSpecialItem(
105  const SvXMLItemMapEntry& rEntry,
106  SfxPoolItem& rItem,
107  SfxItemSet& rItemSet,
108  const OUString& rValue,
109  const SvXMLUnitConverter& rUnitConv )
110 {
111  bool bRet = false;
112  sal_uInt16 nMemberId = static_cast< sal_Int16 >(rEntry.nMemberId & MID_SW_FLAG_MASK);
113  switch( rItem.Which() )
114  {
115  case RES_LR_SPACE:
116  switch (nMemberId)
117  {
118  case MID_L_MARGIN:
119  m_bHaveMargin[LEFT] = true;
120  break;
121  case MID_R_MARGIN:
122  m_bHaveMargin[RIGHT] = true;
123  break;
124  }
126  rItem, rValue, nMemberId, rUnitConv);
127  break;
128  case RES_UL_SPACE:
129  switch (nMemberId)
130  {
131  case MID_UP_MARGIN:
132  m_bHaveMargin[TOP] = true;
133  break;
134  case MID_LO_MARGIN:
135  m_bHaveMargin[BOTTOM] = true;
136  break;
137  }
139  rItem, rValue, nMemberId, rUnitConv);
140  break;
141  case RES_FRM_SIZE:
142  switch( nMemberId )
143  {
145  // If the item is existing already, a relative value has been set
146  // already that must be preserved.
147  if( SfxItemState::SET != rItemSet.GetItemState( RES_FRM_SIZE,
148  false ) )
150  rItem, rValue, nMemberId, rUnitConv );
151  break;
152  }
153  }
154 
155  return bRet;
156 }
157 
158 bool SwXMLImportTableItemMapper_Impl::handleNoItem(
159  SvXMLItemMapEntry const& rEntry,
160  SfxItemSet & rSet,
161  OUString const& rValue,
162  SvXMLUnitConverter const& rUnitConverter,
163  SvXMLNamespaceMap const& rNamespaceMap)
164 {
165  if ((XML_NAMESPACE_FO == rEntry.nNameSpace) &&
167  {
168  m_FoMarginValue = rValue;
169  return true;
170  }
171  else
172  {
174  rEntry, rSet, rValue, rUnitConverter, rNamespaceMap);
175  }
176 }
177 
178 void SwXMLImportTableItemMapper_Impl::finished(
179  SfxItemSet & rSet, SvXMLUnitConverter const& rUnitConverter) const
180 {
181  if (m_FoMarginValue.isEmpty())
182  return;
183 
184  sal_uInt16 const Ids[4][2] = {
189  };
190  for (int i = 0; i < 4; ++i)
191  {
192  if (m_bHaveMargin[i])
193  {
194  continue; // already read fo:margin-top etc.
195  }
196  // first get item from itemset
197  SfxPoolItem const* pItem = nullptr;
198  SfxItemState eState =
199  rSet.GetItemState(Ids[i][0], true, &pItem);
200 
201  // if not set, try the pool
202  if ((SfxItemState::SET != eState) && SfxItemPool::IsWhich(Ids[i][0]))
203  {
204  pItem = &rSet.GetPool()->GetDefaultItem(Ids[i][0]);
205  }
206 
207  // do we have an item?
208  if (eState >= SfxItemState::DEFAULT && pItem)
209  {
210  std::unique_ptr<SfxPoolItem> pNewItem(pItem->Clone());
211  bool const bPut = PutXMLValue(
212  *pNewItem, m_FoMarginValue, Ids[i][1], rUnitConverter);
213  if (bPut)
214  {
215  rSet.Put(std::move(pNewItem));
216  }
217  }
218  else
219  {
220  OSL_ENSURE(false, "could not get item");
221  }
222  }
223 }
224 
226 {
228  util::MeasureUnit::TWIP, util::MeasureUnit::TWIP ) );
229 
234 
235  m_pTableItemMapper.reset( new SwXMLImportTableItemMapper_Impl( m_xTableItemMap ) );
236 }
237 
239 {
240  m_pTableItemMapper.reset();
241  m_pTwipUnitConv.reset();
242 }
243 
245  sal_Int32 nElement,
246  const Reference< xml::sax::XFastAttributeList > & xAttrList,
247  XmlStyleFamily nFamily,
248  SfxItemSet& rItemSet )
249 {
250  SvXMLItemMapEntriesRef xItemMap;
251 
252  switch( nFamily )
253  {
254  case XmlStyleFamily::TABLE_TABLE:
255  xItemMap = m_xTableItemMap;
256  break;
257  case XmlStyleFamily::TABLE_COLUMN:
258  xItemMap = m_xTableColItemMap;
259  break;
260  case XmlStyleFamily::TABLE_ROW:
261  xItemMap = m_xTableRowItemMap;
262  break;
263  case XmlStyleFamily::TABLE_CELL:
264  xItemMap = m_xTableCellItemMap;
265  break;
266  default: break;
267  }
268 
269  m_pTableItemMapper->setMapEntries( xItemMap );
270 
271  return new SwXMLItemSetContext( *this, nElement,
272  xAttrList, rItemSet,
274  *m_pTwipUnitConv );
275 }
276 
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SvXMLUnitConverter > m_pTwipUnitConv
Definition: xmlimp.hxx:64
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
SvXMLItemMapEntry const aXMLTableItemMap[]
Definition: xmlitemm.cxx:42
constexpr TypedWhichId< SwFormatFrameSize > RES_FRM_SIZE(89)
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:266
SvXMLItemMapEntriesRef m_xTableCellItemMap
Definition: xmlimp.hxx:74
SvXMLItemMapEntry const aXMLTableColItemMap[]
Definition: xmlitemm.cxx:122
const SvXMLImportItemMapper & GetTableItemMapper() const
Definition: xmlimp.hxx:175
SvXMLItemMapEntriesRef m_xTableRowItemMap
Definition: xmlimp.hxx:73
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:75
SvXMLItemMapEntriesRef m_xTableItemMap
Definition: xmlimp.hxx:71
const sal_uInt32 RIGHT
#define MID_L_MARGIN
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:238
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:277
#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:310
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:254
const sal_uInt32 BOTTOM
sal_uInt32 nMemberId
Definition: xmlitmap.hxx:48
SvXMLImportContext * CreateTableItemImportContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, XmlStyleFamily nSubFamily, SfxItemSet &rItemSet)
Definition: xmlitemi.cxx:244
const sal_uInt32 LEFT
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
SfxItemState
css::uno::Reference< css::uno::XComponentContext > const & GetComponentContext() const
void InitItemImport()
Definition: xmlitemi.cxx:225
#define MID_LO_MARGIN
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(91)
sal_uInt16 nNameSpace
Definition: xmlitmap.hxx:41
SvXMLItemMapEntriesRef m_xTableColItemMap
Definition: xmlimp.hxx:72
SvXMLItemMapEntry const aXMLTableCellItemMap[]
Definition: xmlitemm.cxx:201
#define MID_FRMSIZE_COL_WIDTH
Definition: unomid.h:81
SvXMLItemMapEntry const aXMLTableRowItemMap[]
Definition: xmlitemm.cxx:129
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