KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > groovy > buildernodes > AppendContainerNode


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Michael Rimov *
9  *****************************************************************************/

10
11
12 package org.nanocontainer.script.groovy.buildernodes;
13
14 import java.util.Map JavaDoc;
15 import org.nanocontainer.NanoContainer;
16 import org.nanocontainer.script.NanoContainerMarkupException;
17 import org.picocontainer.PicoContainer;
18
19 /**
20  * Sometimes it is worthwhile to split apart Nanocontainer building
21  * into functions. For example, you might want to group adding the domain
22  * object repositories (DAOs) into a single function to make your composition script
23  * easier to maintain.
24  * <p>Unfortunately, normally this is not allowed under normal builder rules. If
25  * you wish to separate code you must revert to standard picocontainer calling
26  * systax.</p>
27  * <p>This node corrects that deficiency.</p>
28  * <p>With it you can perform:
29  * <code><pre>
30  * pico = builder.container(parent:parent) {
31  * &nbsp;&nbsp;component(....)
32  * &nbsp;&nbsp;//...
33  * }
34  * <br/>
35  * //<em>Now add more to pico.</em>
36  * builder.append(container: pico) {
37  * &nbsp;&nbsp;component(....)
38  * &nbsp;&nbsp;//...
39  * }
40  * </pre></code>
41  * </p>
42  * @author Michael Rimov
43  * @version 1.0
44  */

45 public class AppendContainerNode extends AbstractBuilderNode {
46     /**
47      * Node name.
48      */

49     public static final String JavaDoc NODE_NAME = "append";
50
51
52     /**
53      * Supported Attribute (Required): 'container.' Reference to the container
54      * we are going to append to.
55      */

56     public static final String JavaDoc CONTAINER = "container";
57
58     /**
59      * Constructs an append container node.
60      */

61     public AppendContainerNode() {
62         super(NODE_NAME);
63     }
64
65     /**
66      * Returns the container passed in as the &quot;container&quot; attribute.
67      * @param current Object unused.
68      * @param attributes Map attributes passed in. This must have the container
69      * attribute defined.
70      * @return Object the passed in Nanocontainer.
71      * @throws NanoContainerMarkupException if the container attribute
72      * is not supplied.
73      * @throws ClassCastException if the container node specified is not
74      * a nano or picocontainer.
75      */

76     public Object JavaDoc createNewNode(final Object JavaDoc current, final Map JavaDoc attributes) throws NanoContainerMarkupException, ClassCastException JavaDoc {
77         if (!isAttribute(attributes, CONTAINER)) {
78             throw new NanoContainerMarkupException(NODE_NAME + " must have a container attribute");
79         }
80
81
82         Object JavaDoc attributeValue = attributes.get(CONTAINER);
83         if (! (attributeValue instanceof NanoContainer) && !(attributeValue instanceof PicoContainer) ) {
84             throw new ClassCastException JavaDoc(attributeValue.toString() + " must be a derivative of nanocontainer. Got: "
85                 + attributeValue.getClass().getName() + " instead.");
86         }
87         return attributeValue;
88     }
89
90
91 }
92
Popular Tags