KickJava   Java API By Example, From Geeks To Geeks.

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


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 James Strachan *
9  *****************************************************************************/

10
11
12 package org.nanocontainer.script.groovy.buildernodes;
13
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.nanocontainer.NanoContainer;
18 import org.nanocontainer.script.NodeBuilderDecorationDelegate;
19 import org.nanocontainer.script.ComponentElementHelper;
20 import org.picocontainer.MutablePicoContainer;
21 import org.picocontainer.Parameter;
22 import org.picocontainer.defaults.ConstantParameter;
23
24 /**
25  *
26  * @author James Strachan
27  * @author Paul Hammant
28  * @author Aslak Hellesøy
29  * @author Michael Rimov
30  * @author Mauro Talevi
31  * @version $Revision: 2695 $
32  */

33 public class ComponentNode extends AbstractBuilderNode {
34
35     public static final String JavaDoc NODE_NAME = "component";
36
37     /**
38      * Attributes 'key'
39      */

40     public static final String JavaDoc KEY = "key";
41
42     /**
43      * Class attribute.
44      */

45     private static final String JavaDoc CLASS = "class";
46
47     /**
48      * Class Name Key Attribute.
49      */

50     private static final String JavaDoc CLASS_NAME_KEY = "classNameKey";
51
52     /**
53      * Instance attribute name.
54      */

55     private static final String JavaDoc INSTANCE = "instance";
56
57     /**
58      * Parameters attribute name.
59      */

60     private static final String JavaDoc PARAMETERS = "parameters";
61
62
63     private final NodeBuilderDecorationDelegate delegate;
64
65     public ComponentNode(NodeBuilderDecorationDelegate builderDelegate) {
66         super(NODE_NAME);
67
68         this.delegate = builderDelegate;
69
70
71         //Supported attributes.
72
this.addAttribute(KEY)
73             .addAttribute(CLASS)
74             .addAttribute(CLASS_NAME_KEY)
75             .addAttribute(INSTANCE)
76             .addAttribute(PARAMETERS);
77     }
78
79     /**
80      * Execute the handler for the given node builder.
81      * TODO - wrong Javadoc
82      * @param name Object the parent object.
83      * @param value The Node value. This is almost never used, but it kept
84      * in for consistency with the Groovy Builder API. Normally set to
85      * null.
86      * @param current The current node.
87      * @param attributes Map attributes specified in the groovy script for
88      * the builder node.
89      * @return Object
90      */

91     public Object JavaDoc createNewNode(final Object JavaDoc current, final Map JavaDoc attributes) {
92         delegate.rememberComponentKey(attributes);
93         Object JavaDoc key = attributes.remove(KEY);
94         Object JavaDoc cnkey = attributes.remove(CLASS_NAME_KEY);
95         Object JavaDoc classValue = attributes.remove(CLASS);
96         Object JavaDoc instance = attributes.remove(INSTANCE);
97         List JavaDoc parameters = (List JavaDoc) attributes.remove(PARAMETERS);
98
99         ComponentElementHelper.makeComponent(cnkey, key, getParameters(parameters), classValue, (NanoContainer) current, instance);
100
101         return this.getNodeName();
102     }
103
104     private static Parameter[] getParameters(List JavaDoc paramsList) {
105         if (paramsList == null) {
106             return null;
107         }
108         int n = paramsList.size();
109         Parameter[] parameters = new Parameter[n];
110         for (int i = 0; i < n; ++i) {
111             parameters[i] = toParameter(paramsList.get(i));
112         }
113         return parameters;
114     }
115
116
117
118     private static Parameter toParameter(Object JavaDoc obj) {
119         return obj instanceof Parameter ? (Parameter) obj : new ConstantParameter(obj);
120     }
121
122
123 }
124
Popular Tags