ValueSection.java

/**
 * ValueSection.java
 *
 *
 * @author David Green
 * @version 1.0.0 10/25/99 dgg -- original coding
 */

import java.util.*;
    
public class ValueSection extends Section {

    protected Hashtable values;
    public final static String KEY_NOT_FOUND = "!!!KEY_NOT_FOUND!!";
    
    public ValueSection( String sect_name ) {
        super( sect_name );
        type = "ValueSection";
        values = new Hashtable();
    }

    /**
     * give a text representation of section
     * @return section
     */
    public String toString() {
        String s;                // output string
        Enumeration e;           // enumeration of keys in vs
        String key;              // a particular key
        String value;            //   and its value

        // create name and rest of section
        // bug: does not handle/loses comments
        s = "[" + section_name + "]\n";

        // get an enumeration of all keys
        e = values.keys();

        // walk through enumeration and build the section
        while ( e.hasMoreElements() ) {
            key = (String) e.nextElement();
            value = (String) values.get( key );
            s = s + key + "=" + value + "\n";
        }
        
        return s;
    }

    /**
     * add a line to section
     * @return true if failure (for whatever reason)
     */
    public boolean addLine( String line ) {
        int loc_equal;       // location of = in line
        String key;          // left part of line (before = )
        String value;        // right part of line (after = )
            
        // split on = into key, value
        loc_equal = line.indexOf("=");
        if ( loc_equal == -1 )
            return true;   // bad formatting
        key = line.substring( 0, loc_equal);
        value = line.substring( loc_equal+1, line.length() );

        // bug: key, value not sanitized

        // debug code:
        // System.out.println( "line = " + line );
        // System.out.println( "key = " + key );
        // System.out.println( "value = " + value );
        
        // (not implemented) look to see if duplicated key
        /*
        if ( values.containsKey( key ) ) {
            // what to do ?  presently just overwriting
        }
        */
        // store into hash
        values.put( key, value );
        return false;
    }

    /**
     * search for search_key and and return it's value
     * @return value found (or KEY_NOT_FOUND on failure)
     */
    public String getValue( String search_key ) {
        // if search_key exists then return value
        if (values.containsKey( search_key ) ) {
            return (String) values.get( search_key );
        }
        else {
        // return KEY_NOT_FOUND
            return KEY_NOT_FOUND;
        }
    }

    /* *************** TEST DRIVER and SUPPORT CODE ********/
    
    public static void main( String[] args ) {

        System.out.println("Test ValueSection" ) ;
        
        ValueSection vs = new ValueSection( "TestSection" );

        vs.addLine( "Test=1" );
        vs.addLine( "Test2=2" );

        printResult( "Test", vs );
        printResult( "Test2", vs );
        printResult( "TestBad", vs );

        System.out.println( vs );
          
    }

    public static void printResult ( String key, ValueSection vs ) {

        String result = vs.getValue( key );
        
        if ( result.equals( KEY_NOT_FOUND ) ) {
            System.out.println( "Could not find " + key );
        }
        else {
            System.out.println( key + " = " + result );
        }
    }
        
    
} // ValueSection

Generated by GNU enscript 1.6.1.