Erubis-J Users' Guide

3   Guide for Developers

3-1   Generator, Converter, and Evaluator

There are three important interface in Erubis-J.

interface Converter
Parse embedded text and convert it to target-language code. It requires Generator object. This is language-independent.
interface Generator
Generate JavaScript/Java/Ruby code. This is language-specific.
interface Evaluator
Evaluate JavaScript code. This is language-specific.

Here is the class diagram (click to display in large size).

class diagram of Erubis-J

The following is an example code of Genrator, Converter, Evaluator, and their concrete classes.

ErubisExample01.java:
import erubis.*;
import java.util.*;

public class ErubisExample01 {
  public static void main(String[] args) {
    // embedded text
    String input = ""
        + "<% for (i = 0, n = list.length; i < n; i++) { %>\n"
        + "<b><%= list[i] %></b>\n"
        + "<% } %>\n"
        ;

    // context data (or 'bidning object')
    List list = new ArrayList();
    list.add("Foo");  list.add("Bar");  list.add("Baz");
    Map context = new HashMap();
    context.put("list", list);

    // convert embedded text info JavaScript code
    Generator generator = new erubis.js.Generator();
    Converter converter = new StandardConverter(generator);
    String code = converter.convert(input);

    // evaluate javaScript code
    Evaluator evaluator = new erubis.js.Evaluator();
    String result = (String)evaluator.evaluate(code, context);

    System.out.print(result);
  }
}

If you want to use Processing Instructions as embedded pattern, use PIConverter class instead of StandardConverter class.

    ....
    // convert embedded text info JavaScript code
    Generator generator = new erubis.js.Generator();
    Converter converter = new PIConverter(generator, true, "js", "@");
    String code = converter.convert(input);
    ....

3-2   Processor

There is a facade class (interface) for convenience.

Processor (interface)
A composition of Converter and Evaluator.

The following is an example of Processor interface and it's concrete class.

ErubisExample02.java
import erubis.*;
import java.util.*;

public class ErubisExample02 {
  public static void main(String[] args) {
    // embedded text
    String input = ""
        + "<% for (var i = 0, n = list.length; i < n; i++) { %>\n"
        + "<b><%= list[i] %></b>\n"
        + "<% } %>\n"
        ;

    // context data (or 'bidning object')
    List list = new ArrayList();
    list.add("Foo");  list.add("Bar");  list.add("Baz");
    Map context = new HashMap();
    context.put("list", list);

    // convert and evaluate
    Processor processor = new erubis.js.Processor();
    String result = processor.process(input, context);
    // or
    // String code = processor.convert(input);
    // String result = (String)processor.evaluate(code, context);

    System.out.print(result);
  }
}

If you want to parse Processing Instructions pattern, use erubis.js.PIProcessor class instead of erubis.js.Processor class.

    ....
    // convert and evaluate
    Processor processor = new erubis.js.PIProcessor(true, "js", "@");
    String result = processor.process(input, context);
    ....

3-3   Decorator

Decorator is an implementation class of Decorator Pattern.

abstract class GeneratorDecorator
Decorator class for Generator. It implements Genertor interface.
abstract class GeneratorConverter
Decorator class for Converter. It implements Converter interface.
abstract class GeneratorEvaluator
Decorator class for Evaluator. It implements Evaluator interface.

Currently, GeneratorDecorator's subclasses are provided.

  • erubis.decorator.NoTextDecorator
  • erubis.decorator.NoCodeDecorator
  • erubis.decorator.BodyOnlyDecorator

The following is an example of GeneratorDecorator and it's concrete classes.

ErubisExample03.java
import erubis.*;
import java.util.*;

public class ErubisExample03 {
  public static void main(String[] args) {
    // embedded text
    String input = ""
        + "<% for (i = 0, n = list.length; i < n; i++) { %>\n"
        + "<b><%= list[i] %></b>\n"
        + "<% } %>\n"
        ;

    // context data (or 'bidning object')
    List list = new ArrayList();
    list.add("Foo");  list.add("Bar");  list.add("Baz");
    Map context = new HashMap();
    context.put("list", list);

    // generator and decorator
    Generator generator = new erubis.js.Generator();
    generator = new erubis.decorator.NoTextDecorator(generator);
    generator = new erubis.decorator.BodyOnlyDecorator(generator);
    
    // convert text into JavaScript code
    Converter converter = new StandardConverter(generator);
    String code = converter.convert(input);

    System.out.print(code);
  }
}

3-4   Escape Expression

To escape '<%= ... %> by default (equivarent to '-e' option),

  • Use Converter#setEscaped(boolean) or Processor#setEscaped(boolean).
  • Or pass true to constructor of Converter or Processor.
Generator generator = new erubis.js.Generator()
Converter converter = new StandardConverter(generator);
converter.setEscaped(true);
// or
Converter converter = new StandardConverter(generator, true);
// or
Processor processor = erubis.js.Processor();
processor.setEscaped(true);
// or
Processor processor = erubis.js.Processor(true);

To change JavaScript escape function name from '_escape()' to other,

  • Create subclass of Generator and override Generator#escapedExpression().
  • Or call Generator#setEscapeFunctionName(String funcname).
public class MyJavaScriptGenerator extends erubis.js.Generator {
  public String escapedExpression(String code) {
    //return "_escape(" + code.trim() + ")";
    return "escapeXml(" + code.trim() + ")";
  }
}
// or
Generator generator = new erubis.js.Generator();
generator.setEscapeFunctionName("escapeXml");
Converter converter = new StandardConverter(generator);

To change the definition of _escape() function,

  • Use Evaluator#setInitCode(String) or Processor#setInitCode(String)
  • Or pass initialization code to constructor of JavaScriptEvaluator.
Evaluator evaluator = new erubis.js.Evaluator();
evaluator.setInitCode("function _escape(value) { ... }");
// or
Processor processor = new erubis.js.Processor();
processor.setInitCode("function _escape(value) { ... }");
// or
String initCode = "function _escape(value) { ... }";
Evaluator evaluator = new erubis.js.Evaluator(true, initCode);