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 
228 {
230  util::MeasureUnit::TWIP, util::MeasureUnit::TWIP ) );
231 
236 
237  m_pTableItemMapper.reset( new SwXMLImportTableItemMapper_Impl( m_xTableItemMap ) );
238 }
239 
241 {
242  m_pTableItemMapper.reset();
243  m_pTwipUnitConv.reset();
244 }
245 
247  sal_Int32 nElement,
248  const Reference< xml::sax::XFastAttributeList > & xAttrList,
249  XmlStyleFamily nFamily,
250  SfxItemSet& rItemSet )
251 {
252  SvXMLItemMapEntriesRef xItemMap;
253 
254  switch( nFamily )
255  {
256  case XmlStyleFamily::TABLE_TABLE:
257  xItemMap = m_xTableItemMap;
258  break;
259  case XmlStyleFamily::TABLE_COLUMN:
260  xItemMap = m_xTableColItemMap;
261  break;
262  case XmlStyleFamily::TABLE_ROW:
263  xItemMap = m_xTableRowItemMap;
264  break;
265  case XmlStyleFamily::TABLE_CELL:
266  xItemMap = m_xTableCellItemMap;
267  break;
268  default: break;
269  }
270 
271  m_pTableItemMapper->setMapEntries( xItemMap );
272 
273  return new SwXMLItemSetContext( *this, nElement,
274  xAttrList, rItemSet,
276  *m_pTwipUnitConv );
277 }
278 
279 /* 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:43
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:76
SvXMLItemMapEntry const aXMLTableColItemMap[]
Definition: xmlitemm.cxx:123
const SvXMLImportItemMapper & GetTableItemMapper() const
Definition: xmlimp.hxx:180
SvXMLItemMapEntriesRef m_xTableRowItemMap
Definition: xmlimp.hxx:75
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:73
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:240
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:246
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:227
#define MID_LO_MARGIN
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(91)
sal_uInt16 nNameSpace
Definition: xmlitmap.hxx:41
SvXMLItemMapEntriesRef m_xTableColItemMap
Definition: xmlimp.hxx:74
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