001package bradleyross.common;
002/**
003 * Create and access a list of name/value pairs.
004 * <p>This class will process a file where each line contains
005 *    a name/value pair with the items separated by a colon.  Spaces 
006 *    and colons may not occur within the names.</p>
007 * @author Bradley Ross
008 *
009 */
010public class processList 
011{
012/** 
013 *     If false (default condition), 
014 *     names of pairs are processed in a case
015 *     insensitive manner and all names are converted
016 *     to lower case.
017 *     @see #isCaseSensitive()
018 *     @see #setCaseSensitive boolean;
019 */
020protected boolean caseSensitive = false;
021/**
022 * Getter for caseSensitive.
023 * @return Value of caseSensitive
024 * @see #caseSensitive
025 */
026public boolean isCaseSensitive()
027        { return caseSensitive; }
028/**
029 * Setter for caseSensitive.
030 * @param valueIn Value to be used for caseSensitive
031 * @see #caseSensitive
032 */
033public void setCaseSensitive(boolean valueIn)
034        { caseSensitive = valueIn; }
035/**
036 * Controls amount of diagnostic output.
037 * 
038 * Higher files produce more diagnostic output.
039 * @see #getDebugLevel()
040 * @see #setDebugLevel(int)
041 */
042protected int debugLevel = 5;
043/**
044 * Getter for debugLevel.
045 * @return Value of debugLevel
046 * @see #debugLevel
047 */
048public int getDebugLevel()
049        { return debugLevel; }
050/**
051 * Setter for debug level.
052 * @param levelIn Value to be used for debugLevel
053 * @see #debugLevel
054 */
055public void setDebugLevel(int levelIn)
056        { debugLevel = levelIn; }
057/** Internal class for name/value pairs.
058 * 
059 * @author Bradley Ross
060 *
061 */
062protected static class namePair
063        {
064        protected String name;
065        protected String value;
066        public namePair(String nameIn, String valueIn)
067                { name = nameIn; value=valueIn; }
068        /**
069         * Getter for name.
070         * @return Name of data pair
071         * @see #name
072         */
073        protected String getName()
074                { return name; }
075        /**
076         * Getter for value.
077         * @return Value for pair
078         * @see #value
079         */
080        protected String getValue()
081                { return value; }
082        }
083protected  java.util.Vector<namePair> internalList;
084public processList()
085{
086        internalList = new java.util.Vector<namePair>();
087}
088/**
089 * Parse a text file to create the name/value pairs.
090 * @param input Object containing input file
091 */
092public void processInput(java.io.LineNumberReader input)
093        {
094        String lineContents = null;
095        try
096                {
097                while (true)
098                        {
099                        String working = null;
100                        String name = null;
101                        String value = null;
102                        lineContents = input.readLine();
103                        if (lineContents == null) { break; }
104                        /* Process line */
105                        working = lineContents.trim();
106                        int pos = working.indexOf(":");
107                        if (pos <= 0) { continue; }
108                        if (pos == working.length() - 1) { continue; }
109                        name = working.substring(0, pos).trim();
110                        if (!caseSensitive) { name = name.toLowerCase(); }
111                        value = working.substring(pos+1).trim();
112                        internalList.add(new namePair(name, value));
113                        }
114                }
115        catch (java.io.IOException e)
116                {
117                System.out.println("Error in processing input: " + 
118                                e.getClass().getName() + " : " + e.getMessage());
119                e.printStackTrace();
120                }
121        }
122/**
123 * Diagnostic method for printing contents of list.
124 */
125public void listPairs()
126        {
127        System.out.println("Listing contents of internal list");
128        for (int i = 0; i < internalList.size(); i++)
129                {
130                namePair working = internalList.get(i);
131                System.out.println(working.getName() + " : " + working.getValue());
132                }
133        }
134public String getValue(String name)
135        {
136        String working = null;
137        String newValue = null;
138        if (isCaseSensitive())
139        { working = name; }
140        else
141        { working = name.toLowerCase(); }
142        for (int i = 0; i < internalList.size(); i++)
143                { 
144                namePair extract = internalList.get(i);
145                if (extract.getName().equals(working))
146                        { newValue = extract.getValue(); }
147                }
148        return newValue;
149        }
150public String getIntValue(String name)
151        {
152        String value = getValue(name);
153        int loc = value.indexOf(".");
154        if (loc >= 0)
155                { return value.substring(0, loc); }
156        else
157                { return value; }
158        }
159/**
160 * Test driver
161 * @param args Argument 1 is name of file
162 */
163public static void main (String args[])
164        {
165        System.out.println("Starting test driver");
166        String fileName = "a.txt";
167        if (args.length > 0) fileName = args[0];
168        processList processor = new processList();
169        try
170                {
171                java.io.Reader inputFile = new java.io.FileReader(fileName);
172                java.io.LineNumberReader reader = new java.io.LineNumberReader(inputFile);
173                processor.processInput(reader);
174                processor.listPairs();
175                System.out.println("*****");
176                System.out.println("Going for individual values");
177                System.out.println("dewpoint " + processor.getValue("dewpoint"));
178                System.out.println("sunrisetime " + processor.getValue("sunrisetime"));
179                System.out.println("stationname " + processor.getValue("stationname"));
180                }
181        catch (java.io.IOException e)
182                {
183                System.out.println("Error in main: " + e.getClass().getName() + " : " + e.getMessage());
184                e.printStackTrace();
185                }
186        }
187}