001 package org.shiftone.jrat.util; 002 003 004 import org.shiftone.jrat.util.log.Logger; 005 006 import java.text.DateFormat; 007 import java.text.SimpleDateFormat; 008 import java.util.Date; 009 import java.util.Properties; 010 import java.util.StringTokenizer; 011 012 013 /** 014 * Class StringUtil 015 * 016 * @author jeff@shiftone.org (Jeff Drost) 017 */ 018 public class StringUtil { 019 020 public static final String PROPERTY_DELIMITER = "|"; 021 public static final char DEFAULT_PAD_CHAR = ' '; 022 private static final Logger LOG = Logger.getLogger(StringUtil.class); 023 private static DateFormat dateFormat = new SimpleDateFormat("MMM d, yy h:mm:ss:S aaa"); 024 private static final DurationUnit DU_MILLI_SECONDS = new DurationUnit(1, "ms", "ms"); 025 private static final DurationUnit DU_SECONDS = new DurationUnit(1000, "sec", "sec"); 026 private static final DurationUnit DU_MINUTES = new DurationUnit(DU_SECONDS.ms * 60, "minute", "minutes"); 027 private static final DurationUnit DU_HOURS = new DurationUnit(DU_MINUTES.ms * 60, "hour", "hours"); 028 private static final DurationUnit DU_DAYS = new DurationUnit(DU_HOURS.ms * 24, "day", "days"); 029 private static final DurationUnit DU_YEARS = new DurationUnit(DU_DAYS.ms * 365, "year", "years"); 030 private static final DurationUnit DU_DECADES = new DurationUnit(DU_YEARS.ms * 10, "decade", "decades"); 031 private static final DurationUnit DU_CENTURIES = new DurationUnit(DU_DECADES.ms * 10, "century", 032 "centuries"); 033 private static final DurationUnit[] UNITS = 034 { 035 DU_CENTURIES, DU_DECADES, DU_YEARS, DU_DAYS, DU_HOURS, DU_MINUTES, DU_SECONDS, DU_MILLI_SECONDS 036 }; 037 private static final String[] SPACES = new String[16]; 038 private static final String[] ZEROS = new String[16]; 039 040 static { 041 SPACES[0] = ZEROS[0] = ""; 042 043 for (int i = 1; i < SPACES.length; i++) { 044 SPACES[i] = SPACES[i - 1] + " "; 045 ZEROS[i] = ZEROS[i - 1] + "0"; 046 } 047 } 048 049 public static Properties parsePropertiesString(String propString) { 050 051 Properties properties = new Properties(); 052 053 parsePropertiesString(propString, properties); 054 055 return properties; 056 } 057 058 059 public static boolean isEmpty(String text) { 060 return (text == null) || (text.trim().length() == 0); 061 } 062 063 public static void parsePropertiesString(String propString, Properties properties) { 064 065 StringTokenizer tokenizer = null; 066 String token = null; 067 String key = null; 068 String value = null; 069 int eqIndex; 070 071 Assert.assertNotNull("propString", propString); 072 Assert.assertNotNull("properties", properties); 073 074 tokenizer = new StringTokenizer(propString, PROPERTY_DELIMITER); 075 076 while (tokenizer.hasMoreTokens()) { 077 token = tokenizer.nextToken(); 078 eqIndex = token.indexOf('='); 079 080 if (eqIndex > 0) { 081 key = token.substring(0, eqIndex); 082 value = token.substring(eqIndex + 1); 083 084 properties.put(key, value); 085 } else { 086 LOG.warn("property assignment can not be parsed from '" + token + "'"); 087 } 088 } 089 090 LOG.debug(properties); 091 } 092 093 094 public static String bufferString(int desiredLength, char padChar) { 095 096 StringBuffer sb = new StringBuffer(); 097 098 for (int i = 0; i < desiredLength; i++) { 099 sb.append(padChar); 100 } 101 102 return sb.toString(); 103 } 104 105 106 public static String rightPad(String input, int desiredLength, char padChar) { 107 108 Assert.assertNotNull("input string", input); 109 110 String result = input; 111 int padAmount = desiredLength - input.length(); 112 113 if (padAmount > 0) { 114 result = bufferString(padAmount, padChar) + result; 115 } 116 117 return result; 118 } 119 120 121 public static String leftPad(String input, int desiredLength, char padChar) { 122 123 Assert.assertNotNull("input string", input); 124 125 String result = input; 126 int padAmount = desiredLength - input.length(); 127 128 if (padAmount > 0) { 129 result = result + bufferString(padAmount, padChar); 130 } 131 132 return result; 133 } 134 135 136 public static String rightPad(Object input, int desiredLength) { 137 return rightPad(String.valueOf(input), desiredLength, DEFAULT_PAD_CHAR); 138 } 139 140 141 public static String rightPad(int input, int desiredLength) { 142 return rightPad(String.valueOf(input), desiredLength, DEFAULT_PAD_CHAR); 143 } 144 145 146 public static String rightPad(long input, int desiredLength) { 147 return rightPad(String.valueOf(input), desiredLength, DEFAULT_PAD_CHAR); 148 } 149 150 151 public static String leftPad(Object input, int desiredLength) { 152 return leftPad(String.valueOf(input), desiredLength, DEFAULT_PAD_CHAR); 153 } 154 155 156 public static String leftPad(int input, int desiredLength) { 157 return leftPad(String.valueOf(input), desiredLength, DEFAULT_PAD_CHAR); 158 } 159 160 161 public static String leftPad(long input, int desiredLength) { 162 return leftPad(String.valueOf(input), desiredLength, DEFAULT_PAD_CHAR); 163 } 164 165 166 public static String[] tokenize(String str, String delim, boolean returnDelims) { 167 168 StringTokenizer tokenizer = null; 169 String[] matches = null; 170 171 Assert.assertNotNull("string", str); 172 Assert.assertNotNull("delimiter", delim); 173 174 tokenizer = new StringTokenizer(str, delim, returnDelims); 175 matches = new String[tokenizer.countTokens()]; 176 177 for (int i = 0; i < matches.length; i++) { 178 matches[i] = tokenizer.nextToken(); 179 } 180 181 return matches; 182 } 183 184 185 /** 186 * $Revision: 1.26 $ -> 1.1 187 */ 188 public static String revision(String rev) { 189 190 String ver = "?"; 191 192 if ((rev != null) && (rev.startsWith("$" + "Revision: "))) { 193 ver = rev.substring(11, rev.length() - 2); 194 } 195 196 return ver; 197 } 198 199 200 public static String booleanToString(boolean b) { 201 202 return (b 203 ? "yes" 204 : "no"); 205 } 206 207 208 public static String dateToString(long d) { 209 return dateToString(new Date(d)); 210 } 211 212 213 public static String dateToString(Date date) { 214 215 Assert.assertNotNull("date", date); 216 217 synchronized (dateFormat) { 218 return dateFormat.format(date); 219 } 220 } 221 222 223 public static String hex(long l) { 224 225 String s = Long.toHexString(l); 226 227 return "0x" + ZEROS[16 - s.length()] + s.toUpperCase(); 228 } 229 230 231 public static String hex(int i) { 232 233 String s = Integer.toHexString(i); 234 235 return "0x" + ZEROS[8 - s.length()] + s.toUpperCase(); 236 } 237 238 239 public static byte[] toBytes(String value) { 240 241 byte[] bytes = new byte[value.length()]; 242 243 for (int i = 0; i < bytes.length; i++) { 244 bytes[i] = (byte) value.charAt(i); 245 } 246 247 return bytes; 248 } 249 250 251 public static String removeNonLetterOrDigit(String string) { 252 253 Assert.assertNotNull("string", string); 254 255 StringBuffer sb = new StringBuffer(string.length()); 256 char[] in = string.toCharArray(); 257 258 for (int i = 0; i < in.length; i++) { 259 if (Character.isLetterOrDigit(in[i])) { 260 sb.append(in[i]); 261 } 262 } 263 264 return sb.toString(); 265 } 266 267 268 public static String durationToString(long duration) { 269 270 StringBuffer sb = new StringBuffer(); 271 boolean match = false; 272 273 for (int i = 0; i < UNITS.length; i++) { 274 DurationUnit du = UNITS[i]; 275 long units = duration / du.ms; 276 277 if (units > 0) { 278 duration -= (units * du.ms); 279 280 if (match) { 281 sb.append(", "); 282 } 283 284 sb.append(units); 285 sb.append(" "); 286 287 if (units > 1) { 288 sb.append(du.pName); 289 } else { 290 sb.append(du.sName); 291 } 292 293 match = true; 294 } 295 } 296 297 return sb.toString(); 298 } 299 } 300 301 class DurationUnit { 302 303 long ms; 304 String sName; 305 String pName; 306 307 public DurationUnit(long ms, String sName, String pName) { 308 309 this.ms = ms; 310 this.pName = pName; 311 this.sName = sName; 312 } 313 }