1 10 11 12 package org.nanocontainer.script.groovy.buildernodes; 13 14 import java.util.List ; 15 import java.util.Map ; 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 33 public class ComponentNode extends AbstractBuilderNode { 34 35 public static final String NODE_NAME = "component"; 36 37 40 public static final String KEY = "key"; 41 42 45 private static final String CLASS = "class"; 46 47 50 private static final String CLASS_NAME_KEY = "classNameKey"; 51 52 55 private static final String INSTANCE = "instance"; 56 57 60 private static final String 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 this.addAttribute(KEY) 73 .addAttribute(CLASS) 74 .addAttribute(CLASS_NAME_KEY) 75 .addAttribute(INSTANCE) 76 .addAttribute(PARAMETERS); 77 } 78 79 91 public Object createNewNode(final Object current, final Map attributes) { 92 delegate.rememberComponentKey(attributes); 93 Object key = attributes.remove(KEY); 94 Object cnkey = attributes.remove(CLASS_NAME_KEY); 95 Object classValue = attributes.remove(CLASS); 96 Object instance = attributes.remove(INSTANCE); 97 List parameters = (List ) 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 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 obj) { 119 return obj instanceof Parameter ? (Parameter) obj : new ConstantParameter(obj); 120 } 121 122 123 } 124 | Popular Tags |