groovy.util
Class FactoryBuilderSupport

Mix of BuilderSupport and SwingBuilder's factory support.

author:
James Strachan
author:
Andres Almiray

Field Summary
 String CURRENT_FACTORY
           
 String CURRENT_NAME
           
 String CURRENT_NODE
           
 Logger LOG
           
 String OWNER
           
 String PARENT_CONTEXT
           
 String PARENT_FACTORY
           
 String PARENT_NAME
           
 String PARENT_NODE
           
 LinkedList attributeDelegates
           
 LinkedList contexts
           
 List disposalClosures
           
 Map factories
           
 Closure nameMappingClosure
           
 LinkedList postInstantiateDelegates
           
 LinkedList postNodeCompletionDelegates
           
 LinkedList preInstantiateDelegates
           
 FactoryBuilderSupport proxyBuilder
           
 
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.
Object createNode(Object name, Map attributes, Object value)
           This method is responsible for instanciating a node and configure its properties.
void dispose()
          
Object doInvokeMethod(String methodName, Object name, Object args)
           This method is the workhorse of the builder.
Map getContext()
           Returns the context of the current node.
Object getContextAttribute(String key)
          
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.
String getCurrentName()
           Returns the current node's name.
Map getFactories()
           Returns the factory map (Unmodifiable Map).
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.
String getParentName()
           Returns the parent's name of the current node.
Object getParentNode()
           Returns the parent of the current node.
FactoryBuilderSupport getProxyBuilder()
           Returns the current builder that serves as a proxy.
void handleNodeAttributes(Object node, Map attributes)
           Assigns any existing properties to the node.
Object invokeMethod(String methodName)
           Convenience method when no arguments are required
Object invokeMethod(String methodName, Object args)
          
void newContext()
           Pushes a new context on the stack.
void nodeCompleted(Object parent, Object node)
           A hook to allow nodes to be processed once they have had all of their children applied.
Map popContext()
           Removes the last context from the stack.
void postInstantiate(Object name, Map attributes, Object node)
           A hook after the factory creates the node and before attributes are set.
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.
void preInstantiate(Object name, Map attributes, Object value)
           A hook before the factory creates the node.
void registerBeanFactory(String theName, Class beanClass)
           Registers a factory for a JavaBean.
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.
void reset()
           Clears the context stack.
Factory resolveFactory(Object name, Map attributes, Object value)
           Returns the Factory associated with name.
void setClosureDelegate(Closure closure, Object node)
           A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders.
void setNodeAttributes(Object node, Map attributes)
           Maps attributes key/values to properties on node.
void setParent(Object parent, Object child)
           Strategy method to stablish parent/child relationships.
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.
Object withBuilder(FactoryBuilderSupport builder, String name, Closure closure)
           Switches the builder's proxyBuilder during the execution of a closure.
Object withBuilder(Map attributes, FactoryBuilderSupport builder, String name, Closure closure)
           Switches the builder's proxyBuilder during the execution of a closure.
 

Constructor Detail

FactoryBuilderSupport

public FactoryBuilderSupport()


FactoryBuilderSupport

public FactoryBuilderSupport(Closure nameMappingClosure)


Method Detail

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.
param:
attrDelegate


addDisposalClosure

public void addDisposalClosure(Closure closure)


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.
param:
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.
param:
delegate


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.
param:
delegate


build

public Object build(Class viewClass)


build

public Object build(Script script)


build

public Object build(String script, GroovyClassLoader loader)


checkValueIsNull

public static void checkValueIsNull(Object value, Object name)
Throws an exception if value is null.
param:
value the node's value
param:
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.
param:
value the node's value
param:
name the node's name
param:
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.
param:
value the node's value
param:
name the node's name
param:
type a Class that may be assignable to the value's class


createNode

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


dispose

public void dispose()


doInvokeMethod

Object doInvokeMethod(String methodName, Object name, Object args)
This method is the workhorse of the builder.


getContext

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


getContextAttribute

Object getContextAttribute(String key)


getContexts

LinkedList getContexts()
Returns the stack of available contexts.


getCurrent

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


getCurrentFactory

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


getCurrentName

public String getCurrentName()
Returns the current node's name.


getFactories

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


getName

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.
param:
methodName the name of the desired method
return:
the object representing the name


getParentContext

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


getParentFactory

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


getParentName

public String getParentName()
Returns the parent's name of the current node.


getParentNode

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


getProxyBuilder

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

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.


invokeMethod

public Object invokeMethod(String methodName)
Convenience method when no arguments are required
return:
the result of the call
param:
methodName the name of the method to invoke


invokeMethod

public Object invokeMethod(String methodName, Object args)


newContext

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


nodeCompleted

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


popContext

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


postInstantiate

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

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.
param:
node the current node being processed
param:
parent the parent of the node being processed
return:
the node, possibly new, that represents the markup element


preInstantiate

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.


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.


removeAttributeDelegate

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


removePostInstantiateDelegate

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


removePostNodeCompletionDelegate

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


removePreInstantiateDelegate

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


reset

void reset()
Clears the context stack.


resolveFactory

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.


setClosureDelegate

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)
param:
closure the closure on which to call setDelegate()
param:
node the node value that we've just created, which could be a
builder


setNodeAttributes

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


setParent

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


setProxyBuilder

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


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.
param:
builder the temporary builder to switch to as proxyBuilder.
param:
closure the closure to be executed under the temporary builder.
throws:
RuntimeException - any exception the closure might have thrown during
execution.
return:
the execution result of the closure.


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'.
param:
builder the temporary builder to switch to as proxyBuilder.
param:
name the node to build on the 'parent' builder.
param:
closure the closure to be executed under the temporary builder.
throws:
RuntimeException - any exception the closure might have thrown during
execution.
return:
a node that responds to value of name with the closure's result as its
value.


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.
param:
attributes additional properties for the node on the parent builder.
param:
builder the temporary builder to switch to as proxyBuilder.
param:
name the node to build on the 'parent' builder.
param:
closure the closure to be executed under the temporary builder.
throws:
RuntimeException - any exception the closure might have thrown during
execution.
return:
a node that responds to value of name with the closure's result as its
value.