    001    package org.nanocontainer.webcontainer.groovy.adapters;
    002
    003    import org.nanocontainer.webcontainer.PicoContext;
    004    import org.nanocontainer.NanoContainer;
    005    import org.nanocontainer.DefaultNanoContainer;
    006    import org.picocontainer.MutablePicoContainer;
    007    import org.picocontainer.PicoContainer;
    008
    009    import java.util.Map;
    010
    011    import groovy.util.NodeBuilder;
    012
    013    public class NodeBuilderAdapter {
    014
    015        private final String nodeBuilderClassName;
    016        private final PicoContext context;
    017        private final MutablePicoContainer parentContainer;
    018        private final Map attributes;
    019
    020        public NodeBuilderAdapter(String nodeBuilderClassName, PicoContext context, MutablePicoContainer parentContainer, Map attributes) {
    021            this.nodeBuilderClassName = nodeBuilderClassName;
    022            this.context = context;
    023            this.parentContainer = parentContainer;
    024            this.attributes = attributes;
    025        }
    026
    027        public NodeBuilder getNodeBuilder() {
    028            NanoContainer factory = new DefaultNanoContainer();
    029            factory.getPico().registerComponentInstance(PicoContext.class, context);
    030            factory.getPico().registerComponentInstance(PicoContainer.class, parentContainer);
    031            factory.getPico().registerComponentInstance(Map.class, attributes);
    032            try {
    033                factory.registerComponentImplementation("nodeBuilder", nodeBuilderClassName);
    034                return (NodeBuilder) factory.getPico().getComponentInstance("nodeBuilder");
    035            } catch (ClassNotFoundException e) {
    036                throw new org.nanocontainer.script.BuilderClassNotFoundException(nodeBuilderClassName + " class name not found", e);
    037            }
    038        }
    039
    040    }