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.util; 24 25 26 import java.util.Properties; 27 import java.util.List; 28 import java.util.LinkedList; 29 30 31 /*** 32 * Enhanced properties. Provides flexible scheme of keys naming. 33 * <br>Property name may be defined by base object (it's class name), 34 * some modifier and short key. 35 * <br>Search order for keys resolution: 36 * <ul> 37 * <li>class name with package name + modifier + key 38 * <li>parent class name + modifier + key 39 * <li>parent class name + key 40 * <li>key 41 * </ul> 42 * 43 * @author Siegfried GOESCHL 44 * @author Dima STADNIK 45 */ 46 47 public class EnhancedProperties extends Properties { 48 49 /*** Intermediate character that is used to construct properties keys. */ 50 public static final char SEPARATOR = '.'; 51 52 /*** 53 * Retrieves single property. 54 * @param base Base object. 55 * @param modifier Property key modifier. 56 * @param key Property key. 57 * @return Property value. 58 */ 59 60 public String getProperty(Object base, String modifier, String key) { 61 String propertyKey = getPropertyKey(base, modifier, key); 62 63 if (propertyKey == null) { 64 throw new IllegalArgumentException(key); 65 } 66 return getProperty(propertyKey); 67 } 68 69 /*** 70 * Retrieves property that is represented by array of strings. 71 * @param base Base object. 72 * @param modifier Property key modifier. 73 * @param key Property key. 74 * @return Property value. 75 */ 76 77 public String[] getProperties(Object base, String modifier, String key) { 78 String propertyKey = getPropertiesKey(base, modifier, key); 79 80 if (propertyKey == null) { 81 throw new IllegalArgumentException(key); 82 } 83 return getProperties(propertyKey); 84 } 85 86 /*** 87 * Generates property key. 88 * @param base Base object. 89 * @param modifier Property key modifier. 90 * @param key Property key. 91 * @return Key in underlying properties file or <code>null</code> 92 * if there is no such key. 93 */ 94 95 protected String getPropertyKey(Object base, String modifier, String key) { 96 String propertyKey = null; 97 String className = base.getClass().getName(); 98 99 // check for class name with package name + modifier + key 100 if (modifier != null) { 101 propertyKey = className + SEPARATOR + modifier + SEPARATOR + key; 102 if (containsKey(propertyKey)) { 103 return propertyKey; 104 } 105 } 106 107 String parentName = 108 className.substring(className.lastIndexOf('.') + 1, className.length()); 109 110 // check for parent class name + modifier + key 111 if (modifier != null) { 112 propertyKey = parentName + SEPARATOR + modifier + SEPARATOR + key; 113 if (containsKey(propertyKey)) { 114 return propertyKey; 115 } 116 } 117 118 // check for parent class name + key 119 propertyKey = parentName + SEPARATOR + key; 120 if (containsKey(propertyKey)) { 121 return propertyKey; 122 } 123 124 // check for key 125 if (containsKey(key)) { 126 return key; 127 } 128 129 // property was not found 130 return null; 131 } 132 133 /*** 134 * Generates base of properties key. 135 * 136 * @param base Base object. 137 * @param modifier Property key modifier. 138 * @param key Property key. 139 * @return Base part of key for properties array 140 * in underlying properties file or <code>null</code> if there is no such key. 141 */ 142 143 protected String getPropertiesKey(Object base, String modifier, String key) { 144 String propertyKey = null; 145 String className = base.getClass().getName(); 146 147 // check for class name with package name + modifier + key 148 if (modifier != null) { 149 propertyKey = className + SEPARATOR + modifier + SEPARATOR + key; 150 if (containsKeys(propertyKey)) { 151 return propertyKey; 152 } 153 } 154 // check for parent class name + modifier + key 155 String parentName = className.substring(className.lastIndexOf('.') + 1, className.length()); 156 157 if (modifier != null) { 158 propertyKey = parentName + SEPARATOR + modifier + SEPARATOR + key; 159 if (containsKeys(propertyKey)) { 160 return propertyKey; 161 } 162 } 163 // check for parent class name + key 164 propertyKey = parentName + SEPARATOR + key; 165 if (containsKeys(propertyKey)) { 166 return propertyKey; 167 } 168 // check for key 169 if (containsKeys(key)) { 170 return key; 171 } 172 // property was not found 173 return null; 174 } 175 176 /*** 177 * Checks whether underlying properties contains key of properties array. 178 * @param key Key in underlying properties. 179 * @return <code>true</code> if key exists. 180 */ 181 182 public boolean containsKeys(String key) { 183 return containsKey(key) || containsKey(key + ".0") || containsKey(key + ".1"); 184 } 185 186 /*** 187 * Retrieves properties array. 188 * @param key Base key for properties array. 189 * @return Properties array. 190 */ 191 192 public String[] getProperties(String key) { 193 int i = 1; 194 List lprops = new LinkedList(); 195 String propertyKey = key + ".0"; 196 String propertyValue = getProperty(propertyKey, "").trim(); 197 198 if (propertyValue.length() == 0) { 199 i++; 200 propertyKey = key + ".1"; 201 propertyValue = getProperty(propertyKey, "").trim(); 202 if (propertyValue.length() == 0) { 203 return new String[0]; 204 } 205 } 206 for (; propertyValue.length() != 0; i++) { 207 lprops.add(propertyValue); 208 propertyKey = key + "." + i; 209 propertyValue = getProperty(propertyKey, "").trim(); 210 } 211 // convert array to strings 212 Object[] oprops = lprops.toArray(); 213 int length = oprops.length; 214 String[] props = new String[length]; 215 216 for (i = 0; i < length; i++) { 217 props[i] = (String) oprops[i]; 218 } 219 return props; 220 } 221 } 222

This page was automatically generated by Maven