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);
| Constructor and Description |
|---|
JavaScriptBuilder(Object root,
Object... objectsToExclude)
Constructs a new JavaScriptBuilder to build JS for the root object supplied.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
public JavaScriptBuilder(Object root, Object... objectsToExclude)
root - The root object from which to being translation into JavaScriptobjectsToExclude - Zero or more Strings and/or Classes to be excluded
from translation.public JavaScriptBuilder addPropertyExclusion(String... property)
property - one or more property names to be excludedpublic JavaScriptBuilder addClassExclusion(Class<?>... clazz)
clazz - one or more classes to excludepublic void setRootVariableName(String rootVariableName)
rootVariableName - the name to use when declaring the root variablepublic String getRootVariableName()
public String build()
public void build(Writer writer)
public boolean isExcludedType(Class<?> type)
public boolean isScalarType(Object in)
public String getScalarAsString(Object in)
public static String quote(String string)
string - a String to be escaped and quoted© Copyright 2005-2006, Stripes Development Team.