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 "sal/config.h"
22 #include "cppuhelper/factory.hxx"
23 #include "cppuhelper/implementationentry.hxx"
24 #include <cppuhelper/implbase.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include "com/sun/star/lang/XServiceInfo.hpp"
27 #include "com/sun/star/inspection/XStringRepresentation.hpp"
28 #include "com/sun/star/lang/XInitialization.hpp"
29 #include "com/sun/star/script/CannotConvertException.hpp"
30 #include "com/sun/star/script/XTypeConverter.hpp"
31 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32 #include <com/sun/star/reflection/XConstantsTypeDescription.hpp>
33 #include <com/sun/star/beans/XIntrospection.hpp>
34 #include <com/sun/star/util/DateTime.hpp>
35 #include <com/sun/star/util/Date.hpp>
36 #include <com/sun/star/util/Time.hpp>
37 #include <comphelper/sequence.hxx>
38 #include <connectivity/dbconversion.hxx>
39 #include <tools/resary.hxx>
40 #include "formresid.hrc"
41 #include "pcrservices.hxx"
42 #include <comphelper/types.hxx>
43 #include "modulepcr.hxx"
48 // component helper namespace
49 namespace comp_StringRepresentation
{
51 using namespace ::com::sun::star
;
53 // component and service helper functions:
54 OUString SAL_CALL
_getImplementationName();
55 uno::Sequence
< OUString
> SAL_CALL
_getSupportedServiceNames();
56 uno::Reference
< uno::XInterface
> SAL_CALL
_create( uno::Reference
< uno::XComponentContext
> const & context
);
58 } // closing component helper namespace
63 using namespace ::com::sun::star
;
64 using namespace ::com::sun::star::uno
;
66 class StringRepresentation
:
67 public ::cppu::WeakImplHelper
<
69 inspection::XStringRepresentation
,
70 lang::XInitialization
>
73 explicit StringRepresentation(uno::Reference
< uno::XComponentContext
> const & context
);
74 StringRepresentation (const StringRepresentation
&) = delete;
75 StringRepresentation
& operator=(const StringRepresentation
&) = delete;
77 // lang::XServiceInfo:
78 virtual OUString SAL_CALL
getImplementationName() override
;
79 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
80 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
82 // inspection::XStringRepresentation:
83 virtual OUString SAL_CALL
convertToControlValue(const uno::Any
& PropertyValue
) override
;
84 virtual uno::Any SAL_CALL
convertToPropertyValue(const OUString
& ControlValue
, const uno::Type
& ControlValueType
) override
;
86 // lang::XInitialization:
87 virtual void SAL_CALL
initialize(const uno::Sequence
< uno::Any
> & aArguments
) override
;
90 virtual ~StringRepresentation() override
{}
92 /** converts a generic value into a string representation
94 If you want to convert values whose string representation does not depend
95 on a concrete property, use this version
98 if and only if the value could be converted
100 static bool convertGenericValueToString(
101 const uno::Any
& _rValue
,
102 OUString
& _rStringRep
105 /** converts string representation into generic value
107 If you want to convert values whose string representation does not depend
108 on a concrete property, use this version
111 if and only if the value could be converted
113 static bool convertStringToGenericValue(
114 const OUString
& _rStringRep
,
116 const uno::Type
& _rTargetType
119 /** uses the simple convert method from the type converter
121 * \param _rValue the value to be converted
122 * \return the converted string.
124 OUString
convertSimpleToString( const uno::Any
& _rValue
);
126 /** converts a string into his constant value if it exists, otherwise the type converter is used.
127 * \param _rValue the value to be converted
128 * \param _ePropertyType the type of the property to be converted into
129 * \return the converted value
131 uno::Any
convertStringToSimple( const OUString
& _rValue
,const uno::TypeClass
& _ePropertyType
);
133 uno::Reference
< uno::XComponentContext
> m_xContext
;
134 uno::Reference
< script::XTypeConverter
> m_xTypeConverter
;
135 uno::Reference
< reflection::XConstantsTypeDescription
> m_xTypeDescription
;
136 uno::Sequence
< OUString
> m_aValues
;
137 uno::Sequence
< uno::Reference
< reflection::XConstantTypeDescription
> > m_aConstants
;
141 StringRepresentation::StringRepresentation(uno::Reference
< uno::XComponentContext
> const & context
) :
145 // com.sun.star.uno.XServiceInfo:
146 OUString SAL_CALL
StringRepresentation::getImplementationName()
148 return comp_StringRepresentation::_getImplementationName();
151 sal_Bool SAL_CALL
StringRepresentation::supportsService(OUString
const & serviceName
)
153 return cppu::supportsService(this, serviceName
);
156 uno::Sequence
< OUString
> SAL_CALL
StringRepresentation::getSupportedServiceNames()
158 return comp_StringRepresentation::_getSupportedServiceNames();
161 // inspection::XStringRepresentation:
162 OUString SAL_CALL
StringRepresentation::convertToControlValue(const uno::Any
& PropertyValue
)
165 if ( !convertGenericValueToString( PropertyValue
, sReturn
) )
167 sReturn
= convertSimpleToString( PropertyValue
);
169 if ( sReturn
.isEmpty() && PropertyValue
.hasValue() )
171 OString
sMessage( "StringRepresentation::convertPropertyValueToStringRepresentation: cannot convert values of type '" );
172 sMessage
+= OString( PropertyValue
.getValueType().getTypeName().getStr(), PropertyValue
.getValueType().getTypeName().getLength(), RTL_TEXTENCODING_ASCII_US
);
173 sMessage
+= OString( "'!" );
174 OSL_FAIL( sMessage
.getStr() );
182 uno::Any SAL_CALL
StringRepresentation::convertToPropertyValue(const OUString
& ControlValue
, const uno::Type
& ControlValueType
)
186 uno::TypeClass ePropertyType
= ControlValueType
.getTypeClass();
187 switch ( ePropertyType
)
189 case uno::TypeClass_FLOAT
:
190 case uno::TypeClass_DOUBLE
:
191 case uno::TypeClass_BYTE
:
192 case uno::TypeClass_SHORT
:
193 case uno::TypeClass_LONG
:
194 case uno::TypeClass_HYPER
:
195 case uno::TypeClass_UNSIGNED_SHORT
:
196 case uno::TypeClass_UNSIGNED_LONG
:
197 case uno::TypeClass_UNSIGNED_HYPER
:
200 aReturn
= convertStringToSimple(ControlValue
, ePropertyType
);
202 catch( const script::CannotConvertException
& ) { }
203 catch( const lang::IllegalArgumentException
& ) { }
207 #if OSL_DEBUG_LEVEL > 0
210 convertStringToGenericValue( ControlValue
, aReturn
, ControlValueType
);
212 #if OSL_DEBUG_LEVEL > 0
213 // could not convert ...
214 if ( !bCanConvert
&& !ControlValue
.isEmpty() )
216 OString
sMessage( "StringRepresentation::convertStringRepresentationToPropertyValue: cannot convert into values of type '" );
217 sMessage
+= OString( ControlValueType
.getTypeName().getStr(), ControlValueType
.getTypeName().getLength(), RTL_TEXTENCODING_ASCII_US
);
218 sMessage
+= OString( "'!" );
219 OSL_FAIL( sMessage
.getStr() );
229 // This comparison functor assumes an underlying set of constants with pairwise
230 // unequal values that are all of UNO SHORT or LONG type:
231 struct CompareConstants
{
233 css::uno::Reference
< css::reflection::XConstantTypeDescription
> const &
235 css::uno::Reference
< css::reflection::XConstantTypeDescription
> const &
238 return c1
->getConstantValue().get
<sal_Int32
>()
239 < c2
->getConstantValue().get
<sal_Int32
>();
245 // lang::XInitialization:
246 void SAL_CALL
StringRepresentation::initialize(const uno::Sequence
< uno::Any
> & aArguments
)
248 sal_Int32 nLength
= aArguments
.getLength();
251 const uno::Any
* pIter
= aArguments
.getConstArray();
252 m_xTypeConverter
.set(*pIter
++,uno::UNO_QUERY
);
255 OUString sConstantName
;
256 *pIter
++ >>= sConstantName
;
257 *pIter
>>= m_aValues
;
259 if ( m_xContext
.is() )
261 uno::Reference
< container::XHierarchicalNameAccess
> xTypeDescProv(
262 m_xContext
->getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager"),
263 uno::UNO_QUERY_THROW
);
265 m_xTypeDescription
.set( xTypeDescProv
->getByHierarchicalName( sConstantName
), uno::UNO_QUERY_THROW
);
267 uno::Reference
< reflection::XConstantTypeDescription
> >
268 cs(m_xTypeDescription
->getConstants());
269 std::sort(cs
.begin(), cs
.end(), CompareConstants());
276 OUString
StringRepresentation::convertSimpleToString( const uno::Any
& _rValue
)
279 if ( m_xTypeConverter
.is() && _rValue
.hasValue() )
283 if ( m_aConstants
.getLength() )
285 sal_Int16 nConstantValue
= 0;
286 if ( _rValue
>>= nConstantValue
)
288 const uno::Reference
< reflection::XConstantTypeDescription
>* pIter
= m_aConstants
.getConstArray();
289 const uno::Reference
< reflection::XConstantTypeDescription
>* pEnd
= pIter
+ m_aConstants
.getLength();
290 for(sal_Int32 i
= 0;pIter
!= pEnd
;++pIter
,++i
)
292 if ( (*pIter
)->getConstantValue() == _rValue
)
294 OSL_ENSURE(i
< m_aValues
.getLength() ,"StringRepresentation::convertSimpleToString: Index is not in range of m_aValues");
295 sReturn
= m_aValues
[i
];
302 if ( sReturn
.isEmpty() )
303 m_xTypeConverter
->convertToSimpleType( _rValue
, uno::TypeClass_STRING
) >>= sReturn
;
305 catch( const script::CannotConvertException
& ) { }
306 catch( const lang::IllegalArgumentException
& ) { }
314 struct ConvertIntegerFromAndToString
316 OUString
operator()( sal_Int32 _rIntValue
) const
318 return OUString::number( _rIntValue
);
320 sal_Int32
operator()( const OUString
& _rStringValue
) const
322 return _rStringValue
.toInt32();
326 struct StringIdentity
328 OUString
operator()( const OUString
& _rValue
) const
334 template < class ElementType
, class Transformer
>
335 OUString
composeSequenceElements( const Sequence
< ElementType
>& _rElements
, const Transformer
& _rTransformer
)
339 // loop through the elements and concatenate the string representations of the integers
340 // (separated by a line break)
341 const ElementType
* pElements
= _rElements
.getConstArray();
342 const ElementType
* pElementsEnd
= pElements
+ _rElements
.getLength();
343 for ( ; pElements
!= pElementsEnd
; ++pElements
)
345 sCompose
+= OUString( _rTransformer( *pElements
) );
346 if ( pElements
!= pElementsEnd
)
353 template < class ElementType
, class Transformer
>
354 void splitComposedStringToSequence( const OUString
& _rComposed
, Sequence
< ElementType
>& _out_SplitUp
, const Transformer
& _rTransformer
)
356 _out_SplitUp
.realloc( 0 );
357 if ( _rComposed
.isEmpty() )
359 sal_Int32 tokenPos
= 0;
362 _out_SplitUp
.realloc( _out_SplitUp
.getLength() + 1 );
363 _out_SplitUp
[ _out_SplitUp
.getLength() - 1 ] = static_cast<ElementType
>(_rTransformer( _rComposed
.getToken( 0, '\n', tokenPos
) ));
365 while ( tokenPos
!= -1 );
370 bool StringRepresentation::convertGenericValueToString( const uno::Any
& _rValue
, OUString
& _rStringRep
)
372 bool bCanConvert
= true;
374 switch ( _rValue
.getValueTypeClass() )
376 case uno::TypeClass_STRING
:
377 _rValue
>>= _rStringRep
;
380 case uno::TypeClass_BOOLEAN
:
382 ResStringArray
aListEntries(PcrRes(RID_RSC_ENUM_YESNO
));
385 _rStringRep
= bValue
? aListEntries
.GetString(1)
386 : aListEntries
.GetString(0);
390 // some sequence types
391 case uno::TypeClass_SEQUENCE
:
393 Sequence
< OUString
> aStringValues
;
394 Sequence
< sal_Int8
> aInt8Values
;
395 Sequence
< sal_uInt16
> aUInt16Values
;
396 Sequence
< sal_Int16
> aInt16Values
;
397 Sequence
< sal_uInt32
> aUInt32Values
;
398 Sequence
< sal_Int32
> aInt32Values
;
401 if ( _rValue
>>= aStringValues
)
403 _rStringRep
= composeSequenceElements( aStringValues
, StringIdentity() );
406 else if ( _rValue
>>= aInt8Values
)
408 _rStringRep
= composeSequenceElements( aInt8Values
, ConvertIntegerFromAndToString() );
411 else if ( _rValue
>>= aUInt16Values
)
413 _rStringRep
= composeSequenceElements( aUInt16Values
, ConvertIntegerFromAndToString() );
416 else if ( _rValue
>>= aInt16Values
)
418 _rStringRep
= composeSequenceElements( aInt16Values
, ConvertIntegerFromAndToString() );
421 else if ( _rValue
>>= aUInt32Values
)
423 _rStringRep
= composeSequenceElements( aUInt32Values
, ConvertIntegerFromAndToString() );
426 else if ( _rValue
>>= aInt32Values
)
428 _rStringRep
= composeSequenceElements( aInt32Values
, ConvertIntegerFromAndToString() );
434 case uno::TypeClass_CONSTANT
:
442 case uno::TypeClass_STRUCT
:
443 OSL_FAIL( "StringRepresentation::convertGenericValueToString(STRUCT): this is dead code - isn't it?" );
444 if ( _rValue
.getValueType().equals( cppu::UnoType
< util::Date
>::get() ))
446 // weird enough, the string representation of dates, as used
447 // by the control displaying dates, and thus as passed through the layers,
450 _rValue
>>= aUnoDate
;
451 _rStringRep
= ::dbtools::DBTypeConversion::toDateString(aUnoDate
);
453 else if ( _rValue
.getValueType().equals( cppu::UnoType
< util::Time
>::get() ))
455 // similar for time (HHMMSSHH)
457 _rValue
>>= aUnoTime
;
458 _rStringRep
= ::dbtools::DBTypeConversion::toTimeString(aUnoTime
);
460 else if ( _rValue
.getValueType().equals( cppu::UnoType
< util::DateTime
>::get() ))
462 util::DateTime aUnoDateTime
;
463 _rValue
>>= aUnoDateTime
;
464 _rStringRep
= ::dbtools::DBTypeConversion::toDateTimeString(aUnoDateTime
);
478 uno::Any
StringRepresentation::convertStringToSimple( const OUString
& _rValue
,const uno::TypeClass
& _ePropertyType
)
481 if ( m_xTypeConverter
.is() && !_rValue
.isEmpty() )
485 if ( m_aConstants
.getLength() && m_aValues
.getLength() )
487 const OUString
* pIter
= m_aValues
.getConstArray();
488 const OUString
* pEnd
= pIter
+ m_aValues
.getLength();
489 for(sal_Int32 i
= 0;pIter
!= pEnd
;++pIter
,++i
)
491 if ( *pIter
== _rValue
)
493 OSL_ENSURE(i
< m_aConstants
.getLength() ,"StringRepresentation::convertSimpleToString: Index is not in range of m_aValues");
494 aReturn
= m_aConstants
[i
]->getConstantValue();
500 if ( !aReturn
.hasValue() )
501 aReturn
= m_xTypeConverter
->convertToSimpleType( makeAny( _rValue
), _ePropertyType
);
503 catch( const script::CannotConvertException
& ) { }
504 catch( const lang::IllegalArgumentException
& ) { }
509 bool StringRepresentation::convertStringToGenericValue( const OUString
& _rStringRep
, uno::Any
& _rValue
, const uno::Type
& _rTargetType
)
511 bool bCanConvert
= true;
513 switch ( _rTargetType
.getTypeClass() )
515 case uno::TypeClass_STRING
:
516 _rValue
<<= _rStringRep
;
519 case uno::TypeClass_BOOLEAN
:
521 ResStringArray
aListEntries(PcrRes(RID_RSC_ENUM_YESNO
));
522 _rValue
<<= aListEntries
.GetString(0) != _rStringRep
;
526 case uno::TypeClass_SEQUENCE
:
528 uno::Type aElementType
= ::comphelper::getSequenceElementType( _rTargetType
);
530 switch ( aElementType
.getTypeClass() )
532 case uno::TypeClass_STRING
:
534 Sequence
< OUString
> aElements
;
535 splitComposedStringToSequence( _rStringRep
, aElements
, StringIdentity() );
536 _rValue
<<= aElements
;
539 case uno::TypeClass_SHORT
:
541 Sequence
< sal_Int16
> aElements
;
542 splitComposedStringToSequence( _rStringRep
, aElements
, ConvertIntegerFromAndToString() );
543 _rValue
<<= aElements
;
546 case uno::TypeClass_UNSIGNED_SHORT
:
548 Sequence
< sal_uInt16
> aElements
;
549 splitComposedStringToSequence( _rStringRep
, aElements
, ConvertIntegerFromAndToString() );
550 _rValue
<<= aElements
;
553 case uno::TypeClass_LONG
:
555 Sequence
< sal_Int32
> aElements
;
556 splitComposedStringToSequence( _rStringRep
, aElements
, ConvertIntegerFromAndToString() );
557 _rValue
<<= aElements
;
560 case uno::TypeClass_UNSIGNED_LONG
:
562 Sequence
< sal_uInt32
> aElements
;
563 splitComposedStringToSequence( _rStringRep
, aElements
, ConvertIntegerFromAndToString() );
564 _rValue
<<= aElements
;
567 case uno::TypeClass_BYTE
:
569 Sequence
< sal_Int8
> aElements
;
570 splitComposedStringToSequence( _rStringRep
, aElements
, ConvertIntegerFromAndToString() );
571 _rValue
<<= aElements
;
581 case uno::TypeClass_STRUCT
:
582 OSL_FAIL( "StringRepresentation::convertStringToGenericValue(STRUCT): this is dead code - isn't it?" );
583 if ( _rTargetType
.equals( cppu::UnoType
< util::Date
>::get() ))
585 // weird enough, the string representation of dates, as used
586 // by the control displaying dates, and thus as passed through the layers,
589 _rValue
<<= ::dbtools::DBTypeConversion::toDate(_rStringRep
);
591 else if ( _rTargetType
.equals( cppu::UnoType
< util::Time
>::get() ))
593 // similar for time (HHMMSSHH)
594 _rValue
<<= ::dbtools::DBTypeConversion::toTime(_rStringRep
);
596 else if ( _rTargetType
.equals( cppu::UnoType
< util::DateTime
>::get() ))
598 _rValue
<<= ::dbtools::DBTypeConversion::toDateTime(_rStringRep
);
616 // component helper namespace
617 namespace comp_StringRepresentation
{
619 OUString SAL_CALL
_getImplementationName() {
621 "StringRepresentation");
624 uno::Sequence
< OUString
> SAL_CALL
_getSupportedServiceNames()
626 uno::Sequence
< OUString
> s
{ "com.sun.star.inspection.StringRepresentation" };
630 uno::Reference
< uno::XInterface
> SAL_CALL
_create(
631 const uno::Reference
< uno::XComponentContext
> & context
)
633 return static_cast< ::cppu::OWeakObject
* >(new pcr::StringRepresentation(context
));
636 } // closing component helper namespace
639 extern "C" void SAL_CALL
createRegistryInfo_StringRepresentation()
641 ::pcr::PcrModule::getInstance().registerImplementation(
642 comp_StringRepresentation::_getImplementationName(),
643 comp_StringRepresentation::_getSupportedServiceNames(),
644 comp_StringRepresentation::_create
648 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */