1 24 25 package com.mckoi.util; 26 27 import java.text.CharacterIterator ; 28 import java.text.ParseException ; 29 import java.math.BigDecimal ; 30 31 38 39 public class GeneralParser { 40 41 45 private static final BigDecimal MILLIS_IN_WEEK = 46 new BigDecimal (7 * 24 * 60 * 60 * 1000); 47 private static final BigDecimal MILLIS_IN_DAY = 48 new BigDecimal (24 * 60 * 60 * 1000); 49 private static final BigDecimal MILLIS_IN_HOUR = 50 new BigDecimal (60 * 60 * 1000); 51 private static final BigDecimal MILLIS_IN_MINUTE = 52 new BigDecimal (60 * 1000); 53 private static final BigDecimal MILLIS_IN_SECOND = 54 new BigDecimal (1000); 55 56 60 public static void parseDigitString(CharacterIterator i, StringBuffer digit_str) { 61 char c = i.current(); 62 while (Character.isDigit(c)) { 63 digit_str.append(c); 64 c = i.next(); 65 } 66 } 67 68 72 public static void parseWordString(CharacterIterator i, 73 StringBuffer word_buffer) { 74 char c = i.current(); 75 while (Character.isLetter(c)) { 76 word_buffer.append(c); 77 c = i.next(); 78 } 79 } 80 81 85 public static void skipWhiteSpace(CharacterIterator i) { 86 char c = i.current(); 87 while (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 88 c = i.next(); 89 } 90 } 91 92 97 public static BigDecimal parseBigDecimal(CharacterIterator i) 98 throws ParseException { 99 boolean done_decimal = false; 100 StringBuffer str_val = new StringBuffer (); 101 102 char c = i.current(); 104 if (c == '-') { 105 str_val.append(c); 106 c = i.next(); 107 } 108 if (c == '.') { 110 done_decimal = true; 111 str_val.append(c); 112 c = i.next(); 113 } 114 if (!Character.isDigit(c)) { 116 throw new ParseException ("Parsing BigDecimal", i.getIndex()); 117 } 118 parseDigitString(i, str_val); 120 c = i.current(); 122 if (!done_decimal && c == '.') { 123 str_val.append(c); 124 c = i.next(); 125 parseDigitString(i, str_val); 126 } 127 128 return new BigDecimal (new String (str_val)); 129 } 130 131 160 public static BigDecimal parseTimeMeasure(CharacterIterator i) 161 throws ParseException { 162 boolean time_measured = false; 163 BigDecimal time_measure = new BigDecimal (0); 164 boolean[] time_parsed = new boolean[6]; 165 StringBuffer word_buffer = new StringBuffer (); 166 BigDecimal num; 167 168 while (true) { 169 skipWhiteSpace(i); 171 try { 172 num = parseBigDecimal(i); 173 } 174 catch (ParseException e) { 175 if (time_measured) { 178 return time_measure; 179 } 180 else { 181 throw new ParseException ("No time value found", i.getIndex()); 182 } 183 } 184 if (num.signum() < 0) { 185 throw new ParseException ("Invalid time value: " + num, i.getIndex()); 186 } 187 188 skipWhiteSpace(i); 189 190 word_buffer.setLength(0); 192 parseWordString(i, word_buffer); 193 194 String str = new String (word_buffer).toLowerCase(); 195 if ((str.startsWith("week") || 196 str.equals("w")) && 197 !time_parsed[0]) { 198 time_measure = time_measure.add(num.multiply(MILLIS_IN_WEEK)); 199 time_parsed[0] = true; 200 } 201 else if ((str.startsWith("day") || 202 str.equals("d")) && 203 !time_parsed[1]) { 204 time_measure = time_measure.add(num.multiply(MILLIS_IN_DAY)); 205 time_parsed[1] = true; 206 } 207 else if ((str.startsWith("hour") || 208 str.startsWith("hr") || 209 str.equals("h")) && 210 !time_parsed[2]) { 211 time_measure = time_measure.add(num.multiply(MILLIS_IN_HOUR)); 212 time_parsed[2] = true; 213 } 214 else if ((str.startsWith("minute") || 215 str.startsWith("min") || 216 str.equals("m")) && 217 !time_parsed[3]) { 218 time_measure = time_measure.add(num.multiply(MILLIS_IN_MINUTE)); 219 time_parsed[3] = true; 220 } 221 else if ((str.startsWith("second") || 222 str.startsWith("sec") || 223 str.equals("s")) && 224 !time_parsed[4]) { 225 time_measure = time_measure.add(num.multiply(MILLIS_IN_SECOND)); 226 time_parsed[4] = true; 227 } 228 else if ((str.startsWith("millisecond") || 229 str.equals("ms")) && 230 !time_parsed[5]) { 231 time_measure = time_measure.add(num); 232 time_parsed[5] = true; 233 } 234 else { 235 throw new ParseException ("Unknown time measure: " + str, i.getIndex()); 236 } 237 time_measured = true; 238 239 } 240 241 } 242 243 } 244 | Popular Tags |