001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.directory;
023
024import java.io.BufferedReader;
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031import java.util.StringTokenizer;
032
033/**
034 * <p>This class encapsulates all the parsing of the OBDA registry
035 * configuration file.</p>
036 *
037 * @author Brian Gilman
038 * @author Thomas Down
039 * @author Keith James
040 * @version $Revision$
041 */
042public class OBDARegistryParser {
043
044    /**
045     * <code>parseRegistry</code> parses an Open Bioinformatics Database
046     * Access (OBDA) configuration file.
047     *
048     * @param in a <code>BufferedReader</code>.
049     * @param locator a <code>String</code> a configuration file
050     * locator.
051     *
052     * @return a <code>RegistryConfiguration</code>.
053     *
054     * @exception IOException if an error reading the configuration
055     * file.
056     * @exception RegistryException if the configuration setup fails.
057     */
058    public static RegistryConfiguration parseRegistry(BufferedReader in,
059                                                      String locator)
060        throws IOException, RegistryException {
061        String line = "";
062        String dbName = "";
063        String key = "";
064        String value = "";
065        Map config = new HashMap();
066        Map currentDB = null;
067        
068        while ((line = in.readLine()) != null) {
069            if (line.trim().length() > 0) {
070                // We currently don't do anything with the version
071                // number
072                if (line.startsWith("VERSION=")) {
073                    continue;
074                }
075
076                if (line.indexOf("[") > -1) {
077                    dbName = line.substring(1, line.indexOf("]"));
078                    currentDB = new HashMap();
079                    currentDB.put("dbname", dbName);
080
081                    // Create a List of Maps through which we can fall
082                    // back if the first does not work
083                    if (config.containsKey(dbName)) {
084                        ((List) config.get(dbName)).add(currentDB);
085                    } else {
086                        List fallbacks = new ArrayList();
087                        fallbacks.add(currentDB);
088                        config.put(dbName, fallbacks);
089                    }
090
091                } else {
092                    StringTokenizer strTok = new StringTokenizer(line, "=");
093                    // Here we assume that there are only key = value
094                    // pairs in the config file
095                    key = strTok.nextToken();
096                    if (strTok.hasMoreTokens()) {
097                        value = strTok.nextToken();
098                    } else {
099                        value = "";
100                    }
101
102                    currentDB.put(key.trim(), value.trim());
103                }
104            }
105        }
106
107        return new RegistryConfiguration.Impl(locator, Collections.unmodifiableMap(config));
108    }
109}