Subject:Exception while calling Java methods.Java script from XSLT Author:sravya yarabati Date:03 Dec 2015 08:51 AM Originally Posted: 03 Dec 2015 08:50 AM
Hi,
I am getting below exceptions while executing the javascript/Java method from xslt using xalan.
I included bfs 2.4.jar,js 1.7.jar,xalan 2.7.jar in my class path. Please help in calling the javascript/java method from xslt and resolve these exceptions
Caused by: javax.xml.transform.TransformerException: Could not initialize BSF Manager
at org.apache.xalan.extensions.ExtensionHandlerGeneral.<init>(ExtensionHandlerGeneral.java:216)
... 21 more
---------
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.xalan.extensions.ExtensionNamespaceSupport.launch(ExtensionNamespaceSupport.java:95)
at org.apache.xalan.extensions.ExtensionsTable.<init>(ExtensionsTable.java:66)
at org.apache.xalan.transformer.TransformerImpl.setExtensionsTable(TransformerImpl.java:441)
at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1312)
at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3447)
at org.apache.xalan.transformer.TransformerHandlerImpl.endDocument(TransformerHandlerImpl.java:408)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker.transform(JAXPSAXProcessorInvoker.java:220)
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker.transform(JAXPSAXProcessorInvoker.java:186)
at org.eclipse.wst.xsl.jaxp.debug.debugger.AbstractDebugger.run(AbstractDebugger.java:111)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.transform.TransformerException: Could not initialize BSF Manager
at org.apache.xalan.extensions.ExtensionHandlerGeneral.<init>(ExtensionHandlerGeneral.java:216)
... 21 more
file:/C:/Users/syarabat/workspace/XSLExampleProject/files/JC_Test.xsl; Line #31; Column #42; java.lang.NullPointerException
------------------------------------------------------------------------
20:37:25,465 ERROR [main] JAXPSAXProcessorInvoker - The first argument to the non-static Java function 'getdate' is not a valid object reference.
20:37:25,470 ERROR [main] JAXPSAXProcessorInvoker - Could not compile stylesheet
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformerHandler(Unknown Source)
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker.addStylesheet(JAXPSAXProcessorInvoker.java:136)
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker.addStylesheet(JAXPSAXProcessorInvoker.java:127)
at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.addStyleSheet(PipelineDefinition.java:161)
at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.configure(PipelineDefinition.java:152)
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.Main.main(Main.java:72)
20:37:25,477 FATAL [main] Main - Error with stylesheet: file:/C:/Users/syarabat/workspace/XSLExampleProject/files/JC_Test.xsl
org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException: Error with stylesheet: file:/C:/Users/syarabat/workspace/XSLExampleProject/files/JC_Test.xsl
at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.addStyleSheet(PipelineDefinition.java:163)
at org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition.configure(PipelineDefinition.java:152)
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.Main.main(Main.java:72)
XSLT script:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
xmlns:my-ext="ext1"
extension-element-prefixes="my-ext">
<xalan:component prefix="my-ext" elements="timelapse" functions="getdate">
<xalan:script lang="javascript">
var multiplier=1;
System.setProperty("org.apache.xalan.extensions.bsf.BSFManager","org.apache.bsf.BSFManager");
function timelapse(xslProcessorContext, elem)
{ multiplier=parseInt(elem.getAttribute("multiplier")); // The element return value is placed in the result tree. // If you do not want a return value, return null. return null; }
function getdate(numdays)
{ var d = new Date(); var totalDays = parseInt(numdays) * multiplier; d.setDate(d.getDate() + totalDays); return d.toLocaleString(); }
</xalan:script>
</xalan:component>
<xsl:template match="deadline">
<p><my-ext:timelapse multiplier="2"/>We have logged your enquiry and will
respond by <xsl:value-of select="my-ext:getdate(string(@numdays))"/>.</p>
</xsl:template>
</xsl:stylesheet>