You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Gary L Peskin <ga...@firstech.com> on 2000/12/14 18:54:00 UTC

Re: Xalan Extensions: using a single instance rather than new instantiation

Bassi Suk wrote:
> Therefore, in the XSLT stylesheet we want to call the
> mySingleton.getInstance() and get an instance of mySingleton and then be
> able to use the mySingleton.myMethod().
> 
> The QUESTION is, what should the XSLT syntax be for the above to occur.

Suk --

This depends on whether you're using XalanJ1 or XalanJ2, as I'll explain
below.  Let's start with XalanJ1 since that's the most stable version,
for now.

First of all, I recommend not using the lxslt:component/script
construct.  It's not needed for java language extensions.  Second, you
don't need the extension-element-prefixes attribute.  That's only if you
have extension -elements- and your example is limited to an extension
-function-.

In XalanJ1, you should be able to prefix your namespace URI with
"class:" to prevent instantiation of the class by Xalan.  This is not
necessary in XalanJ2.

Finally, you might want to consider just making myMethod a static
method.  That way you wouldn't need to instantiate any instances of
mySingleton at all.

Assuming myMethod is static, your java would look like:

public class mySingleton {
  private mySingleton() {}
  public static String myMethod() {
    return "We are humbled";
  }
}

You could invoke this method with a stylesheet that looks like this:

<?xml version="1.0" ?> 
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xyzExt="class:mySingleton"
        version="1.0">

<xsl:variable name="value" select="xyzExt:myMethod()"/>
 ...
</xsl:stylesheet>

If you wanted to stick with instantiating a singleton object, you java
code would look like that in your email and the stylesheet could look
like this:


<?xml version="1.0" ?> 
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xyzExt="class:mySingleton"
        version="1.0">

<xsl:variable name="my-singleton" select="xyzExt:getInstance()"/>
<xsl:variable name="value" select="xyzExt:myMethod($my-singleton)"/>
 ...
</xsl:stylesheet>

When calling an instance method, you need to supply the object as the
first argument to the method call.  The extension mechanism uses the
first argument to determine on what object the method is being invoked. 
It then strips off that first argument and passes the remaining
arguments to the actual java function.

HTH,
Gary