You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by in...@marketing.net.uk on 2000/11/24 10:57:42 UTC

Re: Using Xalan with JPython via BSF

I wrote:

> I'm trying to work out how to use Apache Xalan with JPython.
> Xalan uses BSF for its scripting.

and reported no success.

I've investigated the problem further, and my initial problem was due to
whitespace -
a JPython function declaration must begin at the left margin when included
in a <lsxlt:script> tag.

I added an element tag "testElement", and received the JPython error:

testElement(org.apache.xalan.xslt.XSLProcessorContext@7ddc31,org.apache.xalan.xslt.ElemExtensionCall@35282)
                                                      ^
SyntaxError: Lexical error at line 1, column 54.  Encountered: "@" (64),
after : ""

The problem here is that Python doesn't allow the @ character to be used in
function arguments. Since the
"org.apache.xalan.xslt.XSLProcessorContext@7ddc31" it complains
about appeared to be the default Java Object.toString(), I tried adding
toString()
methods to both ElemExtensionCall and XSLProcessorContext.

and the error changed to a JPython Name error -
     NameError: org
where I'd changed the toString for XSLProcessorContext to return
"org.apache.xalan.xslt.XSLProcessorContext"

Interestingly, when I changed both toString() methods to return "123", then
the call to testElement succeeded, but the context and element were the
number 123, and obviously not too useful for retrieving document data.

A similar problem occurs when passing arguments to a function rather than
an element - anything is passed other than a number, then a JPython Name
error occurs.

My intuition is that the problem is somewhere in the BSF engine code for
passing Java objects to the JPython - but that is purely a guess.

I'll investigate further, but I'd be grateful for any pointers.



I'm using Xalan 1.2 (although I also tested with Xalan 2.0D01), JPython
1.1, and the BSF shipped with Xalan - I'm not sure how to check its version
number.

The Java I'm using is trivial... essentially it's
    XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
    processor.process(new XSLTInputSource("foo.xml"),
                      new XSLTInputSource("foo.xsl"),
                      new XSLTResultTarget(System.out));

This is the stylesheet I'm using:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:lxslt="http://xml.apache.org/xslt"
    extension-element-prefixes="corianderpy"  xmlns:corianderpy
="corianderpy">

<lxslt:component prefix="corianderpy" functions="doImport" elements
="testelement">
<lxslt:script lang="jpython">

# Note the indentation - the function declaration must be
# flush with the left margin
def testFunction(arg1):
    return arg1*2

def testElement(context,element):
    return "Hello"
</lxslt:script>
</lxslt:component>

  <xsl:template match="/">
    <xsl:value-of select="corianderpy:testFunction(2)"/>
    <corianderpy:testElement anattr="yes" />
  </xsl:template>
</xsl:stylesheet>


Inigo Surguy


Re: Using Xalan with JPython via BSF

Posted by Gary L Peskin <ga...@firstech.com>.
Gary L Peskin wrote:
> inigo@marketing.net.uk wrote:
> > I added an element tag "testElement", and received the JPython error:
> >
> > testElement(org.apache.xalan.xslt.XSLProcessorContext@7ddc31,org.apache.xalan.xslt.ElemExtensionCall@35282)
> >                                                       ^
> > SyntaxError: Lexical error at line 1, column 54.  Encountered: "@" (64),
> > after : ""
> >
> > My intuition is that the problem is somewhere in the BSF engine code for
> > passing Java objects to the JPython - but that is purely a guess.
> >
> > I'll investigate further, but I'd be grateful for any pointers.
> 
> I'm not a JPython person but glancing at the BSF JPython engine, it
> looks like it's just forming a string a passing that string onto the
> PythonInterpreter.eval() or exec() methods.  Arguments are converted to
> a string using the toString() method.  This won't do for extension
> element invocation since we need to pass java objects to the JPython
> method -- not strings.
> 
> Hopefully, someone will be able to give you a more complete answer but
> if you're looking for a place to start on your own, I'd write a small
> Java program that will create some java objects and pass those in to a
> JPython script that receives those objects.  Then, you can post your
> Java program here and we can figure out the changes to propose to the
> BSF people.
> 
> This really does seem like a BSF problem to me.
> 
> Gary

Looking at this just a little further, it seems that this might be fixed
by generating dummy names for the arguments and using
PythonInterpreter.set() for each of the arguments followed by an exec()
or eval().  I'll try to mess around with this Monday and see if I get
anywhere.

Gary

Re: Using Xalan with JPython via BSF

Posted by Gary L Peskin <ga...@firstech.com>.
inigo@marketing.net.uk wrote:
> I added an element tag "testElement", and received the JPython error:
> 
> testElement(org.apache.xalan.xslt.XSLProcessorContext@7ddc31,org.apache.xalan.xslt.ElemExtensionCall@35282)
>                                                       ^
> SyntaxError: Lexical error at line 1, column 54.  Encountered: "@" (64),
> after : ""
> 
> My intuition is that the problem is somewhere in the BSF engine code for
> passing Java objects to the JPython - but that is purely a guess.
> 
> I'll investigate further, but I'd be grateful for any pointers.

I'm not a JPython person but glancing at the BSF JPython engine, it
looks like it's just forming a string a passing that string onto the
PythonInterpreter.eval() or exec() methods.  Arguments are converted to
a string using the toString() method.  This won't do for extension
element invocation since we need to pass java objects to the JPython
method -- not strings.

Hopefully, someone will be able to give you a more complete answer but
if you're looking for a place to start on your own, I'd write a small
Java program that will create some java objects and pass those in to a
JPython script that receives those objects.  Then, you can post your
Java program here and we can figure out the changes to propose to the
BSF people.

This really does seem like a BSF problem to me.

Gary