Archive for January, 2009

JavaFX Script for Java developers, Part 1

25 January 2009

First touch

The main method in JavaFX is called run().

function run(args: String[]) {
    // implementation
}

Let’s print something to the output. System.out.print() is simply print() in JavaFX.

function run(args: String[]) {
    print("Hello from JavaFX!!");
}

Moreover run() is not mandatory for the developer :

print("Hello from JavaFX!!");

As simple as it gets; all scattered statements in a script file are properly compiled and executed.

Variables

Variable declaration is done in a concise way. This is how to declare an Integer,

var age = 7;

a Double,

var price = 7.0;

and a Boolean.

var isCool = true;

The following program that prints true three times,

var age = 7;
var price = 7.0;
var isCool = true;

println(age instanceof java.lang.Integer);
println(price instanceof java.lang.Double);
println(isCool instanceof java.lang.Boolean);

may also be restated as :

println(7 instanceof java.lang.Integer);
println(7.0 instanceof java.lang.Double);
println(true instanceof java.lang.Boolean);

Speaking of new features, JavaFX has an extra arrow in its quiver :

var duration = 7ms;
println(duration instanceof javafx.lang.Duration);

Finally, to declare a final variable you may change var with def.

def steady = 100;

Methods

A void method is declared as follows.

function say() {
    print("hello!");
}

This is how to specify arguments.

function say(something: String) {
    print(something);
}

Of course overloading does exist, so the following is a valid program.

function say() {
    print("hello!");
}
function say(something: String) {
    print(something);
}
say();
say("Hey Niko!");

Finally, a return type is added at the end of the method signature.

function square(data: Number): Number {
    return data * data;
}

Class

Let’s design persons. For every person we’re interested about his name and age.

public class Person {
    public var name: String;
    public var age: Integer;
}

Of course you may encapsulate the state of a JavaFX class according to the JavaBean specification (private fields, getters, setters), but the above syntax allows handy initialization of objects.

Here’s the definion of a car.

public class Car {
    var brand: String;
}

Maybe a Person should HAS-A Car.

public class Person {
    public var name: String;
    public var age: Integer;
    public var car: Car;
}

Objects

Let’s create a person :

Person {
    name: 'Michael Jordan'
    age: 46
}

Using Java, this is equivalent to an anonymous inner class.

// Java
new Person {
    setName("Michael Jordan");
    setAge(46);
}

To give the instance a name:

var person = Person {
    name: 'Michael Jordan'
    age: 46
}

In Java this may also be expressed as:

// Java
Person person = new Person();
person.setName("Michael Jordan");
person.setAge(46);

At this point, Michael Jordan needs a good car.

var person = Person {
    name: 'Michael Jordan'
    age: 46
    car: Car {
        brand: 'BMW Z4 Roaster'
    }
}

Just to be clear, in Java this would be:

// Java
Car car = new Car();
car.setBrand("BMW Z4 Roaster");

Person person = new Person();
person.setName("Michael Jordan");
person.setAge(46);
person.setCar(car);

Wrap up

JavaFX Script is a visual-oriented language for creating rich applications. As an advantage, it’s built on the powerful basis of Java. Moreover, as we’ll experience in the next part, its syntax is simpler and more expressive.

Thank you.

Custom Tags Tutorial

24 January 2009

This tutorial presents practical skills for Custom Tags; the most powerful feature of the JSP technology. Its purpose is to be a good review for those preparing for Sun Certified Web Component Developer certification and, at the same time, a good reference at work.

  1. Skills for Tag Files
  2. Skills for Simple Tag handlers
  3. Skills for Classic Tag handlers
  4. Body manipulation of Classic Tags

Thank you.

Custom Tags 4 – Body manipulation of Classic Tags

19 January 2009

This post explains how to manipulate the body of a classic tag.

Step 1. Write a BodyTagSupport and provide its doAfterBody() method.

