Hi there, a couple of weeks ago I misplaced a posting in the Cocoon-listserver lists with respect to a BSF ("Bean Scripting Framework", used in quite a few places of Apache projects) engine not working (the correct place was Xalan and BSF).
This message is just to state, that BSF works and that the problem I ran into my own implementation of a scripting language (Rexx, cf. <http://www2.hursley.ibm.com/rexx/>, <http://www.rexxla.org/Links/links.html>) running under BSF has been resolved. In order to present the entire case, I forward you the message clarifying the problem and the resolution mostly for BSF developers. In addition you will find those files attached, which demonstrate how to define and use an extension to XSLT, where the function is implemented in Rexx and JavaScript. Maybe that helps the one or other solve problems. Regards, ---rony P.S.: I will be subscribed to the Cocoon lists for a week in case there are questions w.r.t. BSF. -------- Original Message -------- Betreff: A feature suggestion (and a solution to ... Re: [Bsf-discussion] Where's the script code if in BSFEngine.call() ??? Request for help. Datum: Thu, 03 Oct 2002 16:30:25 +0200 Von: "Rony G. Flatscher" <[EMAIL PROTECTED]> An: [EMAIL PROTECTED] Hi, on 2002-09-17 I sought help for the following problem: ------------------ cut here ----------------- Hi there, in the course of tracing down a particular problem with a particular BSFEngine it turns out that it's call() method does *not* supply the script code to be executed, but rather the name of the function/method to be invoked and optional arguments. So, could anyone shed some light where one could find the script code in such a situation? [The problem occurred for the first time with the RexxEngine, which assumes in the call() method that the code either is passed in as the first argument "object" or as the second argument "name", which seems to not be correct at all! As Apache's Cocoon (acutally Xalan) employs BSF it would be very productive, if one could write XSLT-extensions using the Rexx language. But at the moment I am clueless, where to locate/retrieve the script-code to be executed/passed to the Rexx interpreter; the name of the function as well as the arguments arrive as expected.] Any help and any hints *very* welcome ! Regards, ---rony ------------------ cut here ----------------- In the meantime I found the solution, thanks to the very kind help of one of Xalan's developers who informed me, that prior to invoking BSFEngine.call() the method BSFEngine.exec() is invoked, so that it becomes possible for scripting engines to compile the script (if possible for that language at all). So the solution was to "catch" the source in method BSFEngine.exec() and use it for invocation in method BSFEngine.call(). The appropriately adjusted RexxEninge.java and RexxEngine.class can be found at: <http://nestroy.wi-inf.uni-essen.de/Forschung/rgf/> or <http://nestroy.wi-inf.uni-essen.de/Forschung/rgf/bsf4rexx/index.htm> (You could point either to the bsf*.jar in the above package or extend your BSF environment with the above Rexx BSF engine.) The attached files, originally created by Gerald Michalitz, demonstrate the usage of the XSLT extension mechanism via BSF in Apache's Xalan (the Java implementation). As a matter of fact the supplied files allow you to use Apache's "publishing frontend" Cocoon for testing purposes: this way one can use any of the BSF-supported languages to create functions which can be used/invoked in the XSL-transformation process. *Very* powerful stuff. Hence, I thought this might be interesting for readers of this list (users as well as developers). Mereley replace the Rexx-code with your language's code and adjust the programming language attribute to contain the name known to BSF. --- FEATURE SUGGESTION for BSF: For purposes like this BSF *should* define a method "BSFEngine.prepareScript()" with the same signature as BSFEngine.apply(): this method should be invoked immediately before "BSFEngine.call()". BSF-languages could then use this to pre-compile the code, interpreter-only languages could use this method to get a hold of the source code method BSFEngine.call() expects to be executed. --- Regards, ---rony
<?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" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <xsl:param name="view-source"/> <xsl:template match="page"> <html> <head> <title> <xsl:value-of select="title"/> </title> </head> <body bgcolor="white" alink="red" link="blue" vlink="blue"> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="title"> <h2 style="color: navy; text-align: center"> <xsl:if test="not($view-source)"> <xsl:apply-templates/> </xsl:if> <xsl:if test="$view-source"> <A> <xsl:attribute name="HREF">../view-source?filename=/<xsl:value-of select="$view-source"/></xsl:attribute> <xsl:attribute name="TARGET">_blank</xsl:attribute> <xsl:apply-templates/> </A> </xsl:if> </h2> </xsl:template> <xsl:template match="para"> <p align="left"> <i><xsl:apply-templates/></i> </p> </xsl:template> <xsl:template match="@*|node()" priority="-2"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:template> <!--The component and its script are in the lxslt namespace and define the implementation of the extension.--> <lxslt:component prefix="my-ext" functions="calctime"> <lxslt:script lang="rexx"> <![CDATA[ /* calctime rexx - [Object] Rexx, ---rgf, 2002-09-27, wuw */ if arg()=0 then return "(Rexx-calctime: sorry no argument received!)" parse arg mon +3 . day "," year . /* parse date */ due_date = date("s", day mon year, "n") /* convert to sorted date */ due_days = date("b", day mon year, "n") /* convert argument into # of days since 0001-01-01 */ today_date = date("s") /* get today's date in sorted date */ today_days = date("b", today_date, "s") /* convert to days since 0001-01-01 */ days=today_days-due_days /* calc difference of days */ /* build common return text */ tmpStr="Received due date=[" || due_date || "], today=[" || today_date tmpStr=tmpStr || "], hence difference=[" || days || "] days." if days<7 then return tmpStr "We tolerate this!" else if days=7 then return tmpStr "Lucky you, we still tolerate this!" return tmpStr "Sorry, we can't accept your delay!" ]]> </lxslt:script> </lxslt:component> <xsl:template match="deadline"> <xsl:value-of select="my-ext:calctime(string(@due_date))"/> </xsl:template> </xsl:stylesheet>
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <xsl:template match="/"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="page" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-before extent="3cm"/> <fo:region-body margin-top="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> <fo:page-sequence-master master-name="all"> <fo:repeatable-page-master-alternatives> <fo:conditional-page-master-reference master-reference="page" page-position="first"/> </fo:repeatable-page-master-alternatives> </fo:page-sequence-master> </fo:layout-master-set> <fo:page-sequence master-reference="all"> <fo:static-content flow-name="xsl-region-after"> <fo:block text-align="right" font-size="10pt" font-family="serif" line-height="14pt">Page: <fo:page-number/></fo:block> </fo:static-content> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="title"> <fo:block font-size="36pt" space-before.optimum="24pt" text-align="center"><xsl:apply-templates/></fo:block> </xsl:template> <xsl:template match="para"> <fo:block font-size="12pt" space-before.optimum="12pt" text-align="center"><xsl:apply-templates/></fo:block> </xsl:template> <!--The component and its script are in the lxslt namespace and define the implementation of the extension.--> <lxslt:component prefix="my-ext" elements="timelapse" functions="getdate"> <lxslt:script lang="javascript"> <![CDATA[ function getdate(duedate) { var due_date = new Date(duedate); var now = new Date(); var diff = now.getTime() - due_date.getTime(); var days = Math.floor(diff / (1000 * 60 * 60 * 24)); if (days < 7) { var data = "The difference between the due date: " + due_date + " and today: " + now + " are " + days + " days and we tolreate this!"; return data; } else if (days > 7) { var data = "The difference between the due date: " + due_date + " and today: " + now + " are " + days + " days and sorry, we can't accept your delay!"; return data; } else if (days == 7) { var data = "The difference between the due date:: " + due_date + " and today: " + now + " are" + days + " days and lucky you, we still tolerate this!"; return data; } else { var data = "It's already done!"; return data; } }]]> </lxslt:script> </lxslt:component> <xsl:template match="deadline"> <fo:block font-size="12pt" space-before.optimum="12pt" text-align="center"> <xsl:value-of select="my-ext:getdate(string(@due_date))"/> </fo:block> </xsl:template> </xsl:stylesheet>
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <xsl:template match="/"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="page" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-before extent="3cm"/> <fo:region-body margin-top="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> <fo:page-sequence-master master-name="all"> <fo:repeatable-page-master-alternatives> <fo:conditional-page-master-reference master-reference="page" page-position="first"/> </fo:repeatable-page-master-alternatives> </fo:page-sequence-master> </fo:layout-master-set> <fo:page-sequence master-reference="all"> <fo:static-content flow-name="xsl-region-after"> <fo:block text-align="right" font-size="10pt" font-family="serif" line-height="14pt">Page: <fo:page-number/></fo:block> </fo:static-content> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="title"> <fo:block font-size="36pt" space-before.optimum="24pt" text-align="center"><xsl:apply-templates/></fo:block> </xsl:template> <xsl:template match="para"> <fo:block font-size="12pt" font-color="red" space-before.optimum="12pt" text-align="center"><xsl:apply-templates/></fo:block> </xsl:template> <!--The component and its script are in the lxslt namespace and define the implementation of the extension.--> <lxslt:component prefix="my-ext" functions="calctime"> <lxslt:script lang="rexx"> <![CDATA[ /* calctime rexx - [Object] Rexx, ---rgf, 2002-09-27, wuw */ if arg()=0 then return "(Rexx-calctime: sorry no argument received!)" parse arg mon +3 . day "," year . /* parse date */ due_date = date("s", day mon year, "n") /* convert to sorted date */ due_days = date("b", day mon year, "n") /* convert argument into # of days since 0001-01-01 */ today_date = date("s") /* get today's date in sorted date */ today_days = date("b", today_date, "s") /* convert to days since 0001-01-01 */ days=today_days-due_days /* calc difference of days */ /* build common return text */ tmpStr="Received due date=[" || due_date || "], today=[" || today_date tmpStr=tmpStr || "], hence difference=[" || days || "] days." if days<7 then return tmpStr "We tolerate this!" else if days=7 then return tmpStr "Lucky you, we still tolerate this!" return tmpStr "Sorry, we can't accept your delay!" ]]> </lxslt:script> </lxslt:component> <xsl:template match="deadline"> <fo:block font-size="12pt" space-before.optimum="12pt" text-align="center"> <xsl:value-of select="my-ext:calctime(string(@due_date))"/> </fo:block> </xsl:template> </xsl:stylesheet>
<?xml version="1.0"?> <!DOCTYPE page [ <!ELEMENT page (title?, content)> <!ELEMENT title (#PCDATA)> <!ELEMENT content (para+, deadline)> <!ELEMENT para (#PCDATA)> <!ELEMENT deadline (#PCDATA)> <!ATTLIST deadline due_date CDATA #REQUIRED > ]> <?xml-stylesheet type="text/xsl"?> <page> <title>XSL-Transformation of a XML-file to a HTML-file or to a PDF-file</title> <content> <para>This is an output of an xsl-transformation of an xml-file to a html-file or a pdf-file, using bsf (bean scripting framework) and a Javascript or a (object)rexx script.</para> <deadline due_date="June 30, 2002"/> </content> </page>
<?xml version="1.0"?> <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0"> <!-- =========================== Components ================================ --> <map:components> <map:matchers default="wildcard"/> <map:selectors default="browser"/> <!-- <map:actions/> --> <map:generators default="file" label="content"/> <map:transformers default="xslt"/> <map:readers default="resource"/> <map:serializers default="html"/> <map:serializer name="fo2pdf" logger="sitemap.serializer.fo2pdf" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf"/> </map:components> <!-- =========================== Pipelines ================================= --> <map:pipelines> <map:pipeline> <map:match pattern="hallo-javascript.pdf"> <map:generate src="hello-page.xml"/> <map:transform src="page2fo-javascript.xsl"/> <map:serialize type="fo2pdf"/> </map:match> </map:pipeline> <map:pipeline> <map:match pattern="hallo-javascript.html"> <map:generate src="hello-page.xml"/> <map:transform src="page2html-javascript.xsl"/> <map:serialize/> </map:match> </map:pipeline> <map:pipeline> <map:match pattern="hallo-objectrexx.html"> <map:generate src="hello-page.xml"/> <map:transform src="page2html-objectrexx.xsl"/> <map:serialize/> </map:match> </map:pipeline> <map:pipeline> <map:match pattern="hallo-objectrexx1.html"> <map:generate src="hello-page.xml"/> <map:transform src="page2html-objectrexx1.xsl"/> <map:serialize/> </map:match> </map:pipeline> <map:pipeline> <map:match pattern="hallo-objectrexx.pdf"> <map:generate src="hello-page.xml"/> <map:transform src="page2fo-objectrexx.xsl"/> <map:serialize type="fo2pdf"/> </map:match> </map:pipeline> </map:pipelines> </map:sitemap> <!-- end of file -->
<?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" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <xsl:param name="view-source"/> <xsl:template match="page"> <html> <head> <title> <xsl:value-of select="title"/> </title> </head> <body bgcolor="white" alink="red" link="blue" vlink="blue"> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="title"> <h2 style="color: navy; text-align: center"> <xsl:if test="not($view-source)"> <xsl:apply-templates/> </xsl:if> <xsl:if test="$view-source"> <A> <xsl:attribute name="HREF">../view-source?filename=/<xsl:value-of select="$view-source"/></xsl:attribute> <xsl:attribute name="TARGET">_blank</xsl:attribute> <xsl:apply-templates/> </A> </xsl:if> </h2> </xsl:template> <xsl:template match="para"> <p align="left"> <i><xsl:apply-templates/></i> </p> </xsl:template> <xsl:template match="@*|node()" priority="-2"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:template> <!--The component and its script are in the lxslt namespace and define the implementation of the extension. --> <lxslt:component prefix="my-ext" elements="timelapse" functions="getdate"> <lxslt:script lang="javascript"> <![CDATA[ function getdate(duedate) { var due_date = new Date(duedate); var now = new Date(); var diff = now.getTime() - due_date.getTime(); var days = Math.floor(diff / (1000 * 60 * 60 * 24)); if (days < 7) { var data = "The difference between the due date: " + due_date + " and today: " + now + " are " + days + " days and we tolreate this!"; return data; } else if (days > 7) { var data = "The difference between the due date: " + due_date + " and today: " + now + " are " + days + " days and sorry, we can't accept your delay!"; return data; } else if (days == 7) { var data = "The difference between the due date:: " + due_date + " and today: " + now + " are" + days + " days and lucky you, we still tolerate this!"; return data; } else { var data = "It's already done!"; return data; } }]]> </lxslt:script> </lxslt:component> <xsl:template match="deadline"> <p> <xsl:value-of select="my-ext:getdate(string(@due_date))"/> </p> </xsl:template> </xsl:stylesheet>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]