View Javadoc
1 /* 2 * Copyright (c) 2001 by 3 * Siegfried GOESCHL <mailto:siegfried.goeschl@itserv.at>; 4 * and Dima STADNIK <mailto:5d5@mail.ru>; 5 * 6 * This program is free software. 7 * 8 * You may redistribute it and/or modify it under the terms of the GNU 9 * General Public License as published by the Free Software Foundation. 10 * Version 2 of the license should be included with this distribution in 11 * the file LICENSE, as well as License.html. If the license is not 12 * included with this distribution, you may find a copy at the FSF web 13 * site at 'www.gnu.org' or 'www.fsf.org', or you may write to the 14 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA. 15 * 16 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, 17 * NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR 18 * OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY 19 * CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR 20 * REDISTRIBUTION OF THIS SOFTWARE. 21 */ 22 23 package junit.extensions.conf; 24 25 26 import java.io.*; 27 import java.util.*; 28 import junit.framework.TestCase; 29 import junit.extensions.util.EnhancedProperties; 30 31 32 /*** 33 * Implementation of LoadableConfiguration that uses properties files. 34 * <p> 35 * Note, that class is not thread safe since it is intended to be 36 * immutable after creation. 37 * 38 * @author Siegfried GOESCHL 39 * @author Dima STADNIK 40 */ 41 public class FileConfiguration implements LoadableConfiguration { 42 43 /*** Property key for includes. */ 44 public static final String INCLUDE = ".default"; 45 46 /*** Files search path. */ 47 private String[] path; 48 49 /*** Configuration. */ 50 private EnhancedProperties conf = new EnhancedProperties(); 51 52 /*** Default constructor. */ 53 public FileConfiguration() {} 54 55 /*** 56 * Preloads configuration from file. 57 * 58 * @param fileName Name of properties file. 59 */ 60 public FileConfiguration(String fileName) throws java.io.IOException { 61 load(null, fileName); 62 } 63 64 /*** 65 * Preloads configuration from file. 66 * 67 * @param fileName Name of properties file. 68 * @param path Properties file search path. 69 */ 70 public FileConfiguration(String[] path, String fileName) throws java.io.IOException { 71 load(path, fileName); 72 } 73 74 /*** 75 * Loads configuration from file(s). 76 * 77 * @param fileName Name of properties file. 78 * @param path Properties file search path. 79 */ 80 public void load(String[] path, String fileName) throws IOException { 81 this.path = path; 82 if (fileName == null) { 83 throw new IllegalArgumentException(); 84 } 85 File file = new File(fileName); 86 87 if (!file.exists()) { 88 for (int i = 0; i < path.length; i++) { 89 file = new File(path[i], fileName); 90 if (file.exists()) { 91 break; 92 } 93 } 94 } 95 if (!file.exists()) { 96 throw new FileNotFoundException(fileName); 97 } 98 InputStream in = new FileInputStream(file); 99 100 conf.load(in); 101 System.out.println("Using file " + file.getAbsolutePath()); 102 // Process referenced files 103 String[] refs = conf.getProperties(INCLUDE); 104 105 for (int i = 0; i < refs.length; i++) { 106 FileConfiguration child = new FileConfiguration(path, refs[i]); 107 String key = null; 108 Enumeration keys = child.conf.propertyNames(); 109 110 while (keys.hasMoreElements()) { 111 key = (String) keys.nextElement(); 112 if (conf.getProperty(key) == null) { 113 conf.setProperty(key, child.conf.getProperty(key)); 114 } 115 } 116 } 117 } 118 119 // Configuration implementation /////////////////////////////// 120 121 public boolean getBoolean(Object base, String modifier, String key) { 122 return (Boolean.valueOf(conf.getProperty(base, modifier, key))).booleanValue(); 123 } 124 125 public byte getByte(Object base, String modifier, String key) { 126 return Byte.parseByte(conf.getProperty(base, modifier, key)); 127 } 128 129 public char getChar(Object base, String modifier, String key) { 130 String property = conf.getProperty(base, modifier, key); 131 132 return (property.length() == 0) ? '\0' : property.charAt(0); 133 } 134 135 public double getDouble(Object base, String modifier, String key) { 136 return Double.parseDouble(conf.getProperty(base, modifier, key)); 137 } 138 139 public float getFloat(Object base, String modifier, String key) { 140 return Float.parseFloat(conf.getProperty(base, modifier, key)); 141 } 142 143 public int getInteger(Object base, String modifier, String key) { 144 return Integer.parseInt(conf.getProperty(base, modifier, key)); 145 } 146 147 public long getLong(Object base, String modifier, String key) { 148 return Long.parseLong(conf.getProperty(base, modifier, key)); 149 } 150 151 public short getShort(Object base, String modifier, String key) { 152 return Short.parseShort(conf.getProperty(base, modifier, key)); 153 } 154 155 public String getString(Object base, String modifier, String key) { 156 return conf.getProperty(base, modifier, key); 157 } 158 159 public String[] getStrings(Object base, String modifier, String key) { 160 return conf.getProperties(base, modifier, key); 161 } 162 } 163

This page was automatically generated by Maven