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