/* *************************************************************************** Biomolecular Interaction Network Database (BIND) The Blueprint Initiative 522 University Avenue, 9th Floor, Suite 900 Toronto, Ontario, Canada, M5G 1W7 Hogue Lab - University of Toronto Biochemistry Department Samuel Lunenfeld Research Institute, Mount Sinai Hospital Publication to cite: Bader GD, Betel D, Hogue CW. (2003) BIND: the Biomolecular Interaction Network Database. Nucleic Acids Res. 31(1): 248-50 PMID: 12519993 Copyright Notice: Copyright 2003, 2004 Mount Sinai Hospital (MSH) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit http://www.gnu.org/copyleft/gpl.html ***************************************************************************/ package org.blueprint.seqhound; import java.io.BufferedReader; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileReader; /** * This class provides a simple configuration file * reading functionality. *

* ConfigFileReader will read a properly formatted configuration file. * A properly formatted configuration file contains sections followed * by key-value pairs, one on each line. Each section is clearly labelled * with the section name on a separate line, enclosed in square braces [ ]. * Each key is separated from the value by an '=' sign. * *

* An example of a configuration file 'example.ini': * *

* # this is a comment * *

* [section1]
* key1 = value1 # comments begin after '#'
* key2 = value2
* key3 = value3
* [section2]
* key4 = value4
* [section3]
* key5 = value5
*

* If a configuration file contains duplicate keys, the value found * will be the first defined key. Subsequent overrides will not be * valid. *

* @author hao lieu (hlieu@blueprint.org) */ public class ConfigFileReader { /** * Name of the configuration file. */ private String file; /** * Creates a configuration reader attached to filename * @param filename the file to read */ public ConfigFileReader(String filename) { this.file = filename; } /** * Moves buffered reader into section. * @param section the section to move to. * @return true if successful, else false. * @throws IOException */ private boolean gotoSection(String section, BufferedReader in) throws IOException { String input; while((input = in.readLine()) != null) { /* trim the white spaces in front and back */ input = input.trim(); try { /* in case of empty lines ... */ input = input.substring(1, input.length() - 1); } catch (StringIndexOutOfBoundsException exception) { continue; /* ... just skip over them */ } if(input.equals(section)) return true; } return false; } /** * Returns the value associated with key, or null. * @param key the key to look for and retrieve the value. * @return the value associated with key, or null if not found * @throws IOException */ private String getKey(String key, BufferedReader in) throws IOException { String input; int i; /* we're reached the correct section, now we read in until we hit the correct key. */ while((input = in.readLine()) != null) { input = input.trim(); if(input.length() == 0){ /* empty line so we can skip it */ continue; } if(input.charAt(0) == '#'){ /* comment, so we can skip this */ continue; } if(input.matches("\\[.*\\]")){ /* new section encountered, so our key was not defined where we thought it was, so done searching through our section. */ return null; } if(input.matches(key)){ /* found our key */ /* remove everything trailing after the comment indicator (#) if no #, then remove nothing. */ input = (input.indexOf('#') < 0 ? input : input.substring(0, input.indexOf("#"))); if((i = input.indexOf('=')) < 0) { /* '= value' is not set, so keep going */ continue; }else{ /* '= value' is set, so we try to grab it ... */ try { input = input.substring(i+1, input.length()); } catch (IndexOutOfBoundsException e){ /* ... value not set so keep going, hopefully it is set later on. */ continue; } /* end try */ /* heres our value, so we get rid of front and back white space and return it. */ return input.trim(); } /* end else */ } /* end if(matches) */ } /* end while(input) */ /* if get here, we never found our key match */ return null; } /** * Retrieves the value matching key in section. * If the section or key in section is not found, returns null. * * @param section the section in the configuration file. * Sections are enclosed in [ ] in the configuration file. * @param key the key that identifies the value being retrieved. * @return the value associated with key in section * @throws IOException * @throws FileNotFoundException */ public String get(String section, String key) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new FileReader(this.file)); key = ".*".concat(key).concat(".*=.*");; if(!this.gotoSection(section, in)) { /* section not found */ return null; } String value = this.getKey(key, in); in.close(); return value; } }