001: /*****************************************************************************
002: * Copyright (C) NanoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Original code by James Strachan *
009: *****************************************************************************/package org.nanocontainer.script.groovy.buildernodes;
010:
011: import java.util.List;
012: import java.util.Map;
013:
014: import org.nanocontainer.NanoContainer;
015: import org.nanocontainer.script.NodeBuilderDecorationDelegate;
016: import org.nanocontainer.script.ComponentElementHelper;
017: import org.picocontainer.MutablePicoContainer;
018: import org.picocontainer.Parameter;
019: import org.picocontainer.defaults.ConstantParameter;
020:
021: /**
022: *
023: * @author James Strachan
024: * @author Paul Hammant
025: * @author Aslak Hellesøy
026: * @author Michael Rimov
027: * @author Mauro Talevi
028: * @version $Revision: 2695 $
029: */
030: public class ComponentNode extends AbstractBuilderNode {
031:
032: public static final String NODE_NAME = "component";
033:
034: /**
035: * Attributes 'key'
036: */
037: public static final String KEY = "key";
038:
039: /**
040: * Class attribute.
041: */
042: private static final String CLASS = "class";
043:
044: /**
045: * Class Name Key Attribute.
046: */
047: private static final String CLASS_NAME_KEY = "classNameKey";
048:
049: /**
050: * Instance attribute name.
051: */
052: private static final String INSTANCE = "instance";
053:
054: /**
055: * Parameters attribute name.
056: */
057: private static final String PARAMETERS = "parameters";
058:
059: private final NodeBuilderDecorationDelegate delegate;
060:
061: public ComponentNode(NodeBuilderDecorationDelegate builderDelegate) {
062: super (NODE_NAME);
063:
064: this .delegate = builderDelegate;
065:
066: //Supported attributes.
067: this .addAttribute(KEY).addAttribute(CLASS).addAttribute(
068: CLASS_NAME_KEY).addAttribute(INSTANCE).addAttribute(
069: PARAMETERS);
070: }
071:
072: /**
073: * Execute the handler for the given node builder.
074: * TODO - wrong Javadoc
075: * @param name Object the parent object.
076: * @param value The Node value. This is almost never used, but it kept
077: * in for consistency with the Groovy Builder API. Normally set to
078: * null.
079: * @param current The current node.
080: * @param attributes Map attributes specified in the groovy script for
081: * the builder node.
082: * @return Object
083: */
084: public Object createNewNode(final Object current,
085: final Map attributes) {
086: delegate.rememberComponentKey(attributes);
087: Object key = attributes.remove(KEY);
088: Object cnkey = attributes.remove(CLASS_NAME_KEY);
089: Object classValue = attributes.remove(CLASS);
090: Object instance = attributes.remove(INSTANCE);
091: List parameters = (List) attributes.remove(PARAMETERS);
092:
093: ComponentElementHelper.makeComponent(cnkey, key,
094: getParameters(parameters), classValue,
095: (NanoContainer) current, instance);
096:
097: return this .getNodeName();
098: }
099:
100: private static Parameter[] getParameters(List paramsList) {
101: if (paramsList == null) {
102: return null;
103: }
104: int n = paramsList.size();
105: Parameter[] parameters = new Parameter[n];
106: for (int i = 0; i < n; ++i) {
107: parameters[i] = toParameter(paramsList.get(i));
108: }
109: return parameters;
110: }
111:
112: private static Parameter toParameter(Object obj) {
113: return obj instanceof Parameter ? (Parameter) obj
114: : new ConstantParameter(obj);
115: }
116:
117: }
|