[XML-SIG] Re: [JPYTHON] saxlib and enumerations

Jim Hugunin hugunin@cnri.reston.va.us
Wed, 18 Mar 1998 10:44:30 -0500

> Here's what my Java-trasnlated code to handle attributes looks like:
>         attNames = atts.getAttributeNames()
>         while (attNames.hasMoreElements()):
>                 aname = attNames.nextElement()
>                 ...
> attNames is a Java "enumeration object". Of course the Python equivalent
> is a sequence. So the code would look like this if I was using a Python
> parser:
>         attNames = atts.getAttributeNames()
>         for (aname in attNames.hasMoreElements()):
>                 ...
> I can think of three ways to address this:
>  #1. We could abandon the idea of using Java parsers directly in
> JPython, and always require a thin "wrapper" that translates Java
> enumerations to Python sequences.
>  #2. We could ask Jim to wrap *all* Java enumerations in Python
> sequences.
>  #3. We could port the Java "enumeration" concept to Python for use with
> saxlib.

I feel very strongly that either 1 or 2 are the right options.  This is
clearly an area where Python's syntax is far superior to Java's and there's no
reason to cripple it.  Which one depends on a few factors.

I am willing to wrap all Java objects that implement the java.util.Enumeration
interface so that they behave reasonably under JPython.  I've been willing to
do this for quite some time, but nobody has convinced me it was important
enough to invest the time until now.

There is one important issue here though:  An Enumeration is not the same a
Python list.  The only thing you'd be able to do with an Enumeration object is
to iterate over its items one time in the forward direction.

So, this would work:
attNames = atts.getAttributeNames()
for aname in attNames:
	#do something

But these wouldn't:

This is an important characteristic of enumerations, and it's not something I
plan to get rid of.  I don't know enough about SAX to be able to say whether
or not this is the appropriate abstraction here.

If what is really wanted is a Python list, then you will need to go with
option #1.

I hope this makes sense, and I'm eager to learn whether or not Enumerations
are the appropriate abstraction here (and thus something I should implement
before I get the next JPython release out the door).


PS - In JDK 1.2 Java defines a number of much better generic container
interfaces.  I have every intention of making all Java objects that support
these container interfaces as easy to use as Python's builtin containers.