net.sourceforge.stripes.ajax
Class JavaScriptBuilder

java.lang.Object
  extended by net.sourceforge.stripes.ajax.JavaScriptBuilder

public class JavaScriptBuilder
extends Object

Builds a set of JavaScript statements that will re-construct the value of a Java object, including all Number, String, Enum, Boolean, Collection, Map and Array properties. Safely handles object graph circularities - each object will be translated only once, and all references will be valid.

The JavaScript created by the builder can be evaluated in JavaScript using:

var myObject = eval(generatedFragment);

Since:
Stripes 1.1
Author:
Tim Fennell

Constructor Summary
JavaScriptBuilder(Object root, Object... objectsToExclude)
          Constructs a new JavaScriptBuilder to build JS for the root object supplied.
 
Method Summary
 JavaScriptBuilder addClassExclusion(Class<?>... clazz)
          Adds one or more properties to the list of properties to exclude when translating to JavaScript.
 JavaScriptBuilder addPropertyExclusion(String... property)
          Adds one or more properties to the list of property to exclude when translating to JavaScript.
 String build()
          Causes the JavaScriptBuilder to navigate the properties of the supplied object and convert them to JavaScript.
 void build(Writer writer)
          Causes the JavaScriptBuilder to navigate the properties of the supplied object and convert them to JavaScript, writing them to the supplied writer as it goes.
 String getRootVariableName()
          Returns the name used to declare the root variable to which the built JavaScript object is assigned.
 String getScalarAsString(Object in)
          Fetches the value of a scalar type as a String.
 boolean isExcludedType(Class<?> type)
          Returns true if the supplied type should be excluded from conversion, otherwise returns false.
 boolean isScalarType(Object in)
          Returns true if the object is of a type that can be converted to a simple JavaScript scalar, and false otherwise.
static String quote(String string)
          Quotes the supplied String and escapes all characters that could be problematic when eval()'ing the String in JavaScript.
 void setRootVariableName(String rootVariableName)
          Sets an optional user-supplied root variable name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaScriptBuilder

public JavaScriptBuilder(Object root,
                         Object... objectsToExclude)
Constructs a new JavaScriptBuilder to build JS for the root object supplied.

Parameters:
root - The root object from which to being translation into JavaScript
objectsToExclude - Zero or more Strings and/or Classes to be excluded from translation.
Method Detail

addPropertyExclusion

public JavaScriptBuilder addPropertyExclusion(String... property)
Adds one or more properties to the list of property to exclude when translating to JavaScript.

Parameters:
property - one or more property names to be excluded
Returns:
the JavaScripBuilder instance to simplify method chaining

addClassExclusion

public JavaScriptBuilder addClassExclusion(Class<?>... clazz)
Adds one or more properties to the list of properties to exclude when translating to JavaScript.

Parameters:
clazz - one or more classes to exclude
Returns:
the JavaScripBuilder instance to simplify method chaining

setRootVariableName

public void setRootVariableName(String rootVariableName)
Sets an optional user-supplied root variable name. If set this name will be used by the building when declarind the root variable to which the JS is assigned. If not provided then a randomly generated name will be used.

Parameters:
rootVariableName - the name to use when declaring the root variable

getRootVariableName

public String getRootVariableName()
Returns the name used to declare the root variable to which the built JavaScript object is assigned.


build

public String build()
Causes the JavaScriptBuilder to navigate the properties of the supplied object and convert them to JavaScript.

Returns:
String a fragment of JavaScript that will define and return the JavaScript equivalent of the Java object supplied to the builder.

build

public void build(Writer writer)
Causes the JavaScriptBuilder to navigate the properties of the supplied object and convert them to JavaScript, writing them to the supplied writer as it goes.


isExcludedType

public boolean isExcludedType(Class<?> type)
Returns true if the supplied type should be excluded from conversion, otherwise returns false. A class should be excluded if it is assignable to one of the types listed for exclusion, or, it is an array of such a type.


isScalarType

public boolean isScalarType(Object in)
Returns true if the object is of a type that can be converted to a simple JavaScript scalar, and false otherwise.


getScalarAsString

public String getScalarAsString(Object in)
Fetches the value of a scalar type as a String. The input to this method may not be null, and must be a of a type that will return true when supplied to isScalarType().


quote

public static String quote(String string)
Quotes the supplied String and escapes all characters that could be problematic when eval()'ing the String in JavaScript.

Parameters:
string - a String to be escaped and quoted
Returns:
the escaped and quoted String
Since:
Stripes 1.2 (thanks to Sergey Pariev)


? Copyright 2005-2006, Stripes Development Team.