update dev300-m58
[ooovba.git] / configmgr / source / xml / typeconverter.cxx
bloba422073a0d44802b74c2e26ef4aa7fa3e6fc5ee5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: typeconverter.cxx,v $
10 * $Revision: 1.25 $
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;
50 namespace configmgr
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))
57 rtl::OUString aRes;
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 > (),
75 aDestinationClass,
76 script::FailReason::UNKNOWN, 0
79 xTypeConverter->convertToSimpleType(rValue, uno::TypeClass_STRING) >>= aRes;
80 break;
82 case ::uno::TypeClass_STRING:
83 rValue >>= aRes;
84 break;
86 default:
87 throw script::CannotConvertException(
88 ::rtl::OUString::createFromAscii("Unsupported type: ") + rValue.getValueType().getTypeName(),
89 uno::Reference< uno::XInterface > (),
90 aDestinationClass,
91 script::FailReason::TYPE_NOT_SUPPORTED, 0
95 return aRes;
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))
101 uno::Any aRes;
103 if (uno::TypeClass_STRING == _rTypeClass)
105 aRes <<= _rValue;
107 else if (!xTypeConverter.is())
109 throw script::CannotConvertException(
110 ::rtl::OUString::createFromAscii("Missing Converter Service!"),
111 uno::Reference< uno::XInterface > (),
112 _rTypeClass,
113 script::FailReason::UNKNOWN, 0
116 else try
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 > (),
151 _rTypeClass,
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 > (),
161 _rTypeClass,
162 script::FailReason::UNKNOWN, 0
165 return aRes;
168 ::rtl::OUString toTypeName(const uno::TypeClass& _rTypeClass)
170 ::rtl::OUString aRet;
171 switch(_rTypeClass)
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;
181 default:
183 ::rtl::OString aStr("Wrong typeclass! ");
184 aStr += ::rtl::OString::valueOf((sal_Int32)_rTypeClass);
185 OSL_ENSURE(0,aStr.getStr());
188 return aRet;
191 // *************************************************************************
192 uno::Type toType(const ::rtl::OUString& _rType)
194 uno::Type aRet;
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();
210 else
212 ::rtl::OString aStr("Unknown type! ");
213 aStr += rtl::OUStringToOString(_rType,RTL_TEXTENCODING_ASCII_US);
214 OSL_ENSURE(0,aStr.getStr());
217 return aRet;
219 uno::Type toListType(const ::rtl::OUString& _rsElementType)
221 uno::Type aRet;
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;
247 else
249 ::rtl::OString aStr("Unknown type! ");
250 aStr += rtl::OUStringToOString(_rsElementType,RTL_TEXTENCODING_ASCII_US);
251 OSL_ENSURE(0,aStr.getStr());
254 return aRet;
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))
263 return uno::Type();
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);
275 } //if
277 return uno::Type();
279 uno::Type getBasicType(uno::Type const& rType, bool& bSequence)
281 bSequence = rType.getTypeClass() == uno::TypeClass_SEQUENCE &&
282 rType != SimpleTypeHelper::getBinaryType();
284 if (!bSequence)
285 return rType;
287 return getSequenceElementType(rType);
291 // template names
292 // *************************************************************************
293 ::rtl::OUString toTemplateName(const uno::Type& _rType)
295 bool bList;
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)
308 uno::Type aRet;
310 ::rtl::OUString sBasicTypeName;
311 bool bList;
312 if (parseTemplateName(sTypeName, sBasicTypeName,bList))
313 aRet = toType(sBasicTypeName,bList);
314 // else leave as void
316 return aRet;
319 // *************************************************************************
320 ::rtl::OUString toTemplateName(const ::rtl::OUString& _rBasicTypeName, bool bList)
322 ::rtl::OUString sName = TEMPLATE_MODULE_NATIVE_PREFIX + _rBasicTypeName;
323 if (bList)
324 sName += TEMPLATE_LIST_SUFFIX;
327 return sName;
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())
337 bList = true;
338 _rBasicName = sTypeName.copy(0,nIndex);
340 else
342 bList = false;
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);
349 return true;
352 // *************************************************************************
354 } // namespace configmgr