You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Nils Kaiser <Ni...@gmx.net> on 2006/04/30 13:38:06 UTC

Registering an extension programmatically (not from xsl file)

Hello,

I need to implement an extension to access some external content. The 
content is different on a per-user base and so I need to specify the 
right source to the extension before it is used (the xslt file itself 
does not know about the user...).

How do I specify an extension and its already instanced class 
programmatically before doing the transformation?

Thx,

Nils Kaiser

Re: Registering an extension programmatically (not from xsl file)

Posted by Nils Kaiser <Ni...@gmx.net>.
Thank you very much Henry, that works nicely. I was solving the problem 
by generating a key and access my data through a singleton but now I can 
get rid of it.

BTW, I use the extension to access external content because implementing 
an URIResolver did not work for me. So I return my content as a w3c DOM. 
Would be using an URIResolver faster (as I may be able to return the 
content as SAX stream)?

Can I also return the content as a DTM or any other internal XSLTC 
structure - or does this work for Xalan only?

Thx,

Nils

Henry Zongaro schrieb:
>      The documentation on using Java extensions in XSLTC is poor.  Reading 
> the code, the processor tries to use static information about the types of 
> arguments to determine which method to invoke.  In the case of a 
> stylesheet parameter, it has no static information about the type, so it's 
> only able to match the argument to java.lang.Object.
>
>      You could try making a static method on your class that accepts a 
> java.lang.Object.  Assuming all the instance methods derive from a common 
> base class, you could then cast the Object to the base class, and invoke 
> the method you really want to use.
>
>      I hope that helps.
>
> Thanks,
>
> Henry
> ------------------------------------------------------------------
> Henry Zongaro      Xalan development
> IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044
> mailto:zongaro@ca.ibm.com
>
>
>
>
>   


Re: Registering an extension programmatically (not from xsl file)

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hello, Nils.

Nils Kaiser <Ni...@gmx.net> wrote on 2006-05-02 11:11:45 AM:
> As you mentioned it is not working with XSLTC.
> 
> I get an exception (translated):
> 
> Data type 'reference' cannot be converted to
> "de.kaiser.extensions.Context".

     The documentation on using Java extensions in XSLTC is poor.  Reading 
the code, the processor tries to use static information about the types of 
arguments to determine which method to invoke.  In the case of a 
stylesheet parameter, it has no static information about the type, so it's 
only able to match the argument to java.lang.Object.

     You could try making a static method on your class that accepts a 
java.lang.Object.  Assuming all the instance methods derive from a common 
base class, you could then cast the Object to the base class, and invoke 
the method you really want to use.

     I hope that helps.

Thanks,

Henry
------------------------------------------------------------------
Henry Zongaro      Xalan development
IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com



Re: Registering an extension programmatically (not from xsl file)

Posted by Nils Kaiser <Ni...@gmx.net>.
As you mentioned it is not working with XSLTC.

I get an exception (translated):

Data type 'reference' cannot be converted to "de.kaiser.extensions.Context".

Does anyone know a solution to this?

Thx,

Nils

Nils Kaiser schrieb:
>> Nils,
>>
>> You can register a specific instance of an extension class, on a per 
>> transformation basis,
>> using the transformer.setParameter() method. You still need to 
>> declare the namespace
>> in the XSL declaration, then you need to declare the <xsl:param 
>> name="myInstance"/>
>> as a global variable. Now you can call methods on the specific 
>> instance with something like
>> <xsl:value-of select="myInterface:someMethod($myInstance, param1, 
>> param2...)"/>
>>
>> Xalan uses reflection to resolve method names on the fly so in theory 
>> you can use the setParameter
>> to set myInstance to a different class but you may run into issues it 
>> you are trying to cache/reuse
>> templates. I would make MyInstance be something more generic that 
>> could class load a specific
>> class depending on the instance data...
>>
>> BTW.. This info is for interpretive Xalan, this may behave 
>> differently in compiled XSL...
>>
>> Hope this helps
>> John G
>>
>>
>
>
>


Re: Registering an extension programmatically (not from xsl file)

Posted by Nils Kaiser <Ni...@gmx.net>.
Thank you John,

This is exactly what I was looking for.

Has anyone tried this out on XSLTC?

Greetings,

Nils Kaiser

John Gentilin schrieb:
> Nils,
>
> You can register a specific instance of an extension class, on a per 
> transformation basis,
> using the transformer.setParameter() method. You still need to declare 
> the namespace
> in the XSL declaration, then you need to declare the <xsl:param 
> name="myInstance"/>
> as a global variable. Now you can call methods on the specific 
> instance with something like
> <xsl:value-of select="myInterface:someMethod($myInstance, param1, 
> param2...)"/>
>
> Xalan uses reflection to resolve method names on the fly so in theory 
> you can use the setParameter
> to set myInstance to a different class but you may run into issues it 
> you are trying to cache/reuse
> templates. I would make MyInstance be something more generic that 
> could class load a specific
> class depending on the instance data...
>
> BTW.. This info is for interpretive Xalan, this may behave differently 
> in compiled XSL...
>
> Hope this helps
> John G
>
>