groovy.util
Class FactoryBuilderSupport

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by groovy.lang.Binding
          extended by groovy.util.FactoryBuilderSupport
All Implemented Interfaces:
GroovyObject
Direct Known Subclasses:
ObjectGraphBuilder

public abstract class FactoryBuilderSupport
extends Binding

Mix of BuilderSupport and SwingBuilder's factory support.

Author:
James Strachan, Andres Almiray

Field Summary
static String CURRENT_FACTORY
           
static String CURRENT_NODE
           
static String OWNER
           
static String PARENT_CONTEXT
           
static String PARENT_FACTORY
           
static String PARENT_NODE
           
 
Constructor Summary
FactoryBuilderSupport()
           
FactoryBuilderSupport(Closure nameMappingClosure)
           
 
Method Summary
 Closure addAttributeDelegate(Closure attrDelegate)
          Add an attribute delegate so it can intercept attributes being set.
 void addDisposalClosure(Closure closure)
           
 Closure addPostInstantiateDelegate(Closure delegate)
          Add a postInstantiate delegate so it can intercept nodes after they are created.
 Closure addPostNodeCompletionDelegate(Closure delegate)
          Add a nodeCompletion delegate so it can intercept nodes after they done with building.
 Closure addPreInstantiateDelegate(Closure delegate)
          Add a preInstantiate delegate so it can intercept nodes before they are created.
 Object build(Class viewClass)
           
 Object build(Script script)
           
 Object build(String script, GroovyClassLoader loader)
           
static void checkValueIsNull(Object value, Object name)
          Throws an exception if value is null.
static boolean checkValueIsType(Object value, Object name, Class type)
          Returns true if type is assignalbe to the value's class, false if value is null.
static boolean checkValueIsTypeNotString(Object value, Object name, Class type)
          Returns true if type is assignale to the value's class, false if value is null or a String.
protected  Object createNode(Object name, Map attributes, Object value)
          This method is responsible for instanciating a node and configure its properties.
 void dispose()
           
 Map getContext()
          Returns the context of the current node.
protected  LinkedList getContexts()
          Returns the stack of available contexts.
 Object getCurrent()
          Returns the current node being built.
 Factory getCurrentFactory()
          Returns the factory that built the current node.
 Map getFactories()
          Returns the factory map (Unmodifiable Map).
protected  Object getName(String methodName)
          A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.
 Map getParentContext()
          Returns the context of the parent of the current node.
 Factory getParentFactory()
          Returns the factory of the parent of the current node.
 Object getParentNode()
          Returns the parent of the current node.
protected  FactoryBuilderSupport getProxyBuilder()
          Returns the current builder that serves as a proxy.
Proxy builders are useful for changing the building context, thus enabling mix & match builders.
protected  void handleNodeAttributes(Object node, Map attributes)
          Assigns any existing properties to the node.
It will call attributeDelegates before passing control to the factory that built the node.
 Object invokeMethod(String methodName)
          Convenience method when no arguments are required
 Object invokeMethod(String methodName, Object args)
          Invokes the given method
protected  void newContext()
          Pushes a new context on the stack.
protected  void nodeCompleted(Object parent, Object node)
          A hook to allow nodes to be processed once they have had all of their children applied.
protected  Map popContext()
          Removes the last context from the stack.
protected  void postInstantiate(Object name, Map attributes, Object node)
          A hook after the factory creates the node and before attributes are set.
It will call any registered postInstantiateDelegates, if you override this method be sure to call this impl somewhere in your code.
protected  Object postNodeCompletion(Object parent, Object node)
          A hook to allow nodes to be processed once they have had all of their children applied and allows the actual node object that represents the Markup element to be changed.
It will call any registered postNodeCompletionDelegates, if you override this method be sure to call this impl at the end of your code.
protected  void preInstantiate(Object name, Map attributes, Object value)
          A hook before the factory creates the node.
It will call any registered preInstantiateDelegates, if you override this method be sure to call this impl somewhere in your code.
 void registerBeanFactory(String theName, Class beanClass)
          Registers a factory for a JavaBean.
The JavaBean clas should have a no-args constructor.
 void registerFactory(String name, Factory factory)
          Registers a factory for a node name.
 void removeAttributeDelegate(Closure attrDelegate)
          Remove the most recently added instance of the attribute delegate.
 void removePostInstantiateDelegate(Closure delegate)
          Remove the most recently added instance of the postInstantiate delegate.
 void removePostNodeCompletionDelegate(Closure delegate)
          Remove the most recently added instance of the nodeCompletion delegate.
 void removePreInstantiateDelegate(Closure delegate)
          Remove the most recently added instance of the preInstantiate delegate.
protected  void reset()
          Clears the context stack.
protected  Factory resolveFactory(Object name, Map attributes, Object value)
          Returns the Factory associated with name.
This is a hook for subclasses to plugin a custom strategy for mapping names to factories.
protected  void setClosureDelegate(Closure closure, Object node)
          A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders.
protected  void setNodeAttributes(Object node, Map attributes)
          Maps attributes key/values to properties on node.
