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 org.picocontainer.MutablePicoContainer;
013    import org.picocontainer.script.ScriptedPicoContainerMarkupException;
014    
015    /**
016     * Config node adds configuration entry to mutable pico container. It requires two
017     * named parameters: key and value.  Example usage
018     * <pre>
019     * config(key:'foo',value:'bar')
020     * </pre>
021     * 
022     * @author k.pribluda
023     */
024    @SuppressWarnings("serial")
025    public class ConfigNode extends AbstractBuilderNode {
026    
027        public static final String NODE_NAME = "config";
028    
029        /**
030         * attribute name for key attribute ( Required )
031         */
032        public static final String KEY = "key";
033        /**
034         * attribute name for value attribute ( Required )
035         */
036        public static final String VALUE = "value";
037    
038        public ConfigNode() {
039            super(NODE_NAME);
040        }
041    
042        public Object createNewNode(Object current, Map<String, Object> attributes) {
043            validateScriptedAttributes(attributes);
044            ((MutablePicoContainer) current).addConfig((String) attributes.get(KEY), attributes.get(VALUE));
045            return null;
046        }
047    
048        /**
049         * ansure that node has proper attributes
050         */
051        public void validateScriptedAttributes(Map<String, Object> specifiedAttributes)
052                throws ScriptedPicoContainerMarkupException {
053            if (specifiedAttributes.size() != 2 || !isAttribute(specifiedAttributes, KEY)
054                    || !isAttribute(specifiedAttributes, VALUE)) {
055                throw new ScriptedPicoContainerMarkupException("config has two parameters - key and value");
056            }
057        }
058    }