Revision: 763
http://stripes.svn.sourceforge.net/stripes/?rev=763&view=rev
Author: tfenne
Date: 2008-01-19 08:55:57 -0800 (Sat, 19 Jan 2008)
Log Message:
-----------
Fix for STS-368, allow JSResolution/Builder to be more useful outside of a
streaming response.
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java
trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptResolution.java
Modified: trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java
2008-01-19 15:02:20 UTC (rev 762)
+++ trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java
2008-01-19 16:55:57 UTC (rev 763)
@@ -86,6 +86,9 @@
/** Holds the (potentially empty) set of properties that should be skipped
over. */
private Set<String> excludeProperties;
+ /** Holds an optional user-supplied name for the root property. */
+ private String rootVariableName = "_sj_root_" + new
Random().nextInt(Integer.MAX_VALUE);
+
/**
* Constructs a new JavaScriptBuilder to build JS for the root object
supplied.
*
@@ -100,9 +103,9 @@
for (Object object : objectsToExclude) {
if (object instanceof Class)
- this.excludeClasses.add((Class<?>) object);
+ addClassExclusion((Class<?>) object);
else if (object instanceof String)
- this.excludeProperties.add((String) object);
+ addPropertyExclusion((String) object);
else
log.warn("Don't know to determine exclusion for objects of
type ", object.getClass().getName(), ". You may only pass in instances of Class
and/or String.");
}
@@ -111,6 +114,53 @@
}
/**
+ * Adds one or more properties to the list of property to exclude when
translating
+ * to JavaScript.
+ *
+ * @param property one or more property names to be excluded
+ * @return the JavaScripBuilder instance to simplify method chaining
+ */
+ public JavaScriptBuilder addPropertyExclusion(String... property) {
+ for (String prop : property) {
+ this.excludeProperties.add(prop);
+ }
+ return this;
+ }
+
+ /**
+ * Adds one or more properties to the list of properties to exclude when
translating
+ * to JavaScript.
+ *
+ * @param clazz one or more classes to exclude
+ * @return the JavaScripBuilder instance to simplify method chaining
+ */
+ public JavaScriptBuilder addClassExclusion(Class<?>... clazz) {
+ for (Class<?> c : clazz) {
+ this.excludeClasses.add(c);
+ }
+ return this;
+ }
+
+ /**
+ * 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.
+ *
+ * @param rootVariableName the name to use when declaring the root variable
+ */
+ public void setRootVariableName(final String rootVariableName) {
+ this.rootVariableName = rootVariableName;
+ }
+
+ /**
+ * Returns the name used to declare the root variable to which the built
+ * JavaScript object is assigned.
+ */
+ public String getRootVariableName() {
+ return rootVariableName;
+ }
+
+ /**
* Causes the JavaScriptBuilder to navigate the properties of the supplied
object and
* convert them to JavaScript.
*
@@ -137,11 +187,10 @@
return;
}
- String rootName = "_sj_root_" + new
Random().nextInt(Integer.MAX_VALUE);
- buildNode(rootName, this.rootObject, "");
+ buildNode(this.rootVariableName, this.rootObject, "");
writer.write("var ");
- writer.write(rootName);
+ writer.write(rootVariableName);
writer.write(";\n");
for (Map.Entry<String,String> entry : objectValues.entrySet()) {
@@ -159,7 +208,7 @@
writer.append(";\n");
}
- writer.append(rootName).append(";\n");
+ writer.append(rootVariableName).append(";\n");
}
catch (Exception e) {
throw new StripesRuntimeException("Could not build JavaScript for
object. An " +
@@ -194,7 +243,7 @@
public boolean isScalarType(Object in) {
if (in == null) return true; // Though not strictly scalar, null can
be treated as such
- Class<? extends Object> type = in.getClass();
+ Class<?> type = in.getClass();
return simpleTypes.contains(type)
|| Number.class.isAssignableFrom(type)
|| String.class.isAssignableFrom(type)
Modified:
trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptResolution.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptResolution.java
2008-01-19 15:02:20 UTC (rev 762)
+++ trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptResolution.java
2008-01-19 16:55:57 UTC (rev 763)
@@ -44,6 +44,30 @@
}
/**
+ * Adds one or more properties to the list of types to exclude when
translating
+ * to JavaScript.
+ *
+ * @param property one or more property names to exclude
+ * @return the JavaScripResolution instance to simplify method chaining
+ */
+ public JavaScriptResolution addPropertyExclusion(final String... property)
{
+ this.builder.addPropertyExclusion(property);
+ return this;
+ }
+
+ /**
+ * Adds one or more classes to the list of types to exclude when
translating
+ * to JavaScript.
+ *
+ * @param clazz one or more classes to exclude
+ * @return the JavaScripResolution instance to simplify method chaining
+ */
+ public JavaScriptResolution addClassExclusion(final Class<?>... clazz) {
+ this.builder.addClassExclusion(clazz);
+ return this;
+ }
+
+ /**
* Converts the object passed in to JavaScript and streams it back to the
client.
*/
public void execute(HttpServletRequest request, HttpServletResponse
response) throws Exception {
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development