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"
21 #include "cppuhelper/factory.hxx"
22 #include "cppuhelper/implementationentry.hxx"
23 #include "cppuhelper/implbase3.hxx"
24 #include "com/sun/star/lang/XServiceInfo.hpp"
25 #include "com/sun/star/inspection/XStringRepresentation.hpp"
26 #include "com/sun/star/lang/XInitialization.hpp"
27 #include "com/sun/star/script/XTypeConverter.hpp"
28 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
29 #include <com/sun/star/reflection/XConstantsTypeDescription.hpp>
30 #include <com/sun/star/beans/XIntrospection.hpp>
31 #include <com/sun/star/util/DateTime.hpp>
32 #include <com/sun/star/util/Date.hpp>
33 #include <com/sun/star/util/Time.hpp>
34 #include <comphelper/sequence.hxx>
35 #include <comphelper/sequenceasvector.hxx>
36 #include <connectivity/dbconversion.hxx>
37 #include "formresid.hrc"
38 #include <tools/debug.hxx>
39 #include <tools/string.hxx>
40 #include <tools/StringListResource.hxx>
41 #include <comphelper/types.hxx>
42 #include "modulepcr.hxx"
47 // component helper namespace
48 namespace comp_StringRepresentation
{
50 using namespace ::com::sun::star
;
52 // component and service helper functions:
53 OUString SAL_CALL
_getImplementationName();
54 uno::Sequence
< OUString
> SAL_CALL
_getSupportedServiceNames();
55 uno::Reference
< uno::XInterface
> SAL_CALL
_create( uno::Reference
< uno::XComponentContext
> const & context
);
57 } // closing component helper namespace
62 using namespace ::com::sun::star
;
63 using namespace ::com::sun::star::uno
;
65 class StringRepresentation
:
66 public ::cppu::WeakImplHelper3
<
68 inspection::XStringRepresentation
,
69 lang::XInitialization
>
72 explicit StringRepresentation(uno::Reference
< uno::XComponentContext
> const & context
);
74 // lang::XServiceInfo:
75 virtual OUString SAL_CALL
getImplementationName() throw (uno::RuntimeException
);
76 virtual ::sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) throw (uno::RuntimeException
);
77 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw (uno::RuntimeException
);
79 // inspection::XStringRepresentation:
80 virtual OUString SAL_CALL
convertToControlValue(const uno::Any
& PropertyValue
) throw (uno::RuntimeException
, uno::Exception
);
81 virtual uno::Any SAL_CALL
convertToPropertyValue(const OUString
& ControlValue
, const uno::Type
& ControlValueType
) throw (uno::RuntimeException
, uno::Exception
);
83 // lang::XInitialization:
84 virtual void SAL_CALL
initialize(const uno::Sequence
< uno::Any
> & aArguments
) throw (uno::RuntimeException
, uno::Exception
);
87 StringRepresentation(StringRepresentation
&); // not defined
88 void operator =(StringRepresentation
&); // not defined
90 virtual ~StringRepresentation() {}
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 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 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 propery 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() throw (uno::RuntimeException
)
148 return comp_StringRepresentation::_getImplementationName();
151 ::sal_Bool SAL_CALL
StringRepresentation::supportsService(OUString
const & serviceName
) throw (uno::RuntimeException
)
153 return ::comphelper::existsValue(serviceName
,comp_StringRepresentation::_getSupportedServiceNames());
156 uno::Sequence
< OUString
> SAL_CALL
StringRepresentation::getSupportedServiceNames() throw (uno::RuntimeException
)
158 return comp_StringRepresentation::_getSupportedServiceNames();
161 // inspection::XStringRepresentation:
162 OUString SAL_CALL
StringRepresentation::convertToControlValue(const uno::Any
& PropertyValue
) throw (uno::RuntimeException
, uno::Exception
)
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
) throw (uno::RuntimeException
, uno::Exception
)
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
) throw (uno::RuntimeException
, uno::Exception
)
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( OUString( "/singletons/com.sun.star.reflection.theTypeDescriptionManager" ) ),
263 uno::UNO_QUERY_THROW
);
265 m_xTypeDescription
.set( xTypeDescProv
->getByHierarchicalName( sConstantName
), uno::UNO_QUERY_THROW
);
266 comphelper::SequenceAsVector
<
267 uno::Reference
< reflection::XConstantTypeDescription
> >
268 cs(m_xTypeDescription
->getConstants());
269 std::sort(cs
.begin(), cs
.end(), CompareConstants());
275 //------------------------------------------------------------------------
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
& ) { }
311 //--------------------------------------------------------------------
314 struct ConvertIntegerFromAndToString
316 OUString
operator()( sal_Int32 _rIntValue
) const
318 return OUString::valueOf( (sal_Int32
)_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
+= String( _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 ] = (ElementType
)_rTransformer( _rComposed
.getToken( 0, '\n', tokenPos
) );
365 while ( tokenPos
!= -1 );
369 //--------------------------------------------------------------------
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 ::std::vector
< OUString
> aListEntries
;
383 tools::StringListResource
aRes(PcrRes(RID_RSC_ENUM_YESNO
),aListEntries
);
384 sal_Bool bValue
= sal_False
;
386 _rStringRep
= bValue
? aListEntries
[1] : aListEntries
[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( ::getCppuType( static_cast< util::Date
* >( NULL
) ) ) )
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( ::getCppuType( static_cast< util::Time
* >( NULL
) ) ) )
455 // similar for time (HHMMSSHH)
457 _rValue
>>= aUnoTime
;
458 _rStringRep
= ::dbtools::DBTypeConversion::toTimeString(aUnoTime
);
460 else if ( _rValue
.getValueType().equals( ::getCppuType( static_cast< util::DateTime
* >( NULL
) ) ) )
462 util::DateTime aUnoDateTime
;
463 _rValue
>>= aUnoDateTime
;
464 _rStringRep
= ::dbtools::DBTypeConversion::toDateTimeString(aUnoDateTime
);
477 //------------------------------------------------------------------------
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
& ) { }
508 //--------------------------------------------------------------------
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 ::std::vector
< OUString
> aListEntries
;
522 tools::StringListResource
aRes(PcrRes(RID_RSC_ENUM_YESNO
),aListEntries
);
523 if ( aListEntries
[0] == _rStringRep
)
524 _rValue
<<= (sal_Bool
)sal_False
;
526 _rValue
<<= (sal_Bool
)sal_True
;
530 case uno::TypeClass_SEQUENCE
:
532 uno::Type aElementType
= ::comphelper::getSequenceElementType( _rTargetType
);
534 String
aStr( _rStringRep
);
535 switch ( aElementType
.getTypeClass() )
537 case uno::TypeClass_STRING
:
539 Sequence
< OUString
> aElements
;
540 splitComposedStringToSequence( aStr
, aElements
, StringIdentity() );
541 _rValue
<<= aElements
;
544 case uno::TypeClass_SHORT
:
546 Sequence
< sal_Int16
> aElements
;
547 splitComposedStringToSequence( aStr
, aElements
, ConvertIntegerFromAndToString() );
548 _rValue
<<= aElements
;
551 case uno::TypeClass_UNSIGNED_SHORT
:
553 Sequence
< sal_uInt16
> aElements
;
554 splitComposedStringToSequence( aStr
, aElements
, ConvertIntegerFromAndToString() );
555 _rValue
<<= aElements
;
558 case uno::TypeClass_LONG
:
560 Sequence
< sal_Int32
> aElements
;
561 splitComposedStringToSequence( aStr
, aElements
, ConvertIntegerFromAndToString() );
562 _rValue
<<= aElements
;
565 case uno::TypeClass_UNSIGNED_LONG
:
567 Sequence
< sal_uInt32
> aElements
;
568 splitComposedStringToSequence( aStr
, aElements
, ConvertIntegerFromAndToString() );
569 _rValue
<<= aElements
;
572 case uno::TypeClass_BYTE
:
574 Sequence
< sal_Int8
> aElements
;
575 splitComposedStringToSequence( aStr
, aElements
, ConvertIntegerFromAndToString() );
576 _rValue
<<= aElements
;
586 case uno::TypeClass_STRUCT
:
587 OSL_FAIL( "StringRepresentation::convertStringToGenericValue(STRUCT): this is dead code - isn't it?" );
588 if ( _rTargetType
.equals( ::getCppuType( static_cast< util::Date
* >( NULL
) ) ) )
590 // weird enough, the string representation of dates, as used
591 // by the control displaying dates, and thus as passed through the layers,
594 _rValue
<<= ::dbtools::DBTypeConversion::toDate(_rStringRep
);
596 else if ( _rTargetType
.equals( ::getCppuType( static_cast< util::Time
* >( NULL
) ) ) )
598 // similar for time (HHMMSSHH)
599 _rValue
<<= ::dbtools::DBTypeConversion::toTime(_rStringRep
);
601 else if ( _rTargetType
.equals( ::getCppuType( static_cast< util::DateTime
* >( NULL
) ) ) )
603 _rValue
<<= ::dbtools::DBTypeConversion::toDateTime(_rStringRep
);
616 //------------------------------------------------------------------------
617 //------------------------------------------------------------------------
619 //------------------------------------------------------------------------
622 // component helper namespace
623 namespace comp_StringRepresentation
{
625 OUString SAL_CALL
_getImplementationName() {
627 "StringRepresentation");
630 uno::Sequence
< OUString
> SAL_CALL
_getSupportedServiceNames()
632 uno::Sequence
< OUString
> s(1);
634 "com.sun.star.inspection.StringRepresentation");
638 uno::Reference
< uno::XInterface
> SAL_CALL
_create(
639 const uno::Reference
< uno::XComponentContext
> & context
)
640 SAL_THROW((uno::Exception
))
642 return static_cast< ::cppu::OWeakObject
* >(new pcr::StringRepresentation(context
));
645 } // closing component helper namespace
647 //------------------------------------------------------------------------
648 extern "C" void SAL_CALL
createRegistryInfo_StringRepresentation()
650 ::pcr::PcrModule::getInstance().registerImplementation(
651 comp_StringRepresentation::_getImplementationName(),
652 comp_StringRepresentation::_getSupportedServiceNames(),
653 comp_StringRepresentation::_create
657 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */