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
private  java.util.LinkedList attributeDelegates
           
private  java.util.LinkedList contexts
           
static java.lang.String CURRENT_FACTORY
           
static java.lang.String CURRENT_NAME
           
static java.lang.String CURRENT_NODE
           
private  java.util.List disposalClosures
           
private  java.util.Map factories
           
private static java.util.logging.Logger LOG
           
private  Closure nameMappingClosure
           
static java.lang.String OWNER
           
static java.lang.String PARENT_CONTEXT
           
static java.lang.String PARENT_FACTORY
           
static java.lang.String PARENT_NAME
           
static java.lang.String PARENT_NODE
           
private  java.util.LinkedList postInstantiateDelegates
           
private  java.util.LinkedList postNodeCompletionDelegates
           
private  java.util.LinkedList preInstantiateDelegates
           
private  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.
 java.lang.Object build(java.lang.Class viewClass)
           
 java.lang.Object build(Script script)
           
 java.lang.Object build(java.lang.String script, GroovyClassLoader loader)
           
static void checkValueIsNull(java.lang.Object value, java.lang.Object name)
          Throws an exception if value is null.
static boolean checkValueIsType(java.lang.Object value, java.lang.Object name, java.lang.Class type)
          Returns true if type is assignalbe to the value's class, false if value is null.
static boolean checkValueIsTypeNotString(java.lang.Object value, java.lang.Object name, java.lang.Class type)
          Returns true if type is assignale to the value's class, false if value is null or a String.
protected  java.lang.Object createNode(java.lang.Object name, java.util.Map attributes, java.lang.Object value)
          This method is responsible for instanciating a node and configure its properties.
 void dispose()
           
private  java.lang.Object doInvokeMethod(java.lang.String methodName, java.lang.Object name, java.lang.Object args)
          This method is the workhorse of the builder.
 java.util.Map getContext()
          Returns the context of the current node.
private  java.lang.Object getContextAttribute(java.lang.String key)
           
protected  java.util.LinkedList getContexts()
          Returns the stack of available contexts.
 java.lang.Object getCurrent()
          Returns the current node being built.
 Factory getCurrentFactory()
          Returns the factory that built the current node.
 java.lang.String getCurrentName()
          Returns the current node's name.
 java.util.Map getFactories()
          Returns the factory map (Unmodifiable Map).
protected  java.lang.Object getName(java.lang.String methodName)
          A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.
 java.util.Map getParentContext()
          Returns the context of the parent of the current node.
 Factory getParentFactory()
          Returns the factory of the parent of the current node.
 java.lang.String getParentName()
          Returns the parent's name of the current node.
 java.lang.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(java.lang.Object node, java.util.Map attributes)
          Assigns any existing properties to the node.
It will call attributeDelegates before passing control to the factory that built the node.
 java.lang.Object invokeMethod(java.lang.String methodName)
          Convenience method when no arguments are required
 java.lang.Object invokeMethod(java.lang.String methodName, java.lang.Object args)
          Invokes the given method.
protected  void newContext()
          Pushes a new context on the stack.
protected  void nodeCompleted(java.lang.Object parent, java.lang.Object node)
          A hook to allow nodes to be processed once they have had all of their children applied.
protected  java.util.Map popContext()
          Removes the last context from the stack.
