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 <xmlbahdl.hxx>
22 #include <tools/debug.hxx>
23 #include <sax/tools/converter.hxx>
24 #include <xmloff/xmluconv.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <xmloff/xmltoken.hxx>
28 using namespace ::com::sun::star::uno
;
29 using namespace ::xmloff::token
;
31 static void lcl_xmloff_setAny( Any
& rValue
, sal_Int32 nValue
, sal_Int8 nBytes
)
36 if( nValue
< SCHAR_MIN
)
38 else if( nValue
> SCHAR_MAX
)
40 rValue
<<= (sal_Int8
)nValue
;
43 if( nValue
< SHRT_MIN
)
45 else if( nValue
> SHRT_MAX
)
47 rValue
<<= (sal_Int16
)nValue
;
55 static bool lcl_xmloff_getAny( const Any
& rValue
, sal_Int32
& nValue
,
65 bRet
= rValue
>>= nValue8
;
71 sal_Int16 nValue16
= 0;
72 bRet
= rValue
>>= nValue16
;
77 bRet
= rValue
>>= nValue
;
84 // class XMLNumberPropHdl
86 XMLNumberPropHdl::~XMLNumberPropHdl()
91 bool XMLNumberPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
94 bool bRet
= ::sax::Converter::convertNumber( nValue
, rStrImpValue
);
95 lcl_xmloff_setAny( rValue
, nValue
, nBytes
);
100 bool XMLNumberPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
106 if( lcl_xmloff_getAny( rValue
, nValue
, nBytes
) )
108 ::sax::Converter::convertNumber( aOut
, nValue
);
109 rStrExpValue
= aOut
.makeStringAndClear();
117 // class XMLNumberNonePropHdl
119 XMLNumberNonePropHdl::XMLNumberNonePropHdl( sal_Int8 nB
) :
120 sZeroStr( GetXMLToken(XML_NO_LIMIT
) ),
125 XMLNumberNonePropHdl::XMLNumberNonePropHdl( enum XMLTokenEnum eZeroString
, sal_Int8 nB
) :
126 sZeroStr( GetXMLToken( eZeroString
) ),
131 XMLNumberNonePropHdl::~XMLNumberNonePropHdl()
136 bool XMLNumberNonePropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
140 sal_Int32 nValue
= 0;
141 if( rStrImpValue
== sZeroStr
)
147 bRet
= ::sax::Converter::convertNumber( nValue
, rStrImpValue
);
149 lcl_xmloff_setAny( rValue
, nValue
, nBytes
);
154 bool XMLNumberNonePropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
159 if( lcl_xmloff_getAny( rValue
, nValue
, nBytes
) )
165 aOut
.append( sZeroStr
);
169 ::sax::Converter::convertNumber( aOut
, nValue
);
172 rStrExpValue
= aOut
.makeStringAndClear();
180 // class XMLMeasurePropHdl
182 XMLMeasurePropHdl::~XMLMeasurePropHdl()
187 bool XMLMeasurePropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
189 sal_Int32 nValue
= 0;
190 bool bRet
= rUnitConverter
.convertMeasureToCore( nValue
, rStrImpValue
);
191 lcl_xmloff_setAny( rValue
, nValue
, nBytes
);
195 bool XMLMeasurePropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
201 if( lcl_xmloff_getAny( rValue
, nValue
, nBytes
) )
203 rUnitConverter
.convertMeasureToXML( aOut
, nValue
);
204 rStrExpValue
= aOut
.makeStringAndClear();
212 // class XMLBoolFalsePropHdl
214 XMLBoolFalsePropHdl::~XMLBoolFalsePropHdl()
219 bool XMLBoolFalsePropHdl::importXML( const OUString
&, Any
&, const SvXMLUnitConverter
& ) const
224 bool XMLBoolFalsePropHdl::exportXML( OUString
& rStrExpValue
, const Any
& /*rValue*/, const SvXMLUnitConverter
& rCnv
) const
226 return XMLBoolPropHdl::exportXML( rStrExpValue
, makeAny( sal_False
), rCnv
);
229 // class XMLBoolPropHdl
231 XMLBoolPropHdl::~XMLBoolPropHdl()
236 bool XMLBoolPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
239 bool const bRet
= ::sax::Converter::convertBool( bValue
, rStrImpValue
);
245 bool XMLBoolPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
250 if (rValue
>>= bValue
)
253 ::sax::Converter::convertBool( aOut
, bValue
);
254 rStrExpValue
= aOut
.makeStringAndClear();
262 // class XMLNBoolPropHdl
264 XMLNBoolPropHdl::~XMLNBoolPropHdl()
269 bool XMLNBoolPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
272 bool const bRet
= ::sax::Converter::convertBool( bValue
, rStrImpValue
);
278 bool XMLNBoolPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
283 if (rValue
>>= bValue
)
286 ::sax::Converter::convertBool( aOut
, !bValue
);
287 rStrExpValue
= aOut
.makeStringAndClear();
295 // class XMLPercentPropHdl
297 XMLPercentPropHdl::~XMLPercentPropHdl()
302 bool XMLPercentPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
304 sal_Int32 nValue
= 0;
305 bool const bRet
= ::sax::Converter::convertPercent( nValue
, rStrImpValue
);
306 lcl_xmloff_setAny( rValue
, nValue
, nBytes
);
311 bool XMLPercentPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
317 if( lcl_xmloff_getAny( rValue
, nValue
, nBytes
) )
319 ::sax::Converter::convertPercent( aOut
, nValue
);
320 rStrExpValue
= aOut
.makeStringAndClear();
328 // class XMLDoublePercentPropHdl
330 bool XMLDoublePercentPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
336 if( rStrImpValue
.indexOf( (sal_Unicode
)'%' ) == -1 )
338 fValue
= rStrImpValue
.toDouble();
342 sal_Int32 nValue
= 0;
343 bRet
= ::sax::Converter::convertPercent( nValue
, rStrImpValue
);
344 fValue
= ((double)nValue
) / 100.0;
351 bool XMLDoublePercentPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
356 if( rValue
>>= fValue
)
359 if( fValue
> 0 ) fValue
+= 0.5; else fValue
-= 0.5;
361 sal_Int32 nValue
= (sal_Int32
)fValue
;
364 ::sax::Converter::convertPercent( aOut
, nValue
);
365 rStrExpValue
= aOut
.makeStringAndClear();
373 // class XMLNegPercentPropHdl
375 XMLNegPercentPropHdl::~XMLNegPercentPropHdl()
380 bool XMLNegPercentPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
382 sal_Int32 nValue
= 0;
383 bool const bRet
= ::sax::Converter::convertPercent( nValue
, rStrImpValue
);
384 lcl_xmloff_setAny( rValue
, 100-nValue
, nBytes
);
389 bool XMLNegPercentPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
395 if( lcl_xmloff_getAny( rValue
, nValue
, nBytes
) )
397 ::sax::Converter::convertPercent( aOut
, 100-nValue
);
398 rStrExpValue
= aOut
.makeStringAndClear();
406 // class XMLMeasurePxPropHdl
407 XMLMeasurePxPropHdl::~XMLMeasurePxPropHdl()
412 bool XMLMeasurePxPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
414 sal_Int32 nValue
= 0;
415 bool bRet
= ::sax::Converter::convertMeasurePx( nValue
, rStrImpValue
);
416 lcl_xmloff_setAny( rValue
, nValue
, nBytes
);
420 bool XMLMeasurePxPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
426 if( lcl_xmloff_getAny( rValue
, nValue
, nBytes
) )
428 ::sax::Converter::convertMeasurePx( aOut
, nValue
);
429 rStrExpValue
= aOut
.makeStringAndClear();
437 // class XMLColorPropHdl
439 XMLColorPropHdl::~XMLColorPropHdl()
444 bool XMLColorPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
448 const OUString
astrHSL( "hsl" );
449 if( rStrImpValue
.matchIgnoreAsciiCase( astrHSL
) )
451 sal_Int32 nOpen
= rStrImpValue
.indexOf( '(' );
452 sal_Int32 nClose
= rStrImpValue
.lastIndexOf( ')' );
454 if( (nOpen
!= -1) && (nClose
> nOpen
) )
456 const OUString
aTmp( rStrImpValue
.copy( nOpen
+1, nClose
- nOpen
-1) );
458 sal_Int32 nIndex
= 0;
460 Sequence
< double > aHSL(3);
461 aHSL
[0] = aTmp
.getToken( 0, ',', nIndex
).toDouble();
462 aHSL
[1] = aTmp
.getToken( 0, ',', nIndex
).toDouble() / 100.0;
463 aHSL
[2] = aTmp
.getToken( 0, ',', nIndex
).toDouble() / 100.0;
471 bRet
= ::sax::Converter::convertColor( nColor
, rStrImpValue
);
478 bool XMLColorPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
481 sal_Int32 nColor
= 0;
484 if( rValue
>>= nColor
)
486 ::sax::Converter::convertColor( aOut
, nColor
);
487 rStrExpValue
= aOut
.makeStringAndClear();
493 Sequence
< double > aHSL
;
494 if( (rValue
>>= aHSL
) && (aHSL
.getLength() == 3) )
496 aOut
.append( "hsl(" + OUString::number(aHSL
[0]) + "," + OUString::number(aHSL
[1] * 100.0) + "%," + OUString::number(aHSL
[2] * 100.0) + "%)" );
497 rStrExpValue
= aOut
.makeStringAndClear();
506 // class XMLHexPropHdl
508 XMLHexPropHdl::~XMLHexPropHdl()
513 bool XMLHexPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
518 bRet
= SvXMLUnitConverter::convertHex( nRsid
, rStrImpValue
);
524 bool XMLHexPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
527 sal_uInt32 nRsid
= 0;
530 if( rValue
>>= nRsid
)
532 SvXMLUnitConverter::convertHex( aOut
, nRsid
);
533 rStrExpValue
= aOut
.makeStringAndClear();
545 // class XMLStringPropHdl
547 XMLStringPropHdl::~XMLStringPropHdl()
552 bool XMLStringPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
554 rValue
<<= rStrImpValue
;
558 bool XMLStringPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
562 if( rValue
>>= rStrExpValue
)
568 // class XMLStyleNamePropHdl
570 XMLStyleNamePropHdl::~XMLStyleNamePropHdl()
575 bool XMLStyleNamePropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
579 if( rValue
>>= rStrExpValue
)
581 rStrExpValue
= rUnitConverter
.encodeStyleName( rStrExpValue
);
588 // class XMLDoublePropHdl
590 XMLDoublePropHdl::~XMLDoublePropHdl()
595 bool XMLDoublePropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
597 double fDblValue(0.0);
598 bool const bRet
= ::sax::Converter::convertDouble(fDblValue
, rStrImpValue
);
599 rValue
<<= fDblValue
;
603 bool XMLDoublePropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
609 if( rValue
>>= fValue
)
612 ::sax::Converter::convertDouble( aOut
, fValue
);
613 rStrExpValue
= aOut
.makeStringAndClear();
620 // class XMLColorTransparentPropHdl
622 XMLColorTransparentPropHdl::XMLColorTransparentPropHdl(
623 enum XMLTokenEnum eTransparent
) :
624 sTransparent( GetXMLToken(
625 eTransparent
!= XML_TOKEN_INVALID
? eTransparent
: XML_TRANSPARENT
) )
630 XMLColorTransparentPropHdl::~XMLColorTransparentPropHdl()
635 bool XMLColorTransparentPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
639 if( rStrImpValue
!= sTransparent
)
642 bRet
= ::sax::Converter::convertColor( nColor
, rStrImpValue
);
649 bool XMLColorTransparentPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
652 sal_Int32 nColor
= 0;
654 if( rStrExpValue
== sTransparent
)
656 else if( rValue
>>= nColor
)
659 ::sax::Converter::convertColor( aOut
, nColor
);
660 rStrExpValue
= aOut
.makeStringAndClear();
668 // class XMLIsTransparentPropHdl
670 XMLIsTransparentPropHdl::XMLIsTransparentPropHdl(
671 enum XMLTokenEnum eTransparent
, bool bTransPropVal
) :
672 sTransparent( GetXMLToken(
673 eTransparent
!= XML_TOKEN_INVALID
? eTransparent
: XML_TRANSPARENT
) ),
674 bTransPropValue( bTransPropVal
)
678 XMLIsTransparentPropHdl::~XMLIsTransparentPropHdl()
683 bool XMLIsTransparentPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
685 bool bValue
= ( (bool) (rStrImpValue
== sTransparent
) == bTransPropValue
);
691 bool XMLIsTransparentPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
695 // MIB: This looks a bit strange, because bTransPropValue == bValue should
696 // do the same, but this only applies if 'true' is represented by the same
697 // 8 bit value in bValue and bTransPropValue. Who will ensure this?
698 bool bValue
= *static_cast<sal_Bool
const *>(rValue
.getValue());
699 bool bIsTrans
= bTransPropValue
? bValue
: !bValue
;
703 rStrExpValue
= sTransparent
;
710 // class XMLColorAutoPropHdl
712 XMLColorAutoPropHdl::XMLColorAutoPropHdl()
717 XMLColorAutoPropHdl::~XMLColorAutoPropHdl()
722 bool XMLColorAutoPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
726 // This is a multi property: the value might be set to AUTO_COLOR
727 // already by the XMLIsAutoColorPropHdl!
728 sal_Int32 nColor
= 0;
729 if( !(rValue
>>= nColor
) || -1 != nColor
)
731 bRet
= ::sax::Converter::convertColor( nColor
, rStrImpValue
);
739 bool XMLColorAutoPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
743 sal_Int32 nColor
= 0;
744 if( (rValue
>>= nColor
) && -1 != nColor
)
747 ::sax::Converter::convertColor( aOut
, nColor
);
748 rStrExpValue
= aOut
.makeStringAndClear();
756 // class XMLIsAutoColorPropHdl
758 XMLIsAutoColorPropHdl::XMLIsAutoColorPropHdl()
762 XMLIsAutoColorPropHdl::~XMLIsAutoColorPropHdl()
767 bool XMLIsAutoColorPropHdl::importXML( const OUString
& rStrImpValue
, Any
& rValue
, const SvXMLUnitConverter
& ) const
769 // An auto color overrides any other color set!
771 bool const bRet
= ::sax::Converter::convertBool( bValue
, rStrImpValue
);
773 rValue
<<= (sal_Int32
)-1;
778 bool XMLIsAutoColorPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
781 sal_Int32 nColor
= 0;
783 if( (rValue
>>= nColor
) && -1 == nColor
)
786 ::sax::Converter::convertBool( aOut
, true );
787 rStrExpValue
= aOut
.makeStringAndClear();
795 // class XMLCompareOnlyPropHdl
797 XMLCompareOnlyPropHdl::~XMLCompareOnlyPropHdl()
802 bool XMLCompareOnlyPropHdl::importXML( const OUString
&, Any
&, const SvXMLUnitConverter
& ) const
804 DBG_ASSERT( false, "importXML called for compare-only-property" );
808 bool XMLCompareOnlyPropHdl::exportXML( OUString
&, const Any
&, const SvXMLUnitConverter
& ) const
810 DBG_ASSERT( false, "exportXML called for compare-only-property" );
814 // class XMLNumberWithoutZeroPropHdl
816 XMLNumberWithoutZeroPropHdl::XMLNumberWithoutZeroPropHdl( sal_Int8 nB
) :
821 XMLNumberWithoutZeroPropHdl::~XMLNumberWithoutZeroPropHdl()
825 bool XMLNumberWithoutZeroPropHdl::importXML(
826 const OUString
& rStrImpValue
,
828 const SvXMLUnitConverter
& ) const
830 sal_Int32 nValue
= 0;
831 bool const bRet
= ::sax::Converter::convertNumber( nValue
, rStrImpValue
);
833 lcl_xmloff_setAny( rValue
, nValue
, nBytes
);
837 bool XMLNumberWithoutZeroPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
840 sal_Int32 nValue
= 0;
841 bool bRet
= lcl_xmloff_getAny( rValue
, nValue
, nBytes
);
846 OUStringBuffer aBuffer
;
847 ::sax::Converter::convertNumber( aBuffer
, nValue
);
848 rStrExpValue
= aBuffer
.makeStringAndClear();
854 // class XMLNumberWithAutoInsteadZeroPropHdl
856 XMLNumberWithAutoInsteadZeroPropHdl::~XMLNumberWithAutoInsteadZeroPropHdl()
860 bool XMLNumberWithAutoInsteadZeroPropHdl::importXML(
861 const OUString
& rStrImpValue
,
863 const SvXMLUnitConverter
& ) const
865 sal_Int32 nValue
= 0;
866 bool bRet
= ::sax::Converter::convertNumber( nValue
, rStrImpValue
);
868 lcl_xmloff_setAny( rValue
, nValue
, 2 );
869 else if( rStrImpValue
== GetXMLToken( XML_AUTO
) )
871 rValue
.clear(); // void
877 bool XMLNumberWithAutoInsteadZeroPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
880 sal_Int32 nValue
= 0;
881 bool bRet
= lcl_xmloff_getAny( rValue
, nValue
, 2 );
884 // FIXME: 0 is not a valid value - write "auto" instead
886 rStrExpValue
= GetXMLToken( XML_AUTO
);
889 OUStringBuffer aBuffer
;
890 ::sax::Converter::convertNumber( aBuffer
, nValue
);
891 rStrExpValue
= aBuffer
.makeStringAndClear();
897 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */