1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: typeconverter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
33 #include "typeconverter.hxx"
34 #include "utility.hxx"
35 #include "simpletypehelper.hxx"
36 #include <com/sun/star/script/XTypeConverter.hpp>
37 #include <com/sun/star/script/FailReason.hpp>
38 #include <com/sun/star/uno/Type.hxx>
39 #include <strdecl.hxx>
40 #include <typelib/typedescription.hxx>
43 #include <rtl/ustring.hxx>
44 #include <osl/diagnose.h>
46 namespace uno
= ::com::sun::star::uno
;
47 namespace script
= ::com::sun::star::script
;
48 namespace lang
= ::com::sun::star::lang
;
53 //--------------------------------------------------------------------------------------------------
54 rtl::OUString
toString(const uno::Reference
< script::XTypeConverter
>& xTypeConverter
, const uno::Any
& rValue
)
55 SAL_THROW((script::CannotConvertException
, com::sun::star::uno::RuntimeException
))
58 uno::TypeClass aDestinationClass
= rValue
.getValueType().getTypeClass();
60 switch (aDestinationClass
)
62 case uno::TypeClass_BOOLEAN
:
63 case uno::TypeClass_CHAR
:
64 case uno::TypeClass_BYTE
:
65 case uno::TypeClass_SHORT
:
66 case uno::TypeClass_LONG
:
67 case uno::TypeClass_HYPER
:
68 case uno::TypeClass_FLOAT
:
69 case uno::TypeClass_DOUBLE
:
70 if (!xTypeConverter
.is())
72 throw script::CannotConvertException(
73 ::rtl::OUString::createFromAscii("Missing Converter Service!"),
74 uno::Reference
< uno::XInterface
> (),
76 script::FailReason::UNKNOWN
, 0
79 xTypeConverter
->convertToSimpleType(rValue
, uno::TypeClass_STRING
) >>= aRes
;
82 case ::uno::TypeClass_STRING
:
87 throw script::CannotConvertException(
88 ::rtl::OUString::createFromAscii("Unsupported type: ") + rValue
.getValueType().getTypeName(),
89 uno::Reference
< uno::XInterface
> (),
91 script::FailReason::TYPE_NOT_SUPPORTED
, 0
98 uno::Any
toAny(const uno::Reference
< script::XTypeConverter
>& xTypeConverter
, const ::rtl::OUString
& _rValue
,const uno::TypeClass
& _rTypeClass
)
99 SAL_THROW((script::CannotConvertException
, com::sun::star::uno::RuntimeException
))
103 if (uno::TypeClass_STRING
== _rTypeClass
)
107 else if (!xTypeConverter
.is())
109 throw script::CannotConvertException(
110 ::rtl::OUString::createFromAscii("Missing Converter Service!"),
111 uno::Reference
< uno::XInterface
> (),
113 script::FailReason::UNKNOWN
, 0
118 aRes
= xTypeConverter
->convertToSimpleType(uno::makeAny(_rValue
), _rTypeClass
);
120 catch (script::CannotConvertException
& )
122 // ok, next try with trim()
123 ::rtl::OUString
const sTrimmed
= _rValue
.trim();
124 if (sTrimmed
.getLength() != 0)
128 aRes
= xTypeConverter
->convertToSimpleType(uno::makeAny(sTrimmed
), _rTypeClass
);
130 OSL_ENSURE(aRes
.hasValue(),"Converted non-empty string to NULL\n");
132 catch (script::CannotConvertException
&)
134 OSL_ASSERT(!aRes
.hasValue());
136 catch (uno::Exception
&)
138 OSL_ENSURE(false,"Unexpected exception from XTypeConverter::convertToSimpleType()\n");
139 OSL_ASSERT(!aRes
.hasValue());
142 if (!aRes
.hasValue()) throw;
145 catch (lang::IllegalArgumentException
& iae
)
147 OSL_ENSURE(sal_False
, "Illegal argument for typeconverter. Maybe invalid typeclass ?");
148 throw script::CannotConvertException(
149 ::rtl::OUString::createFromAscii("Invalid Converter Argument:") + iae
.Message
,
150 uno::Reference
< uno::XInterface
> (),
152 script::FailReason::UNKNOWN
, 0
155 catch (uno::Exception
& e
)
157 OSL_ENSURE(false,"Unexpected exception from XTypeConverter::convertToSimpleType()\n");
158 throw script::CannotConvertException(
159 ::rtl::OUString::createFromAscii("Unexpected TypeConverter Failure:") + e
.Message
,
160 uno::Reference
< uno::XInterface
> (),
162 script::FailReason::UNKNOWN
, 0
168 ::rtl::OUString
toTypeName(const uno::TypeClass
& _rTypeClass
)
170 ::rtl::OUString aRet
;
173 case uno::TypeClass_BOOLEAN
: aRet
= TYPE_BOOLEAN
; break;
174 case uno::TypeClass_SHORT
: aRet
= TYPE_SHORT
; break;
175 case uno::TypeClass_LONG
: aRet
= TYPE_INT
; break;
176 case uno::TypeClass_HYPER
: aRet
= TYPE_LONG
; break;
177 case uno::TypeClass_DOUBLE
: aRet
= TYPE_DOUBLE
; break;
178 case uno::TypeClass_STRING
: aRet
= TYPE_STRING
; break;
179 case uno::TypeClass_SEQUENCE
: aRet
= TYPE_BINARY
; break;
180 case uno::TypeClass_ANY
: aRet
= TYPE_ANY
; break;
183 ::rtl::OString
aStr("Wrong typeclass! ");
184 aStr
+= ::rtl::OString::valueOf((sal_Int32
)_rTypeClass
);
185 OSL_ENSURE(0,aStr
.getStr());
191 // *************************************************************************
192 uno::Type
toType(const ::rtl::OUString
& _rType
)
196 if (_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("boolean"))) aRet
= SimpleTypeHelper::getBooleanType();
198 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("short"))) aRet
= SimpleTypeHelper::getShortType();
199 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("int"))) aRet
= SimpleTypeHelper::getIntType();
200 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("integer"))) aRet
= SimpleTypeHelper::getIntType();
201 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("long"))) aRet
= SimpleTypeHelper::getLongType();
203 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("double"))) aRet
= SimpleTypeHelper::getDoubleType();
205 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("string"))) aRet
= SimpleTypeHelper::getStringType();
206 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("binary"))) aRet
= SimpleTypeHelper::getBinaryType();
207 // else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("sequence"))) aRet = uno::TypeClass_SEQUENCE;
209 else if(_rType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("any"))) aRet
= SimpleTypeHelper::getAnyType();
212 ::rtl::OString
aStr("Unknown type! ");
213 aStr
+= rtl::OUStringToOString(_rType
,RTL_TEXTENCODING_ASCII_US
);
214 OSL_ENSURE(0,aStr
.getStr());
219 uno::Type
toListType(const ::rtl::OUString
& _rsElementType
)
223 if (_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("boolean")))
224 aRet
= ::getCppuType(static_cast<uno::Sequence
<sal_Bool
> const*>(0));
226 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("short")))
227 aRet
= ::getCppuType(static_cast<uno::Sequence
<sal_Int16
> const*>(0));
229 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("int")))
230 aRet
= ::getCppuType(static_cast<uno::Sequence
<sal_Int32
> const*>(0));
231 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("integer")))
232 aRet
= ::getCppuType(static_cast<uno::Sequence
<sal_Int32
> const*>(0));
234 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("long")))
235 aRet
= ::getCppuType(static_cast<uno::Sequence
<sal_Int64
> const*>(0));
237 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("double")))
238 aRet
= ::getCppuType(static_cast<uno::Sequence
<double> const*>(0));
240 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("string")))
241 aRet
= ::getCppuType(static_cast<uno::Sequence
<rtl::OUString
> const*>(0));
243 else if(_rsElementType
.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("binary")))
244 aRet
= ::getCppuType(static_cast<uno::Sequence
<uno::Sequence
<sal_Int8
> > const*>(0));
246 // else if(_rsElementType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("sequence"))) aRet = uno::TypeClass_SEQUENCE;
249 ::rtl::OString
aStr("Unknown type! ");
250 aStr
+= rtl::OUStringToOString(_rsElementType
,RTL_TEXTENCODING_ASCII_US
);
251 OSL_ENSURE(0,aStr
.getStr());
257 uno::Type
getSequenceElementType(uno::Type
const& rSequenceType
)
259 OSL_ENSURE(rSequenceType
.getTypeClass() == uno::TypeClass_SEQUENCE
,
260 "getSequenceElementType() must be called with a sequence type");
262 if (!(rSequenceType
.getTypeClass() == uno::TypeClass_SEQUENCE
))
265 uno::TypeDescription
aTD(rSequenceType
);
266 typelib_IndirectTypeDescription
* pSequenceTD
=
267 reinterpret_cast< typelib_IndirectTypeDescription
* >(aTD
.get());
269 OSL_ASSERT(pSequenceTD
);
270 OSL_ASSERT(pSequenceTD
->pType
);
272 if ( pSequenceTD
&& pSequenceTD
->pType
)
274 return uno::Type(pSequenceTD
->pType
);
279 uno::Type
getBasicType(uno::Type
const& rType
, bool& bSequence
)
281 bSequence
= rType
.getTypeClass() == uno::TypeClass_SEQUENCE
&&
282 rType
!= SimpleTypeHelper::getBinaryType();
287 return getSequenceElementType(rType
);
292 // *************************************************************************
293 ::rtl::OUString
toTemplateName(const uno::Type
& _rType
)
296 uno::Type aBaseType
= getBasicType(_rType
,bList
);
297 return toTemplateName(aBaseType
.getTypeClass(), bList
);
300 ::rtl::OUString
toTemplateName(const uno::TypeClass
& _rBasicType
, bool bList
)
302 return toTemplateName(toTypeName(_rBasicType
), bList
);
305 // *************************************************************************
306 uno::Type
parseTemplateName(::rtl::OUString
const& sTypeName
)
310 ::rtl::OUString sBasicTypeName
;
312 if (parseTemplateName(sTypeName
, sBasicTypeName
,bList
))
313 aRet
= toType(sBasicTypeName
,bList
);
314 // else leave as void
319 // *************************************************************************
320 ::rtl::OUString
toTemplateName(const ::rtl::OUString
& _rBasicTypeName
, bool bList
)
322 ::rtl::OUString sName
= TEMPLATE_MODULE_NATIVE_PREFIX
+ _rBasicTypeName
;
324 sName
+= TEMPLATE_LIST_SUFFIX
;
330 bool parseTemplateName(::rtl::OUString
const& sTypeName
, ::rtl::OUString
& _rBasicName
, bool& bList
)
332 ::rtl::OUString
const sSuffix( TEMPLATE_LIST_SUFFIX
);
334 sal_Int32 nIndex
= sTypeName
.lastIndexOf(sSuffix
);
335 if (nIndex
>= 0 && nIndex
+ sSuffix
.getLength() == sTypeName
.getLength())
338 _rBasicName
= sTypeName
.copy(0,nIndex
);
343 _rBasicName
= sTypeName
;
345 // erase the default prefix 'cfg:'
346 if (_rBasicName
.indexOf(TEMPLATE_MODULE_NATIVE_PREFIX
) == 0)
347 _rBasicName
= _rBasicName
.copy(TEMPLATE_MODULE_NATIVE_PREFIX
.m_nLen
);
352 // *************************************************************************
354 } // namespace configmgr