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. It can only
027     * 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        private static Arguments instance;
034    
035        /**
036         * Initializes the arguments class with the CLI arguments
037         * 
038         * @param args
039         *            to be passed in from main( String args )
040         */
041        public static void setArguments(String args[]) {
042            if (instance != null) {
043                com.valhalla.Logger
044                        .debug("WARNING: Arguments was already initiated, and an attempt to initiate it again was just made.  This attempt was ignored.");
045            } else {
046                instance = new Arguments(args);
047            }
048        }
049    
050        /**
051         * Gets the Arguments instance
052         * 
053         * @return the Arguments instance
054         */
055        public static Arguments getInstance() {
056            return instance;
057        }
058    
059        public boolean getBoolean(String key) {
060            String value = getProperty(key);
061            if (value == null || value.equals("false"))
062                return false;
063            else
064                return true;
065        }
066    
067        /**
068         * Parses the arguments
069         * 
070         * @params args passed in from setArguments
071         */
072        private Arguments(String args[]) {
073            instance = this;
074            String nameValue[] = new String[2];
075            String keyValue;
076            Matcher matcher;
077    
078            for (int i = 0; i < args.length; i++) {
079                // parse the arguments into name value pairs
080                nameValue = args[i].split("=");
081                if (nameValue.length == 1)
082                    nameValue = new String[] { nameValue[0], "true" };
083                matcher = Pattern.compile("-").matcher(nameValue[0]);
084                keyValue = matcher.replaceAll("");
085                com.valhalla.Logger.debug("Argument> " + keyValue + "="
086                        + nameValue[1]);
087                setProperty(keyValue, nameValue[1]);
088            }
089        }
090    }