CookXml BSF Extension


 

 

Support This Project

 

stats counter

 

Logo SourceForge.net Logo IntelliJ IDEA

This extension basically adds a <script> tag, which supports Bean Scripting Framework in CookXml. The exact script language can be specified at run time. BSF adds only tiny bit of overheads in script loading, but it provides a framework where different scripting languages could be easily added. The down side is that accessing objects declared in CookXml documents using "id" attribute are not as easily accessed. One needs to call bsf.lookupBean (idName) to get the object.

Tutorial

To add <script> to your existing tag library, say CookSwing, add the following code:

	CookBSFLib.setupTags (CookSwing.getSwingTagLibrary ());

The type of the object this tag creates depends on the script. Script can be inserted in two ways: inside the tag, or in another file/resource specified using "src" attribute.

	<button text="Button 1">
		<script language="beanshell" func="addActionListener">
			import java.awt.event.ActionListener;

			actionPerformed( e )
			{
				bsf.lookupBean ("statusBar").setText (e.getSource ().getText () + " Pressed");
			}

			return (ActionListener)this;
		</script>
	</button>
	<button text="Button 2">
		<!-- calling an external script code (identical to the script above) -->
		<script func="addActionListener" src="examples/cookbsh/action.bsh"/>
	</button>

The language of the script can be determined in two ways, either by explicitly setting the language attribute, or by the file extension of the external script code.

For languages that cannot return a variable at end of the script (e.g. Jython), the return attribute is required to indicate which variable to read back at end of script execution. It also affects the function CookBSF is going to call. If return attribute is specified, BSFManager.exec () function is called, otherwise BSFManager.eval () function is called. For languages such as Jython, it makes a big difference. In this case, if you intend to run a procedure that does not return a value, use return="". For example:

	<button text="Button 1">
		<script language="jython" return="action" func="addActionListener">
from java.awt.event import ActionListener

class ExitAction (ActionListener):
	def actionPerformed(self, e):
		bsf.lookupBean ("statusBar").setText (e.getSource ().getText () + " Pressed");

bsf.registerBean ("action", ExitAction ());
		</script>
	</button>
	<button text="Button 2">
		<!-- calling an external script code (identical to the script above) -->
		<script src="examples/cookbsf/action.py" return="action" func="addActionListener"/>
	</button>

Demo

Try the Java Web Start demo for some simple demos. The frame code was also written in BSF using BeanShell.

Note: BSF 2.3.0 has an uncaught exception in BSFManager class initializer code if it is run within Java Web Start sandbox. See ASF Bugzilla for more information. This demo used the changes mentioned in the bug report to get Java Web Start working.

(c) Copyright 2004-2007 Heng Yuan. All rights reserved.

Valid XHTML 1.0! Valid CSS!