KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > Formater


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.utils;
6
7 import java.util.* ;
8 import java.text.MessageFormat JavaDoc ;
9 import java.text.DateFormat JavaDoc ;
10 import org.apache.commons.lang.StringUtils;
11
12 public class Formater {
13   static private Formater defaultFormater_ = new Formater() ;
14   DateFormat JavaDoc dateFormater_ ;
15
16   public Formater() {
17     dateFormater_ =
18       DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
19   }
20
21   public String JavaDoc format(String JavaDoc s) {
22     if(StringUtils.isEmpty(s))return "" ;
23     return s ;
24   }
25
26   public String JavaDoc format(String JavaDoc s , String JavaDoc defaultValue) {
27     if(StringUtils.isEmpty(s)) return defaultValue ;
28     return s ;
29   }
30   
31   public String JavaDoc format(String JavaDoc s, Object JavaDoc[] params) {
32     return MessageFormat.format(s,params) ;
33   }
34
35   public String JavaDoc head(String JavaDoc 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 JavaDoc head(String JavaDoc 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 JavaDoc format(Date d) {
58     if(d == null) return "N/A" ;
59     return dateFormater_.format(d) ;
60   }
61
62   final public String JavaDoc format(Integer JavaDoc 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 JavaDoc format(Object JavaDoc 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