org.rythmengine.extension
Annotation Type Transformer


@Retention(value=RUNTIME)
@Target(value={METHOD,TYPE})
public @interface Transformer

Transformer could be used to annotate on a public static methods that works on a given object with or without parameters. For example,

@Transformer public static String format(Number number, String formatStr)

When feature.transformer.enabled is set to true (by default it is true), template author can use Transformer to further process an expression value:

@args Date dueDate\n...@dueDate.format("dd/MM/yyyy")

The @dueDate.format("dd/MM/yyyy") in the above example will be transformed into S.format(dueDate, "dd/MM/yyyy") in the generated java source

Note, the above sample code demonstrates a transformer named format, which is built into Rythm engine. However when you want to define your own transformer, you need to use this Transformer annotation to mark on your methods or classes. When the annotation is marked on a class, then all public static methods with return value and at least one parameter will be treated as transformer

You can register them to RythmEngine by RythmEngine.registerTransformer(Class[]) method

, once you have registered your Java extension methods, the template author can use them in template. Be careful of the name conflict of your Java extension and Rythm's built-in Java extension. You can turn off rythm's built java extension by set "rythm.disableBuiltInTransformer" to true

Transformers mechanism is also found in other template engine solutions, but with different names. Freemarker call it built-ins, Velocity called it velocity tools. But none of them are as easy to use as Rythm Transformers


Optional Element Summary
 boolean requireTemplate
          Require passing template instance as implicit argument (the first parameter)
 String value
          The namespace of the transformer.
 String waivePattern
          Once specified the pattern will be used to match the token to see if transformer extension should be waived or not.
 

value

public abstract String value
The namespace of the transformer. When namespace is presented, the template author needs to use the namespace to qualify the transformer in the template source. For example, @x.app_myTransformer()

Default value: "app"

Returns:
the namespace
Default:
"app"

waivePattern

public abstract String waivePattern
Once specified the pattern will be used to match the token to see if transformer extension should be waived or not. For example, @x.escape() should be treated as escape transformer while @s().escape(x) shouldn't because s\\(\\) is a waive pattern

Returns:
the waive pattern
Default:
""

requireTemplate

public abstract boolean requireTemplate
Require passing template instance as implicit argument (the first parameter)

Returns:
true if require passing template instance as the implicit first parameter
Default:
false


Copyright © 2013. All Rights Reserved.