package my;
public class Body extends BodyTagSupport {
  public int doAfterBody() {
    try {
      final String body = bodyContent.getString();
      final StringBuilder result = new StringBuilder();
      for (int index = 0; index < body.length(); index = index + 2) {
        result.append(body.charAt(index));
      }
      bodyContent.getEnclosingWriter().print(result.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return SKIP_BODY;
  }
}

Step 2. Declare it in a TLD.

<taglib ...>
  <uri>nikojava</uri>
  <tag>
    <name>body</name>
    <tag-class>my.Body</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

Step 3. Call it!

<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>ABABABABABABAB</mari:body>
    (After)
  </body>
</html>
Filtering the even characters of a classic tag's body

Filtering the even characters of a classic tag's body

As a result, only the characters at an even position (0, 2, 4, etc.) are printed!

Explanation

BodyTagSupport IS-A TagSupport. However it offers an extra weapon: EVAL_BODY_BUFFERED. If doStartTag() returns this value, then a series of methods is invoked: setBodyContent(BodyContent), then doInitBody(), then the body is evaluated and doAfterBody() is about to execute. At this point we do have access to the actual body content.

In the above example doStartTag() is not overriden, so by default EVAL_BODY_BUFFERED is returned. The actual content is taken as a string,

  final String body = bodyContent.getString();

and its even characters are selected:

  for (int index = 0; index < body.length(); index = index + 2) {
    result.append(body.charAt(index));
  }

More examples

Color alternate letters

public int doAfterBody() {
  try {
    final String body = bodyContent.getString();
    final StringBuilder result = new StringBuilder();
    for (int index = 0; index < body.length(); index++) {
      if (isOdd(index)) {
        result.append("<span style='color:red'>");
        result.append(body.charAt(index));
        result.append("</span>");
      } else {
        result.append(body.charAt(index));
      }
    }
    bodyContent.getEnclosingWriter().print(result.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
private boolean isOdd(int number) {
  return number % 2 == 1;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>ABABABABABABAB</mari:body>
    (After)
  </body>
</html>
Coloring the even characters of a classic tag's body

Coloring the even characters of a classic tag's body

Remove capital letters

public int doAfterBody() {
  try {
    final String body = bodyContent.getString();
    final StringBuilder result = new StringBuilder();
    for (int index = 0; index < body.length(); index++) {
      final char current = body.charAt(index);
      if (!Character.isUpperCase(current)) {
        result.append(current);
      }
    }
    bodyContent.getEnclosingWriter().print(result.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>Google Sun JBoss</mari:body>
    (After)
  </body>
</html>
Disposing the capital letters of a custom tag's body

Disposing the capital letters of a custom tag's body

Have fun

public int doAfterBody() {
  try {
    final StringBuilder body = new StringBuilder(bodyContent.getString());
    final int initialSize = body.length();
    for (int index = 0; index < initialSize; index++) {
      bodyContent.getEnclosingWriter().append(body);
      bodyContent.getEnclosingWriter().append("<br />");
      body.deleteCharAt(body.length() - 1);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    <mari:body>Microsystems</mari:body>
  </body>
</html>
Having fun with the body of a classic tag

Having fun with the body of a classic tag

Custom Tags 3 – Skills for Classic Tag handlers

15 January 2009

This post presents the basic skills of classic tag handling for the JSP technology.

Here’s an overview of the hierarchy. The arrows denote an IS-A relationship.

Hierarchy of Classic tag handlers

Hierarchy of Classic Tag handlers

Step 1. Write a TagSupport and provide its doStartTag() method.

package my;
public class Classic extends TagSupport {
  public int doStartTag() throws JspException {
    try {
      pageContext.getOut().print("Hello from a classic tag handler!!");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return SKIP_BODY;
  }
}

Step 2. Declare it in a TLD.

<taglib ...>
  <uri>nikojava</uri>
  <tag>
    <name>classic</name>
    <tag-class>my.Classic</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Step 3. Call it!

<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    <show:classic />
  </body>
</html>
Invoking a classic tag

Invoking a classic tag

Process its body

Just return EVAL_BODY_INCLUDE to make the body evaluated,

public class Classic extends TagSupport {
  public int doStartTag() throws JspException {
    try {
      pageContext.getOut().print("This is my body: ");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return EVAL_BODY_INCLUDE;
  }
}

as long as the tag is declared to have a body.

<tag>
  <name>classic</name>
  <tag-class>my.Classic</tag-class>
  <body-content>scriptless</body-content>
</tag>
<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    <show:classic>Really useful contents!</show:classic>
  </body>
</html>
Accessing the body of a classic tag

Accessing the body of a classic tag

Define a body attribute

It’s easy to define attributes right inside the body of the tag.

public class Classic extends TagSupport {
  public int doStartTag() throws JspException {
    pageContext.setAttribute("friend", "Nikos");
    return EVAL_BODY_INCLUDE;
  }
}
<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    <show:classic>Hello my friend ${friend}!!</show:classic>
  </body>
</html>
Using an attribute inside the body of a classic tag

Using an attribute inside the body of a classic tag

Define a tag attribute

An attribute is defined as a setter method,

public class Classic extends TagSupport {
  private String friend;
  public int doStartTag() throws JspException {
    try {
      pageContext.getOut().append("Hey you " + friend + "!!");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return EVAL_BODY_INCLUDE;
  }
  public void setFriend(String friend) {
    this.friend = friend;
  }
}

that is also declared in the TLD.

<tag>
  <name>classic</name>
  <tag-class>my.Classic</tag-class>
  <body-content>empty</body-content>
  <attribute>
    <name>friend</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>
<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    <show:classic friend="Niko" />
  </body>
</html>
Using a tag attribute

Using a tag attribute

Dynamic attributes

To process such attributes you should implement the DynamicAttributes and its single method.

public class Classic extends TagSupport implements DynamicAttributes {
private Map<String, Object> map = new HashMap<String, Object>();
  public void setDynamicAttribute(String uri, String name, Object value) {
    map.put(name, value);
  }
public int doStartTag() throws JspException {
  try {
    JspWriter out = pageContext.getOut();
    out.append("These are the dynamic attributes:");
    out.append("<ul>");
    for (Map.Entry<String, Object> element : map.entrySet()) {
      out.append("<li>");
      out.append(element.getKey() + " ⇒ " + element.getValue());
      out.append("</li>");
    }
    out.append("</ul>>");
  } catch (IOException e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
}

The doStartTag() method simply displays the name and value of each dynamic attribute.

<tag>
  <name>classic</name>
  <tag-class>my.Classic</tag-class>
  <body-content>empty</body-content>
  <dynamic-attributes>true</dynamic-attributes>
</tag>
<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    <show:classic custom="tag" open="source" javafx="cool" />
  </body>
</html>
Dynamic attributes in a classic tag

Dynamic attributes in a classic tag

Reevaluate the body

When the doStartTag() method returns EVAL_BODY_INCLUDE, we can evaluate the body again and again. This is possible using the doAfterBody() method.

public class Classic extends TagSupport {
  private int counter;
  public int doStartTag() throws JspException {
    counter = 0;
    return EVAL_BODY_INCLUDE;
  }
  public int doAfterBody() throws JspException {
    while (counter < 3) {
      counter++;
      return EVAL_BODY_AGAIN;
    }
    return SKIP_BODY;
  }
}
<tag>
  <name>classic</name>
  <tag-class>my.Classic</tag-class>
  <body-content>scriptless</body-content>
</tag>
<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    <show:classic>Hello </show:classic>
  </body>
</html>
Iterating over the body of a classic tag again and again

Iterating over the body of a classic tag again and again

Skip the rest of the page

This is accomplished by overriding the doEndTag() method.

public class Classic extends TagSupport {
  public int doStartTag() throws JspException {
    return EVAL_BODY_INCLUDE;
  }
  public int doEndTag() throws JspException {
    return SKIP_PAGE;
  }
}
<tag>
  <name>classic</name>
  <tag-class>my.Classic</tag-class>
  <body-content>scriptless</body-content>
</tag>
<%@ taglib prefix="show" uri="nikojava" %>
<html>
  <body>
    (Start of page)
    <show:classic>Hello from a classic tag!</show:classic>
    (End of page)
  </body>
</html>
Skipping the rest of the page from a classic tag

Skipping the rest of the page from a classic tag

Indeed, the last part of the page is never sent to the client.

Review

doStartTag method is called first. It may return:

  1. EVAL_BODY_INCLUDE = evaluate the body once.
  2. SKIP_BODY = go on, do not evaluate the body of the tag.

doAfterBody method is called only if doStartTag() returns option 1. It may return:

  1. EVAL_BODY_AGAIN = evaluate the body again.
  2. SKIP_BODY = just go on, stop evaluating the body.

doEndTag method is called last. It may return:

  1. SKIP_PAGE = skip the rest of the page.
  2. EVAL_PAGE = proceed normally to the rest of the page.

Custom Tags 2 – Skills for Simple Tag handlers

14 January 2009

This post presents the basic skills of simple tag handling for the JSP technology. In detail, we’ll see how to:

Here’s an overview of the hierarchy. The arrows denote an IS-A relationship.

Hierarchy of Simple tag handlers

Hierarchy of Simple tag handlers

Step 1. Write a SimpleTagSupport and provide its doTag() method.

package my;
public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().append("Hello from a simple tag handler!!");
  }
}

Step 2. Declare it in a TLD.

<taglib ...>
  <uri>nikojava</uri>
  <tag>
    <name>simple</name>
    <tag-class>my.Simple</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Step 3. Call it!

<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple />
  </body>
</html>
Invoking a simple tag handler

Invoking a simple tag handler

Process its body

This is possible using the invoke method of JspFragment,

public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().print("This is my body:<br />");
    getJspBody().invoke(null);
  }
}

as long as the tag is allowed to have a body.

<tag>
  <name>simple</name>
  <tag-class>my.Simple</tag-class>
  <body-content>scriptless</body-content>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple>Some useful content</yo:simple>
  </body>
</html>
Accessing the body of a simple custom tag

Accessing the body of a simple custom tag

Define an attribute inside its body

Also, you may define attributes right inside the body of the tag.

public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().setAttribute("friend", "Niko");
    getJspBody().invoke(null);
  }
}
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple>Hello ${friend}!!</yo:simple>
  </body>
</html>
Using an attribute inside the tag's body

Using an attribute inside the tag's body

Define a tag attribute

An attribute is defined as a setter method,

public class Simple extends SimpleTagSupport {
  private String friend;
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().append("Hello " + friend + "!!");
  }
  public void setFriend(String friend) {
    this.friend = friend;
  }
}

that is also declared in the TLD.

<tag>
  <name>simple</name>
  <tag-class>my.Simple</tag-class>
  <body-content>empty</body-content>
  <attribute>
    <name>friend</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple friend="Niko" />
  </body>
</html>
Using an attribute inside the tag's body

Using an attribute inside the tag's body

Dynamic attributes

To process such attributes you should implement the DynamicAttributes; an interface with a single method.

public class Simple extends SimpleTagSupport implements DynamicAttributes {
  private Map<String, Object> map = new HashMap<String, Object>();
  public void setDynamicAttribute(String uri, String name, Object value) {
    map.put(name, value);
  }
  public void doTag() throws IOException, JspException {
    JspWriter out = getJspContext().getOut();
    out.append("These are the dynamic attributes:");
    out.append("<ul>");
    for (Map.Entry<String, Object> element : map.entrySet()) {
      out.append("<li>");
      out.append(element.getKey() + " &rArr; " + element.getValue());
      out.append("</li>");
    }
    out.append("</ul>");
  }
}

The doTag() method simply displays the name and value of each dynamic attribute.

<tag>
  <name>simple</name>
  <tag-class>my.Simple</tag-class>
  <body-content>empty</body-content>
  <dynamic-attributes>true</dynamic-attributes>
</tag>
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    <yo:simple friend="Niko" casoline="soft" more="lagi" />
  </body>
</html>
Displaying the dynamic attributes

Displaying the dynamic attributes

Skip the rest of the page

This is accomplished by throwing a SkipPageException at the desired place.

public class Simple extends SimpleTagSupport {
  public void doTag() throws IOException, JspException {
    getJspContext().getOut().append("Encapsulation");
    throw new SkipPageException();
  }
}
<%@ taglib prefix="yo" uri="nikojava" %>
<html>
  <body>
    (Start of page)
    <yo:simple />
    (End of page)
  </body>
</html>
Skipping the rest of the page

Skipping the rest of the page