xref: /AOO41X/main/extensions/source/propctrlr/stringrepresentation.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
28*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "sal/config.h"
31*cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
32*cdf0e10cSrcweir #include "cppuhelper/implementationentry.hxx"
33*cdf0e10cSrcweir #include "cppuhelper/implbase3.hxx"
34*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
35*cdf0e10cSrcweir #include "com/sun/star/inspection/XStringRepresentation.hpp"
36*cdf0e10cSrcweir #include "com/sun/star/lang/XInitialization.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/script/XTypeConverter.hpp"
38*cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/reflection/XConstantsTypeDescription.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/beans/XIntrospection.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
44*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
45*cdf0e10cSrcweir #include <connectivity/dbconversion.hxx>
46*cdf0e10cSrcweir #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
47*cdf0e10cSrcweir #include "modulepcr.hxx"
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir #ifndef _EXTENSIONS_FORMCTRLR_PROPRESID_HRC_
50*cdf0e10cSrcweir #include "formresid.hrc"
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir #include <tools/debug.hxx>
53*cdf0e10cSrcweir #include <tools/string.hxx>
54*cdf0e10cSrcweir #include <tools/StringListResource.hxx>
55*cdf0e10cSrcweir #include <comphelper/types.hxx>
56*cdf0e10cSrcweir #ifndef _EXTENSIONS_PROPCTRLR_MODULEPCR_HXX_
57*cdf0e10cSrcweir #include "modulepcr.hxx"
58*cdf0e10cSrcweir #endif
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #include <functional>
61*cdf0e10cSrcweir #include <algorithm>
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir // component helper namespace
64*cdf0e10cSrcweir namespace comp_StringRepresentation {
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir using namespace ::com::sun::star;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // component and service helper functions:
69*cdf0e10cSrcweir ::rtl::OUString SAL_CALL _getImplementationName();
70*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames();
71*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL _create( uno::Reference< uno::XComponentContext > const & context );
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir } // closing component helper namespace
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir namespace pcr{
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir using namespace ::com::sun::star;
79*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir class StringRepresentation:
82*cdf0e10cSrcweir     public ::cppu::WeakImplHelper3<
83*cdf0e10cSrcweir         lang::XServiceInfo,
84*cdf0e10cSrcweir         inspection::XStringRepresentation,
85*cdf0e10cSrcweir         lang::XInitialization>
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir public:
88*cdf0e10cSrcweir     explicit StringRepresentation(uno::Reference< uno::XComponentContext > const & context);
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     // lang::XServiceInfo:
91*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException);
92*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (uno::RuntimeException);
93*cdf0e10cSrcweir     virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (uno::RuntimeException);
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     // inspection::XStringRepresentation:
96*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL convertToControlValue(const uno::Any & PropertyValue) throw (uno::RuntimeException, uno::Exception);
97*cdf0e10cSrcweir     virtual uno::Any SAL_CALL convertToPropertyValue(const ::rtl::OUString & ControlValue, const uno::Type & ControlValueType) throw (uno::RuntimeException, uno::Exception);
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     // lang::XInitialization:
100*cdf0e10cSrcweir     virtual void SAL_CALL initialize(const uno::Sequence< uno::Any > & aArguments) throw (uno::RuntimeException, uno::Exception);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir private:
103*cdf0e10cSrcweir     StringRepresentation(StringRepresentation &); // not defined
104*cdf0e10cSrcweir     void operator =(StringRepresentation &); // not defined
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     virtual ~StringRepresentation() {}
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     /** converts a generic value into a string representation
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         If you want to convert values whose string representation does not depend
111*cdf0e10cSrcweir         on a concrete property, use this version
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         @return <TRUE/>
114*cdf0e10cSrcweir             if and only if the value could be converted
115*cdf0e10cSrcweir     */
116*cdf0e10cSrcweir     bool            convertGenericValueToString(
117*cdf0e10cSrcweir                         const uno::Any&   _rValue,
118*cdf0e10cSrcweir                                 ::rtl::OUString&              _rStringRep
119*cdf0e10cSrcweir                     );
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /** converts string representation into generic value
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         If you want to convert values whose string representation does not depend
124*cdf0e10cSrcweir         on a concrete property, use this version
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         @return <TRUE/>
127*cdf0e10cSrcweir             if and only if the value could be converted
128*cdf0e10cSrcweir     */
129*cdf0e10cSrcweir     bool            convertStringToGenericValue(
130*cdf0e10cSrcweir                         const ::rtl::OUString&              _rStringRep,
131*cdf0e10cSrcweir                                 uno::Any&   _rValue,
132*cdf0e10cSrcweir                         const uno::Type& _rTargetType
133*cdf0e10cSrcweir                     );
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     /** uses the simple convert method from the type converter
136*cdf0e10cSrcweir     *
137*cdf0e10cSrcweir     * \param _rValue the value to be converted
138*cdf0e10cSrcweir     * \return the converted string.
139*cdf0e10cSrcweir     */
140*cdf0e10cSrcweir     ::rtl::OUString convertSimpleToString( const uno::Any& _rValue );
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     /** converts a string into his constant value if it exists, otherwise the type converter is used.
143*cdf0e10cSrcweir     * \param _rValue the value to be converted
144*cdf0e10cSrcweir     * \param _ePropertyType teh type of the propery to be converted into
145*cdf0e10cSrcweir     * \return the converted value
146*cdf0e10cSrcweir     */
147*cdf0e10cSrcweir     uno::Any convertStringToSimple( const ::rtl::OUString& _rValue,const uno::TypeClass& _ePropertyType );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     uno::Reference< uno::XComponentContext >                                m_xContext;
150*cdf0e10cSrcweir     uno::Reference< script::XTypeConverter >                                m_xTypeConverter;
151*cdf0e10cSrcweir     uno::Reference< reflection::XConstantsTypeDescription >                 m_xTypeDescription;
152*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString >                                        m_aValues;
153*cdf0e10cSrcweir     uno::Sequence< uno::Reference< reflection::XConstantTypeDescription> >  m_aConstants;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir };
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir StringRepresentation::StringRepresentation(uno::Reference< uno::XComponentContext > const & context) :
158*cdf0e10cSrcweir     m_xContext(context)
159*cdf0e10cSrcweir {}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir // com.sun.star.uno.XServiceInfo:
162*cdf0e10cSrcweir ::rtl::OUString  SAL_CALL StringRepresentation::getImplementationName() throw (uno::RuntimeException)
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir     return comp_StringRepresentation::_getImplementationName();
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir ::sal_Bool SAL_CALL StringRepresentation::supportsService(::rtl::OUString const & serviceName) throw (uno::RuntimeException)
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir     return ::comphelper::existsValue(serviceName,comp_StringRepresentation::_getSupportedServiceNames());
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString >  SAL_CALL StringRepresentation::getSupportedServiceNames() throw (uno::RuntimeException)
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir     return comp_StringRepresentation::_getSupportedServiceNames();
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir // inspection::XStringRepresentation:
178*cdf0e10cSrcweir ::rtl::OUString SAL_CALL StringRepresentation::convertToControlValue(const uno::Any & PropertyValue) throw (uno::RuntimeException, uno::Exception)
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     ::rtl::OUString sReturn;
181*cdf0e10cSrcweir     if ( !convertGenericValueToString( PropertyValue, sReturn ) )
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         sReturn = convertSimpleToString( PropertyValue );
184*cdf0e10cSrcweir #ifdef DBG_UTIL
185*cdf0e10cSrcweir         if ( !sReturn.getLength() && PropertyValue.hasValue() )
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             ::rtl::OString sMessage( "StringRepresentation::convertPropertyValueToStringRepresentation: cannot convert values of type '" );
188*cdf0e10cSrcweir             sMessage += ::rtl::OString( PropertyValue.getValueType().getTypeName().getStr(), PropertyValue.getValueType().getTypeName().getLength(), RTL_TEXTENCODING_ASCII_US );
189*cdf0e10cSrcweir             sMessage += ::rtl::OString( "'!" );
190*cdf0e10cSrcweir             DBG_ERROR( sMessage.getStr() );
191*cdf0e10cSrcweir         }
192*cdf0e10cSrcweir #endif
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     return sReturn;
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir uno::Any SAL_CALL StringRepresentation::convertToPropertyValue(const ::rtl::OUString & ControlValue, const uno::Type & ControlValueType) throw (uno::RuntimeException, uno::Exception)
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir     uno::Any aReturn;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     uno::TypeClass ePropertyType = ControlValueType.getTypeClass();
203*cdf0e10cSrcweir 	switch ( ePropertyType )
204*cdf0e10cSrcweir 	{
205*cdf0e10cSrcweir 	case uno::TypeClass_FLOAT:
206*cdf0e10cSrcweir 	case uno::TypeClass_DOUBLE:
207*cdf0e10cSrcweir 	case uno::TypeClass_BYTE:
208*cdf0e10cSrcweir 	case uno::TypeClass_SHORT:
209*cdf0e10cSrcweir 	case uno::TypeClass_LONG:
210*cdf0e10cSrcweir 	case uno::TypeClass_HYPER:
211*cdf0e10cSrcweir 	case uno::TypeClass_UNSIGNED_SHORT:
212*cdf0e10cSrcweir 	case uno::TypeClass_UNSIGNED_LONG:
213*cdf0e10cSrcweir 	case uno::TypeClass_UNSIGNED_HYPER:
214*cdf0e10cSrcweir 		try
215*cdf0e10cSrcweir 		{
216*cdf0e10cSrcweir             aReturn = convertStringToSimple(ControlValue, ePropertyType);
217*cdf0e10cSrcweir 		}
218*cdf0e10cSrcweir 		catch( const script::CannotConvertException& ) { }
219*cdf0e10cSrcweir         catch( const lang::IllegalArgumentException& ) { }
220*cdf0e10cSrcweir 		break;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     default:
223*cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
224*cdf0e10cSrcweir         bool bCanConvert =
225*cdf0e10cSrcweir     #endif
226*cdf0e10cSrcweir         convertStringToGenericValue( ControlValue, aReturn, ControlValueType );
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
229*cdf0e10cSrcweir         // could not convert ...
230*cdf0e10cSrcweir         if ( !bCanConvert && ControlValue.getLength() )
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             ::rtl::OString sMessage( "StringRepresentation::convertStringRepresentationToPropertyValue: cannot convert into values of type '" );
233*cdf0e10cSrcweir             sMessage += ::rtl::OString( ControlValueType.getTypeName().getStr(), ControlValueType.getTypeName().getLength(), RTL_TEXTENCODING_ASCII_US );
234*cdf0e10cSrcweir             sMessage += ::rtl::OString( "'!" );
235*cdf0e10cSrcweir             DBG_ERROR( sMessage.getStr() );
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir     #endif
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     return aReturn;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir // lang::XInitialization:
244*cdf0e10cSrcweir void SAL_CALL StringRepresentation::initialize(const uno::Sequence< uno::Any > & aArguments) throw (uno::RuntimeException, uno::Exception)
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir     sal_Int32 nLength = aArguments.getLength();
247*cdf0e10cSrcweir     if ( nLength )
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         const uno::Any* pIter = aArguments.getConstArray();
250*cdf0e10cSrcweir         m_xTypeConverter.set(*pIter++,uno::UNO_QUERY);
251*cdf0e10cSrcweir         if ( nLength == 3 )
252*cdf0e10cSrcweir         {
253*cdf0e10cSrcweir             ::rtl::OUString sConstantName;
254*cdf0e10cSrcweir             *pIter++ >>= sConstantName;
255*cdf0e10cSrcweir             *pIter >>= m_aValues;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir             if ( m_xContext.is() )
258*cdf0e10cSrcweir             {
259*cdf0e10cSrcweir                 uno::Reference< container::XHierarchicalNameAccess > xTypeDescProv(
260*cdf0e10cSrcweir                     m_xContext->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.reflection.theTypeDescriptionManager" ) ) ),
261*cdf0e10cSrcweir                     uno::UNO_QUERY_THROW );
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir                 m_xTypeDescription.set( xTypeDescProv->getByHierarchicalName( sConstantName ), uno::UNO_QUERY_THROW );
264*cdf0e10cSrcweir                 m_aConstants = m_xTypeDescription->getConstants();
265*cdf0e10cSrcweir             }
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir //------------------------------------------------------------------------
270*cdf0e10cSrcweir ::rtl::OUString StringRepresentation::convertSimpleToString( const uno::Any& _rValue )
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir 	::rtl::OUString sReturn;
273*cdf0e10cSrcweir 	if ( m_xTypeConverter.is() && _rValue.hasValue() )
274*cdf0e10cSrcweir 	{
275*cdf0e10cSrcweir 		try
276*cdf0e10cSrcweir 		{
277*cdf0e10cSrcweir             if ( m_aConstants.getLength() )
278*cdf0e10cSrcweir             {
279*cdf0e10cSrcweir                 sal_Int16 nConstantValue = 0;
280*cdf0e10cSrcweir                 if ( _rValue >>= nConstantValue )
281*cdf0e10cSrcweir                 {
282*cdf0e10cSrcweir                     const uno::Reference< reflection::XConstantTypeDescription>* pIter = m_aConstants.getConstArray();
283*cdf0e10cSrcweir                     const uno::Reference< reflection::XConstantTypeDescription>* pEnd  = pIter + m_aConstants.getLength();
284*cdf0e10cSrcweir                     for(sal_Int32 i = 0;pIter != pEnd;++pIter,++i)
285*cdf0e10cSrcweir                     {
286*cdf0e10cSrcweir                         if ( (*pIter)->getConstantValue() == _rValue )
287*cdf0e10cSrcweir                         {
288*cdf0e10cSrcweir                             OSL_ENSURE(i < m_aValues.getLength() ,"StringRepresentation::convertSimpleToString: Index is not in range of m_aValues");
289*cdf0e10cSrcweir                             sReturn = m_aValues[i];
290*cdf0e10cSrcweir                             break;
291*cdf0e10cSrcweir                         }
292*cdf0e10cSrcweir                     }
293*cdf0e10cSrcweir                 }
294*cdf0e10cSrcweir             }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir             if ( !sReturn.getLength() )
297*cdf0e10cSrcweir 			    m_xTypeConverter->convertToSimpleType( _rValue, uno::TypeClass_STRING ) >>= sReturn;
298*cdf0e10cSrcweir 		}
299*cdf0e10cSrcweir 		catch( script::CannotConvertException& ) { }
300*cdf0e10cSrcweir 		catch( lang::IllegalArgumentException& ) { }
301*cdf0e10cSrcweir 	}
302*cdf0e10cSrcweir 	return sReturn;
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir //--------------------------------------------------------------------
306*cdf0e10cSrcweir namespace
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir     struct ConvertIntegerFromAndToString
309*cdf0e10cSrcweir     {
310*cdf0e10cSrcweir         ::rtl::OUString operator()( sal_Int32 _rIntValue ) const
311*cdf0e10cSrcweir         {
312*cdf0e10cSrcweir             return ::rtl::OUString::valueOf( (sal_Int32)_rIntValue );
313*cdf0e10cSrcweir         }
314*cdf0e10cSrcweir         sal_Int32 operator()( const ::rtl::OUString& _rStringValue ) const
315*cdf0e10cSrcweir         {
316*cdf0e10cSrcweir             return _rStringValue.toInt32();
317*cdf0e10cSrcweir         }
318*cdf0e10cSrcweir     };
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     struct StringIdentity
321*cdf0e10cSrcweir     {
322*cdf0e10cSrcweir         ::rtl::OUString operator()( const ::rtl::OUString& _rValue ) const
323*cdf0e10cSrcweir         {
324*cdf0e10cSrcweir             return _rValue;
325*cdf0e10cSrcweir         }
326*cdf0e10cSrcweir     };
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir     template < class ElementType, class Transformer >
329*cdf0e10cSrcweir     ::rtl::OUString composeSequenceElements( const Sequence< ElementType >& _rElements, const Transformer& _rTransformer )
330*cdf0e10cSrcweir     {
331*cdf0e10cSrcweir 		String sCompose;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		// loop through the elements and concatenate the string representations of the integers
334*cdf0e10cSrcweir 		// (separated by a line break)
335*cdf0e10cSrcweir         const ElementType* pElements = _rElements.getConstArray();
336*cdf0e10cSrcweir         const ElementType* pElementsEnd = pElements + _rElements.getLength();
337*cdf0e10cSrcweir         for ( ; pElements != pElementsEnd; ++pElements )
338*cdf0e10cSrcweir         {
339*cdf0e10cSrcweir 			sCompose += String( _rTransformer( *pElements ) );
340*cdf0e10cSrcweir 			if ( pElements != pElementsEnd )
341*cdf0e10cSrcweir 				sCompose += '\n';
342*cdf0e10cSrcweir         }
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir         return sCompose;
345*cdf0e10cSrcweir     }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir     template < class ElementType, class Transformer >
348*cdf0e10cSrcweir     void splitComposedStringToSequence( const ::rtl::OUString& _rComposed, Sequence< ElementType >& _out_SplitUp, const Transformer& _rTransformer )
349*cdf0e10cSrcweir     {
350*cdf0e10cSrcweir         _out_SplitUp.realloc( 0 );
351*cdf0e10cSrcweir         if ( !_rComposed.getLength() )
352*cdf0e10cSrcweir             return;
353*cdf0e10cSrcweir         sal_Int32 tokenPos = 0;
354*cdf0e10cSrcweir         do
355*cdf0e10cSrcweir         {
356*cdf0e10cSrcweir             _out_SplitUp.realloc( _out_SplitUp.getLength() + 1 );
357*cdf0e10cSrcweir             _out_SplitUp[ _out_SplitUp.getLength() - 1 ] = (ElementType)_rTransformer( _rComposed.getToken( 0, '\n', tokenPos ) );
358*cdf0e10cSrcweir         }
359*cdf0e10cSrcweir         while ( tokenPos != -1 );
360*cdf0e10cSrcweir     }
361*cdf0e10cSrcweir }
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir //--------------------------------------------------------------------
364*cdf0e10cSrcweir bool StringRepresentation::convertGenericValueToString( const uno::Any& _rValue, ::rtl::OUString& _rStringRep )
365*cdf0e10cSrcweir {
366*cdf0e10cSrcweir     bool bCanConvert = true;
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 	switch ( _rValue.getValueTypeClass() )
369*cdf0e10cSrcweir 	{
370*cdf0e10cSrcweir     case uno::TypeClass_STRING:
371*cdf0e10cSrcweir         _rValue >>= _rStringRep;
372*cdf0e10cSrcweir         break;
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir 	case uno::TypeClass_BOOLEAN:
375*cdf0e10cSrcweir 	{
376*cdf0e10cSrcweir         ::std::vector< ::rtl::OUString > aListEntries;
377*cdf0e10cSrcweir         tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries);
378*cdf0e10cSrcweir         sal_Bool bValue = sal_False;
379*cdf0e10cSrcweir         _rValue >>= bValue;
380*cdf0e10cSrcweir         _rStringRep = bValue ? aListEntries[1] : aListEntries[0];
381*cdf0e10cSrcweir 	}
382*cdf0e10cSrcweir 	break;
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir 	// some sequence types
385*cdf0e10cSrcweir 	case uno::TypeClass_SEQUENCE:
386*cdf0e10cSrcweir 	{
387*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aStringValues;
388*cdf0e10cSrcweir         Sequence< sal_Int8 > aInt8Values;
389*cdf0e10cSrcweir 		Sequence< sal_uInt16 > aUInt16Values;
390*cdf0e10cSrcweir         Sequence< sal_Int16 > aInt16Values;
391*cdf0e10cSrcweir 		Sequence< sal_uInt32 > aUInt32Values;
392*cdf0e10cSrcweir         Sequence< sal_Int32 > aInt32Values;
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir         // string sequences
395*cdf0e10cSrcweir         if ( _rValue >>= aStringValues )
396*cdf0e10cSrcweir 		{
397*cdf0e10cSrcweir             _rStringRep = composeSequenceElements( aStringValues, StringIdentity() );
398*cdf0e10cSrcweir 		}
399*cdf0e10cSrcweir 		// byte sequences
400*cdf0e10cSrcweir 		else if ( _rValue >>= aInt8Values )
401*cdf0e10cSrcweir 		{
402*cdf0e10cSrcweir             _rStringRep = composeSequenceElements( aInt8Values, ConvertIntegerFromAndToString() );
403*cdf0e10cSrcweir 		}
404*cdf0e10cSrcweir 		// uInt16 sequences
405*cdf0e10cSrcweir 		else if ( _rValue >>= aUInt16Values )
406*cdf0e10cSrcweir 		{
407*cdf0e10cSrcweir             _rStringRep = composeSequenceElements( aUInt16Values, ConvertIntegerFromAndToString() );
408*cdf0e10cSrcweir 		}
409*cdf0e10cSrcweir 		// Int16 sequences
410*cdf0e10cSrcweir 		else if ( _rValue >>= aInt16Values )
411*cdf0e10cSrcweir 		{
412*cdf0e10cSrcweir             _rStringRep = composeSequenceElements( aInt16Values, ConvertIntegerFromAndToString() );
413*cdf0e10cSrcweir 		}
414*cdf0e10cSrcweir 		// uInt32 sequences
415*cdf0e10cSrcweir 		else if ( _rValue >>= aUInt32Values )
416*cdf0e10cSrcweir 		{
417*cdf0e10cSrcweir             _rStringRep = composeSequenceElements( aUInt32Values, ConvertIntegerFromAndToString() );
418*cdf0e10cSrcweir 		}
419*cdf0e10cSrcweir 		// Int32 sequences
420*cdf0e10cSrcweir 		else if ( _rValue >>= aInt32Values )
421*cdf0e10cSrcweir 		{
422*cdf0e10cSrcweir             _rStringRep = composeSequenceElements( aInt32Values, ConvertIntegerFromAndToString() );
423*cdf0e10cSrcweir 		}
424*cdf0e10cSrcweir         else
425*cdf0e10cSrcweir             bCanConvert = false;
426*cdf0e10cSrcweir 	}
427*cdf0e10cSrcweir 	break;
428*cdf0e10cSrcweir     case uno::TypeClass_CONSTANT:
429*cdf0e10cSrcweir         {
430*cdf0e10cSrcweir             int i = 0;
431*cdf0e10cSrcweir             ++i;
432*cdf0e10cSrcweir         }
433*cdf0e10cSrcweir         break;
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 	// some structs
436*cdf0e10cSrcweir     case uno::TypeClass_STRUCT:
437*cdf0e10cSrcweir         OSL_ENSURE( false, "StringRepresentation::convertGenericValueToString(STRUCT): this is dead code - isn't it?" );
438*cdf0e10cSrcweir         if ( _rValue.getValueType().equals( ::getCppuType( static_cast< util::Date* >( NULL ) ) ) )
439*cdf0e10cSrcweir         {
440*cdf0e10cSrcweir             // weird enough, the string representation of dates, as used
441*cdf0e10cSrcweir             // by the control displaying dates, and thus as passed through the layers,
442*cdf0e10cSrcweir             // is YYYYMMDD.
443*cdf0e10cSrcweir             util::Date aUnoDate;
444*cdf0e10cSrcweir             _rValue >>= aUnoDate;
445*cdf0e10cSrcweir             _rStringRep = ::dbtools::DBTypeConversion::toDateString(aUnoDate);
446*cdf0e10cSrcweir         }
447*cdf0e10cSrcweir         else if ( _rValue.getValueType().equals( ::getCppuType( static_cast< util::Time* >( NULL ) ) ) )
448*cdf0e10cSrcweir         {
449*cdf0e10cSrcweir             // similar for time (HHMMSSHH)
450*cdf0e10cSrcweir             util::Time aUnoTime;
451*cdf0e10cSrcweir             _rValue >>= aUnoTime;
452*cdf0e10cSrcweir             _rStringRep = ::dbtools::DBTypeConversion::toTimeString(aUnoTime);
453*cdf0e10cSrcweir         }
454*cdf0e10cSrcweir         else if ( _rValue.getValueType().equals( ::getCppuType( static_cast< util::DateTime* >( NULL ) ) ) )
455*cdf0e10cSrcweir         {
456*cdf0e10cSrcweir             util::DateTime aUnoDateTime;
457*cdf0e10cSrcweir             _rValue >>= aUnoDateTime;
458*cdf0e10cSrcweir             _rStringRep = ::dbtools::DBTypeConversion::toDateTimeString(aUnoDateTime);
459*cdf0e10cSrcweir         }
460*cdf0e10cSrcweir         else
461*cdf0e10cSrcweir             bCanConvert = false;
462*cdf0e10cSrcweir         break;
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir     default:
465*cdf0e10cSrcweir         bCanConvert = false;
466*cdf0e10cSrcweir         break;
467*cdf0e10cSrcweir 	}
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir     return bCanConvert;
470*cdf0e10cSrcweir }
471*cdf0e10cSrcweir //------------------------------------------------------------------------
472*cdf0e10cSrcweir uno::Any StringRepresentation::convertStringToSimple( const ::rtl::OUString& _rValue,const uno::TypeClass& _ePropertyType )
473*cdf0e10cSrcweir {
474*cdf0e10cSrcweir 	uno::Any aReturn;
475*cdf0e10cSrcweir 	if ( m_xTypeConverter.is() && _rValue.getLength() )
476*cdf0e10cSrcweir 	{
477*cdf0e10cSrcweir 		try
478*cdf0e10cSrcweir 		{
479*cdf0e10cSrcweir             if ( m_aConstants.getLength() && m_aValues.getLength() )
480*cdf0e10cSrcweir             {
481*cdf0e10cSrcweir                 const ::rtl::OUString* pIter = m_aValues.getConstArray();
482*cdf0e10cSrcweir                 const ::rtl::OUString* pEnd	  = pIter + m_aValues.getLength();
483*cdf0e10cSrcweir                 for(sal_Int32 i = 0;pIter != pEnd;++pIter,++i)
484*cdf0e10cSrcweir                 {
485*cdf0e10cSrcweir                     if ( *pIter == _rValue )
486*cdf0e10cSrcweir                     {
487*cdf0e10cSrcweir                         OSL_ENSURE(i < m_aConstants.getLength() ,"StringRepresentation::convertSimpleToString: Index is not in range of m_aValues");
488*cdf0e10cSrcweir                         aReturn <<= m_aConstants[i]->getConstantValue();
489*cdf0e10cSrcweir                         break;
490*cdf0e10cSrcweir                     }
491*cdf0e10cSrcweir                 }
492*cdf0e10cSrcweir             }
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir             if ( !aReturn.hasValue() )
495*cdf0e10cSrcweir                 aReturn = m_xTypeConverter->convertToSimpleType( makeAny( _rValue ), _ePropertyType );
496*cdf0e10cSrcweir 		}
497*cdf0e10cSrcweir 		catch( script::CannotConvertException& ) { }
498*cdf0e10cSrcweir 		catch( lang::IllegalArgumentException& ) { }
499*cdf0e10cSrcweir 	}
500*cdf0e10cSrcweir 	return aReturn;
501*cdf0e10cSrcweir }
502*cdf0e10cSrcweir //--------------------------------------------------------------------
503*cdf0e10cSrcweir bool StringRepresentation::convertStringToGenericValue( const ::rtl::OUString& _rStringRep, uno::Any& _rValue, const uno::Type& _rTargetType )
504*cdf0e10cSrcweir {
505*cdf0e10cSrcweir     bool bCanConvert = true;
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir     switch ( _rTargetType.getTypeClass() )
508*cdf0e10cSrcweir     {
509*cdf0e10cSrcweir 	case uno::TypeClass_STRING:
510*cdf0e10cSrcweir         _rValue <<= _rStringRep;
511*cdf0e10cSrcweir         break;
512*cdf0e10cSrcweir 
513*cdf0e10cSrcweir     case uno::TypeClass_BOOLEAN:
514*cdf0e10cSrcweir 	{
515*cdf0e10cSrcweir         ::std::vector< ::rtl::OUString > aListEntries;
516*cdf0e10cSrcweir         tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries);
517*cdf0e10cSrcweir         if ( aListEntries[0] == _rStringRep )
518*cdf0e10cSrcweir 			_rValue <<= (sal_Bool)sal_False;
519*cdf0e10cSrcweir 		else
520*cdf0e10cSrcweir 			_rValue <<= (sal_Bool)sal_True;
521*cdf0e10cSrcweir 	}
522*cdf0e10cSrcweir 	break;
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir 	case uno::TypeClass_SEQUENCE:
525*cdf0e10cSrcweir 	{
526*cdf0e10cSrcweir 		uno::Type aElementType = ::comphelper::getSequenceElementType( _rTargetType );
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir 		String aStr( _rStringRep );
529*cdf0e10cSrcweir 		switch ( aElementType.getTypeClass() )
530*cdf0e10cSrcweir 		{
531*cdf0e10cSrcweir 			case uno::TypeClass_STRING:
532*cdf0e10cSrcweir 			{
533*cdf0e10cSrcweir                 Sequence< ::rtl::OUString > aElements;
534*cdf0e10cSrcweir                 splitComposedStringToSequence( aStr, aElements, StringIdentity() );
535*cdf0e10cSrcweir 				_rValue <<= aElements;
536*cdf0e10cSrcweir 			}
537*cdf0e10cSrcweir 			break;
538*cdf0e10cSrcweir 			case uno::TypeClass_SHORT:
539*cdf0e10cSrcweir 			{
540*cdf0e10cSrcweir                 Sequence< sal_Int16 > aElements;
541*cdf0e10cSrcweir                 splitComposedStringToSequence( aStr, aElements, ConvertIntegerFromAndToString() );
542*cdf0e10cSrcweir 				_rValue <<= aElements;
543*cdf0e10cSrcweir 			}
544*cdf0e10cSrcweir 			break;
545*cdf0e10cSrcweir 			case uno::TypeClass_UNSIGNED_SHORT:
546*cdf0e10cSrcweir 			{
547*cdf0e10cSrcweir                 Sequence< sal_uInt16 > aElements;
548*cdf0e10cSrcweir                 splitComposedStringToSequence( aStr, aElements, ConvertIntegerFromAndToString() );
549*cdf0e10cSrcweir 				_rValue <<= aElements;
550*cdf0e10cSrcweir 			}
551*cdf0e10cSrcweir 			break;
552*cdf0e10cSrcweir 			case uno::TypeClass_LONG:
553*cdf0e10cSrcweir 			{
554*cdf0e10cSrcweir                 Sequence< sal_Int32 > aElements;
555*cdf0e10cSrcweir                 splitComposedStringToSequence( aStr, aElements, ConvertIntegerFromAndToString() );
556*cdf0e10cSrcweir 				_rValue <<= aElements;
557*cdf0e10cSrcweir 			}
558*cdf0e10cSrcweir 			break;
559*cdf0e10cSrcweir 			case uno::TypeClass_UNSIGNED_LONG:
560*cdf0e10cSrcweir 			{
561*cdf0e10cSrcweir                 Sequence< sal_uInt32 > aElements;
562*cdf0e10cSrcweir                 splitComposedStringToSequence( aStr, aElements, ConvertIntegerFromAndToString() );
563*cdf0e10cSrcweir 				_rValue <<= aElements;
564*cdf0e10cSrcweir 			}
565*cdf0e10cSrcweir 			break;
566*cdf0e10cSrcweir 			case uno::TypeClass_BYTE:
567*cdf0e10cSrcweir 			{
568*cdf0e10cSrcweir                 Sequence< sal_Int8 > aElements;
569*cdf0e10cSrcweir                 splitComposedStringToSequence( aStr, aElements, ConvertIntegerFromAndToString() );
570*cdf0e10cSrcweir 				_rValue <<= aElements;
571*cdf0e10cSrcweir 			}
572*cdf0e10cSrcweir 			break;
573*cdf0e10cSrcweir             default:
574*cdf0e10cSrcweir                 bCanConvert = false;
575*cdf0e10cSrcweir                 break;
576*cdf0e10cSrcweir 		}
577*cdf0e10cSrcweir 	}
578*cdf0e10cSrcweir 	break;
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir     case uno::TypeClass_STRUCT:
581*cdf0e10cSrcweir         OSL_ENSURE( false, "StringRepresentation::convertStringToGenericValue(STRUCT): this is dead code - isn't it?" );
582*cdf0e10cSrcweir         if ( _rTargetType.equals( ::getCppuType( static_cast< util::Date* >( NULL ) ) ) )
583*cdf0e10cSrcweir         {
584*cdf0e10cSrcweir             // weird enough, the string representation of dates, as used
585*cdf0e10cSrcweir             // by the control displaying dates, and thus as passed through the layers,
586*cdf0e10cSrcweir             // is YYYYMMDD.
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir             _rValue <<= ::dbtools::DBTypeConversion::toDate(_rStringRep);
589*cdf0e10cSrcweir         }
590*cdf0e10cSrcweir         else if ( _rTargetType.equals( ::getCppuType( static_cast< util::Time* >( NULL ) ) ) )
591*cdf0e10cSrcweir         {
592*cdf0e10cSrcweir             // similar for time (HHMMSSHH)
593*cdf0e10cSrcweir             _rValue <<= ::dbtools::DBTypeConversion::toTime(_rStringRep);
594*cdf0e10cSrcweir         }
595*cdf0e10cSrcweir         else if ( _rTargetType.equals( ::getCppuType( static_cast< util::DateTime* >( NULL ) ) ) )
596*cdf0e10cSrcweir         {
597*cdf0e10cSrcweir             _rValue <<= ::dbtools::DBTypeConversion::toDateTime(_rStringRep);
598*cdf0e10cSrcweir         }
599*cdf0e10cSrcweir         else
600*cdf0e10cSrcweir             bCanConvert = false;
601*cdf0e10cSrcweir         break;
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir     default:
604*cdf0e10cSrcweir         bCanConvert = false;
605*cdf0e10cSrcweir         break;
606*cdf0e10cSrcweir     }
607*cdf0e10cSrcweir 
608*cdf0e10cSrcweir     return bCanConvert;
609*cdf0e10cSrcweir }
610*cdf0e10cSrcweir //------------------------------------------------------------------------
611*cdf0e10cSrcweir //------------------------------------------------------------------------
612*cdf0e10cSrcweir } // pcr
613*cdf0e10cSrcweir //------------------------------------------------------------------------
614*cdf0e10cSrcweir 
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir // component helper namespace
617*cdf0e10cSrcweir namespace comp_StringRepresentation {
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir ::rtl::OUString SAL_CALL _getImplementationName() {
620*cdf0e10cSrcweir     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
621*cdf0e10cSrcweir         "StringRepresentation"));
622*cdf0e10cSrcweir }
623*cdf0e10cSrcweir 
624*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
625*cdf0e10cSrcweir {
626*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > s(1);
627*cdf0e10cSrcweir     s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
628*cdf0e10cSrcweir         "com.sun.star.inspection.StringRepresentation"));
629*cdf0e10cSrcweir     return s;
630*cdf0e10cSrcweir }
631*cdf0e10cSrcweir 
632*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL _create(
633*cdf0e10cSrcweir     const uno::Reference< uno::XComponentContext > & context)
634*cdf0e10cSrcweir         SAL_THROW((uno::Exception))
635*cdf0e10cSrcweir {
636*cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >(new pcr::StringRepresentation(context));
637*cdf0e10cSrcweir }
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir } // closing component helper namespace
640*cdf0e10cSrcweir 
641*cdf0e10cSrcweir //------------------------------------------------------------------------
642*cdf0e10cSrcweir extern "C" void SAL_CALL createRegistryInfo_StringRepresentation()
643*cdf0e10cSrcweir {
644*cdf0e10cSrcweir     ::pcr::PcrModule::getInstance().registerImplementation(
645*cdf0e10cSrcweir             comp_StringRepresentation::_getImplementationName(),
646*cdf0e10cSrcweir             comp_StringRepresentation::_getSupportedServiceNames(),
647*cdf0e10cSrcweir             comp_StringRepresentation::_create
648*cdf0e10cSrcweir         );
649*cdf0e10cSrcweir }
650*cdf0e10cSrcweir 
651