FileVersificationMapping.java |
1 /** 2 * Distribution License: 3 * JSword is free software; you can redistribute it and/or modify it under 4 * the terms of the GNU Lesser General Public License, version 2.1 or later 5 * as published by the Free Software Foundation. This program is distributed 6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 7 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 * See the GNU Lesser General Public License for more details. 9 * 10 * The License is available on the internet at: 11 * http://www.gnu.org/copyleft/lgpl.html 12 * or by writing to: 13 * Free Software Foundation, Inc. 14 * 59 Temple Place - Suite 330 15 * Boston, MA 02111-1307, USA 16 * 17 * © CrossWire Bible Society, 2013 - 2016 18 * 19 */ 20 package org.crosswire.jsword.versification; 21 22 import org.crosswire.common.config.ConfigException; 23 import org.crosswire.common.util.KeyValuePair; 24 import org.crosswire.common.util.ResourceUtil; 25 26 import java.io.BufferedReader; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.io.InputStreamReader; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** 34 * This reads a file up front and creates the key value pairs. Because 35 * we're not quite using the 'properties' file definition because we allow 36 * duplicate keys on either side of the '=' sign, we need to do the processing ourselves. 37 * 38 * @see gnu.lgpl.License The GNU Lesser General Public License for details. 39 * @author Chris Burrell 40 */ 41 public class FileVersificationMapping { 42 //unsure what a typical value would be, so leaving at 16 - best to optimize for memory, 43 //than speed upon reading the first time. 44 private List<KeyValuePair> pairs = new ArrayList<KeyValuePair>(16); 45 46 /** 47 * Allow a default initialising if someone wants to create a mapping file dynamically. 48 */ 49 public FileVersificationMapping() { 50 //no work to do. 51 } 52 53 /** 54 * @param versification the name of the versification maps to the expected .properties file 55 * @throws IOException error reading the mapping files 56 * @throws ConfigException error parsing the contents of the file 57 */ 58 public FileVersificationMapping(Versification versification) throws IOException, ConfigException { 59 //TODO(CJB): deal with Missing Resource Exceptions 60 InputStream s = ResourceUtil.getResourceAsStream(getClass(), versification.getName() + ".properties"); 61 BufferedReader lineReader = new BufferedReader(new InputStreamReader(s)); 62 String line; 63 while ((line = lineReader.readLine()) != null) { 64 if (line.length() == 0 || line.charAt(0) == '#') { 65 continue; 66 } 67 68 int firstEqual = line.indexOf('='); 69 if (firstEqual == -1) { 70 this.addProperty(line, null); 71 } else { 72 this.addProperty(line.substring(0, firstEqual), line.substring(firstEqual + 1)); 73 } 74 } 75 } 76 77 /** 78 * @param key the key 79 * @param value the value 80 */ 81 public void addProperty(String key, String value) { 82 pairs.add(new KeyValuePair(key, value)); 83 } 84 85 public List<KeyValuePair> getMappings() { 86 return this.pairs; 87 } 88 } 89