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>
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
41using namespace ::com::sun::star;
42using namespace ::com::sun::star::uno;
43
44namespace {
45
46class SwXMLImportTableItemMapper_Impl: public SvXMLImportItemMapper
47{
48
49public:
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
71private:
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
81SwXMLImportTableItemMapper_Impl::SwXMLImportTableItemMapper_Impl(
82 const SvXMLItemMapEntriesRef& rMapEntries ) :
83 SvXMLImportItemMapper( rMapEntries )
84{
85 Reset();
86}
87
88void 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
97void SwXMLImportTableItemMapper_Impl::setMapEntries(
98 SvXMLItemMapEntriesRef rMapEntries )
99{
100 Reset();
102}
103
104bool 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
158bool 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
178void 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{
227 m_pTwipUnitConv.reset( new SvXMLUnitConverter( GetComponentContext(),
228 util::MeasureUnit::TWIP, util::MeasureUnit::TWIP,
230
235
236 m_pTableItemMapper.reset( new SwXMLImportTableItemMapper_Impl( m_xTableItemMap ) );
237}
238
240{
241 m_pTableItemMapper.reset();
242 m_pTwipUnitConv.reset();
243}
244
246 sal_Int32 nElement,
247 const Reference< xml::sax::XFastAttributeList > & xAttrList,
248 XmlStyleFamily nFamily,
249 SfxItemSet& rItemSet )
250{
251 SvXMLItemMapEntriesRef xItemMap;
252
253 switch( nFamily )
254 {
255 case XmlStyleFamily::TABLE_TABLE:
256 xItemMap = m_xTableItemMap;
257 break;
258 case XmlStyleFamily::TABLE_COLUMN:
259 xItemMap = m_xTableColItemMap;
260 break;
261 case XmlStyleFamily::TABLE_ROW:
262 xItemMap = m_xTableRowItemMap;
263 break;
264 case XmlStyleFamily::TABLE_CELL:
265 xItemMap = m_xTableCellItemMap;
266 break;
267 default: break;
268 }
269
270 m_pTableItemMapper->setMapEntries( xItemMap );
271
272 return new SwXMLItemSetContext( *this, nElement,
273 xAttrList, rItemSet,
276}
277
278/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
static bool IsWhich(sal_uInt16 nId)
SfxItemPool * GetPool() const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
sal_uInt16 Which() const
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const=0
virtual void setMapEntries(SvXMLItemMapEntriesRef rMapEntries)
Definition: xmlimpit.cxx:75
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
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
virtual void finished(SfxItemSet &rSet, SvXMLUnitConverter const &rUnitConverter) const
This method is called when all attributes have benn processed.
Definition: xmlimpit.cxx:277
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
this class manages an array of SvXMLItemMapEntry.
Definition: xmlitmap.hxx:65
SvXMLItemMapEntriesRef m_xTableItemMap
Definition: xmlimp.hxx:68
SvXMLItemMapEntriesRef m_xTableRowItemMap
Definition: xmlimp.hxx:70
const SvXMLImportItemMapper & GetTableItemMapper() const
Definition: xmlimp.hxx:175
void InitItemImport()
Definition: xmlitemi.cxx:225
SvXMLImportContext * CreateTableItemImportContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, XmlStyleFamily nSubFamily, SfxItemSet &rItemSet)
Definition: xmlitemi.cxx:245
std::unique_ptr< SvXMLUnitConverter > m_pTwipUnitConv
Definition: xmlimp.hxx:61
std::unique_ptr< SvXMLImportItemMapper > m_pTableItemMapper
Definition: xmlimp.hxx:62
SvXMLItemMapEntriesRef m_xTableCellItemMap
Definition: xmlimp.hxx:71
void FinitItemImport()
Definition: xmlitemi.cxx:239
SvXMLItemMapEntriesRef m_xTableColItemMap
Definition: xmlimp.hxx:69
XmlStyleFamily
constexpr TypedWhichId< SwFormatFrameSize > RES_FRM_SIZE(89)
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(97)
constexpr TypedWhichId< SvxULSpaceItem > RES_UL_SPACE(98)
#define MID_LO_MARGIN
#define MID_R_MARGIN
#define MID_L_MARGIN
#define MID_UP_MARGIN
const sal_uInt32 LEFT
const sal_uInt32 TOP
const sal_uInt32 BOTTOM
const sal_uInt32 RIGHT
int i
SfxItemState
static SfxItemSet & rSet
enum::xmloff::token::XMLTokenEnum const eLocalName
Definition: xmlitmap.hxx:46
sal_uInt16 nNameSpace
Definition: xmlitmap.hxx:42
sal_uInt32 nMemberId
Definition: xmlitmap.hxx:49
#define MID_FRMSIZE_COL_WIDTH
Definition: unomid.h:81
const o3tl::span< SvXMLItemMapEntry const > aXMLTableCellItemMap(aTableCellItemMap)
const o3tl::span< SvXMLItemMapEntry const > aXMLTableColItemMap(aTableColItemMap)
const o3tl::span< SvXMLItemMapEntry const > aXMLTableItemMap(aTableItemMap)
const o3tl::span< SvXMLItemMapEntry const > aXMLTableRowItemMap(aTableRowItemMap)
#define MID_SW_FLAG_MASK
Definition: xmlitmap.hxx:29