1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "pyuno_impl.hxx"
#include <rtl/ustrbuf.hxx>
#include <rtl/strbuf.hxx>
#include <typelib/typedescription.hxx>
using com::sun::star::uno::TypeClass;
using com::sun::star::uno::Type;
using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::Any;
using com::sun::star::uno::XInterface;
using com::sun::star::uno::Reference;
using com::sun::star::uno::TypeDescription;
namespace pyuno
{
const char *typeClassToString( TypeClass t )
{
const char * ret = 0;
switch (t)
{
case com::sun::star::uno::TypeClass_VOID:
ret = "VOID"; break;
case com::sun::star::uno::TypeClass_CHAR:
ret = "CHAR"; break;
case com::sun::star::uno::TypeClass_BOOLEAN:
ret = "BOOLEAN"; break;
case com::sun::star::uno::TypeClass_BYTE:
ret = "BYTE"; break;
case com::sun::star::uno::TypeClass_SHORT:
ret = "SHORT"; break;
case com::sun::star::uno::TypeClass_UNSIGNED_SHORT:
ret = "UNSIGNED_SHORT"; break;
case com::sun::star::uno::TypeClass_LONG:
ret = "LONG"; break;
case com::sun::star::uno::TypeClass_UNSIGNED_LONG:
ret = "UNSIGNED_LONG"; break;
case com::sun::star::uno::TypeClass_HYPER:
ret = "HYPER"; break;
case com::sun::star::uno::TypeClass_UNSIGNED_HYPER:
ret = "UNSIGNED_HYPER"; break;
case com::sun::star::uno::TypeClass_FLOAT:
ret = "FLOAT"; break;
case com::sun::star::uno::TypeClass_DOUBLE:
ret = "DOUBLE"; break;
case com::sun::star::uno::TypeClass_STRING:
ret = "STRING"; break;
case com::sun::star::uno::TypeClass_TYPE:
ret = "TYPE"; break;
case com::sun::star::uno::TypeClass_ANY:
ret = "ANY";break;
case com::sun::star::uno::TypeClass_ENUM:
ret = "ENUM";break;
case com::sun::star::uno::TypeClass_STRUCT:
ret = "STRUCT"; break;
case com::sun::star::uno::TypeClass_EXCEPTION:
ret = "EXCEPTION"; break;
case com::sun::star::uno::TypeClass_SEQUENCE:
ret = "SEQUENCE"; break;
case com::sun::star::uno::TypeClass_INTERFACE:
ret = "INTERFACE"; break;
case com::sun::star::uno::TypeClass_TYPEDEF:
ret = "TYPEDEF"; break;
case com::sun::star::uno::TypeClass_SERVICE:
ret = "SERVICE"; break;
case com::sun::star::uno::TypeClass_MODULE:
ret = "MODULE"; break;
case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
ret = "INTERFACE_METHOD"; break;
case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
ret = "INTERFACE_ATTRIBUTE"; break;
default:
ret = "UNKNOWN"; break;
}
return ret;
}
static PyRef getClass( const Runtime & r , const char * name)
{
return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*) name ) );
}
PyRef getTypeClass( const Runtime & r )
{
return getClass( r , "Type" );
}
PyRef getEnumClass( const Runtime & r )
{
return getClass( r , "Enum" );
}
PyRef getCharClass( const Runtime & r )
{
return getClass( r , "Char" );
}
PyRef getByteSequenceClass( const Runtime & r )
{
return getClass( r , "ByteSequence" );
}
PyRef getAnyClass( const Runtime & r )
{
return getClass( r , "Any" );
}
sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
{
PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
if( ! PyUnicode_Check( value.get() ) )
{
throw RuntimeException(
"attribute value of uno.Char is not a unicode string",
Reference< XInterface > () );
}
if( PyUnicode_GetSize( value.get() ) < 1 )
{
throw RuntimeException(
"uno.Char contains an empty unicode string",
Reference< XInterface > () );
}
sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
return c;
}
Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
{
Any ret;
PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
{
throw RuntimeException(
"attributes typeName and/or value of uno.Enum are not strings",
Reference< XInterface > () );
}
OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
char *stringValue = PyStr_AsString( value.get() );
TypeDescription desc( strTypeName );
if( desc.is() )
{
if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
{
OUStringBuffer buf;
buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
buf.appendAscii(
typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
buf.appendAscii( ", expected ENUM" );
throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
}
desc.makeComplete();
typelib_EnumTypeDescription *pEnumDesc = (typelib_EnumTypeDescription*) desc.get();
int i = 0;
for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
{
if( (*((OUString *)&pEnumDesc->ppEnumNames[i])).equalsAscii( stringValue ) )
{
break;
}
}
if( i == pEnumDesc->nEnumValues )
{
OUStringBuffer buf;
buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
buf.appendAscii( PyStr_AsString( typeName.get() ) );
throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
}
ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
}
else
{
OUStringBuffer buf;
buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" );
throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
}
return ret;
}
Type PyType2Type( PyObject * o ) throw(RuntimeException )
{
PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
if( !PyStr_Check( pyName.get() ) )
{
throw RuntimeException(
"type object does not have typeName property",
Reference< XInterface > () );
}
PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
Any enumValue = PyEnum2Enum( pyTC.get() );
OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
TypeDescription desc( name );
if( ! desc.is() )
{
OUStringBuffer buf;
buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
throw RuntimeException(
buf.makeStringAndClear(), Reference< XInterface > () );
}
if( desc.get()->eTypeClass != (typelib_TypeClass) *(sal_Int32*)enumValue.getValue() )
{
OUStringBuffer buf;
buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
buf.appendAscii( ", but type got construct with typeclass " );
buf.appendAscii( typeClassToString( (TypeClass) *(sal_Int32*)enumValue.getValue() ) );
throw RuntimeException(
buf.makeStringAndClear(), Reference< XInterface > () );
}
return desc.get()->pWeakRef;
}
static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
{
PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*)clazz ) );
if( ! code.is() )
{
OStringBuffer buf;
buf.append( "couldn't access uno." );
buf.append( clazz );
PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
return NULL;
}
PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
Py_XINCREF( instance.get() );
return instance.get();
}
PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
{
PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
return callCtor( r, "Enum" , args );
}
PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
{
// retrieve type object
PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
if( ! typeClass )
return NULL;
PyTuple_SetItem( args.get() , 1 , typeClass);
return callCtor( r, "Type" , args );
}
PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
{
// retrieve type object
PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
Py_UNICODE u[2];
u[0] = val;
u[1] = 0;
PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
return callCtor( r, "Char" , args );
}
PyObject *PyUNO_ByteSequence_new(
const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
{
PyRef str(
PyStrBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
SAL_NO_ACQUIRE );
PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
return callCtor( r, "ByteSequence" , args );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|