Line data Source code
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 :
22 : #include <com/sun/star/util/MeasureUnit.hpp>
23 :
24 : #include <rsc/rscsfx.hxx>
25 :
26 : #include <xmloff/i18nmap.hxx>
27 : #include <xmloff/xmluconv.hxx>
28 : #include <xmloff/families.hxx>
29 : #include <xmloff/xmlnmspe.hxx>
30 : #include <xmloff/xmltoken.hxx>
31 :
32 : #include <editeng/boxitem.hxx>
33 : #include <editeng/fontitem.hxx>
34 : #include <editeng/tstpitem.hxx>
35 : #include <editeng/brushitem.hxx>
36 : #include <editeng/langitem.hxx>
37 : #include <editeng/memberids.hrc>
38 :
39 : #include <svx/unomid.hxx>
40 : #include <comphelper/processfactory.hxx>
41 :
42 : #include <hintids.hxx>
43 : #include <paratr.hxx>
44 : #include <doc.hxx>
45 : #include <unomid.h>
46 : #include "xmlbrshi.hxx"
47 : #include "xmlimp.hxx"
48 : #include "xmlitmap.hxx"
49 : #include "xmlimpit.hxx"
50 : #include "xmlitem.hxx"
51 :
52 : using namespace ::com::sun::star;
53 : using namespace ::com::sun::star::uno;
54 :
55 : extern SvXMLItemMapEntry aXMLTableItemMap[];
56 : extern SvXMLItemMapEntry aXMLTableColItemMap[];
57 : extern SvXMLItemMapEntry aXMLTableRowItemMap[];
58 : extern SvXMLItemMapEntry aXMLTableCellItemMap[];
59 :
60 : class SwXMLImportTableItemMapper_Impl: public SvXMLImportItemMapper
61 : {
62 :
63 : public:
64 :
65 : SwXMLImportTableItemMapper_Impl( SvXMLItemMapEntriesRef rMapEntries );
66 : virtual ~SwXMLImportTableItemMapper_Impl();
67 :
68 : virtual bool handleSpecialItem( const SvXMLItemMapEntry& rEntry,
69 : SfxPoolItem& rItem,
70 : SfxItemSet& rSet,
71 : const OUString& rValue,
72 : const SvXMLUnitConverter& rUnitConverter,
73 : const SvXMLNamespaceMap& rNamespaceMap ) SAL_OVERRIDE;
74 :
75 : virtual bool
76 : handleNoItem(SvXMLItemMapEntry const& rEntry,
77 : SfxItemSet & rSet,
78 : OUString const& rValue,
79 : SvXMLUnitConverter const& rUnitConverter,
80 : SvXMLNamespaceMap const& rNamespaceMap) SAL_OVERRIDE;
81 :
82 : virtual void finished(SfxItemSet & rSet,
83 : SvXMLUnitConverter const& rUnitConverter) const SAL_OVERRIDE;
84 :
85 : virtual void setMapEntries( SvXMLItemMapEntriesRef rMapEntries ) SAL_OVERRIDE;
86 :
87 : private:
88 : void Reset();
89 :
90 : OUString m_FoMarginValue;
91 : enum { LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3 };
92 : bool m_bHaveMargin[4];
93 : };
94 :
95 499 : SwXMLImportTableItemMapper_Impl::SwXMLImportTableItemMapper_Impl(
96 : SvXMLItemMapEntriesRef rMapEntries ) :
97 499 : SvXMLImportItemMapper( rMapEntries, RES_UNKNOWNATR_CONTAINER)
98 : {
99 499 : Reset();
100 499 : }
101 :
102 998 : SwXMLImportTableItemMapper_Impl::~SwXMLImportTableItemMapper_Impl()
103 : {
104 998 : }
105 :
106 981 : void SwXMLImportTableItemMapper_Impl::Reset()
107 : {
108 981 : m_FoMarginValue = OUString();
109 3924 : for (int i = 0; i < 3; ++i)
110 : {
111 2943 : m_bHaveMargin[i] = false;
112 : }
113 981 : }
114 :
115 482 : void SwXMLImportTableItemMapper_Impl::setMapEntries(
116 : SvXMLItemMapEntriesRef rMapEntries )
117 : {
118 482 : Reset();
119 482 : SvXMLImportItemMapper::setMapEntries(rMapEntries);
120 482 : }
121 :
122 142 : bool SwXMLImportTableItemMapper_Impl::handleSpecialItem(
123 : const SvXMLItemMapEntry& rEntry,
124 : SfxPoolItem& rItem,
125 : SfxItemSet& rItemSet,
126 : const OUString& rValue,
127 : const SvXMLUnitConverter& rUnitConv,
128 : const SvXMLNamespaceMap& )
129 : {
130 142 : bool bRet = false;
131 142 : sal_uInt16 nMemberId = static_cast< sal_Int16 >(rEntry.nMemberId & MID_SW_FLAG_MASK);
132 142 : switch( rItem.Which() )
133 : {
134 : case RES_LR_SPACE:
135 19 : switch (nMemberId)
136 : {
137 : case MID_L_MARGIN:
138 18 : m_bHaveMargin[LEFT] = true;
139 18 : break;
140 : case MID_R_MARGIN:
141 1 : m_bHaveMargin[RIGHT] = true;
142 1 : break;
143 : }
144 : bRet = SvXMLImportItemMapper::PutXMLValue(
145 19 : rItem, rValue, nMemberId, rUnitConv);
146 19 : break;
147 : case RES_UL_SPACE:
148 0 : switch (nMemberId)
149 : {
150 : case MID_UP_MARGIN:
151 0 : m_bHaveMargin[TOP] = true;
152 0 : break;
153 : case MID_LO_MARGIN:
154 0 : m_bHaveMargin[BOTTOM] = true;
155 0 : break;
156 : }
157 : bRet = SvXMLImportItemMapper::PutXMLValue(
158 0 : rItem, rValue, nMemberId, rUnitConv);
159 0 : break;
160 : case RES_FRM_SIZE:
161 123 : switch( nMemberId )
162 : {
163 : case MID_FRMSIZE_COL_WIDTH:
164 : // If the item is existing already, a relative value has been set
165 : // already that must be preserved.
166 123 : if( SFX_ITEM_SET != rItemSet.GetItemState( RES_FRM_SIZE,
167 123 : false ) )
168 : bRet = SvXMLImportItemMapper::PutXMLValue(
169 123 : rItem, rValue, nMemberId, rUnitConv );
170 123 : break;
171 : }
172 : }
173 :
174 142 : return bRet;
175 : }
176 :
177 0 : bool SwXMLImportTableItemMapper_Impl::handleNoItem(
178 : SvXMLItemMapEntry const& rEntry,
179 : SfxItemSet & rSet,
180 : OUString const& rValue,
181 : SvXMLUnitConverter const& rUnitConverter,
182 : SvXMLNamespaceMap const& rNamespaceMap)
183 : {
184 0 : if ((XML_NAMESPACE_FO == rEntry.nNameSpace) &&
185 0 : (xmloff::token::XML_MARGIN == rEntry.eLocalName))
186 : {
187 0 : m_FoMarginValue = rValue;
188 0 : return true;
189 : }
190 : else
191 : {
192 : return SvXMLImportItemMapper::handleNoItem(
193 0 : rEntry, rSet, rValue, rUnitConverter, rNamespaceMap);
194 : }
195 : }
196 :
197 482 : void SwXMLImportTableItemMapper_Impl::finished(
198 : SfxItemSet & rSet, SvXMLUnitConverter const& rUnitConverter) const
199 : {
200 482 : if (!m_FoMarginValue.isEmpty())
201 : {
202 : sal_uInt16 const Ids[4][2] = {
203 : { RES_LR_SPACE, MID_L_MARGIN },
204 : { RES_LR_SPACE, MID_R_MARGIN },
205 : { RES_UL_SPACE, MID_UP_MARGIN },
206 : { RES_UL_SPACE, MID_LO_MARGIN },
207 0 : };
208 0 : for (int i = 0; i < 4; ++i)
209 : {
210 0 : if (m_bHaveMargin[i])
211 : {
212 0 : continue; // already read fo:margin-top etc.
213 : }
214 : // first get item from itemset
215 0 : SfxPoolItem const* pItem = 0;
216 : SfxItemState eState =
217 0 : rSet.GetItemState(Ids[i][0], true, &pItem);
218 :
219 : // if not set, try the pool
220 0 : if ((SFX_ITEM_SET != eState) && (SFX_WHICH_MAX > Ids[i][0]))
221 : {
222 0 : pItem = &rSet.GetPool()->GetDefaultItem(Ids[i][0]);
223 : }
224 :
225 : // do we have an item?
226 0 : if (eState >= SFX_ITEM_DEFAULT && pItem)
227 : {
228 0 : SfxPoolItem *const pNewItem = pItem->Clone();
229 : bool const bPut = PutXMLValue(
230 0 : *pNewItem, m_FoMarginValue, Ids[i][1], rUnitConverter);
231 0 : if (bPut)
232 : {
233 0 : rSet.Put(*pNewItem);
234 : }
235 : }
236 : else
237 : {
238 : OSL_ENSURE(false, "could not get item");
239 : }
240 : }
241 : }
242 482 : }
243 :
244 : class SwXMLItemSetContext_Impl : public SvXMLItemSetContext
245 : {
246 : SvXMLImportContextRef xBackground;
247 :
248 : using SvXMLItemSetContext::CreateChildContext;
249 :
250 : public:
251 : SwXMLItemSetContext_Impl( SwXMLImport& rImport, sal_uInt16 nPrfx,
252 : const OUString& rLName,
253 : const Reference< xml::sax::XAttributeList > & xAttrList,
254 : SfxItemSet& rItemSet,
255 : SvXMLImportItemMapper & rIMapper,
256 : const SvXMLUnitConverter& rUnitConv );
257 : virtual ~SwXMLItemSetContext_Impl();
258 :
259 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
260 : const OUString& rLocalName,
261 : const ::uno::Reference< xml::sax::XAttributeList > & xAttrList,
262 : SfxItemSet& rItemSet,
263 : const SvXMLItemMapEntry& rEntry,
264 : const SvXMLUnitConverter& rUnitConv ) SAL_OVERRIDE;
265 : };
266 :
267 482 : SwXMLItemSetContext_Impl::SwXMLItemSetContext_Impl(
268 : SwXMLImport& rImport, sal_uInt16 nPrfx,
269 : const OUString& rLName,
270 : const Reference< xml::sax::XAttributeList > & xAttrList,
271 : SfxItemSet& _rItemSet,
272 : SvXMLImportItemMapper & _rIMapper,
273 : const SvXMLUnitConverter& _rUnitConv ) :
274 : SvXMLItemSetContext( rImport, nPrfx, rLName, xAttrList,
275 482 : _rItemSet, _rIMapper, _rUnitConv )
276 : {
277 482 : }
278 :
279 1446 : SwXMLItemSetContext_Impl::~SwXMLItemSetContext_Impl()
280 : {
281 482 : if( xBackground.Is() )
282 : {
283 : const SvxBrushItem& rItem =
284 17 : ((SwXMLBrushItemImportContext*)&xBackground)->GetItem();
285 17 : rItemSet.Put( rItem );
286 : }
287 964 : }
288 :
289 17 : SvXMLImportContext *SwXMLItemSetContext_Impl::CreateChildContext(
290 : sal_uInt16 nPrefix,
291 : const OUString& rLocalName,
292 : const Reference< xml::sax::XAttributeList > & xAttrList,
293 : SfxItemSet& _rItemSet,
294 : const SvXMLItemMapEntry& rEntry,
295 : const SvXMLUnitConverter& _rUnitConv )
296 : {
297 17 : SvXMLImportContext *pContext = 0;
298 :
299 17 : switch( rEntry.nWhichId )
300 : {
301 : case RES_BACKGROUND:
302 : {
303 : const SfxPoolItem *pItem;
304 17 : if( SFX_ITEM_SET == _rItemSet.GetItemState( RES_BACKGROUND,
305 17 : false, &pItem ) )
306 : {
307 : pContext = new SwXMLBrushItemImportContext(
308 17 : GetImport(), nPrefix, rLocalName, xAttrList,
309 17 : _rUnitConv, *(const SvxBrushItem *)pItem );
310 : }
311 : else
312 : {
313 : pContext = new SwXMLBrushItemImportContext(
314 0 : GetImport(), nPrefix, rLocalName, xAttrList,
315 0 : _rUnitConv, RES_BACKGROUND );
316 : }
317 17 : xBackground = pContext;
318 : }
319 17 : break;
320 : }
321 :
322 17 : if( !pContext )
323 : pContext = SvXMLItemSetContext::CreateChildContext( nPrefix, rLocalName,
324 : xAttrList, _rItemSet,
325 0 : rEntry, _rUnitConv );
326 :
327 17 : return pContext;
328 : }
329 :
330 499 : void SwXMLImport::_InitItemImport()
331 : {
332 : pTwipUnitConv = new SvXMLUnitConverter( GetComponentContext(),
333 499 : util::MeasureUnit::TWIP, util::MeasureUnit::TWIP );
334 :
335 499 : xTableItemMap = new SvXMLItemMapEntries( aXMLTableItemMap );
336 499 : xTableColItemMap = new SvXMLItemMapEntries( aXMLTableColItemMap );
337 499 : xTableRowItemMap = new SvXMLItemMapEntries( aXMLTableRowItemMap );
338 499 : xTableCellItemMap = new SvXMLItemMapEntries( aXMLTableCellItemMap );
339 :
340 499 : pTableItemMapper = new SwXMLImportTableItemMapper_Impl( xTableItemMap );
341 499 : }
342 :
343 499 : void SwXMLImport::_FinitItemImport()
344 : {
345 499 : delete pTableItemMapper;
346 499 : delete pTwipUnitConv;
347 499 : }
348 :
349 482 : SvXMLImportContext *SwXMLImport::CreateTableItemImportContext(
350 : sal_uInt16 nPrefix,
351 : const OUString& rLocalName,
352 : const Reference< xml::sax::XAttributeList > & xAttrList,
353 : sal_uInt16 nFamily,
354 : SfxItemSet& rItemSet )
355 : {
356 482 : SvXMLItemMapEntriesRef xItemMap;
357 :
358 482 : switch( nFamily )
359 : {
360 : case XML_STYLE_FAMILY_TABLE_TABLE:
361 41 : xItemMap = xTableItemMap;
362 41 : break;
363 : case XML_STYLE_FAMILY_TABLE_COLUMN:
364 123 : xItemMap = xTableColItemMap;
365 123 : break;
366 : case XML_STYLE_FAMILY_TABLE_ROW:
367 49 : xItemMap = xTableRowItemMap;
368 49 : break;
369 : case XML_STYLE_FAMILY_TABLE_CELL:
370 269 : xItemMap = xTableCellItemMap;
371 269 : break;
372 : }
373 :
374 482 : pTableItemMapper->setMapEntries( xItemMap );
375 :
376 : return new SwXMLItemSetContext_Impl( *this, nPrefix, rLocalName,
377 : xAttrList, rItemSet,
378 : GetTableItemMapper(),
379 482 : GetTwipUnitConverter() );
380 : }
381 :
382 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|