001    /*
002     * Copyright 2006 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.lang;
020    
021    import net.dpml.util.ElementHelper;
022    import net.dpml.util.Resolver;
023    
024    import org.w3c.dom.Element;
025    
026    /**
027     * Utility used to decode values from DOM elements.
028     *
029     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030     * @version 1.1.0
031     */
032    public final class ValueDecoder
033    {
034       /**
035        * Build an array of values for the supplied element array.
036        * @param elements the elements
037        * @return the resolved values
038        */
039        public Value[] decodeValues( Element[] elements )
040        {
041            return decodeValues( elements, null );
042        }
043        
044       /**
045        * Build an array of values for the supplied element array.
046        * @param elements the elements
047        * @return the resolved values
048        */
049        public Value[] decodeValues( Element[] elements, Resolver resolver )
050        {
051            Value[] values = new Value[ elements.length ];
052            for( int i=0; i<elements.length; i++ )
053            {
054                values[i] = decodeValue( elements[i], resolver );
055            }
056            return values;
057        }
058        
059       /**
060        * Build a single value instance from a supplied element.
061        * @param element the element
062        * @return the resolved value
063        */
064        public Value decodeValue( Element element )
065        {
066            return decodeValue( element, null );
067        }
068        
069       /**
070        * Build a single value instance from a supplied element.
071        * @param element the element
072        * @return the resolved value
073        */
074        public Value decodeValue( Element element, Resolver resolver )
075        {
076            String classname = ElementHelper.getAttribute( element, "class", null, resolver );
077            String method = ElementHelper.getAttribute( element, "method", null, resolver );
078            Element[] elements = ElementHelper.getChildren( element, "param" );
079            if( elements.length > 0 )
080            {
081                Value[] values = decodeValues( elements, resolver );
082                return new Construct( classname, method, values );
083            }
084            else
085            {
086                String value = ElementHelper.getAttribute( element, "value", null, resolver );
087                return new Construct( classname, method, value );
088            }
089        }
090    }