1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustring.hxx>
21 #include <osl/diagnose.h>
23 #include <com/sun/star/util/MeasureUnit.hpp>
25 #include <xmloff/xmluconv.hxx>
26 #include <xmloff/families.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/xmltoken.hxx>
30 #include <editeng/memberids.h>
31 #include <svl/itemset.hxx>
32 #include <svl/itempool.hxx>
34 #include <hintids.hxx>
37 #include "xmlitmap.hxx"
38 #include "xmlimpit.hxx"
39 #include "xmlitem.hxx"
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::uno
;
46 class SwXMLImportTableItemMapper_Impl
: public SvXMLImportItemMapper
51 explicit SwXMLImportTableItemMapper_Impl(const SvXMLItemMapEntriesRef
& rMapEntries
);
53 virtual bool handleSpecialItem( const SvXMLItemMapEntry
& rEntry
,
56 const OUString
& rValue
,
57 const SvXMLUnitConverter
& rUnitConverter
) override
;
60 handleNoItem(SvXMLItemMapEntry
const& rEntry
,
62 OUString
const& rValue
,
63 SvXMLUnitConverter
const& rUnitConverter
,
64 SvXMLNamespaceMap
const& rNamespaceMap
) override
;
66 virtual void finished(SfxItemSet
& rSet
,
67 SvXMLUnitConverter
const& rUnitConverter
) const override
;
69 virtual void setMapEntries( SvXMLItemMapEntriesRef rMapEntries
) override
;
74 OUString m_FoMarginValue
;
75 enum { LEFT
= 0, RIGHT
= 1, TOP
= 2, BOTTOM
= 3 };
76 bool m_bHaveMargin
[4];
81 SwXMLImportTableItemMapper_Impl::SwXMLImportTableItemMapper_Impl(
82 const SvXMLItemMapEntriesRef
& rMapEntries
) :
83 SvXMLImportItemMapper( rMapEntries
)
88 void SwXMLImportTableItemMapper_Impl::Reset()
90 m_FoMarginValue
.clear();
91 for (int i
= 0; i
< 3; ++i
)
93 m_bHaveMargin
[i
] = false;
97 void SwXMLImportTableItemMapper_Impl::setMapEntries(
98 SvXMLItemMapEntriesRef rMapEntries
)
101 SvXMLImportItemMapper::setMapEntries(rMapEntries
);
104 bool SwXMLImportTableItemMapper_Impl::handleSpecialItem(
105 const SvXMLItemMapEntry
& rEntry
,
107 SfxItemSet
& rItemSet
,
108 const OUString
& rValue
,
109 const SvXMLUnitConverter
& rUnitConv
)
112 sal_uInt16 nMemberId
= static_cast< sal_Int16
>(rEntry
.nMemberId
& MID_SW_FLAG_MASK
);
113 switch( rItem
.Which() )
119 m_bHaveMargin
[LEFT
] = true;
122 m_bHaveMargin
[RIGHT
] = true;
125 bRet
= SvXMLImportItemMapper::PutXMLValue(
126 rItem
, rValue
, nMemberId
, rUnitConv
);
132 m_bHaveMargin
[TOP
] = true;
135 m_bHaveMargin
[BOTTOM
] = true;
138 bRet
= SvXMLImportItemMapper::PutXMLValue(
139 rItem
, rValue
, nMemberId
, rUnitConv
);
144 case MID_FRMSIZE_COL_WIDTH
:
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
,
149 bRet
= SvXMLImportItemMapper::PutXMLValue(
150 rItem
, rValue
, nMemberId
, rUnitConv
);
158 bool SwXMLImportTableItemMapper_Impl::handleNoItem(
159 SvXMLItemMapEntry
const& rEntry
,
161 OUString
const& rValue
,
162 SvXMLUnitConverter
const& rUnitConverter
,
163 SvXMLNamespaceMap
const& rNamespaceMap
)
165 if ((XML_NAMESPACE_FO
== rEntry
.nNameSpace
) &&
166 (xmloff::token::XML_MARGIN
== rEntry
.eLocalName
))
168 m_FoMarginValue
= rValue
;
173 return SvXMLImportItemMapper::handleNoItem(
174 rEntry
, rSet
, rValue
, rUnitConverter
, rNamespaceMap
);
178 void SwXMLImportTableItemMapper_Impl::finished(
179 SfxItemSet
& rSet
, SvXMLUnitConverter
const& rUnitConverter
) const
181 if (m_FoMarginValue
.isEmpty())
184 sal_uInt16
const Ids
[4][2] = {
185 { RES_LR_SPACE
, MID_L_MARGIN
},
186 { RES_LR_SPACE
, MID_R_MARGIN
},
187 { RES_UL_SPACE
, MID_UP_MARGIN
},
188 { RES_UL_SPACE
, MID_LO_MARGIN
},
190 for (int i
= 0; i
< 4; ++i
)
192 if (m_bHaveMargin
[i
])
194 continue; // already read fo:margin-top etc.
196 // first get item from itemset
197 SfxPoolItem
const* pItem
= nullptr;
198 SfxItemState eState
=
199 rSet
.GetItemState(Ids
[i
][0], true, &pItem
);
201 // if not set, try the pool
202 if ((SfxItemState::SET
!= eState
) && SfxItemPool::IsWhich(Ids
[i
][0]))
204 pItem
= &rSet
.GetPool()->GetDefaultItem(Ids
[i
][0]);
207 // do we have an item?
208 if (eState
>= SfxItemState::DEFAULT
&& pItem
)
210 std::unique_ptr
<SfxPoolItem
> pNewItem(pItem
->Clone());
211 bool const bPut
= PutXMLValue(
212 *pNewItem
, m_FoMarginValue
, Ids
[i
][1], rUnitConverter
);
215 rSet
.Put(std::move(pNewItem
));
220 OSL_ENSURE(false, "could not get item");
225 void SwXMLImport::InitItemImport()
227 m_pTwipUnitConv
.reset( new SvXMLUnitConverter( GetComponentContext(),
228 util::MeasureUnit::TWIP
, util::MeasureUnit::TWIP
,
229 SvtSaveOptions::ODFSVER_LATEST_EXTENDED
) );
231 m_xTableItemMap
= new SvXMLItemMapEntries( aXMLTableItemMap
);
232 m_xTableColItemMap
= new SvXMLItemMapEntries( aXMLTableColItemMap
);
233 m_xTableRowItemMap
= new SvXMLItemMapEntries( aXMLTableRowItemMap
);
234 m_xTableCellItemMap
= new SvXMLItemMapEntries( aXMLTableCellItemMap
);
236 m_pTableItemMapper
.reset( new SwXMLImportTableItemMapper_Impl( m_xTableItemMap
) );
239 void SwXMLImport::FinitItemImport()
241 m_pTableItemMapper
.reset();
242 m_pTwipUnitConv
.reset();
245 SvXMLImportContext
*SwXMLImport::CreateTableItemImportContext(
247 const Reference
< xml::sax::XFastAttributeList
> & xAttrList
,
248 XmlStyleFamily nFamily
,
249 SfxItemSet
& rItemSet
)
251 SvXMLItemMapEntriesRef xItemMap
;
255 case XmlStyleFamily::TABLE_TABLE
:
256 xItemMap
= m_xTableItemMap
;
258 case XmlStyleFamily::TABLE_COLUMN
:
259 xItemMap
= m_xTableColItemMap
;
261 case XmlStyleFamily::TABLE_ROW
:
262 xItemMap
= m_xTableRowItemMap
;
264 case XmlStyleFamily::TABLE_CELL
:
265 xItemMap
= m_xTableCellItemMap
;
270 m_pTableItemMapper
->setMapEntries( xItemMap
);
272 return new SwXMLItemSetContext( *this, nElement
,
274 GetTableItemMapper(),
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */