1 5 package org.exoplatform.commons.utils; 6 7 import java.util.* ; 8 import java.text.MessageFormat ; 9 import java.text.DateFormat ; 10 import org.apache.commons.lang.StringUtils; 11 12 public class Formater { 13 static private Formater defaultFormater_ = new Formater() ; 14 DateFormat dateFormater_ ; 15 16 public Formater() { 17 dateFormater_ = 18 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); 19 } 20 21 public String format(String s) { 22 if(StringUtils.isEmpty(s))return "" ; 23 return s ; 24 } 25 26 public String format(String s , String defaultValue) { 27 if(StringUtils.isEmpty(s)) return defaultValue ; 28 return s ; 29 } 30 31 public String format(String s, Object [] params) { 32 return MessageFormat.format(s,params) ; 33 } 34 35 public String head(String s) { 36 if(StringUtils.isEmpty(s))return "" ; 37 if (s.length() < 100) return s ; 38 int index = s.indexOf(' ', 50) ; 39 if (index > 0) { 40 s = s.substring(0, index); 41 s = s + "..." ; 42 } 43 return s ; 44 } 45 46 final public String head(String s, int length) { 47 if(StringUtils.isEmpty(s))return "" ; 48 if (s.length() < length) return s ; 49 int index = s.indexOf(' ', length) ; 50 if (index > 0) { 51 s = s.substring(0, index); 52 s = s + "..." ; 53 } 54 return s ; 55 } 56 57 final public String format(Date d) { 58 if(d == null) return "N/A" ; 59 return dateFormater_.format(d) ; 60 } 61 62 final public String format(Integer number) { 63 if(number == null) return "" ; 64 return number.toString() ; 65 } 66 67 static public Formater getDefaultFormater() { 68 return defaultFormater_ ; 69 } 70 71 static public Formater getFormater(Locale local) { 72 return defaultFormater_ ; 73 } 74 75 final public String format(Object obj) { 76 if(obj == null) return "" ; 77 if (obj instanceof Date) { 78 return dateFormater_.format((Date) obj) ; 79 } 80 return obj.toString() ; 81 } 82 } | Popular Tags |