| « Mosquitosophagus | Lifesteams and Kiloblog » |
Binding XML to Maps and Beans
May 17th, 2007With Stencil, you start with the output XHTML page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns:s="flag://stencil.agtrz.com/snippits"
xmlns:var="flag://stencil.agtrz.com/attributes" s:snippit="hello"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello, World!</title>
</head>
<body>
<form>
First Name: <input name="firstName" value="Fred" var:firstName="value"/><br/>
Last Name: <input name="lastName" value="Flintstone" var:lastName="value"/>
Hometown: <input name="lastName" value="Bedrock" var:address.city="value"/>
</form>
<s:switch>
<s:if test="neighbors">
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
</tr>
<s:each name="neighbors">
<tr>
<td><s:var name="firstName">Barney</s:var></td>
<td><s:var name="lastName">Rubble</s:var></td>
</tr>
</s:each>
</table>
</s:if>
<s:default>
<p>No neighbors.</p>
</s:default>
</s:switch>
</body>
</html>
<!-- vim: set ts=2 sw=2 et: -->
You will note that this XHTML will not look right in a browser. You have to strip all things in the flag://stencil.agtrz.com/snippits namespace before it looks like XHTML. Stripping the Stencil elements is easy to do though. XSLT anyone?
It is easier to preview the output through an XSLT transform, or through a servlet that strips the Stencil elements, then depend on mockups of business logic.
Binding
public interface Person {
public String getFirstName();
public String getLastName();
public Iterator getNeighbors();
}
It all comes down to strings. Back in the days of Perl, HTML was string, SQL was a string, and CGI was a string. String processing made life easy.
We don’t want to extract structure out of the Java, we want to extract strings. The structure is dictated by the Stencil. You compose an object graph that matches the Stencil, creating facade objects where necessary.
How do we get the strings out of Java? How do we bind Java and XML?
There are three ways to bind XML to Java. One it to go the JSP route, and write Java code into the template language. Another is to go the XStream or Castor route, and express the structure of a Java object graph in XML. The other is to go an OGNL route, create a path language, that will evaluate a little bit or a lot of Java, to get string values and put them in a structure.
I’m going to go with a minimal path language. Bare minimum. No swapping. No choosing your own path language.
Dirt Simple Path Language
Dirt Simple Path Language (DSPL) is a dot separated path to a final object that will be evaluated to a String using that objects toString method.
For any Java object, you will get the class name in DSLP using:
class.name
This is for the case of obvious nested objects like:
employee.address.zip
That would save the hassle of writing a bean wrapper to get the ZIP.
There are no accomodations for indexes or map keys. No conditionals. No operators. You should feel lucky to have this much.
Variable Elements and Attributes
There is always a context object. That object is either an object or else it is a map. In the case of an Object the object is treated as a JavaBean and the variable names are bean properties. In the case of a Map the names are interpreted as hash keys.
Variable Elements
Variables are indicated with the var element. The var element is replaced by calling the the toString method of the object returned by evaluating the property. If the value is null, then the element is simply deleted.
Question Do I accommodate null values? It could be done by adding a null attribute.
Variable Attributes
Variable attributes are indicated using the namespace
flag://stencil.agtrz.com/attributes
An attribute in that namespace is interpreted in the following fashion.
The local name is the DSPL to apply to the context object
The attribute value is the qualified name of the new attribute to add to the element, the attribute local name is the DSPL to apply to the context object to obtain the value of the new attribute.
If the attribute already exists in the template, it is removed.
If the value evaluates to null, the attribute is not added.
This backwards naming convention is simple to implement. Added bonus of allowing a placeholder value for static editing.
Question Do I accommodate null values? It could be done by adding the null value replacement after a comma. Is there a common case where one attribute is added in lieu of another for a specific condition? What about zebra striping, how do I know which row is even or odd? Do I use ListIterator?
No Expressions
While there is logic above, there are no expressions. Truth in Stencil is somewhat like truth in Perl. In addition to boolean false, null is false, as is an empty iterator.
When an iterator is used as a condition, it is cached, so that it can be later used for iterator. Quite naturally, after an iterator is used for iteration, it will always return false.
Control Structures
There are no expressions, only control structures that take a truth value returned from the underlying object model. Those control structures are as follows.
if– The content of theifelement are evaluated if the property named by thetestattribute is true.unless– The content of theunlesselement are evaluated if the property named by thetestattribute is false.each– For each object in theIteratororCollectionreturned by the property named by thenameattribute, the context switches to the object, and the contents of theeachelement are evaluated.with– The context switches to the object return by the property named by thenameelement, and the contents of thewithelement are evaluated.switch– Theswitchelement can contain any of the above elements, the first to be evaluated will replace theswitchelement. Theifandunlessare evaluated conditionally, whileeachandwithwill always evaluate and terminate theswitch.default– For use in aswitchstatement, the default condition that will always execute and terminate theswitch.
Then there are two more to consider, invoke or include. It is intended that Stencil will allow creation of pages through composition, reuse of Stencils through object orientation.
comments