protected  void postInstantiate(java.lang.Object name, java.util.Map attributes, java.lang.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  java.lang.Object postNodeCompletion(java.lang.Object parent, java.lang.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(java.lang.Object name, java.util.Map attributes, java.lang.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(java.lang.String theName, java.lang.Class beanClass)
          Registers a factory for a JavaBean.
The JavaBean clas should have a no-args constructor.
 void registerFactory(java.lang.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(java.lang.Object name, java.util.Map attributes, java.lang.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, java.lang.Object node)
          A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders.
protected  void setNodeAttributes(java.lang.Object node, java.util.Map attributes)
          Maps attributes key/values to properties on node.
protected  void setParent(java.lang.Object parent, java.lang.Object child)
          Strategy method to stablish parent/child relationships.
protected  void setProxyBuilder(FactoryBuilderSupport proxyBuilder)
          Sets the builder to be used as a proxy.
 java.lang.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.
 java.lang.Object withBuilder(FactoryBuilderSupport builder, java.lang.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.
 java.lang.Object withBuilder(java.util.Map attributes, FactoryBuilderSupport builder, java.lang.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 java.lang.String CURRENT_FACTORY
See Also:
Constant Field Values

PARENT_FACTORY

public static final java.lang.String PARENT_FACTORY
See Also:
Constant Field Values

PARENT_NODE

public static final java.lang.String PARENT_NODE
See Also:
Constant Field Values

CURRENT_NODE

public static final java.lang.String CURRENT_NODE
See Also:
Constant Field Values

PARENT_CONTEXT

public static final java.lang.String PARENT_CONTEXT
See Also:
Constant Field Values

PARENT_NAME

public static final java.lang.String PARENT_NAME
See Also:
Constant Field Values

CURRENT_NAME

public static final java.lang.String CURRENT_NAME
See Also:
Constant Field Values

OWNER

public static final java.lang.String OWNER
See Also:
Constant Field Values

LOG

private static final java.util.logging.Logger LOG

contexts

private java.util.LinkedList contexts

attributeDelegates

private java.util.LinkedList attributeDelegates

disposalClosures

private java.util.List disposalClosures

factories

private java.util.Map factories

nameMappingClosure

private Closure nameMappingClosure

proxyBuilder

private FactoryBuilderSupport proxyBuilder

preInstantiateDelegates

private java.util.LinkedList preInstantiateDelegates

postInstantiateDelegates

private java.util.LinkedList postInstantiateDelegates

postNodeCompletionDelegates

private java.util.LinkedList postNodeCompletionDelegates
Constructor Detail

FactoryBuilderSupport

public FactoryBuilderSupport()

FactoryBuilderSupport

public FactoryBuilderSupport(Closure nameMappingClosure)
Method Detail

checkValueIsNull

public static void checkValueIsNull(java.lang.Object value,
                                    java.lang.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(java.lang.Object value,
                                       java.lang.Object name,
                                       java.lang.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(java.lang.Object value,
                                                java.lang.Object name,
                                                java.lang.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 java.util.Map getFactories()
Returns the factory map (Unmodifiable Map).


getContext

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


getCurrent

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


getCurrentFactory

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


getCurrentName

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


getParentFactory

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


getParentNode

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


getParentName

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


getParentContext

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


getContextAttribute

private java.lang.Object getContextAttribute(java.lang.String key)

invokeMethod

public java.lang.Object invokeMethod(java.lang.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 java.lang.Object invokeMethod(java.lang.String methodName,
                                     java.lang.Object args)
Description copied from interface: GroovyObject
Invokes the given method.

Specified by:
invokeMethod in interface GroovyObject
Overrides:
invokeMethod in class GroovyObjectSupport
Parameters:
methodName - the name of the method to call
args - the arguments to use for the method call
Returns:
the result of invoking the method

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(java.lang.String theName,
                                java.lang.Class beanClass)
Registers a factory for a JavaBean.
The JavaBean clas should have a no-args constructor.


registerFactory

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


createNode

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


resolveFactory

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


doInvokeMethod

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


getName

protected java.lang.Object getName(java.lang.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(java.lang.Object node,
                                    java.util.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(java.lang.Object parent,
                             java.lang.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 java.util.Map popContext()
Removes the last context from the stack.


postInstantiate

protected void postInstantiate(java.lang.Object name,
                               java.util.Map attributes,
                               java.lang.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 java.lang.Object postNodeCompletion(java.lang.Object parent,
                                              java.lang.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(java.lang.Object name,
                              java.util.Map attributes,
                              java.lang.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,
                                  java.lang.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(java.lang.Object node,
                                 java.util.Map attributes)
Maps attributes key/values to properties on node.


setParent

protected void setParent(java.lang.Object parent,
                         java.lang.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 java.util.LinkedList getContexts()
Returns the stack of available contexts.


build

public java.lang.Object build(java.lang.Class viewClass)

build

public java.lang.Object build(Script script)

build

public java.lang.Object build(java.lang.String script,
                              GroovyClassLoader loader)

withBuilder

public java.lang.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:
java.lang.RuntimeException - - any exception the closure might have thrown during execution.

withBuilder

public java.lang.Object withBuilder(FactoryBuilderSupport builder,
                                    java.lang.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:
java.lang.RuntimeException - - any exception the closure might have thrown during execution.

withBuilder

public java.lang.Object withBuilder(java.util.Map attributes,
                                    FactoryBuilderSupport builder,
                                    java.lang.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:
java.lang.RuntimeException - - any exception the closure might have thrown during execution.

addDisposalClosure

public void addDisposalClosure(Closure closure)

dispose

public void dispose()


Copyright © ${year} The Codehaus. All Rights Reserved.