protected  void setParent(Object parent, Object child)
          Strategy method to stablish parent/child relationships.
protected  void setProxyBuilder(FactoryBuilderSupport proxyBuilder)
          Sets the builder to be used as a proxy.
 Object withBuilder(FactoryBuilderSupport builder, Closure closure)
          Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup.
 Object withBuilder(FactoryBuilderSupport builder, String name, Closure closure)
          Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup.
 Object withBuilder(Map attributes, FactoryBuilderSupport builder, String name, Closure closure)
          Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup.
 
Methods inherited from class groovy.lang.Binding
getProperty, getVariable, getVariables, setProperty, setVariable
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CURRENT_FACTORY

public static final String CURRENT_FACTORY
See Also:
Constant Field Values

PARENT_FACTORY

public static final String PARENT_FACTORY
See Also:
Constant Field Values

PARENT_NODE

public static final String PARENT_NODE
See Also:
Constant Field Values

CURRENT_NODE

public static final String CURRENT_NODE
See Also:
Constant Field Values

PARENT_CONTEXT

public static final String PARENT_CONTEXT
See Also:
Constant Field Values

OWNER

public static final String OWNER
See Also:
Constant Field Values
Constructor Detail

FactoryBuilderSupport

public FactoryBuilderSupport()

FactoryBuilderSupport

public FactoryBuilderSupport(Closure nameMappingClosure)
Method Detail

checkValueIsNull

public static void checkValueIsNull(Object value,
                                    Object name)
Throws an exception if value is null.

Parameters:
value - the node's value
name - the node's name

checkValueIsType

public static boolean checkValueIsType(Object value,
                                       Object name,
                                       Class type)
Returns true if type is assignalbe to the value's class, false if value is null.

Parameters:
value - the node's value
name - the node's name
type - a Class that may be assignable to the value's class

checkValueIsTypeNotString

public static boolean checkValueIsTypeNotString(Object value,
                                                Object name,
                                                Class type)
Returns true if type is assignale to the value's class, false if value is null or a String.

Parameters:
value - the node's value
name - the node's name
type - a Class that may be assignable to the value's class

getFactories

public Map getFactories()
Returns the factory map (Unmodifiable Map).


getContext

public Map getContext()
Returns the context of the current node.


getCurrent

public Object getCurrent()
Returns the current node being built.


getCurrentFactory

public Factory getCurrentFactory()
Returns the factory that built the current node.


getParentFactory

public Factory getParentFactory()
Returns the factory of the parent of the current node.


getParentNode

public Object getParentNode()
Returns the parent of the current node.


getParentContext

public Map getParentContext()
Returns the context of the parent of the current node.


invokeMethod

public Object invokeMethod(String methodName)
Convenience method when no arguments are required

Parameters:
methodName - the name of the method to invoke
Returns:
the result of the call

invokeMethod

public Object invokeMethod(String methodName,
                           Object args)
Description copied from interface: GroovyObject
Invokes the given method

Specified by:
invokeMethod in interface GroovyObject
Overrides:
invokeMethod in class GroovyObjectSupport

addAttributeDelegate

public Closure addAttributeDelegate(Closure attrDelegate)
Add an attribute delegate so it can intercept attributes being set. Attribute delegates are fired in a FILO pattern, so that nested delegates get first crack.

Parameters:
attrDelegate -

removeAttributeDelegate

public void removeAttributeDelegate(Closure attrDelegate)
Remove the most recently added instance of the attribute delegate.

Parameters:
attrDelegate -

addPreInstantiateDelegate

public Closure addPreInstantiateDelegate(Closure delegate)
Add a preInstantiate delegate so it can intercept nodes before they are created. PreInstantiate delegates are fired in a FILO pattern, so that nested delegates get first crack.

Parameters:
delegate -

removePreInstantiateDelegate

public void removePreInstantiateDelegate(Closure delegate)
Remove the most recently added instance of the preInstantiate delegate.

Parameters:
delegate -

addPostInstantiateDelegate

public Closure addPostInstantiateDelegate(Closure delegate)
Add a postInstantiate delegate so it can intercept nodes after they are created. PostInstantiate delegates are fired in a FILO pattern, so that nested delegates get first crack.

Parameters:
delegate -

removePostInstantiateDelegate

public void removePostInstantiateDelegate(Closure delegate)
Remove the most recently added instance of the postInstantiate delegate.

Parameters:
delegate -

addPostNodeCompletionDelegate

public Closure addPostNodeCompletionDelegate(Closure delegate)
Add a nodeCompletion delegate so it can intercept nodes after they done with building. NodeCompletion delegates are fired in a FILO pattern, so that nested delegates get first crack.

Parameters:
delegate -

removePostNodeCompletionDelegate

public void removePostNodeCompletionDelegate(Closure delegate)
Remove the most recently added instance of the nodeCompletion delegate.

Parameters:
delegate -

registerBeanFactory

public void registerBeanFactory(String theName,
                                Class beanClass)
Registers a factory for a JavaBean.
The JavaBean clas should have a no-args constructor.


registerFactory

