Jelly Executable XML
Jelly Executable XML
Jelly Executable XML
Agenda
What is Jelly?
Anatomy of a Jelly script
Jelly 'Core' Tag Library
Other supplied tag libraries
Scripting languages in Jelly
Embedding Jelly
What Is Jelly?
Java and XML based scripting engine
Executes XML and produces XML, HTML, etc.
Front end to other XML tools such as Ant
Borrows heavily from JSP and JSTL
Comes with many tag libraries for common tasks
Uses Jexl for expression support
Extensible via Java or Jelly tag libraries
Jelly Architecture
XMLParser
Context
Tags Script
XMLOutput
TagLibrary
Key classes
XMLParser
¾ Takes XML document and creates a script
Context
¾ Variables available to the Jelly script at run time
Script
¾ Executable 'code' made up of text and tags
TagLibrary
¾ Group of tags with a registered name
Agenda
What is Jelly?
Anatomy of a Jelly script
Jelly 'Core' Tag Library
Other supplied tag libraries
Scripting languages in Jelly
Embedding Jelly
A Jelly Script
<j:jelly xmlns:j="jelly:core">
Hello World!
</j:jelly>
Demo: aJellyScript-4.jelly
Agenda
What is Jelly?
Anatomy of a Jelly script
Jelly 'Core' Tag Library
Other supplied tag libraries
Scripting languages in Jelly
Embedding Jelly
Core TagLibrary
Jelly was originally one big jar with all the tag
libraries included
Too many dependencies were difficult to track
Split into core and ant, antlr, avalon, bean,
beanshell, betwixt, bsf, define, dynabean,
email, fmt, html, http, interaction, jetty, jface,
jms, jmx, jsl, junit, log, ojb, quartz, soap, sql,
swing, swt, threads, util, validate, velocity,
xml, xmlunit
Core – set
<j:set var=”name” value=”${expr}”/>
¾ Or <j:set var=”name”>${stringExpr}</j:set>
Assignment operator
Not like Ant properties
value can be any jexl expression
Demo: core1.jelly
Core – if
<if test=”${booleanExpr}”>
...more tags...
</if>
Evaluates its body if the expression is true
No else tag!
operators supported >, >=, <, <=, !
Core – if Example
<?xml version="1.0" ?>
<jelly xmlns="jelly:core" trim="no">
Core – forEach
You've seen it in action
The 'for loop' for jelly
<forEach var=”item” items=”${expr}”
indexVar=”counter” begin=”number”
end=”number” step=”number”>
...statements...
</forEach>
Core – new
Create a new object
<new var=”name” className=”fullName” />
The class must be available to Jelly either in
the current thread's context class loader, or
on the classpath
This is very different to Ant's <property/>
Arguments can be passed to the constructor
using <arg>
Demo: core4.jelly
<new var="myList"
className="java.util.ArrayList" />
Size is ${size(myList)}
</jelly>
Demo: core5.jelly
Demo: core6.jelly
Core – switch
Switch, case, default from Java
<switch on=”${expr}”>
<case value=”${otherExpr}”>
...
</case>
<default>
...
</default>
</switch>
Demo: core8.jelly
Core – choose
Similar to switch, but not necessarily on a
single value
<choose>
<when test=”${booleanExpr}”>
...
</when>
<otherwise></otherwise>
</choose>
Core – while
While loop for Jelly
<while test=”${booleanExpr}”>...</while>
Demo: core11.jelly
Core – break
Breaks out of while or forEach
<break test=”${booleanExpr}”>...</while>
Demo: core12.jelly
Core – thread
Creates a new thread and runs its body on that
thread
Output can be to XMLOutput, file, or inherited
All the usual threading issues apply
Script finishes when the threads are completed
Demo: core13.jelly
Core – useBean
Create a bean of the given class
Convert XML attributes into bean properties
and call the setters
Make it available as a variable
<useBean var=”name” class=”fullName”
property1=”value” property2=”value”/>
<useBean var="frame"
class="javax.swing.JFrame" title="My Frame" />
${frame.setSize(dimension)}
<setProperties object="${frame}"
visible="true"/>
Demo: core14.jelly
Core – useList
Create a new list and populate it from the given
items
<useList var=”name” items=”${expr}”/>
<useList var="myList"
items="${systemScope.keySet()}" />
list is '${myList}'
Demo: core15.jelly
Core - file
Write the tag body to a file, or variable
<file name=”${fileName}”
omitXmlDeclaration=”${bool}”
outputMode=”XML/HTML” prettyPrint=”${bool}”
encoding=”${value}” var=”alternateVariable”
escapeText=”${bool}”>
...
</file>
Demo: core16.jelly
<file name="${systemScope['user.home']}
/temp.jelly" omitXmlDeclaration="true">
When you hear the air attack warning...
</file>
Demo: scope.jelly
Agenda
What is Jelly?
Anatomy of a Jelly script
Jelly 'Core' Tag Library
Other supplied tag libraries
Scripting languages in Jelly
Embedding Jelly
Other TagLibraries
Ant – allows ant tasks to be invoked from Jelly
Antlr – Generate code from antlr grammars
Avalon – An Avalon-based service to run scripts
Bean – map tags to beans like Ant does
BeanShell – runs beanshell scripts
Betwixt – turn beans into XML and XML into beans
<j:forEach var="script"
items="${scripts.iterator()}" trim="yes">
<echo>script is ${script}</echo>
</j:forEach>
Demo: ant1.jelly
XML Taglib
Parse
¾ <x:parse var=”name”
xml=”${uriFileReaderOrInputStream}”/>
¾ ${name} is now a Dom4J Document
¾ xmlns:x="jelly:xml"
¾ Need taglib jar available
Other Sessions:
¾ Bonnie B. Ricca “XSLT 2.0: Not Your Mothers XSLT”
Demo: xml3.jelly
SQL Taglib
Allows processing of SQL queries
Transformation into XML/HTML easily
Based on the JSTL taglib
Tags provided
¾ Transaction, driver, query, update, resultSet, row,
dataSource
<sql:query var="results">
select * from ${databaseTable}
</sql:query>
Demo: sql1.jelly
</j:forEach>
Util Taglib
Miscellaneous utility tags
<util:available file=”${fileName}”
uri=”relativePath”>...</util:available>
<util:loadText var=”name” file=”${fileName}”
uri=”relativePath” />
<util:tokenize var=”name”
delim=”${char}”>${stringContent}</util:tokenize>
Demo: util1.jelly
User Interfaces
Jelly comes with both a Swing and SWT tag library
This allows the UI to be specified in XML
It's not XUL
Agenda
What is Jelly?
Anatomy of a Jelly script
Jelly 'Core' Tag Library
Other supplied tag libraries
Scripting languages in Jelly
Embedding Jelly
Scripting Languages
XML is often unwieldy
Code can be far more compact and
expressive, but is difficult to generate
As a scripting language itself, Jelly is unusual
in that it lets you load and run scripts for
other languages within a script
Scripting – BeanShell
BeanShell tag library
Need bean shell jars
Use xmlns:bsh="jelly:beanshell"
<bsh:script>
Properties sysprops =
System.getProperties();
System.out.println("-testsysprop =
" + sysprops.get("testsysprop"));
</bsh:script>
Scripting – BSF
Ex-IBM Bean Scripting Framework
Need BSF jars
Use xmlns:javascript="jelly:javascript”
Provides Jython, Pnuts, JavaScript
Registers Jelly Context with BSF Manager
Expressions are passed through to BSF to manage
Agenda
What is Jelly?
Anatomy of a Jelly script
Jelly 'Core' Tag Library
Other supplied tag libraries
Scripting languages in Jelly
Embedding Jelly
Embedding Jelly
Where is the output to go?
What variables do you want available to the script?
Set up XMLOutput
Run the script
What do you want to do with the output?
Summary
XML based scripting
Large tag library support
Based on JSTL / JSP
XML can be intermixed with Java and tags
to produce output
Resources
http://jakarta.apache.org/commons/jelly/
http://jakarta.apache.org/commons/jexl/
http://cocoon.apache.org/
http://maven.apache.org/
dion@multitask.com.au