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 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "comphelper/anytostring.hxx" 32*cdf0e10cSrcweir #include "osl/diagnose.h" 33*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx" 34*cdf0e10cSrcweir #include "typelib/typedescription.h" 35*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp" 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir using namespace ::com::sun::star; 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir namespace comphelper { 40*cdf0e10cSrcweir namespace { 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir void appendTypeError( 43*cdf0e10cSrcweir rtl::OUStringBuffer & buf, typelib_TypeDescriptionReference * typeRef ) 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir buf.appendAscii( 46*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("<cannot get type description of type ") ); 47*cdf0e10cSrcweir buf.append( rtl::OUString::unacquired( &typeRef->pTypeName ) ); 48*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('>') ); 49*cdf0e10cSrcweir } 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir inline void appendChar( rtl::OUStringBuffer & buf, sal_Unicode c ) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir if (c < ' ' || c > '~') { 54*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\X") ); 55*cdf0e10cSrcweir rtl::OUString const s( 56*cdf0e10cSrcweir rtl::OUString::valueOf( static_cast< sal_Int32 >(c), 16 ) ); 57*cdf0e10cSrcweir for ( sal_Int32 f = 4 - s.getLength(); f > 0; --f ) 58*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('0') ); 59*cdf0e10cSrcweir buf.append( s ); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir else { 62*cdf0e10cSrcweir buf.append( c ); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir //------------------------------------------------------------------------------ 67*cdf0e10cSrcweir void appendValue( rtl::OUStringBuffer & buf, 68*cdf0e10cSrcweir void const * val, typelib_TypeDescriptionReference * typeRef, 69*cdf0e10cSrcweir bool prependType ) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir if (typeRef->eTypeClass == typelib_TypeClass_VOID) { 72*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("void") ); 73*cdf0e10cSrcweir return; 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir OSL_ASSERT( val != 0 ); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir if (prependType && 78*cdf0e10cSrcweir typeRef->eTypeClass != typelib_TypeClass_STRING && 79*cdf0e10cSrcweir typeRef->eTypeClass != typelib_TypeClass_CHAR && 80*cdf0e10cSrcweir typeRef->eTypeClass != typelib_TypeClass_BOOLEAN) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('(') ); 83*cdf0e10cSrcweir buf.append( rtl::OUString::unacquired( &typeRef->pTypeName ) ); 84*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(") ") ); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir switch (typeRef->eTypeClass) { 88*cdf0e10cSrcweir case typelib_TypeClass_INTERFACE: { 89*cdf0e10cSrcweir buf.append( static_cast<sal_Unicode>('@') ); 90*cdf0e10cSrcweir buf.append( reinterpret_cast< sal_Int64 >( 91*cdf0e10cSrcweir *static_cast< void * const * >(val) ), 16 ); 92*cdf0e10cSrcweir uno::Reference< lang::XServiceInfo > xServiceInfo( 93*cdf0e10cSrcweir *static_cast< uno::XInterface * const * >(val), 94*cdf0e10cSrcweir uno::UNO_QUERY ); 95*cdf0e10cSrcweir if (xServiceInfo.is()) { 96*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( 97*cdf0e10cSrcweir " (ImplementationName = \"") ); 98*cdf0e10cSrcweir buf.append( xServiceInfo->getImplementationName() ); 99*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") ); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir break; 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir case typelib_TypeClass_STRUCT: 104*cdf0e10cSrcweir case typelib_TypeClass_EXCEPTION: { 105*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 106*cdf0e10cSrcweir typelib_TypeDescription * typeDescr = 0; 107*cdf0e10cSrcweir typelib_typedescriptionreference_getDescription( &typeDescr, typeRef ); 108*cdf0e10cSrcweir if (typeDescr == 0 || !typelib_typedescription_complete( &typeDescr )) { 109*cdf0e10cSrcweir appendTypeError( buf, typeRef ); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir else { 112*cdf0e10cSrcweir typelib_CompoundTypeDescription * compType = 113*cdf0e10cSrcweir reinterpret_cast< typelib_CompoundTypeDescription * >( 114*cdf0e10cSrcweir typeDescr ); 115*cdf0e10cSrcweir sal_Int32 nDescr = compType->nMembers; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir if (compType->pBaseTypeDescription) { 118*cdf0e10cSrcweir appendValue( 119*cdf0e10cSrcweir buf, val, reinterpret_cast< 120*cdf0e10cSrcweir typelib_TypeDescription * >( 121*cdf0e10cSrcweir compType->pBaseTypeDescription)->pWeakRef, false ); 122*cdf0e10cSrcweir if (nDescr > 0) 123*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") ); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir typelib_TypeDescriptionReference ** ppTypeRefs = 127*cdf0e10cSrcweir compType->ppTypeRefs; 128*cdf0e10cSrcweir sal_Int32 * memberOffsets = compType->pMemberOffsets; 129*cdf0e10cSrcweir rtl_uString ** ppMemberNames = compType->ppMemberNames; 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nDescr; ++nPos ) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir buf.append( ppMemberNames[ nPos ] ); 134*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") ); 135*cdf0e10cSrcweir typelib_TypeDescription * memberType = 0; 136*cdf0e10cSrcweir TYPELIB_DANGER_GET( &memberType, ppTypeRefs[ nPos ] ); 137*cdf0e10cSrcweir if (memberType == 0) { 138*cdf0e10cSrcweir appendTypeError( buf, ppTypeRefs[ nPos ] ); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir else { 141*cdf0e10cSrcweir appendValue( buf, 142*cdf0e10cSrcweir static_cast< char const * >( 143*cdf0e10cSrcweir val ) + memberOffsets[ nPos ], 144*cdf0e10cSrcweir memberType->pWeakRef, true ); 145*cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( memberType ); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir if (nPos < (nDescr - 1)) 148*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") ); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 152*cdf0e10cSrcweir if (typeDescr != 0) 153*cdf0e10cSrcweir typelib_typedescription_release( typeDescr ); 154*cdf0e10cSrcweir break; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir case typelib_TypeClass_SEQUENCE: { 157*cdf0e10cSrcweir typelib_TypeDescription * typeDescr = 0; 158*cdf0e10cSrcweir TYPELIB_DANGER_GET( &typeDescr, typeRef ); 159*cdf0e10cSrcweir if (typeDescr == 0) { 160*cdf0e10cSrcweir appendTypeError( buf,typeRef ); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir else { 163*cdf0e10cSrcweir typelib_TypeDescriptionReference * elementTypeRef = 164*cdf0e10cSrcweir reinterpret_cast< 165*cdf0e10cSrcweir typelib_IndirectTypeDescription * >(typeDescr)->pType; 166*cdf0e10cSrcweir typelib_TypeDescription * elementTypeDescr = 0; 167*cdf0e10cSrcweir TYPELIB_DANGER_GET( &elementTypeDescr, elementTypeRef ); 168*cdf0e10cSrcweir if (elementTypeDescr == 0) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir appendTypeError( buf, elementTypeRef ); 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir else 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir sal_Int32 nElementSize = elementTypeDescr->nSize; 175*cdf0e10cSrcweir uno_Sequence * seq = 176*cdf0e10cSrcweir *static_cast< uno_Sequence * const * >(val); 177*cdf0e10cSrcweir sal_Int32 nElements = seq->nElements; 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir if (nElements > 0) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 182*cdf0e10cSrcweir char const * pElements = seq->elements; 183*cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir appendValue( 186*cdf0e10cSrcweir buf, pElements + (nElementSize * nPos), 187*cdf0e10cSrcweir elementTypeDescr->pWeakRef, false ); 188*cdf0e10cSrcweir if (nPos < (nElements - 1)) 189*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") ); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir else 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") ); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( elementTypeDescr ); 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( typeDescr ); 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir break; 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir case typelib_TypeClass_ANY: { 204*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 205*cdf0e10cSrcweir uno_Any const * pAny = static_cast< uno_Any const * >(val); 206*cdf0e10cSrcweir appendValue( buf, pAny->pData, pAny->pType, true ); 207*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 208*cdf0e10cSrcweir break; 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir case typelib_TypeClass_TYPE: 211*cdf0e10cSrcweir buf.append( (*reinterpret_cast< 212*cdf0e10cSrcweir typelib_TypeDescriptionReference * const * >(val) 213*cdf0e10cSrcweir )->pTypeName ); 214*cdf0e10cSrcweir break; 215*cdf0e10cSrcweir case typelib_TypeClass_STRING: { 216*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('\"') ); 217*cdf0e10cSrcweir rtl::OUString const & str = rtl::OUString::unacquired( 218*cdf0e10cSrcweir static_cast< rtl_uString * const * >(val) ); 219*cdf0e10cSrcweir sal_Int32 len = str.getLength(); 220*cdf0e10cSrcweir for ( sal_Int32 pos = 0; pos < len; ++pos ) 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir sal_Unicode c = str[ pos ]; 223*cdf0e10cSrcweir if (c == '\"') 224*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\"") ); 225*cdf0e10cSrcweir else if (c == '\\') 226*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") ); 227*cdf0e10cSrcweir else 228*cdf0e10cSrcweir appendChar( buf, c ); 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('\"') ); 231*cdf0e10cSrcweir break; 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir case typelib_TypeClass_ENUM: { 234*cdf0e10cSrcweir typelib_TypeDescription * typeDescr = 0; 235*cdf0e10cSrcweir typelib_typedescriptionreference_getDescription( &typeDescr, typeRef ); 236*cdf0e10cSrcweir if (typeDescr == 0 || !typelib_typedescription_complete( &typeDescr )) { 237*cdf0e10cSrcweir appendTypeError( buf, typeRef ); 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir else 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir sal_Int32 * pValues = 242*cdf0e10cSrcweir reinterpret_cast< typelib_EnumTypeDescription * >( 243*cdf0e10cSrcweir typeDescr )->pEnumValues; 244*cdf0e10cSrcweir sal_Int32 nPos = reinterpret_cast< typelib_EnumTypeDescription * >( 245*cdf0e10cSrcweir typeDescr )->nEnumValues; 246*cdf0e10cSrcweir while (nPos--) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir if (pValues[ nPos ] == *static_cast< int const * >(val)) 249*cdf0e10cSrcweir break; 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir if (nPos >= 0) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir buf.append( reinterpret_cast< typelib_EnumTypeDescription * >( 254*cdf0e10cSrcweir typeDescr )->ppEnumNames[ nPos ] ); 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir else 257*cdf0e10cSrcweir { 258*cdf0e10cSrcweir buf.appendAscii( 259*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("?unknown enum value?") ); 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir if (typeDescr != 0) 263*cdf0e10cSrcweir typelib_typedescription_release( typeDescr ); 264*cdf0e10cSrcweir break; 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir case typelib_TypeClass_BOOLEAN: 267*cdf0e10cSrcweir if (*static_cast< sal_Bool const * >(val) != sal_False) 268*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") ); 269*cdf0e10cSrcweir else 270*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") ); 271*cdf0e10cSrcweir break; 272*cdf0e10cSrcweir case typelib_TypeClass_CHAR: { 273*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('\'') ); 274*cdf0e10cSrcweir sal_Unicode c = *static_cast< sal_Unicode const * >(val); 275*cdf0e10cSrcweir if (c == '\'') 276*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\'") ); 277*cdf0e10cSrcweir else if (c == '\\') 278*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") ); 279*cdf0e10cSrcweir else 280*cdf0e10cSrcweir appendChar( buf, c ); 281*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('\'') ); 282*cdf0e10cSrcweir break; 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir case typelib_TypeClass_FLOAT: 285*cdf0e10cSrcweir buf.append( *static_cast< float const * >(val) ); 286*cdf0e10cSrcweir break; 287*cdf0e10cSrcweir case typelib_TypeClass_DOUBLE: 288*cdf0e10cSrcweir buf.append( *static_cast< double const * >(val) ); 289*cdf0e10cSrcweir break; 290*cdf0e10cSrcweir case typelib_TypeClass_BYTE: 291*cdf0e10cSrcweir buf.append( static_cast< sal_Int32 >( 292*cdf0e10cSrcweir *static_cast< sal_Int8 const * >(val) ) ); 293*cdf0e10cSrcweir break; 294*cdf0e10cSrcweir case typelib_TypeClass_SHORT: 295*cdf0e10cSrcweir buf.append( static_cast< sal_Int32 >( 296*cdf0e10cSrcweir *static_cast< sal_Int16 const * >(val) ) ); 297*cdf0e10cSrcweir break; 298*cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_SHORT: 299*cdf0e10cSrcweir buf.append( static_cast< sal_Int32 >( 300*cdf0e10cSrcweir *static_cast< sal_uInt16 const * >(val) ) ); 301*cdf0e10cSrcweir break; 302*cdf0e10cSrcweir case typelib_TypeClass_LONG: 303*cdf0e10cSrcweir buf.append( *static_cast< sal_Int32 const * >(val) ); 304*cdf0e10cSrcweir break; 305*cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_LONG: 306*cdf0e10cSrcweir buf.append( static_cast< sal_Int64 >( 307*cdf0e10cSrcweir *static_cast< sal_uInt32 const * >(val) ) ); 308*cdf0e10cSrcweir break; 309*cdf0e10cSrcweir case typelib_TypeClass_HYPER: 310*cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_HYPER: 311*cdf0e10cSrcweir buf.append( *static_cast< sal_Int64 const * >(val) ); 312*cdf0e10cSrcweir break; 313*cdf0e10cSrcweir // case typelib_TypeClass_UNION: 314*cdf0e10cSrcweir // case typelib_TypeClass_ARRAY: 315*cdf0e10cSrcweir // case typelib_TypeClass_UNKNOWN: 316*cdf0e10cSrcweir // case typelib_TypeClass_SERVICE: 317*cdf0e10cSrcweir // case typelib_TypeClass_MODULE: 318*cdf0e10cSrcweir default: 319*cdf0e10cSrcweir buf.append( static_cast< sal_Unicode >('?') ); 320*cdf0e10cSrcweir break; 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir } // anon namespace 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir //============================================================================== 327*cdf0e10cSrcweir rtl::OUString anyToString( uno::Any const & value ) 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir rtl::OUStringBuffer buf; 330*cdf0e10cSrcweir appendValue( buf, value.getValue(), value.getValueTypeRef(), true ); 331*cdf0e10cSrcweir return buf.makeStringAndClear(); 332*cdf0e10cSrcweir } 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir } // namespace comphelper 335*cdf0e10cSrcweir 336