public void registerFactory(String name,
                            Factory factory)
Registers a factory for a node name.


createNode

protected Object createNode(Object name,
                            Map attributes,
                            Object value)
This method is responsible for instanciating a node and configure its properties.


resolveFactory

protected Factory resolveFactory(Object name,
                                 Map attributes,
                                 Object value)
Returns the Factory associated with name.
This is a hook for subclasses to plugin a custom strategy for mapping names to factories.


getName

protected Object getName(String methodName)
A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.

Parameters:
methodName - the name of the desired method
Returns:
the object representing the name

getProxyBuilder

protected FactoryBuilderSupport getProxyBuilder()
Returns the current builder that serves as a proxy.
Proxy builders are useful for changing the building context, thus enabling mix & match builders.


handleNodeAttributes

protected void handleNodeAttributes(Object node,
                                    Map attributes)
Assigns any existing properties to the node.
It will call attributeDelegates before passing control to the factory that built the node.


newContext

protected void newContext()
Pushes a new context on the stack.


nodeCompleted

protected void nodeCompleted(Object parent,
                             Object node)
A hook to allow nodes to be processed once they have had all of their children applied.

Parameters:
node - the current node being processed
parent - the parent of the node being processed

popContext

protected Map popContext()
Removes the last context from the stack.


postInstantiate

protected void postInstantiate(Object name,
                               Map attributes,
                               Object node)
A hook after the factory creates the node and before attributes are set.
It will call any registered postInstantiateDelegates, if you override this method be sure to call this impl somewhere in your code.


postNodeCompletion

protected Object postNodeCompletion(Object parent,
                                    Object node)
A hook to allow nodes to be processed once they have had all of their children applied and allows the actual node object that represents the Markup element to be changed.
It will call any registered postNodeCompletionDelegates, if you override this method be sure to call this impl at the end of your code.

Parameters:
node - the current node being processed
parent - the parent of the node being processed
Returns:
the node, possibly new, that represents the markup element

preInstantiate

protected void preInstantiate(Object name,
                              Map attributes,
                              Object value)
A hook before the factory creates the node.
It will call any registered preInstantiateDelegates, if you override this method be sure to call this impl somewhere in your code.


reset

protected void reset()
Clears the context stack.


setClosureDelegate

protected void setClosureDelegate(Closure closure,
                                  Object node)
A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders. This method should call the setDelegate() method on the closure which by default passes in this but if node is-a builder we could pass that in instead (or do something wacky too)

Parameters:
closure - the closure on which to call setDelegate()
node - the node value that we've just created, which could be a builder

setNodeAttributes

protected void setNodeAttributes(Object node,
                                 Map attributes)
Maps attributes key/values to properties on node.


setParent

protected void setParent(Object parent,
                         Object child)
Strategy method to stablish parent/child relationships.


setProxyBuilder

protected void setProxyBuilder(FactoryBuilderSupport proxyBuilder)
Sets the builder to be used as a proxy.


getContexts

protected LinkedList getContexts()
Returns the stack of available contexts.


build

public Object build(Class viewClass)

build

public Object build(Script script)

build

public Object build(String script,
                    GroovyClassLoader loader)

withBuilder

public Object withBuilder(FactoryBuilderSupport builder,
                          Closure closure)
Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup. It will also take care of restoring the previous proxyBuilder when the execution finishes, even if an exception was thrown from inside the closure.

Parameters:
builder - the temporary builder to switch to as proxyBuilder.
closure - the closure to be executed under the temporary builder.
Returns:
the execution result of the closure.
Throws:
RuntimeException - - any exception the closure might have thrown during execution.

withBuilder

public Object withBuilder(FactoryBuilderSupport builder,
                          String name,
                          Closure closure)
Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup. It will also take care of restoring the previous proxyBuilder when the execution finishes, even if an exception was thrown from inside the closure. Additionally it will use the closure's result as the value for the node identified by 'name'.

Parameters:
builder - the temporary builder to switch to as proxyBuilder.
name - the node to build on the 'parent' builder.
closure - the closure to be executed under the temporary builder.
Returns:
a node that responds to value of name with the closure's result as its value.
Throws:
RuntimeException - - any exception the closure might have thrown during execution.

withBuilder

public Object withBuilder(Map attributes,
                          FactoryBuilderSupport builder,
                          String name,
                          Closure closure)
Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup. It will also take care of restoring the previous proxyBuilder when the execution finishes, even if an exception was thrown from inside the closure. Additionally it will use the closure's result as the value for the node identified by 'name' and assign any attributes that might have been set.

Parameters:
attributes - additional properties for the node on the parent builder.
builder - the temporary builder to switch to as proxyBuilder.
name - the node to build on the 'parent' builder.
closure - the closure to be executed under the temporary builder.
Returns:
a node that responds to value of name with the closure's result as its value.
Throws:
RuntimeException - - any exception the closure might have thrown during execution.

addDisposalClosure

public void addDisposalClosure(Closure closure)

dispose

public void dispose()

Copyright © 2003-2007 The Codehaus. All rights reserved.