See attached.

DR


On Monday 09 December 2002 03:04 pm, Mark Meyer wrote:
> hi everyone..
>
> i am looking for freeware sites / sources that have java classes to
> handle standard Windows .ini files.
>
> i am aware of the Hashtable and Properties classes in java - but i don't
> see support in either of these classes for the concept of a section.
>
> example:
>
> [My Section Tag]
> blah_key1 = value
> blah_key2 = value
>
>
> any help or direction would be appreciated
> thx
> mark
>
> ____________________________________________________
> To change your JDJList options, please visit:
> http://www.sys-con.com/java/list.cfm
>
> Be respectful! Clean up your posts before replying
> ____________________________________________________
/*
 * Copyright (c) 2002, Aleri Inc.   All rights reserved.
 *
 * $Author: ngambi $
 *
 * $Date: 2002/12/05 09:53:52 $
 *
 * $Revision: 1.3 $
 */

package com.aleri.shared.util;

import java.io.*;
import java.util.*;

/**
 ** Parse a microsoft-style ini file.
 **/
public class IniFile {

    private HashMap sectionMap = new HashMap();

    public
	IniFile( String  filePathName )
	throws IOException {
		this(new File(filePathName));
	}

    public
	IniFile( File f )
	throws IOException {
        LineNumberReader rd = new LineNumberReader( new FileReader( f ) );
        Section currentSection = new Section( "Defaults" );
        String s = rd.readLine();
        while( s != null ) {
            s = s.trim();
            if( "".equals( s ) || s.startsWith( "#" ) )
                ; // do nothing
            else if( s.startsWith( "[" ) && s.endsWith( "]" ) ) {
                String name = s.substring( 1, s.length()-1 ).trim();
                currentSection = new Section( name );
                sectionMap.put( name, currentSection );
            }
            else if( s.startsWith( "[" ) )
                throw new IOException( "syntax error at line " + rd.getLineNumber() );
            else {    // property definition
                Tokenizer t = new Tokenizer( s );
                String key = t.getToken();
                if( ! "=".equals( t.getToken() ) )
                    throw new IOException( "syntax error at line " + rd.getLineNumber() );
                String val = t.getToken();
                while( (s = t.getToken()) != null )
                    val += " " + t;
                currentSection.put( key, val );
            }
            s = rd.readLine();
        }
		rd.close();
    }

    private static class Tokenizer {

        private String s;
        private int ix;
        private int end;

        private Tokenizer( String s ) {
            this.s = s;
            ix = 0;
            end = s.indexOf( '#' );
            if( end == -1 )
                end = s.length();
        }

        private String getToken() {
            while( ix < end && Character.isWhitespace(s.charAt(ix)) )
                ++ix;
            if( ix < end && s.charAt(ix) == '=' ) {
                ++ix;
                return "=";
            }
            int start = ix;
            while( ix < end && !Character.isWhitespace(s.charAt(ix)) && s.charAt(ix) != '=' )
                ++ix;
            return start == ix ? null : s.substring( start, ix );
        }

    }

    public Set sections() {
        HashSet s = new HashSet();
        s.addAll( sectionMap.values() );
        return s;
    }

    public Section getSection( String name ) {
        return (Section) sectionMap.get( name );
    }

    public static class Section {

        private String name;
        private HashMap map;

        private Section() {}

        private Section( String name ) {
            this.name = name;
            this.map = new HashMap();
        }

        private void put( String key, String value ) {
            map.put( key, value );
        }

        public String getName() {
            return name;
        }

        public Map getMap() {
            return (Map) map.clone();
        }

        public Properties getProperties() {
            Properties p = new Properties();
            p.putAll( map );
            return p;
        }

        public String get( String key ) {
            return (String) map.get( key );
        }

        public Set getKeys() {
            return map.keySet();
        }
        
    }


    // just for testing

    public static void main( String[] args ) throws Exception {
        IniFile inf = new IniFile( new File( args[0] ) );
        Set sects = inf.sections();
        for( Iterator i = sects.iterator(); i.hasNext(); ) {
            Section s = (Section) i.next();
            System.out.println( "[" + s.getName() + "]" );
            Map m = s.getMap();
            for( Iterator j = m.keySet().iterator(); j.hasNext(); ) {
                String key = (String) j.next();
                System.out.println( "  " + key + " = " + m.get( key ) );
            }
        }
    }

}


____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to