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 java.io.Writer;
022    import java.io.IOException;
023    
024    /**
025     * Utility used to write value instances to an output stream as XML.
026     *
027     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
028     * @version 1.0.3
029     */
030    public final class ValueEncoder
031    {
032       /**
033        * Write an array of values to XML.
034        * @param writer the output stream writer
035        * @param values the value array
036        * @param pad the character offset
037        * @exception IOException if an IO error occurs
038        */
039        public void encodeValues( Writer writer, Value[] values, String pad ) throws IOException
040        {
041            for( int i=0; i<values.length; i++ )
042            {
043                Value value = values[i];
044                encodeValue( writer, value, pad );
045            }
046        }
047        
048        
049       /**
050        * Write a value to XML.
051        * @param writer the output stream writer
052        * @param value the value
053        * @param pad the character offset
054        * @exception IOException if an IO error occurs
055        */
056        public void encodeValue( Writer writer, Value value, String pad ) throws IOException
057        {
058            if( value instanceof Construct )
059            {
060                Construct construct = (Construct) value;
061                String method = construct.getMethodName();
062                String target = construct.getTargetExpression();
063                
064                writer.write( "\n" + pad + "<param" );
065                if( null != target )
066                {
067                    writer.write( " class=\"" + target + "\"" );
068                }
069                if( null != method )
070                {
071                    writer.write( " method=\"" + method  + "\"" );
072                }
073                if( construct.isCompound() )
074                {
075                    writer.write( ">" );
076                    Value[] values = construct.getValues();
077                    encodeValues( writer, values, pad + "  " );
078                    writer.write( "\n" + pad + "</param>" );
079                }
080                else
081                {
082                    String v = construct.getBaseValue();
083                    if( null != v )
084                    {
085                        writer.write( " value=\"" + v  + "\"" );
086                    }
087                    writer.write( "/>" );
088                }
089            }
090            else
091            {
092                final String error = 
093                  "Value class [" 
094                  + value.getClass().getName()
095                  + "] not supported.";
096                throw new IOException( error );
097            }
098        }
099    }