android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / filter / xml / xmlitemi.cxx
blob9281604c4fd91cd4ad5beafa711a2ea8c22af93b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
35 #include <unomid.h>
36 #include "xmlimp.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;
44 namespace {
46 class SwXMLImportTableItemMapper_Impl: public SvXMLImportItemMapper
49 public:
51 explicit SwXMLImportTableItemMapper_Impl(const SvXMLItemMapEntriesRef& rMapEntries);
53 virtual bool handleSpecialItem( const SvXMLItemMapEntry& rEntry,
54 SfxPoolItem& rItem,
55 SfxItemSet& rSet,
56 const OUString& rValue,
57 const SvXMLUnitConverter& rUnitConverter ) override;
59 virtual bool
60 handleNoItem(SvXMLItemMapEntry const& rEntry,
61 SfxItemSet & rSet,
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;
71 private:
72 void Reset();
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 )
85 Reset();
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 )
100 Reset();
101 SvXMLImportItemMapper::setMapEntries(rMapEntries);
104 bool SwXMLImportTableItemMapper_Impl::handleSpecialItem(
105 const SvXMLItemMapEntry& rEntry,
106 SfxPoolItem& rItem,
107 SfxItemSet& rItemSet,
108 const OUString& rValue,
109 const SvXMLUnitConverter& rUnitConv )
111 bool bRet = false;
112 sal_uInt16 nMemberId = static_cast< sal_Int16 >(rEntry.nMemberId & MID_SW_FLAG_MASK);
113 switch( rItem.Which() )
115 case RES_LR_SPACE:
116 switch (nMemberId)
118 case MID_L_MARGIN:
119 m_bHaveMargin[LEFT] = true;
120 break;
121 case MID_R_MARGIN:
122 m_bHaveMargin[RIGHT] = true;
123 break;
125 bRet = SvXMLImportItemMapper::PutXMLValue(
126 rItem, rValue, nMemberId, rUnitConv);
127 break;
128 case RES_UL_SPACE:
129 switch (nMemberId)
131 case MID_UP_MARGIN:
132 m_bHaveMargin[TOP] = true;
133 break;
134 case MID_LO_MARGIN:
135 m_bHaveMargin[BOTTOM] = true;
136 break;
138 bRet = SvXMLImportItemMapper::PutXMLValue(
139 rItem, rValue, nMemberId, rUnitConv);
140 break;
141 case RES_FRM_SIZE:
142 switch( nMemberId )
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,
148 false ) )
149 bRet = SvXMLImportItemMapper::PutXMLValue(
150 rItem, rValue, nMemberId, rUnitConv );
151 break;
155 return bRet;
158 bool SwXMLImportTableItemMapper_Impl::handleNoItem(
159 SvXMLItemMapEntry const& rEntry,
160 SfxItemSet & rSet,
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;
169 return true;
171 else
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())
182 return;
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);
213 if (bPut)
215 rSet.Put(std::move(pNewItem));
218 else
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(
246 sal_Int32 nElement,
247 const Reference< xml::sax::XFastAttributeList > & xAttrList,
248 XmlStyleFamily nFamily,
249 SfxItemSet& rItemSet )
251 SvXMLItemMapEntriesRef xItemMap;
253 switch( nFamily )
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;
270 m_pTableItemMapper->setMapEntries( xItemMap );
272 return new SwXMLItemSetContext( *this, nElement,
273 xAttrList, rItemSet,
274 GetTableItemMapper(),
275 *m_pTwipUnitConv );
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */