01: /*****************************************************************************
02: * Copyright (C) NanoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Original code by James Strachan *
09: *****************************************************************************/package org.nanocontainer.script.groovy.buildernodes;
10:
11: import java.util.Map;
12:
13: import org.nanocontainer.NanoContainer;
14: import org.nanocontainer.script.NanoContainerMarkupException;
15: import groovy.lang.GroovyObject;
16: import org.nanocontainer.DefaultNanoContainer;
17: import org.picocontainer.MutablePicoContainer;
18:
19: /**
20: * Handles the child of container 'newBuilder' node.
21: * @author James Strachan
22: * @author Paul Hammant
23: * @author Aslak Hellesøy
24: * @author Michael Rimov
25: * @author Mauro Talevi
26: * @version $Revision: 2695 $
27: */
28: public class NewBuilderNode extends AbstractBuilderNode {
29:
30: /**
31: * Node name we're handling: 'newBuilder'.
32: */
33: public static final String NODE_NAME = "newBuilder";
34:
35: /**
36: * Supported attribute: 'class'.
37: */
38: public static final String CLASS_ATTRIBUTE = "class";
39:
40: /**
41: * Suppoerted attribute 'validating'. Indicates that attributes should
42: * be validated and NanoContainerMarkupException should be thrown
43: * if invalid attributes are found.
44: * @todo Not yet implemented. How do we get NanoContainer to register
45: * a component instance? -MR
46: */
47: public static final String VALIDATE_ATTRIBUTE = "validating";
48:
49: public NewBuilderNode() {
50: super (NODE_NAME);
51:
52: addAttribute(CLASS_ATTRIBUTE);
53: addAttribute(VALIDATE_ATTRIBUTE);
54: }
55:
56: public Object createNewNode(final Object current,
57: final Map attributes) {
58: Object builderClass = attributes.remove(CLASS_ATTRIBUTE);
59:
60: NanoContainer factory = new DefaultNanoContainer();
61: MutablePicoContainer parentPico = ((NanoContainer) current)
62: .getPico();
63: factory.getPico().registerComponentInstance(
64: MutablePicoContainer.class, parentPico);
65: try {
66: if (builderClass instanceof String) {
67: factory.registerComponentImplementation(
68: GroovyObject.class, (String) builderClass);
69: } else {
70: factory.getPico().registerComponentImplementation(
71: GroovyObject.class, (Class) builderClass);
72: }
73: } catch (ClassNotFoundException e) {
74: throw new NanoContainerMarkupException(
75: "ClassNotFoundException " + builderClass);
76: }
77: Object componentInstance = factory.getPico()
78: .getComponentInstance(GroovyObject.class);
79: return componentInstance;
80: }
81:
82: }
|