groovytools.builder
Class CollectionSchemaNode

java.lang.Object
  extended by groovy.util.Node
      extended by groovytools.builder.SchemaNode
          extended by groovytools.builder.CollectionSchemaNode
All Implemented Interfaces:
Factory, Serializable

public class CollectionSchemaNode
extends SchemaNode
implements Factory

SchemaNode sub-class that handles certain collection behaviors.

Version:
$Id: CollectionSchemaNode.java 62 2009-07-28 07:32:10Z didge $
Author:
didge
See Also:
Serialized Form

Field Summary
 
Fields inherited from class groovytools.builder.SchemaNode
_parent
 
Constructor Summary
CollectionSchemaNode(SchemaNode parent, Object name)
           
CollectionSchemaNode(SchemaNode parent, Object name, Map attributes)
           
CollectionSchemaNode(SchemaNode parent, Object name, Map attributes, Object value)
           
CollectionSchemaNode(SchemaNode parent, Object name, Object value)
           
 
Method Summary
 void checkDef(FactoryBuilderSupport builder, Object collectionParent)
           
 void checkSize(Object collectionParent)
           
 SchemaNode deepCopy()
           
 Object getParentBean()
          Returns the build-time parent of the collection.
 boolean isHandlesNodeChildren()
           
 boolean isLeaf()
           
protected  Object key(Object keyAttr, Object child)
          Returns the key that may be used to add a child to the collection.
 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
           
 void onFactoryRegistration(FactoryBuilderSupport builder, String registerdName, String registeredGroupName)
           
 boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes)
           
 boolean onNodeChildren(FactoryBuilderSupport builder, Object node, Closure childContent)
           
 void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node)
           
 void setChild(FactoryBuilderSupport builder, Object parent, Object child)
          Sets the child on the parent.
 void setParent(FactoryBuilderSupport builder, Object parent, Object child)
           
protected  Object size(Object sizeAttr, Object parent)
           
 
Methods inherited from class groovytools.builder.SchemaNode
appendNode, appendNode, appendNode, appendNode, appendNode, deepCopyChildren, firstChild, fqn, fqn, parent
 
Methods inherited from class groovy.util.Node
append, attribute, attributes, breadthFirst, children, depthFirst, get, getAt, iterator, name, print, remove, setValue, text, toString, value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CollectionSchemaNode

public CollectionSchemaNode(SchemaNode parent,
                            Object name)

CollectionSchemaNode

public CollectionSchemaNode(SchemaNode parent,
                            Object name,
                            Object value)

CollectionSchemaNode

public CollectionSchemaNode(SchemaNode parent,
                            Object name,
                            Map attributes)

CollectionSchemaNode

public CollectionSchemaNode(SchemaNode parent,
                            Object name,
                            Map attributes,
                            Object value)
Method Detail

isLeaf

public boolean isLeaf()
Specified by:
isLeaf in interface Factory

newInstance

public Object newInstance(FactoryBuilderSupport builder,
                          Object name,
                          Object value,
                          Map attributes)
                   throws InstantiationException,
                          IllegalAccessException
Specified by:
newInstance in interface Factory
Throws:
InstantiationException
IllegalAccessException

onHandleNodeAttributes

public boolean onHandleNodeAttributes(FactoryBuilderSupport builder,
                                      Object node,
                                      Map attributes)
Specified by:
onHandleNodeAttributes in interface Factory

onNodeCompleted

public void onNodeCompleted(FactoryBuilderSupport builder,
                            Object parent,
                            Object node)
Specified by:
onNodeCompleted in interface Factory

setParent

public void setParent(FactoryBuilderSupport builder,
                      Object parent,
                      Object child)
Specified by:
setParent in interface Factory

getParentBean

public Object getParentBean()
Returns the build-time parent of the collection.

Returns:
see above

key

protected Object key(Object keyAttr,
                     Object child)
Returns the key that may be used to add a child to the collection.

Parameters:
keyAttr - may be a property name or closure accepting the child as the only argument
child - the child
Returns:
see above

setChild

public void setChild(FactoryBuilderSupport builder,
                     Object parent,
                     Object child)
Sets the child on the parent.

By default, reflection is used to find collection on the parent using the name of the schema. For example, if the schema's name is foos then this method will attempt to access the collection using the method, foos(). The access method of the collection may be overridden with the attribute, collection. If collection is a String, then the collection is accessed as a property of the parent using the collection value as the property's name. However, if the collection attribute value is a Closure where the first argument is the parent, then the closure will be used to access the collection.

When the collection object is not accessible or updateable, the add attribute must be used to specify the name of an alternate method accepting a single argument (the child) or a Closure accepting two arguments (the parent and the child).

For example, if foos() is not available or does not return an updateable collection, then 'add' maybe set to either addFoo or { p, c -> p.addFoo(c) }.

When using the attribute the collection attribute is ignored since it is superflous.

In either case, if the collection is a Map, then the key attribute must be specified in order to retrieve the child's key. The key may either specify a property name or a Closure accepting one argument (the child).

The value returned by calling key on the child, if it exists, is to put the child into the parent's collection.

The following shows the different ways in which to use the attributes described above:

 parent {
  collections {
      listOfChildren { // simple example of a collection of child objects
          child()
      }
      listOfChildren2(collection: 'listOfChildren') { // uses the collection above
          child()
      }
      listOfChildren3(collection: { p -> p.getListOfChildren() } ) {
          child()
      }
      listOfChildren4(add: 'addChild' ) {
          child()
      }
      listOfChildren5(add: { p, c -> p.addChild(c) } ) {
          child()
      }
      mapOfChildren(key: 'name') { // simple example of a Map of child objects, using getName() as the key
          child(name: 'Jer')
      }
      mapOfChildren2(collection: 'mapOfChildren', key: 'name') {
          child(name: 'Joe')
      }
      mapOfChildren3(collection: { p -> getMapOfChildren() }, key: 'name') {
          child(name: 'Jen')
      }
      mapOfChildren4(add: 'addChild', key: 'name') {  // note, addChild called like this: p.addChild(key, child)
          child(name: 'Jay')
      }
      mapOfChildren5(add: { p, k, c -> p.addChild(k, c) }, key: 'name') {
          child(name: 'Jan')
      }
      mapOfChildren6(add: { p, c -> p.addChild(c.getName(), c) }) {
          child(name: 'Jon')
      }
 }
 

Specified by:
setChild in interface Factory
Parameters:
builder -
parent -
child -

size

protected Object size(Object sizeAttr,
                      Object parent)

checkDef

public void checkDef(FactoryBuilderSupport builder,
                     Object collectionParent)

checkSize

public void checkSize(Object collectionParent)

isHandlesNodeChildren

public boolean isHandlesNodeChildren()

onFactoryRegistration

public void onFactoryRegistration(FactoryBuilderSupport builder,
                                  String registerdName,
                                  String registeredGroupName)

onNodeChildren

public boolean onNodeChildren(FactoryBuilderSupport builder,
                              Object node,
                              Closure childContent)

deepCopy

public SchemaNode deepCopy()
Overrides:
deepCopy in class SchemaNode


Copyright © 2008 FoundryLogic, LLC. All Rights Reserved.