android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / filter / xml / xmlimpit.cxx
blobeaabf789e2acde0b318ec0b0b546129280675a58
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 "xmlimpit.hxx"
22 #include <sal/log.hxx>
23 #include <sax/tools/converter.hxx>
24 #include <utility>
25 #include <xmloff/xmluconv.hxx>
26 #include <svl/itempool.hxx>
27 #include <svl/poolitem.hxx>
28 #include <svl/itemset.hxx>
29 #include <xmloff/namespacemap.hxx>
30 #include <editeng/xmlcnitm.hxx>
31 #include <editeng/memberids.h>
32 #include <osl/diagnose.h>
34 #include <hintids.hxx>
35 #include <unomid.h>
36 #include <svx/unomid.hxx>
37 #include <editeng/lrspitem.hxx>
38 #include <editeng/ulspitem.hxx>
39 #include <editeng/shaditem.hxx>
40 #include <editeng/boxitem.hxx>
41 #include <editeng/formatbreakitem.hxx>
42 #include <editeng/keepitem.hxx>
43 #include <editeng/brushitem.hxx>
44 #include <editeng/frmdir.hxx>
45 #include <fmtpdsc.hxx>
46 #include <fmtornt.hxx>
47 #include <fmtfsize.hxx>
49 #include <xmloff/prhdlfac.hxx>
50 #include <xmloff/xmltypes.hxx>
51 #include <xmloff/xmlprhdl.hxx>
52 #include <xmloff/xmlimp.hxx>
53 #include <xmloff/xmlnamespace.hxx>
54 #include "xmlithlp.hxx"
55 #include <com/sun/star/uno/Any.hxx>
57 using ::editeng::SvxBorderLine;
58 using namespace ::com::sun::star;
59 using namespace ::xmloff::token;
60 using uno::Any;
62 constexpr sal_uInt16 nUnknownWhich = RES_UNKNOWNATR_CONTAINER;
64 SvXMLImportItemMapper::SvXMLImportItemMapper(
65 SvXMLItemMapEntriesRef aMapEntries ) :
66 mrMapEntries(std::move( aMapEntries ))
70 SvXMLImportItemMapper::~SvXMLImportItemMapper()
74 void
75 SvXMLImportItemMapper::setMapEntries( SvXMLItemMapEntriesRef rMapEntries )
77 mrMapEntries = std::move(rMapEntries);
80 // fills the given itemset with the attributes in the given list
81 void SvXMLImportItemMapper::importXML( SfxItemSet& rSet,
82 uno::Reference< xml::sax::XFastAttributeList > const & xAttrList,
83 const SvXMLUnitConverter& rUnitConverter,
84 const SvXMLNamespaceMap& rNamespaceMap )
86 std::unique_ptr<SvXMLAttrContainerItem> pUnknownItem;
87 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
89 if( IsTokenInNamespace(aIter.getToken(), XML_NAMESPACE_XMLNS) )
90 continue;
92 sal_Int32 nToken = aIter.getToken();
93 const OUString sValue = aIter.toString();
95 // find a map entry for this attribute
96 sal_Int32 nLookupToken = nToken;
97 // compatibility namespaces need to be transformed into current namespace before looking up
98 if (IsTokenInNamespace(nLookupToken, XML_NAMESPACE_FO_COMPAT))
99 nLookupToken = XML_ELEMENT(FO, (nLookupToken & TOKEN_MASK));
100 SvXMLItemMapEntry const * pEntry = mrMapEntries->getByName( nLookupToken );
102 if( pEntry )
104 // we have a valid map entry here, so lets use it...
105 if( 0 == (pEntry->nMemberId & (MID_SW_FLAG_NO_ITEM_IMPORT|
106 MID_SW_FLAG_ELEMENT_ITEM_IMPORT)) )
108 // first get item from itemset
109 const SfxPoolItem* pItem = nullptr;
110 SfxItemState eState = rSet.GetItemState( pEntry->nWhichId, true,
111 &pItem );
113 // if it's not set, try the pool
114 if (SfxItemState::SET != eState && SfxItemPool::IsWhich(pEntry->nWhichId))
115 pItem = &rSet.GetPool()->GetDefaultItem(pEntry->nWhichId);
117 // do we have an item?
118 if(eState >= SfxItemState::DEFAULT && pItem)
120 std::unique_ptr<SfxPoolItem> pNewItem(pItem->Clone());
121 bool bPut = false;
123 if( 0 == (pEntry->nMemberId&MID_SW_FLAG_SPECIAL_ITEM_IMPORT) )
125 bPut = PutXMLValue( *pNewItem, sValue,
126 o3tl::narrowing<sal_uInt16>( pEntry->nMemberId & MID_SW_FLAG_MASK ),
127 rUnitConverter );
130 else
132 bPut = handleSpecialItem( *pEntry, *pNewItem, rSet,
133 sValue, rUnitConverter );
136 if( bPut )
137 rSet.Put( std::move(pNewItem) );
139 else
141 OSL_FAIL( "Could not get a needed item for xml import!" );
144 else if( 0 != (pEntry->nMemberId & MID_SW_FLAG_NO_ITEM_IMPORT) )
146 handleNoItem( *pEntry, rSet, sValue, rUnitConverter,
147 rNamespaceMap );
150 else
152 if( !pUnknownItem )
154 const SfxPoolItem* pItem = nullptr;
155 if( SfxItemState::SET == rSet.GetItemState( nUnknownWhich, true,
156 &pItem ) )
158 pUnknownItem.reset( static_cast<SvXMLAttrContainerItem*>( pItem->Clone() ) );
160 else
162 pUnknownItem.reset( new SvXMLAttrContainerItem( nUnknownWhich ) );
165 if( pUnknownItem )
167 if( IsTokenInNamespace(nToken, XML_NAMESPACE_NONE) )
168 pUnknownItem->AddAttr( SvXMLImport::getNameFromToken( nToken ), sValue );
169 else
171 const OUString& rAttrNamespacePrefix = SvXMLImport::getNamespacePrefixFromToken(nToken, &rNamespaceMap);
172 OUString sAttrName = SvXMLImport::getNameFromToken( nToken );
173 if ( !rAttrNamespacePrefix.isEmpty() )
174 sAttrName = rAttrNamespacePrefix + SvXMLImport::aNamespaceSeparator + sAttrName;
175 OUString aLocalName, aPrefix, aNamespace;
176 rNamespaceMap.GetKeyByAttrName( sAttrName, &aPrefix, &aLocalName,
177 &aNamespace );
178 if ( !rAttrNamespacePrefix.isEmpty() )
179 pUnknownItem->AddAttr( rAttrNamespacePrefix, aNamespace, aLocalName,
180 sValue );
181 else
182 pUnknownItem->AddAttr( aLocalName, sValue );
188 importXMLUnknownAttributes(rSet, xAttrList, rUnitConverter, pUnknownItem);
190 if( pUnknownItem )
192 rSet.Put( *pUnknownItem );
195 finished(rSet, rUnitConverter);
198 void SvXMLImportItemMapper::importXMLUnknownAttributes( SfxItemSet& rSet,
199 uno::Reference< xml::sax::XFastAttributeList > const & xAttrList,
200 const SvXMLUnitConverter& rUnitConverter,
201 std::unique_ptr<SvXMLAttrContainerItem>& pUnknownItem)
203 const css::uno::Sequence< css::xml::Attribute > unknownAttributes = xAttrList->getUnknownAttributes();
204 for (const auto & rAttribute : unknownAttributes)
206 if( !pUnknownItem )
208 const SfxPoolItem* pItem = nullptr;
209 if( SfxItemState::SET == rSet.GetItemState( nUnknownWhich, true,
210 &pItem ) )
212 pUnknownItem.reset( static_cast<SvXMLAttrContainerItem*>( pItem->Clone() ) );
214 else
216 pUnknownItem.reset( new SvXMLAttrContainerItem( nUnknownWhich ) );
219 if( pUnknownItem )
221 if( rAttribute.NamespaceURL.isEmpty() )
222 pUnknownItem->AddAttr( rAttribute.Name, rAttribute.Value );
223 else
225 OUString sPrefix;
226 OUString sName = rAttribute.Name;
227 int i = sName.indexOf(':');
228 if (i != -1)
230 sPrefix = sName.copy(0, i-1);
231 sName = sName.copy(i+1);
233 // the sax parser doesn't reject these, strangely
234 if (sName.indexOf(':') == -1)
235 pUnknownItem->AddAttr( sPrefix, rAttribute.NamespaceURL, sName,
236 rAttribute.Value );
237 else
238 SAL_WARN("sw", "ignoring dodgy attribute: " + rAttribute.Name);
243 if( pUnknownItem )
245 rSet.Put( *pUnknownItem );
248 finished(rSet, rUnitConverter);
251 /** this method is called for every item that has the
252 MID_SW_FLAG_SPECIAL_ITEM_IMPORT flag set */
253 bool
254 SvXMLImportItemMapper::handleSpecialItem( const SvXMLItemMapEntry& /*rEntry*/,
255 SfxPoolItem& /*rItem*/,
256 SfxItemSet& /*rSet*/,
257 const OUString& /*rValue*/,
258 const SvXMLUnitConverter& /*rUnitConverter*/ )
260 OSL_FAIL( "unsupported special item in xml import" );
261 return false;
264 /** this method is called for every item that has the
265 MID_SW_FLAG_NO_ITEM_IMPORT flag set */
266 bool SvXMLImportItemMapper::handleNoItem( const SvXMLItemMapEntry& /*rEntry*/,
267 SfxItemSet& /*rSet*/,
268 const OUString& /*rValue*/,
269 const SvXMLUnitConverter& /*rUnitConverter*/,
270 const SvXMLNamespaceMap& /*rNamespaceMap*/ )
272 OSL_FAIL( "unsupported no item in xml import" );
273 return false;
276 void
277 SvXMLImportItemMapper::finished(SfxItemSet &, SvXMLUnitConverter const&) const
279 // nothing to do here
282 namespace {
284 struct BoxHolder
286 std::unique_ptr<SvxBorderLine> pTop;
287 std::unique_ptr<SvxBorderLine> pBottom;
288 std::unique_ptr<SvxBorderLine> pLeft;
289 std::unique_ptr<SvxBorderLine> pRight;
291 BoxHolder(BoxHolder const&) = delete;
292 BoxHolder& operator=(BoxHolder const&) = delete;
294 explicit BoxHolder(SvxBoxItem const & rBox)
296 if (rBox.GetTop())
297 pTop.reset(new SvxBorderLine( *rBox.GetTop() ));
298 if (rBox.GetBottom())
299 pBottom.reset(new SvxBorderLine( *rBox.GetBottom() ));
300 if (rBox.GetLeft())
301 pLeft.reset(new SvxBorderLine( *rBox.GetLeft() ));
302 if (rBox.GetRight())
303 pRight.reset(new SvxBorderLine( *rBox.GetRight() ));
309 // put an XML-string value into an item
310 bool SvXMLImportItemMapper::PutXMLValue(
311 SfxPoolItem& rItem,
312 const OUString& rValue,
313 sal_uInt16 nMemberId,
314 const SvXMLUnitConverter& rUnitConverter )
316 bool bOk = false;
318 switch (rItem.Which())
320 case RES_MARGIN_FIRSTLINE:
321 case RES_MARGIN_TEXTLEFT:
322 case RES_MARGIN_RIGHT:
323 assert(false); // is only called for frame formats?
324 break;
326 case RES_LR_SPACE:
328 SvxLRSpaceItem& rLRSpace = dynamic_cast<SvxLRSpaceItem&>(rItem);
330 switch( nMemberId )
332 case MID_L_MARGIN:
333 case MID_R_MARGIN:
335 sal_Int32 nProp = 100;
336 sal_Int32 nAbs = 0;
338 if( rValue.indexOf( '%' ) != -1 )
339 bOk = ::sax::Converter::convertPercent(nProp, rValue);
340 else
341 bOk = rUnitConverter.convertMeasureToCore(nAbs, rValue);
343 if( bOk )
345 switch( nMemberId )
347 case MID_L_MARGIN:
348 rLRSpace.SetTextLeft( nAbs, o3tl::narrowing<sal_uInt16>(nProp) );
349 break;
350 case MID_R_MARGIN:
351 rLRSpace.SetRight( nAbs, o3tl::narrowing<sal_uInt16>(nProp) );
352 break;
356 break;
358 case MID_FIRST_LINE_INDENT:
360 assert(false); // it looks like this can't be called? (frame formats only, aTableItemMap)
361 sal_Int32 nProp = 100;
362 sal_Int32 nAbs = 0;
364 if( rValue.indexOf( '%' ) != -1 )
365 bOk = ::sax::Converter::convertPercent(nProp, rValue);
366 else
367 bOk = rUnitConverter.convertMeasureToCore(nAbs, rValue,
368 -0x7fff, 0x7fff );
370 rLRSpace.SetTextFirstLineOffset( static_cast<short>(nAbs), o3tl::narrowing<sal_uInt16>(nProp) );
372 break;
374 case MID_FIRST_AUTO:
376 assert(false); // it looks like this can't be called? (frame formats only, aTableItemMap)
377 bool bAutoFirst(false);
378 bOk = ::sax::Converter::convertBool( bAutoFirst, rValue );
379 if( bOk )
380 rLRSpace.SetAutoFirst( bAutoFirst );
382 break;
384 default:
385 OSL_FAIL( "unknown member id!");
388 break;
390 case RES_UL_SPACE:
392 SvxULSpaceItem& rULSpace = dynamic_cast<SvxULSpaceItem&>(rItem);
394 sal_Int32 nProp = 100;
395 sal_Int32 nAbs = 0;
397 if( rValue.indexOf( '%' ) != -1 )
398 bOk = ::sax::Converter::convertPercent( nProp, rValue );
399 else
400 bOk = rUnitConverter.convertMeasureToCore( nAbs, rValue );
402 switch( nMemberId )
404 case MID_UP_MARGIN:
405 rULSpace.SetUpper( o3tl::narrowing<sal_uInt16>(nAbs), o3tl::narrowing<sal_uInt16>(nProp) );
406 break;
407 case MID_LO_MARGIN:
408 rULSpace.SetLower( o3tl::narrowing<sal_uInt16>(nAbs), o3tl::narrowing<sal_uInt16>(nProp) );
409 break;
410 default:
411 OSL_FAIL("unknown MemberId");
414 break;
416 case RES_SHADOW:
418 SvxShadowItem& rShadow = dynamic_cast<SvxShadowItem&>(rItem);
420 bool bColorFound = false;
421 bool bOffsetFound = false;
423 SvXMLTokenEnumerator aTokenEnum( rValue );
425 Color aColor( 128,128, 128 );
426 rShadow.SetLocation( SvxShadowLocation::BottomRight );
428 std::u16string_view aToken;
429 while( aTokenEnum.getNextToken( aToken ) )
431 if( IsXMLToken( aToken, XML_NONE ) )
433 rShadow.SetLocation( SvxShadowLocation::NONE );
434 bOk = true;
436 else if( !bColorFound && aToken.substr(0,1) == u"#" )
438 bOk = ::sax::Converter::convertColor( aColor, aToken );
439 if( !bOk )
440 return false;
442 bColorFound = true;
444 else if( !bOffsetFound )
446 sal_Int32 nX = 0, nY = 0;
448 bOk = rUnitConverter.convertMeasureToCore( nX, aToken );
449 if( bOk && aTokenEnum.getNextToken( aToken ) )
450 bOk = rUnitConverter.convertMeasureToCore( nY, aToken );
452 if( bOk )
454 if( nX < 0 )
456 if( nY < 0 )
458 rShadow.SetLocation( SvxShadowLocation::TopLeft );
460 else
462 rShadow.SetLocation( SvxShadowLocation::BottomLeft );
465 else
467 if( nY < 0 )
469 rShadow.SetLocation( SvxShadowLocation::TopRight );
471 else
473 rShadow.SetLocation( SvxShadowLocation::BottomRight );
477 if( nX < 0 ) nX *= -1;
478 if( nY < 0 ) nY *= -1;
480 rShadow.SetWidth( static_cast< sal_uInt16 >( (nX + nY) >> 1 ) );
485 if( bOk && ( bColorFound || bOffsetFound ) )
487 rShadow.SetColor(aColor);
489 else
490 bOk = false;
492 break;
494 case RES_BOX:
496 SvxBoxItem& rBox = dynamic_cast<SvxBoxItem&>(rItem);
498 // copy SvxBorderLines
499 BoxHolder aBoxes(rBox);
501 sal_Int32 nTemp;
503 switch( nMemberId )
505 case ALL_BORDER_PADDING:
506 case LEFT_BORDER_PADDING:
507 case RIGHT_BORDER_PADDING:
508 case TOP_BORDER_PADDING:
509 case BOTTOM_BORDER_PADDING:
510 if (!rUnitConverter.convertMeasureToCore( nTemp, rValue,
511 0, 0xffff ))
513 return false;
516 if( nMemberId == LEFT_BORDER_PADDING ||
517 nMemberId == ALL_BORDER_PADDING )
518 rBox.SetDistance( o3tl::narrowing<sal_uInt16>(nTemp), SvxBoxItemLine::LEFT );
519 if( nMemberId == RIGHT_BORDER_PADDING ||
520 nMemberId == ALL_BORDER_PADDING )
521 rBox.SetDistance( o3tl::narrowing<sal_uInt16>(nTemp), SvxBoxItemLine::RIGHT );
522 if( nMemberId == TOP_BORDER_PADDING ||
523 nMemberId == ALL_BORDER_PADDING )
524 rBox.SetDistance( o3tl::narrowing<sal_uInt16>(nTemp), SvxBoxItemLine::TOP );
525 if( nMemberId == BOTTOM_BORDER_PADDING ||
526 nMemberId == ALL_BORDER_PADDING )
527 rBox.SetDistance( o3tl::narrowing<sal_uInt16>(nTemp), SvxBoxItemLine::BOTTOM);
528 break;
530 case ALL_BORDER:
531 case LEFT_BORDER:
532 case RIGHT_BORDER:
533 case TOP_BORDER:
534 case BOTTOM_BORDER:
536 bool bHasStyle = false;
537 bool bHasWidth = false;
538 bool bHasColor = false;
540 sal_uInt16 nStyle = USHRT_MAX;
541 sal_uInt16 nWidth = 0;
542 sal_uInt16 nNamedWidth = USHRT_MAX;
544 Color aColor( COL_BLACK );
546 if( !sw_frmitems_parseXMLBorder( rValue, rUnitConverter,
547 bHasStyle, nStyle,
548 bHasWidth, nWidth, nNamedWidth,
549 bHasColor, aColor ) )
550 return false;
552 if( TOP_BORDER == nMemberId || ALL_BORDER == nMemberId )
553 sw_frmitems_setXMLBorder( aBoxes.pTop,
554 bHasStyle, nStyle,
555 bHasWidth, nWidth, nNamedWidth,
556 bHasColor, aColor );
558 if( BOTTOM_BORDER == nMemberId || ALL_BORDER == nMemberId )
559 sw_frmitems_setXMLBorder( aBoxes.pBottom,
560 bHasStyle, nStyle,
561 bHasWidth, nWidth, nNamedWidth,
562 bHasColor, aColor );
564 if( LEFT_BORDER == nMemberId || ALL_BORDER == nMemberId )
565 sw_frmitems_setXMLBorder( aBoxes.pLeft,
566 bHasStyle, nStyle,
567 bHasWidth, nWidth, nNamedWidth,
568 bHasColor, aColor );
570 if( RIGHT_BORDER == nMemberId || ALL_BORDER == nMemberId )
571 sw_frmitems_setXMLBorder( aBoxes.pRight,
572 bHasStyle, nStyle,
573 bHasWidth, nWidth, nNamedWidth,
574 bHasColor, aColor );
576 break;
577 case ALL_BORDER_LINE_WIDTH:
578 case LEFT_BORDER_LINE_WIDTH:
579 case RIGHT_BORDER_LINE_WIDTH:
580 case TOP_BORDER_LINE_WIDTH:
581 case BOTTOM_BORDER_LINE_WIDTH:
583 SvXMLTokenEnumerator aTokenEnum( rValue );
585 sal_Int32 nInWidth, nDistance, nOutWidth;
587 std::u16string_view aToken;
588 if( !aTokenEnum.getNextToken( aToken ) )
589 return false;
591 if (!rUnitConverter.convertMeasureToCore(nInWidth, aToken))
592 return false;
594 if( !aTokenEnum.getNextToken( aToken ) )
595 return false;
597 if (!rUnitConverter.convertMeasureToCore(nDistance, aToken))
598 return false;
600 if( !aTokenEnum.getNextToken( aToken ) )
601 return false;
603 if (!rUnitConverter.convertMeasureToCore(nOutWidth, aToken))
604 return false;
606 // #i61946: accept line style even it's not part of our "normal" set of line styles
607 sal_uInt16 nWidth = 0;
609 if( TOP_BORDER_LINE_WIDTH == nMemberId ||
610 ALL_BORDER_LINE_WIDTH == nMemberId )
611 sw_frmitems_setXMLBorder( aBoxes.pTop, nWidth,
612 static_cast< sal_uInt16 >( nOutWidth ),
613 static_cast< sal_uInt16 >( nInWidth ),
614 static_cast< sal_uInt16 >( nDistance ) );
616 if( BOTTOM_BORDER_LINE_WIDTH == nMemberId ||
617 ALL_BORDER_LINE_WIDTH == nMemberId )
618 sw_frmitems_setXMLBorder( aBoxes.pBottom, nWidth,
619 static_cast< sal_uInt16 >( nOutWidth ),
620 static_cast< sal_uInt16 >( nInWidth ),
621 static_cast< sal_uInt16 >( nDistance ) );
623 if( LEFT_BORDER_LINE_WIDTH == nMemberId ||
624 ALL_BORDER_LINE_WIDTH == nMemberId )
625 sw_frmitems_setXMLBorder( aBoxes.pLeft, nWidth,
626 static_cast< sal_uInt16 >( nOutWidth ),
627 static_cast< sal_uInt16 >( nInWidth ),
628 static_cast< sal_uInt16 >( nDistance ) );
630 if( RIGHT_BORDER_LINE_WIDTH == nMemberId ||
631 ALL_BORDER_LINE_WIDTH == nMemberId )
632 sw_frmitems_setXMLBorder( aBoxes.pRight, nWidth,
633 static_cast< sal_uInt16 >( nOutWidth ),
634 static_cast< sal_uInt16 >( nInWidth ),
635 static_cast< sal_uInt16 >( nDistance ) );
637 break;
640 rBox.SetLine( aBoxes.pTop.get(), SvxBoxItemLine::TOP );
641 rBox.SetLine( aBoxes.pBottom.get(), SvxBoxItemLine::BOTTOM );
642 rBox.SetLine( aBoxes.pLeft.get(), SvxBoxItemLine::LEFT );
643 rBox.SetLine( aBoxes.pRight.get(), SvxBoxItemLine::RIGHT );
645 bOk = true;
647 break;
649 case RES_BREAK:
651 SvxFormatBreakItem& rFormatBreak = dynamic_cast<SvxFormatBreakItem&>(rItem);
652 sal_uInt16 eEnum{};
654 if( !SvXMLUnitConverter::convertEnum( eEnum, rValue, psXML_BreakType ) )
655 return false;
657 if( eEnum == 0 )
659 rFormatBreak.SetValue( SvxBreak::NONE );
660 bOk = true;
662 else
664 switch( nMemberId )
666 case MID_BREAK_BEFORE:
667 rFormatBreak.SetValue( eEnum == 1 ?
668 SvxBreak::ColumnBefore :
669 SvxBreak::PageBefore );
670 break;
671 case MID_BREAK_AFTER:
672 rFormatBreak.SetValue( eEnum == 1 ?
673 SvxBreak::ColumnAfter :
674 SvxBreak::PageAfter );
675 break;
677 bOk = true;
680 break;
682 case RES_KEEP:
684 SvxFormatKeepItem& rFormatKeep = dynamic_cast<SvxFormatKeepItem&>(rItem);
686 if( IsXMLToken( rValue, XML_ALWAYS ) ||
687 IsXMLToken( rValue, XML_TRUE ) )
689 rFormatKeep.SetValue( true );
690 bOk = true;
692 else if( IsXMLToken( rValue, XML_AUTO ) ||
693 IsXMLToken( rValue, XML_FALSE ) )
695 rFormatKeep.SetValue( false );
696 bOk = true;
699 break;
701 case RES_BACKGROUND:
703 SvxBrushItem& rBrush = dynamic_cast<SvxBrushItem&>(rItem);
705 Color aTempColor;
706 switch( nMemberId )
708 case MID_BACK_COLOR:
709 if( IsXMLToken( rValue, XML_TRANSPARENT ) )
711 rBrush.GetColor().SetAlpha(0);
712 bOk = true;
714 else if (::sax::Converter::convertColor(aTempColor, rValue))
716 aTempColor.SetAlpha(255);
717 rBrush.SetColor( aTempColor );
718 bOk = true;
720 break;
722 case MID_GRAPHIC_REPEAT:
724 SvxGraphicPosition eGraphicPos = rBrush.GetGraphicPos();
725 SvxGraphicPosition nPos = GPOS_NONE;
726 if( SvXMLUnitConverter::convertEnum( nPos, rValue,
727 psXML_BrushRepeat ) )
729 if( GPOS_MM != nPos || GPOS_NONE == eGraphicPos ||
730 GPOS_AREA == eGraphicPos || GPOS_TILED == eGraphicPos )
731 rBrush.SetGraphicPos( nPos );
732 bOk = true;
735 break;
737 case MID_GRAPHIC_POSITION:
739 SvxGraphicPosition ePos = GPOS_NONE, eTmp;
740 SvxGraphicPosition nTmp;
741 SvXMLTokenEnumerator aTokenEnum( rValue );
742 std::u16string_view aToken;
743 bool bHori = false, bVert = false;
744 bOk = true;
745 while( bOk && aTokenEnum.getNextToken( aToken ) )
747 if( bHori && bVert )
749 bOk = false;
751 else if( std::u16string_view::npos != aToken.find( '%' ) )
753 sal_Int32 nPrc = 50;
754 if (::sax::Converter::convertPercent(nPrc, aToken))
756 if( !bHori )
758 ePos = nPrc < 25 ? GPOS_LT :
759 (nPrc < 75 ? GPOS_MM : GPOS_RB);
760 bHori = true;
762 else
764 eTmp = nPrc < 25 ? GPOS_LT:
765 (nPrc < 75 ? GPOS_LM : GPOS_LB);
766 sw_frmitems_MergeXMLVertPos( ePos, eTmp );
767 bVert = true;
770 else
772 // wrong percentage
773 bOk = false;
776 else if( IsXMLToken( aToken, XML_CENTER ) )
778 if( bHori )
779 sw_frmitems_MergeXMLVertPos( ePos, GPOS_MM );
780 else if ( bVert )
781 sw_frmitems_MergeXMLHoriPos( ePos, GPOS_MM );
782 else
783 ePos = GPOS_MM;
785 else if( SvXMLUnitConverter::convertEnum( nTmp, aToken,
786 psXML_BrushHoriPos ) )
788 if( bVert )
789 sw_frmitems_MergeXMLHoriPos(
790 ePos, nTmp );
791 else if( !bHori )
792 ePos = nTmp;
793 else
794 bOk = false;
795 bHori = true;
797 else if( SvXMLUnitConverter::convertEnum( nTmp, aToken,
798 psXML_BrushVertPos ) )
800 if( bHori )
801 sw_frmitems_MergeXMLVertPos(
802 ePos, nTmp );
803 else if( !bVert )
804 ePos = nTmp;
805 else
806 bOk = false;
807 bVert = true;
809 else
811 bOk = false;
815 if( GPOS_NONE == ePos ) bOk = false;
816 if( bOk )
817 rBrush.SetGraphicPos( ePos );
819 break;
821 case MID_GRAPHIC_FILTER:
822 rBrush.SetGraphicFilter( rValue );
823 bOk = true;
824 break;
827 break;
829 case RES_PAGEDESC:
831 SwFormatPageDesc& rPageDesc = dynamic_cast<SwFormatPageDesc&>(rItem);
833 if( MID_PAGEDESC_PAGENUMOFFSET==nMemberId )
835 sal_Int32 nVal;
836 bOk = ::sax::Converter::convertNumber(
837 nVal, rValue, 0, USHRT_MAX);
838 // i#114163 tdf#77111: OOo < 3.3 had a bug where it wrote
839 // "auto" as "0" for tables - now that we support a real offset
840 // 0, this fake "0" MUST NOT be imported as offset 0!
841 if( bOk && nVal > 0 )
842 rPageDesc.SetNumOffset( o3tl::narrowing<sal_uInt16>(nVal) );
845 break;
847 case RES_LAYOUT_SPLIT:
848 case RES_ROW_SPLIT:
850 SfxBoolItem& rSplit = dynamic_cast<SfxBoolItem&>(rItem);
852 if( IsXMLToken( rValue, XML_AUTO ) ||
853 IsXMLToken( rValue, XML_TRUE ) )
855 rSplit.SetValue( true );
856 bOk = true;
858 else if( IsXMLToken( rValue, XML_ALWAYS ) ||
859 IsXMLToken( rValue, XML_FALSE ) )
861 rSplit.SetValue( false );
862 bOk = true;
865 break;
867 case RES_PRINT:
869 SfxBoolItem& rHasTextChangesOnly = dynamic_cast<SfxBoolItem&>(rItem);
871 if( IsXMLToken( rValue, XML_TRUE ) )
873 rHasTextChangesOnly.SetValue( true );
874 bOk = true;
876 else if( IsXMLToken( rValue, XML_FALSE ) )
878 rHasTextChangesOnly.SetValue( false );
879 bOk = true;
882 break;
884 case RES_HORI_ORIENT:
886 SwFormatHoriOrient& rHoriOrient = dynamic_cast<SwFormatHoriOrient&>(rItem);
888 sal_Int16 nValue;
889 bOk = SvXMLUnitConverter::convertEnum( nValue, rValue,
890 aXMLTableAlignMap );
891 if( bOk )
892 rHoriOrient.SetHoriOrient( nValue );
894 break;
896 case RES_VERT_ORIENT:
898 SwFormatVertOrient& rVertOrient = dynamic_cast<SwFormatVertOrient&>(rItem);
900 sal_Int16 nValue;
901 bOk = SvXMLUnitConverter::convertEnum( nValue, rValue,
902 aXMLTableVAlignMap );
903 if( bOk )
904 rVertOrient.SetVertOrient( nValue );
905 //#i8855# text::VertOrientation::NONE is stored as empty string and should be applied here
906 else if(rValue.isEmpty())
908 rVertOrient.SetVertOrient( text::VertOrientation::NONE );
909 bOk = true;
912 break;
914 case RES_FRM_SIZE:
916 SwFormatFrameSize& rFrameSize = dynamic_cast<SwFormatFrameSize&>(rItem);
918 bool bSetHeight = false;
919 bool bSetWidth = false;
920 bool bSetSizeType = false;
921 SwFrameSize eSizeType = SwFrameSize::Variable;
922 sal_Int32 nMin = MINLAY;
924 switch( nMemberId )
926 case MID_FRMSIZE_REL_WIDTH:
928 sal_Int32 nValue;
929 bOk = ::sax::Converter::convertPercent( nValue, rValue );
930 if( bOk )
932 if( nValue < 1 )
933 nValue = 1;
934 else if( nValue > 100 )
935 nValue = 100;
937 rFrameSize.SetWidthPercent( static_cast<sal_Int8>(nValue) );
940 break;
941 case MID_FRMSIZE_WIDTH:
942 bSetWidth = true;
943 break;
944 case MID_FRMSIZE_MIN_HEIGHT:
945 eSizeType = SwFrameSize::Minimum;
946 bSetHeight = true;
947 nMin = 1;
948 bSetSizeType = true;
949 break;
950 case MID_FRMSIZE_FIX_HEIGHT:
951 eSizeType = SwFrameSize::Fixed;
952 bSetHeight = true;
953 nMin = 1;
954 bSetSizeType = true;
955 break;
956 case MID_FRMSIZE_COL_WIDTH:
957 eSizeType = SwFrameSize::Fixed;
958 bSetWidth = true;
959 bSetSizeType = true;
960 break;
961 case MID_FRMSIZE_REL_COL_WIDTH:
963 sal_Int32 nPos = rValue.indexOf( '*' );
964 if( -1 != nPos )
966 sal_Int32 nValue = rValue.toInt32();
967 if( nValue < MINLAY )
968 nValue = MINLAY;
969 else if( nValue > SAL_MAX_UINT16 )
970 nValue = SAL_MAX_UINT16;
972 rFrameSize.SetWidth( o3tl::narrowing<sal_uInt16>(nValue) );
973 rFrameSize.SetHeightSizeType( SwFrameSize::Variable );
974 bOk = true;
977 break;
980 if( bSetHeight || bSetWidth )
982 sal_Int32 nValue;
983 bOk = rUnitConverter.convertMeasureToCore(nValue, rValue, nMin,
984 USHRT_MAX );
985 if( bOk )
987 if( bSetWidth )
988 rFrameSize.SetWidth( o3tl::narrowing<sal_uInt16>(nValue) );
989 if( bSetHeight )
990 rFrameSize.SetHeight( o3tl::narrowing<sal_uInt16>(nValue) );
991 if( bSetSizeType )
992 rFrameSize.SetHeightSizeType( eSizeType );
996 break;
998 case RES_FRAMEDIR:
1000 if (IsXMLToken(rValue, XML_BT_LR))
1002 // Read bt-lr from the extension namespace, handle other values
1003 // below.
1004 Any aAny;
1005 aAny <<= static_cast<sal_uInt16>(SvxFrameDirection::Vertical_LR_BT);
1006 bOk = rItem.PutValue(aAny, 0);
1008 else if (IsXMLToken(rValue, XML_TB_RL90))
1010 // Read tb-rl90 from the extension namespace.
1011 Any aAny;
1012 aAny <<= static_cast<sal_uInt16>(SvxFrameDirection::Vertical_RL_TB90);
1013 bOk = rItem.PutValue(aAny, 0);
1015 else
1017 std::unique_ptr<XMLPropertyHandler> pWritingModeHandler =
1018 XMLPropertyHandlerFactory::CreatePropertyHandler(
1019 XML_TYPE_TEXT_WRITING_MODE_WITH_DEFAULT );
1020 Any aAny;
1021 bOk = pWritingModeHandler->importXML( rValue, aAny,
1022 rUnitConverter );
1023 if( bOk )
1024 bOk = rItem.PutValue( aAny, 0 );
1027 break;
1029 case RES_COLLAPSING_BORDERS:
1031 SfxBoolItem& rBorders = dynamic_cast<SfxBoolItem&>(rItem);
1033 if( IsXMLToken( rValue, XML_COLLAPSING ) )
1035 rBorders.SetValue(true);
1036 bOk = true;
1038 else if( IsXMLToken( rValue, XML_SEPARATING ) )
1040 rBorders.SetValue(false);
1041 bOk = true;
1043 else
1044 bOk = false;
1046 break;
1048 default:
1049 OSL_FAIL("Item not implemented!");
1050 break;
1053 return bOk;
1056 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */