1 22 23 package org.cofax; 24 25 import java.util.*; 26 import java.text.*; 27 28 37 public final class CofaxUtil { 38 39 42 public final static int MILLISECONDS_PER_MINUTE = 60 * 1000; 43 44 47 public final static int MILLISECONDS_PER_SECOND = 1000; 48 49 52 public final static String NEW_LINE = "\r\n"; 53 54 57 public final static String XHTML_NEW_LINE = "<br />\r\n"; 58 59 64 public final static int COMMON_DATA_SIZE = 16384; 65 66 74 public static boolean isDigits(String stringToParse) { 75 76 char[] charsToTest = stringToParse.toCharArray(); 77 boolean isDigits = true; 78 for (int x = 0; x < charsToTest.length; x++) { 79 80 if (!Character.isDigit(charsToTest[x])) { 81 isDigits = false; 82 break; 83 } 84 } 85 86 return isDigits; 87 } 88 89 98 public static List split(String separator, String str) { 99 100 ArrayList list = new ArrayList(); 101 String substr; 102 103 int cursor = 0; 104 int sepratorPosition; 105 106 if (!str.substring(str.length() - separator.length()).equals(separator)) { 107 str = str + separator; 108 } 109 110 while ((sepratorPosition = str.indexOf(separator, cursor)) > -1) { 111 substr = str.substring(cursor, sepratorPosition); 112 cursor = sepratorPosition + 1; 113 list.add(substr); 114 } 115 return list; 116 } 117 118 125 public final static String encodeUrl(String url) { 126 127 return replace(url, "?", "%3F"); 128 } 129 130 143 public static String replace(String originalText, char charToFind, String subStringToReplaceWith) { 144 145 char[] originalTextAsArray = originalText.toCharArray(); 146 StringBuffer newText = new StringBuffer (originalTextAsArray.length); 147 148 for (int i = 0; i < originalTextAsArray.length; i++) { 149 if (originalTextAsArray[i] == charToFind) { 150 newText.append(subStringToReplaceWith); 151 } else { 152 newText.append(originalTextAsArray[i]); 153 } 154 } 155 156 return newText.toString(); 157 } 158 159 161 173 public static String replace(String originalText, String subStringToFind, String subStringToReplaceWith) { 174 int s = 0; 175 int e = 0; 176 177 StringBuffer newText = new StringBuffer (); 178 179 while ((e = originalText.indexOf(subStringToFind, s)) >= 0) { 180 181 newText.append(originalText.substring(s, e)); 182 newText.append(subStringToReplaceWith); 183 s = e + subStringToFind.length(); 184 185 } 186 187 newText.append(originalText.substring(s)); 188 return newText.toString(); 189 } 190 191 193 204 public static String quotemetaLight(String s) { 205 206 s = replace(s, '\\', "\\\\"); 207 s = replace(s, '/', "\\/"); 208 s = replace(s, '$', "$"); 209 210 return s; 211 } 212 213 215 225 public static String getDateFormat(String style, String pubDate) { 226 227 int year = 0; 228 int month = 0; 229 int day = 0; 230 231 Calendar cal; 232 233 236 if (!pubDate.equals("")) { 237 238 try { 239 Iterator parts = split("/", pubDate).iterator(); 240 year = Integer.parseInt((String ) parts.next()); 241 month = Integer.parseInt((String ) parts.next()); 242 day = Integer.parseInt((String ) parts.next()); 243 244 month--; 245 246 cal = new GregorianCalendar(year, month, day); 247 248 } catch (Exception e) { 249 cal = new GregorianCalendar(); 250 } 251 252 } else { 253 cal = new GregorianCalendar(); 254 } 255 256 Locale currentLocale = new Locale("en", "US"); 257 258 SimpleDateFormat formatter = new SimpleDateFormat(style); 259 return formatter.format(cal.getTime()); 260 } 261 262 271 public static String getDateFormat(String style, Date dateToFormat) { 272 273 Locale currentLocale = new Locale("en", "US"); 274 SimpleDateFormat formatter = new SimpleDateFormat(style); 275 return formatter.format(dateToFormat); 276 } 277 278 289 public static String getString(HashMap fields, String fieldName) { 290 291 String value = null; 292 293 try { 294 value = fields.get(fieldName).toString(); 295 } catch (Exception e) { 296 value = (String ) fields.get(fieldName); 297 } 298 299 if (value == null) { 300 return ""; 301 } else { 302 return value; 303 } 304 305 } 306 307 317 public static String getIntegerAsString(HashMap fields, String fieldName) { 318 319 String value = "0"; 320 321 if (fields.containsKey(fieldName)) { 322 try { 323 value = fields.get(fieldName).toString(); 324 } catch (Exception e) { 325 value = (String ) fields.get(fieldName); 326 } 327 } 328 329 return value; 330 331 } 332 333 343 public static String dateToMySql(String pstrValeur) throws ParseException { 344 345 Date date1 = null; 346 347 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.FRENCH); 348 String strDateToNum = pstrValeur; 349 350 try { 351 352 date1 = sdf.parse(strDateToNum); 354 sdf.applyPattern("yyyy-MM-dd"); 356 strDateToNum = sdf.format(date1); 357 358 } catch (Exception e) { 359 } finally { 361 364 } 365 return strDateToNum; 366 367 } 368 369 } 370 | Popular Tags |