xref: /aoo42x/main/sw/source/filter/xml/xmlitemi.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <rtl/ustring.hxx>
28 
29 #include <rsc/rscsfx.hxx>
30 
31 #include <xmloff/i18nmap.hxx>
32 #include <xmloff/xmluconv.hxx>
33 #include <xmloff/families.hxx>
34 #include <xmloff/xmlnmspe.hxx>
35 #include <xmloff/xmltoken.hxx>
36 
37 #include <editeng/boxitem.hxx>
38 #include <editeng/fontitem.hxx>
39 #include <editeng/tstpitem.hxx>
40 #include <editeng/boxitem.hxx>
41 #include <editeng/brshitem.hxx>
42 #include <editeng/langitem.hxx>
43 #include <editeng/memberids.hrc>
44 
45 #include <svx/unomid.hxx>
46 
47 #include <hintids.hxx>
48 #include <paratr.hxx>
49 #include <doc.hxx>
50 #include <unomid.h>
51 #include "xmlbrshi.hxx"
52 #include "xmlimp.hxx"
53 #include "xmlitmap.hxx"
54 #include "xmlimpit.hxx"
55 #include "xmlitem.hxx"
56 
57 using ::rtl::OUString;
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
60 
61 extern SvXMLItemMapEntry aXMLTableItemMap[];
62 extern SvXMLItemMapEntry aXMLTableColItemMap[];
63 extern SvXMLItemMapEntry aXMLTableRowItemMap[];
64 extern SvXMLItemMapEntry aXMLTableCellItemMap[];
65 
66 class SwXMLImportTableItemMapper_Impl: public SvXMLImportItemMapper
67 {
68 
69 public:
70 
71 	SwXMLImportTableItemMapper_Impl( SvXMLItemMapEntriesRef rMapEntries );
72 	virtual ~SwXMLImportTableItemMapper_Impl();
73 
74 	virtual sal_Bool handleSpecialItem( const SvXMLItemMapEntry& rEntry,
75 								SfxPoolItem& rItem,
76 								SfxItemSet& rSet,
77 								const OUString& rValue,
78 								const SvXMLUnitConverter& rUnitConverter,
79                                 const SvXMLNamespaceMap& rNamespaceMap );
80 
81     virtual sal_Bool
82     handleNoItem(SvXMLItemMapEntry const& rEntry,
83                  SfxItemSet & rSet,
84                  ::rtl::OUString const& rValue,
85                  SvXMLUnitConverter const& rUnitConverter,
86                  SvXMLNamespaceMap const& rNamespaceMap);
87 
88     virtual void finished(SfxItemSet & rSet,
89                           SvXMLUnitConverter const& rUnitConverter) const;
90 
91     virtual void setMapEntries( SvXMLItemMapEntriesRef rMapEntries );
92 
93 private:
94     void Reset();
95 
96     ::rtl::OUString m_FoMarginValue;
97     enum { LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3 };
98     bool m_bHaveMargin[4];
99 };
100 
SwXMLImportTableItemMapper_Impl(SvXMLItemMapEntriesRef rMapEntries)101 SwXMLImportTableItemMapper_Impl::SwXMLImportTableItemMapper_Impl(
102 										SvXMLItemMapEntriesRef rMapEntries ) :
103 	SvXMLImportItemMapper( rMapEntries, RES_UNKNOWNATR_CONTAINER)
104 {
105     Reset();
106 }
107 
~SwXMLImportTableItemMapper_Impl()108 SwXMLImportTableItemMapper_Impl::~SwXMLImportTableItemMapper_Impl()
109 {
110 }
111 
Reset()112 void SwXMLImportTableItemMapper_Impl::Reset()
113 {
114     m_FoMarginValue = ::rtl::OUString();
115     for (int i = 0; i < 3; ++i)
116     {
117         m_bHaveMargin[i] = false;
118     }
119 }
120 
setMapEntries(SvXMLItemMapEntriesRef rMapEntries)121 void SwXMLImportTableItemMapper_Impl::setMapEntries(
122         SvXMLItemMapEntriesRef rMapEntries )
123 {
124     Reset();
125     SvXMLImportItemMapper::setMapEntries(rMapEntries);
126 }
127 
handleSpecialItem(const SvXMLItemMapEntry & rEntry,SfxPoolItem & rItem,SfxItemSet & rItemSet,const OUString & rValue,const SvXMLUnitConverter & rUnitConv,const SvXMLNamespaceMap &)128 sal_Bool SwXMLImportTableItemMapper_Impl::handleSpecialItem(
129 										const SvXMLItemMapEntry& rEntry,
130 										SfxPoolItem& rItem,
131 										SfxItemSet& rItemSet,
132 										const OUString& rValue,
133 										const SvXMLUnitConverter& rUnitConv,
134                                         const SvXMLNamespaceMap& )
135 {
136 	sal_Bool bRet = sal_False;
137     sal_uInt16 nMemberId = static_cast< sal_Int16 >(rEntry.nMemberId & MID_SW_FLAG_MASK);
138 	switch( rItem.Which() )
139 	{
140     case RES_LR_SPACE:
141         switch (nMemberId)
142         {
143             case MID_L_MARGIN:
144                 m_bHaveMargin[LEFT] = true;
145                 break;
146             case MID_R_MARGIN:
147                 m_bHaveMargin[RIGHT] = true;
148                 break;
149         }
150         bRet = SvXMLImportItemMapper::PutXMLValue(
151                 rItem, rValue, nMemberId, rUnitConv);
152         break;
153     case RES_UL_SPACE:
154         switch (nMemberId)
155         {
156             case MID_UP_MARGIN:
157                 m_bHaveMargin[TOP] = true;
158                 break;
159             case MID_LO_MARGIN:
160                 m_bHaveMargin[BOTTOM] = true;
161                 break;
162         }
163         bRet = SvXMLImportItemMapper::PutXMLValue(
164                 rItem, rValue, nMemberId, rUnitConv);
165         break;
166 	case RES_FRM_SIZE:
167 		switch( nMemberId )
168 		{
169 		case MID_FRMSIZE_COL_WIDTH:
170 			// If the item is existing already, a relative value has been set
171 			// already that must be preserved.
172 			if( SFX_ITEM_SET != rItemSet.GetItemState( RES_FRM_SIZE,
173 													   sal_False ) )
174                 bRet = SvXMLImportItemMapper::PutXMLValue(
175                     rItem, rValue, nMemberId, rUnitConv );
176 			break;
177 		}
178 	}
179 
180 	return bRet;
181 }
182 
183 
handleNoItem(SvXMLItemMapEntry const & rEntry,SfxItemSet & rSet,::rtl::OUString const & rValue,SvXMLUnitConverter const & rUnitConverter,SvXMLNamespaceMap const & rNamespaceMap)184 sal_Bool SwXMLImportTableItemMapper_Impl::handleNoItem(
185      SvXMLItemMapEntry const& rEntry,
186      SfxItemSet & rSet,
187      ::rtl::OUString const& rValue,
188      SvXMLUnitConverter const& rUnitConverter,
189      SvXMLNamespaceMap const& rNamespaceMap)
190 {
191     if ((XML_NAMESPACE_FO == rEntry.nNameSpace) &&
192         (xmloff::token::XML_MARGIN == rEntry.eLocalName))
193     {
194         m_FoMarginValue = rValue;
195         return true;
196     }
197     else
198     {
199         return SvXMLImportItemMapper::handleNoItem(
200                 rEntry, rSet, rValue, rUnitConverter, rNamespaceMap);
201     }
202 }
203 
finished(SfxItemSet & rSet,SvXMLUnitConverter const & rUnitConverter) const204 void SwXMLImportTableItemMapper_Impl::finished(
205         SfxItemSet & rSet, SvXMLUnitConverter const& rUnitConverter) const
206 {
207     if (m_FoMarginValue.getLength())
208     {
209         sal_uInt16 const Ids[4][2] = {
210             { RES_LR_SPACE, MID_L_MARGIN },
211             { RES_LR_SPACE, MID_R_MARGIN },
212             { RES_UL_SPACE, MID_UP_MARGIN },
213             { RES_UL_SPACE, MID_LO_MARGIN },
214         };
215         for (int i = 0; i < 4; ++i)
216         {
217             if (m_bHaveMargin[i])
218             {
219                 continue; // already read fo:margin-top etc.
220             }
221             // first get item from itemset
222             SfxPoolItem const* pItem = 0;
223             SfxItemState eState =
224                 rSet.GetItemState(Ids[i][0], sal_True, &pItem);
225 
226             // if not set, try the pool
227             if ((SFX_ITEM_SET != eState) && (SFX_WHICH_MAX > Ids[i][0]))
228             {
229                 pItem = &rSet.GetPool()->GetDefaultItem(Ids[i][0]);
230             }
231 
232             // do we have an item?
233             if (eState >= SFX_ITEM_DEFAULT && pItem)
234             {
235                 SfxPoolItem *const pNewItem = pItem->Clone();
236                 bool const bPut = PutXMLValue(
237                         *pNewItem, m_FoMarginValue, Ids[i][1], rUnitConverter);
238                 if (bPut)
239                 {
240                     rSet.Put(*pNewItem);
241                 }
242             }
243             else
244             {
245                 OSL_ENSURE(false, "could not get item");
246             }
247         }
248     }
249 }
250 
251 // ---------------------------------------------------------------------
252 
253 class SwXMLItemSetContext_Impl : public SvXMLItemSetContext
254 {
255 	SvXMLImportContextRef xBackground;
256 
257     using SvXMLItemSetContext::CreateChildContext;
258 
259 public:
260 	SwXMLItemSetContext_Impl( SwXMLImport& rImport, sal_uInt16 nPrfx,
261 				  const OUString& rLName,
262 				  const Reference< xml::sax::XAttributeList > & xAttrList,
263 				  SfxItemSet&  rItemSet,
264                   SvXMLImportItemMapper & rIMapper,
265 				  const SvXMLUnitConverter& rUnitConv );
266 	virtual ~SwXMLItemSetContext_Impl();
267 
268     virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
269                    const ::rtl::OUString& rLocalName,
270                    const ::uno::Reference< xml::sax::XAttributeList > & xAttrList,
271 				   SfxItemSet&  rItemSet,
272 				   const SvXMLItemMapEntry& rEntry,
273 				   const SvXMLUnitConverter& rUnitConv );
274 };
275 
SwXMLItemSetContext_Impl(SwXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const Reference<xml::sax::XAttributeList> & xAttrList,SfxItemSet & _rItemSet,SvXMLImportItemMapper & _rIMapper,const SvXMLUnitConverter & _rUnitConv)276 SwXMLItemSetContext_Impl::SwXMLItemSetContext_Impl(
277 				 SwXMLImport& rImport, sal_uInt16 nPrfx,
278 				 const OUString& rLName,
279 				 const Reference< xml::sax::XAttributeList > & xAttrList,
280 				 SfxItemSet&  _rItemSet,
281                  SvXMLImportItemMapper & _rIMapper,
282 				 const SvXMLUnitConverter& _rUnitConv ) :
283 	SvXMLItemSetContext( rImport, nPrfx, rLName, xAttrList,
284 					     _rItemSet, _rIMapper, _rUnitConv )
285 {
286 }
287 
~SwXMLItemSetContext_Impl()288 SwXMLItemSetContext_Impl::~SwXMLItemSetContext_Impl()
289 {
290 	if( xBackground.Is() )
291 	{
292 		const SvxBrushItem& rItem =
293 			((SwXMLBrushItemImportContext*)&xBackground)->GetItem();
294 		rItemSet.Put( rItem );
295 	}
296 }
297 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<xml::sax::XAttributeList> & xAttrList,SfxItemSet & _rItemSet,const SvXMLItemMapEntry & rEntry,const SvXMLUnitConverter & _rUnitConv)298 SvXMLImportContext *SwXMLItemSetContext_Impl::CreateChildContext(
299 				   sal_uInt16 nPrefix,
300 				   const OUString& rLocalName,
301 				   const Reference< xml::sax::XAttributeList > & xAttrList,
302 				   SfxItemSet&  _rItemSet,
303 				   const SvXMLItemMapEntry& rEntry,
304 				   const SvXMLUnitConverter& _rUnitConv )
305 {
306 	SvXMLImportContext *pContext = 0;
307 
308 	switch( rEntry.nWhichId )
309 	{
310 	case RES_BACKGROUND:
311 		{
312 			const SfxPoolItem *pItem;
313 			if( SFX_ITEM_SET == _rItemSet.GetItemState( RES_BACKGROUND,
314 													   sal_False, &pItem ) )
315 			{
316 				pContext = new SwXMLBrushItemImportContext(
317 								GetImport(), nPrefix, rLocalName, xAttrList,
318 							    _rUnitConv, *(const SvxBrushItem *)pItem );
319 			}
320 			else
321 			{
322 				pContext = new SwXMLBrushItemImportContext(
323 								GetImport(), nPrefix, rLocalName, xAttrList,
324 							    _rUnitConv, RES_BACKGROUND );
325 			}
326 			xBackground = pContext;
327 		}
328 		break;
329 	}
330 
331 	if( !pContext )
332 		pContext = SvXMLItemSetContext::CreateChildContext( nPrefix, rLocalName,
333 															xAttrList, _rItemSet,
334 															rEntry, _rUnitConv );
335 
336 	return pContext;
337 }
338 
339 // ---------------------------------------------------------------------
340 
_InitItemImport()341 void SwXMLImport::_InitItemImport()
342 {
343 	// #110680#
344 	pTwipUnitConv = new SvXMLUnitConverter( MAP_TWIP, MAP_TWIP, getServiceFactory() );
345 
346 	xTableItemMap = new SvXMLItemMapEntries( aXMLTableItemMap );
347 	xTableColItemMap = new SvXMLItemMapEntries( aXMLTableColItemMap );
348 	xTableRowItemMap = new SvXMLItemMapEntries( aXMLTableRowItemMap );
349 	xTableCellItemMap = new SvXMLItemMapEntries( aXMLTableCellItemMap );
350 
351 	pTableItemMapper = new SwXMLImportTableItemMapper_Impl( xTableItemMap );
352 }
353 
_FinitItemImport()354 void SwXMLImport::_FinitItemImport()
355 {
356 	delete pTableItemMapper;
357 	delete pTwipUnitConv;
358 }
359 
CreateTableItemImportContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<xml::sax::XAttributeList> & xAttrList,sal_uInt16 nFamily,SfxItemSet & rItemSet)360 SvXMLImportContext *SwXMLImport::CreateTableItemImportContext(
361 				  sal_uInt16 nPrefix,
362 			      const OUString& rLocalName,
363 				  const Reference< xml::sax::XAttributeList > & xAttrList,
364 				  sal_uInt16 nFamily,
365 				  SfxItemSet& rItemSet )
366 {
367 	SvXMLItemMapEntriesRef xItemMap;
368 
369 	switch( nFamily )
370 	{
371 	case XML_STYLE_FAMILY_TABLE_TABLE:
372 		xItemMap = xTableItemMap;
373 		break;
374 	case XML_STYLE_FAMILY_TABLE_COLUMN:
375 		xItemMap = xTableColItemMap;
376 		break;
377 	case XML_STYLE_FAMILY_TABLE_ROW:
378 		xItemMap = xTableRowItemMap;
379 		break;
380 	case XML_STYLE_FAMILY_TABLE_CELL:
381 		xItemMap = xTableCellItemMap;
382 		break;
383 	}
384 
385 	pTableItemMapper->setMapEntries( xItemMap );
386 
387 	return new SwXMLItemSetContext_Impl( *this, nPrefix, rLocalName,
388 											xAttrList, rItemSet,
389 											GetTableItemMapper(),
390 											GetTwipUnitConverter() );
391 }
392