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    
023    import org.w3c.dom.Element;
024    
025    /**
026     * Utility used to decode values from DOM elements.
027     *
028     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
029     * @version 1.0.3
030     */
031    public final class ValueDecoder
032    {
033       /**
034        * Build an array of values for the supplied element array.
035        * @param elements the elements
036        * @return the resolved values
037        */
038        public Value[] decodeValues( Element[] elements )
039        {
040            Value[] values = new Value[ elements.length ];
041            for( int i=0; i<elements.length; i++ )
042            {
043                values[i] = decodeValue( elements[i] );
044            }
045            return values;
046        }
047        
048       /**
049        * Build a single value instance from a supplied element.
050        * @param element the element
051        * @return the resolved value
052        */
053        public Value decodeValue( Element element )
054        {
055            String classname = ElementHelper.getAttribute( element, "class" );
056            String method = ElementHelper.getAttribute( element, "method" );
057            Element[] elements = ElementHelper.getChildren( element, "param" );
058            if( elements.length > 0 )
059            {
060                Value[] values = decodeValues( elements );
061                return new Construct( classname, method, values );
062            }
063            else
064            {
065                String value = ElementHelper.getAttribute( element, "value" );
066                return new Construct( classname, method, value );
067            }
068        }
069    }