Re: use of css.container.EnumerableMap

Ariel Constenla-Haile <[email protected]> Sat, 18 Dec 2010 21:34:00 -0300
Newsgroups gmane.comp.openoffice.devel.api
Message-ID <[email protected]>
Hello Paolo,

On Saturday 18 December 2010, 11:00, Paolo Mantovani wrote:
> Hi,
> 
> after many years, ooo api users have finally got a concrete generic
> implementation of an "associative array" or "Lookup table" or
> "dictionary list"
> 
> That's great! this powerful service could greatly simplify the
> development of macros / extension
> but......
> 
> unfortunately the implementation is so  complicated and counterintuitive
> that nobody will use it.
> 
> Of course I'm able to use it even in basic, but how can I explain it to
> a novice that needs to translate from VBA a macro that uses VBA
> collections? (however a starbasic example would be grateful)
> 
> 
> The new uno service needs initialization, not fully supported in starbasic

 css.container.EnumerableMap is a new-style service, with constructors, and 
AFAIK, new-style service constructors are now (I don't recall since when) 
supported in OOo Basic; did you try to use the constructors directly?

> The new uno service needs two parameters "UNO Type" a sort of object
> very complex and abstract for a novice not expert in OOP (for example
> 99% of macro writers)

I wonder how you get that to work in Basic.
AFAIK UNO type is mapped to com.sun.star.reflection.XIdlClass
vid 
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Basic/Mapping_of_Simple_Types

so I thought I needed to use reflection
http://api.openoffice.org/docs/common/ref/com/sun/star/reflection/XIdlReflection.html#getType

But passing a com.sun.star.reflection.XIdlClass to the constructors does not 
work.

Sub Test_1
	Dim aData(4)
	aData(0) = Array(1,"Enero")
	aData(1) = Array(2,"Febrero")
	aData(2) = Array(3,"Marzo")
	aData(3) = Array(4,"Abril")
	aData(4) = Array(5,"Mayo")

	Dim oReflection
	oReflection = CreateUnoService("com.sun.star.reflection.CoreReflection")
	
	Dim aKeyType, aValueType
	aKeyType = oReflection.getType(aData(0)(0))
	aValueType = oReflection.getType(aData(0)(1))
	
	Dim aMap	
	aMap = com.sun.star.container.EnumerableMap.create(aKeyType,aValueType)

	Dim aPair
	For Each aPair In aData
		aMap.put( aPair(0), aPair(1) )
	Next
		
	oReflection.dispose()
End Sub


it fails with the exception from 
http://svn.services.openoffice.org/opengrok/xref/DEV300_m95/comphelper/source/container/enumerablemap.cxx#534

Then I tried the CreateUnoValue, with no luck...

Sub Test_2
	Dim aData(4)
	aData(0) = Array(1,"Enero")
	aData(1) = Array(2,"Febrero")
	aData(2) = Array(3,"Marzo")
	aData(3) = Array(4,"Abril")
	aData(4) = Array(5,"Mayo")

	Dim oReflection
	oReflection = CreateUnoService("com.sun.star.reflection.CoreReflection")
	
	Dim aKeyType, aValueType
	aKeyType = oReflection.getType(aData(0)(0))
	aValueType = oReflection.getType(aData(0)(1))
	
	Dim aArgs(1)
	aArgs(0) = CreateUnoValue("type",aKeyType)
	aArgs(1) = CreateUnoValue("type",aValueType)
	
	Dim aMap	
	aMap = com.sun.star.container.EnumerableMap.create(aKeyType,aValueType)
	'aMap = 
GetDefaultContext().getServiceManager().createInstanceWithArgumentsAndContext(_
	'		"com.sun.star.container.EnumerableMap",aArgs, 
GetDefaultContext())

	Dim aPair
	For Each aPair In aData
		aMap.put( aPair(0), aPair(1) )
	Next
		
	oReflection.dispose()
End Sub

Here CreateUnoValue("type",aKeyType) fails.

> This kind of situations tends to discourage anybody that would like to
> migrate but is afraid of loosing his/her skills

IMHO the wrong choice was to use the UNO type instead of 
com.sun.star.uno.TypeClass. 
With TypeClass and the support for new-style service constructor, it could 
have been as simple as:

        Dim aMap        
        aMap = com.sun.star.container.EnumerableMap.create(_
                        com.sun.star.uno.TypeClass.SHORT,_
                        com.sun.star.uno.TypeClass.STRING)

In Java this look also a little complicated:


    public static void main(String[] args) {
        try {
            int bound = 5000;
            Pair[] data = new Pair[bound];
            for ( int i = 0; i < bound; i++) {
                data[i] = new Pair(Integer.valueOf(i+1), String.valueOf(i+1));
            }

            // get the remote office component context
            XComponentContext xContext = Bootstrap.bootstrap();
            if (xContext != null) {
                XEnumerableMap aMap = EnumerableMap.create(
                        xContext,
                        AnyConverter.getType(data[0].First),
                        AnyConverter.getType(data[0].Second));
                for (Pair pair : data) {
                    aMap.put(pair.First, pair.Second);
                }

                XEnumeration xEnumeration = 
aMap.createElementEnumeration(false);
                while (xEnumeration.hasMoreElements()) {
                    Object elem = xEnumeration.nextElement();
                    Pair pair = (Pair) ( (Any)elem ).getObject();
                    System.out.printf("%d - %s\n", pair.First, pair.Second);
                }
            }
        }
        catch (java.lang.Exception e){
            e.printStackTrace();
        }
        finally {
            System.exit( 0 );
        }
    }


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)

iQIcBAABCAAGBQJNDVL/AAoJEMjP1bm45QNWNDQQAJbhDPAR0EvvcbEHhgpRSk2L
uVZ3fMTr8dUq8UtSjwCDYpElt2MSdX2TAAdybB+nEry0wXAMsNfagqkpqRwnaj5q
hJOwzslQVQUQsa3OrSjoa+U6gMPiiuVsVIT/k+gvJCWyLEfOjSEyEqhhuLphAn2a
cNGOszq9/lhi71CS2kpP5liK+GAIRTRXL1sfXVTTBZvz9svaEz4XDiyci2wYkZ1N
EjjT7JBUQfgECF9b1NQm5oq4UaBNc05dyDnjrU1Ru+rjjEmKePH2w6hLOewJcdqs
X2T94yrQ0nPqlwnS0a3qt64Yin6ay8+J30PqUOUWZ6UjnhFeLaJM4XlQyCKez4kZ
aKuAmaa9k74f+ABwiPaJV9/Po0T3p2XRNGAicPHPEmPik6bDeSGGTycFf1sRTYwo
tUVLo2N0jFa+5jSjjL4yAs9eZVmMdxa/U+40JxAM6C4d5oXLXff3OTg4W0K6o4ZJ
DgRZmxJGRfpjXgvKDSV4ex+gP2WuVt+AN8PKU2yIaxcjiyGI2tbV0tBzhNXYjpxL
xw/zchkm/kiWNOVg4Y48qE3qL6snhcKM1Vjf+lDhfBI8T52WJdllM2uuCdRJFLHV
SLZYcweqlvPQTOMhEm/9C6tJDWYclShSl/00S6gwGh5+cSye3Fz1Ovtpka+MoV7b
p0ZzJUYgn5v9jD3Cu8AV
=X7Gj
-----END PGP SIGNATURE-----