001 /******************************************************************************* 002 * Copyright (C) PicoContainer Organization. All rights reserved. 003 * --------------------------------------------------------------------------- 004 * The software in this package is published under the terms of the BSD style 005 * license a copy of which has been included with this distribution in the 006 * LICENSE.txt file. 007 ******************************************************************************/ 008 package org.picocontainer.script.groovy.nodes; 009 010 import java.util.Map; 011 012 import groovy.lang.GroovyObject; 013 014 import org.picocontainer.MutablePicoContainer; 015 import org.picocontainer.classname.ClassLoadingPicoContainer; 016 import org.picocontainer.classname.DefaultClassLoadingPicoContainer; 017 import org.picocontainer.classname.ClassName; 018 019 /** 020 * Handles the child of container 'newBuilder' node. 021 * 022 * @author James Strachan 023 * @author Paul Hammant 024 * @author Aslak Hellesøy 025 * @author Michael Rimov 026 * @author Mauro Talevi 027 */ 028 @SuppressWarnings("serial") 029 public class NewBuilderNode extends AbstractBuilderNode { 030 031 /** 032 * Node name we're handling: 'newBuilder'. 033 */ 034 public static final String NODE_NAME = "newBuilder"; 035 036 /** 037 * Supported attribute: 'class'. 038 */ 039 public static final String CLASS_ATTRIBUTE = "class"; 040 041 /** 042 * Supported attribute 'validating'. Indicates that attributes should be 043 * validated and ScriptedPicoContainerMarkupException should be thrown if 044 * invalid attributes are found. 045 * 046 * @todo Not yet implemented. How do we get PicoContainer to register a 047 * component instance? -MR 048 */ 049 public static final String VALIDATE_ATTRIBUTE = "validating"; 050 051 public NewBuilderNode() { 052 super(NODE_NAME); 053 054 addAttribute(CLASS_ATTRIBUTE); 055 addAttribute(VALIDATE_ATTRIBUTE); 056 } 057 058 public Object createNewNode(final Object current, final Map<String,Object> attributes) { 059 Object builderClass = attributes.remove(CLASS_ATTRIBUTE); 060 061 ClassLoadingPicoContainer factory = new DefaultClassLoadingPicoContainer(); 062 MutablePicoContainer parentPico = ((ClassLoadingPicoContainer) current); 063 factory.addComponent(MutablePicoContainer.class, parentPico); 064 if (builderClass instanceof String) { 065 factory.addComponent(GroovyObject.class, new ClassName((String) builderClass)); 066 } else { 067 factory.addComponent(GroovyObject.class, builderClass); 068 } 069 return factory.getComponent(GroovyObject.class); 070 } 071 072 }