001    /*
002        Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005        it under the terms of the GNU General Public License as published by
006        the Free Software Foundation; either version 1, or (at your option)
007        any later version.
008    
009        This program is distributed in the hope that it will be useful,
010        but WITHOUT ANY WARRANTY; without even the implied warranty of
011        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012        GNU General Public License for more details.
013    
014        You should have received a copy of the GNU General Public License
015        along with this program; if not, write to the Free Software
016        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.settings;
020    
021    import java.util.Properties;
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    /**
026     * This is a singleton class.  setArguments can only be called once.
027     * It can only have one reference to it, which is created inside the class.
028     *
029     * @author Adam Olsen
030     * @version 1.0
031    **/
032    public class Arguments extends Properties
033    {
034            private static Arguments instance;
035    
036            /**
037             * Initializes the arguments class with the CLI arguments
038             * @param args to be passed in from main( String args )
039            */
040            public static void setArguments( String args[] )
041            {
042                    if( instance != null )
043                    {
044                            com.valhalla.Logger.debug( "WARNING: Arguments was already initiated, and an attempt to initiate it again was just made.  This attempt was ignored." );
045                    }
046                    else {
047                            instance = new Arguments( args );
048                    }
049            }
050    
051            /**
052             * Gets the Arguments instance
053             * @return the Arguments instance
054            */
055            public static Arguments getInstance() { return instance; }
056    
057            /**
058             * Parses the arguments
059             * @params args passed in from setArguments
060            **/
061            private Arguments( String args[] )
062            {
063                    instance = this;
064                    String nameValue[] = new String[2];
065                    String keyValue;
066                    Matcher matcher;
067    
068                    for( int i = 0; i < args.length; i++ )
069                    {
070                            // parse the arguments into name value pairs
071                            nameValue = args[i].split( "=" );
072                            if( nameValue.length == 1 ) nameValue = new String[] { nameValue[0], "true" };
073                            matcher = Pattern.compile( "-" ).matcher( nameValue[0] );
074                            keyValue = matcher.replaceAll( "" );
075                            com.valhalla.Logger.debug( "Argument> " + keyValue + "=" + nameValue[1] );
076                            setProperty( keyValue, nameValue[1] );
077                    }
078            }
079    }