public class JavaScriptBuilder
extends java.lang.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(java.lang.Object root,
java.lang.Object... objectsToExclude)
Constructs a new JavaScriptBuilder to build JS for the root object supplied.
|
Modifier and Type | Method and Description |
---|---|
JavaScriptBuilder |
addClassExclusion(java.lang.Class<?>... clazz)
Adds one or more properties to the list of properties to exclude when translating
to JavaScript.
|
JavaScriptBuilder |
addPropertyExclusion(java.lang.String... property)
Adds one or more properties to the list of property to exclude when translating
to JavaScript.
|
java.lang.String |
build()
Causes the JavaScriptBuilder to navigate the properties of the supplied object and
convert them to JavaScript.
|
void |
build(java.io.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.
|
java.lang.String |
getRootVariableName()
Returns the name used to declare the root variable to which the built
JavaScript object is assigned.
|
java.lang.String |
getScalarAsString(java.lang.Object in)
Fetches the value of a scalar type as a String.
|
boolean |
isExcludedType(java.lang.Class<?> type)
Returns true if the supplied type should be excluded from conversion, otherwise
returns false.
|
boolean |
isScalarType(java.lang.Object in)
Returns true if the object is of a type that can be converted to a simple JavaScript
scalar, and false otherwise.
|
static java.lang.String |
quote(java.lang.String string)
Quotes the supplied String and escapes all characters that could be problematic
when eval()'ing the String in JavaScript.
|
void |
setRootVariableName(java.lang.String rootVariableName)
Sets an optional user-supplied root variable name.
|
public JavaScriptBuilder(java.lang.Object root, java.lang.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(java.lang.String... property)
property
- one or more property names to be excludedpublic JavaScriptBuilder addClassExclusion(java.lang.Class<?>... clazz)
clazz
- one or more classes to excludepublic void setRootVariableName(java.lang.String rootVariableName)
rootVariableName
- the name to use when declaring the root variablepublic java.lang.String getRootVariableName()
public java.lang.String build()
public void build(java.io.Writer writer)
public boolean isExcludedType(java.lang.Class<?> type)
public boolean isScalarType(java.lang.Object in)
public java.lang.String getScalarAsString(java.lang.Object in)
public static java.lang.String quote(java.lang.String string)
string
- a String to be escaped and quoted? Copyright 2005-2006, Stripes Development Team.