KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.nanocontainer.script.groovy.buildernodes;
12
13 import java.util.Map JavaDoc;
14
15 import org.nanocontainer.NanoContainer;
16 import org.nanocontainer.script.NanoContainerMarkupException;
17 import groovy.lang.GroovyObject;
18 import org.nanocontainer.DefaultNanoContainer;
19 import org.picocontainer.MutablePicoContainer;
20
21 /**
22  * Handles the child of container 'newBuilder' node.
23  * @author James Strachan
24  * @author Paul Hammant
25  * @author Aslak Hellesøy
26  * @author Michael Rimov
27  * @author Mauro Talevi
28  * @version $Revision: 2695 $
29  */

30 public class NewBuilderNode extends AbstractBuilderNode {
31
32     /**
33      * Node name we're handling: 'newBuilder'.
34      */

35     public static final String JavaDoc NODE_NAME = "newBuilder";
36
37     /**
38      * Supported attribute: 'class'.
39      */

40     public static final String JavaDoc CLASS_ATTRIBUTE = "class";
41
42     /**
43      * Suppoerted attribute 'validating'. Indicates that attributes should
44      * be validated and NanoContainerMarkupException should be thrown
45      * if invalid attributes are found.
46      * @todo Not yet implemented. How do we get NanoContainer to register
47      * a component instance? -MR
48      */

49     public static final String JavaDoc VALIDATE_ATTRIBUTE = "validating";
50
51
52     public NewBuilderNode() {
53         super(NODE_NAME);
54
55         addAttribute(CLASS_ATTRIBUTE);
56         addAttribute(VALIDATE_ATTRIBUTE);
57     }
58
59     public Object JavaDoc createNewNode(final Object JavaDoc current, final Map JavaDoc attributes) {
60         Object JavaDoc builderClass = attributes.remove(CLASS_ATTRIBUTE);
61
62
63         NanoContainer factory = new DefaultNanoContainer();
64         MutablePicoContainer parentPico = ((NanoContainer) current).getPico();
65         factory.getPico().registerComponentInstance(MutablePicoContainer.class, parentPico);
66         try {
67             if (builderClass instanceof String JavaDoc) {
68                 factory.registerComponentImplementation(GroovyObject.class, (String JavaDoc) builderClass);
69             } else {
70                 factory.getPico().registerComponentImplementation(GroovyObject.class, (Class JavaDoc) builderClass);
71             }
72         } catch (ClassNotFoundException JavaDoc e) {
73             throw new NanoContainerMarkupException("ClassNotFoundException " + builderClass);
74         }
75         Object JavaDoc componentInstance = factory.getPico().getComponentInstance(GroovyObject.class);
76         return componentInstance;
77     }
78
79 }
80
Popular Tags