KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > caramel > util > StringUtils


1 /*
2 ** Caramel - Non-GUI Java Addons
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package caramel.util;
24
25 import java.net.*;
26 import java.text.*;
27 import java.util.*;
28 import houston.*;
29
30 public class StringUtils
31 {
32    static Logger T = Logger.getLogger( StringUtils.class );
33    private static SimpleDateFormat _dateTimeFormatter = new SimpleDateFormat( "dd.MM.yyyy HH:mm" );
34
35    private static SimpleDateFormat _timeFormatter = new SimpleDateFormat( "HH:mm" );
36
37    public static String JavaDoc getPrettyPrintedDate( Date date )
38    {
39       Date now = new Date();
40
41       // is it today?
42
if( date.getDate() == now.getDate()
43              && date.getMonth() == now.getMonth()
44              && date.getYear() == now.getYear() )
45       {
46          return "Today " + _timeFormatter.format( date );
47       }
48       else
49       {
50          // it's not today, calculate difference in days
51

52          return _dateTimeFormatter.format( date );
53       }
54    }
55
56    public static String JavaDoc getPrettyPrintedDateDifference( Date date, boolean useDays )
57    {
58       Date now = new Date();
59
60       // is it today?
61
if( date.getDate() == now.getDate()
62              && date.getMonth() == now.getMonth()
63              && date.getYear() == now.getYear() )
64       {
65          /*
66           * return empty string
67           */

68          return "";
69       }
70       else
71       {
72          StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
73
74          long diff = now.getTime() - date.getTime();
75
76          // make sure diffInDays rounds up/down to the next integer (e.g. 0.45 -> 1)
77
if( diff > 0 )
78             diff += ( 1000 * 60 * 60 * 24 ) - 1;
79          // milli*seconds*minutes*hours = 1 day
80
else
81             diff -= ( 1000 * 60 * 60 * 24 ) - 1;
82
83          long diffInDays = diff / ( 1000 * 60 * 60 * 24 );
84
85          if( diffInDays > 0 )
86          {
87             buf.append( "" + diffInDays );
88
89             if( useDays )
90             {
91                if( diffInDays == 1 )
92                   buf.append( " day ago" );
93                else
94                   buf.append( " days ago" );
95             }
96          }
97          else
98          {
99             buf.append( "" + ( -diffInDays ) );
100
101             if( useDays )
102             {
103                if( diffInDays == -1 )
104                   buf.append( " day" );
105                else
106                   buf.append( " days" );
107             }
108          }
109
110          return buf.toString();
111       }
112    }
113
114    public static String JavaDoc fillTemplate(
115          Properties vars, String JavaDoc argStr )
116    {
117       StringBuffer JavaDoc argBuf = new StringBuffer JavaDoc();
118
119       for( int pos = 0; pos < argStr.length(); )
120       {
121          char ch = argStr.charAt( pos );
122
123          switch ( ch )
124          {
125             case '$':
126                StringBuffer JavaDoc nameBuf = new StringBuffer JavaDoc();
127                for( ++pos; pos < argStr.length(); ++pos )
128                {
129                   ch = argStr.charAt( pos );
130                   if( ch == '_'
131                          || ch == '.'
132                          || ch == '-'
133                          || Character.isLetterOrDigit( ch ) )
134                   {
135                      nameBuf.append( ch );
136                   }
137                   else
138                      break;
139                }
140
141                if( nameBuf.length() > 0 )
142                {
143                   String JavaDoc value =
144                         vars.getProperty( nameBuf.toString() );
145
146                   T.debug( nameBuf.toString() + "=" + value );
147
148                   if( value != null )
149                      argBuf.append( value );
150                }
151                break;
152             default:
153                argBuf.append( ch );
154                ++pos;
155                break;
156          }
157       }
158       return argBuf.toString();
159    }
160
161    public static String JavaDoc prettyPrintDouble( double value, int digits )
162    {
163       NumberFormat form = NumberFormat.getNumberInstance( Locale.US );
164
165       form.setMinimumFractionDigits( digits );
166       form.setMaximumFractionDigits( digits );
167
168       return form.format( value );
169    }
170
171    public static String JavaDoc prettyPrintDuration( long duration_in_secs )
172    {
173       long secs = duration_in_secs % 60;
174       long minutes = duration_in_secs / 60;
175
176       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
177
178       if( minutes < 10 )
179          buf.append( "0" + minutes + ":" );
180       else
181          buf.append( "" + minutes + ":" );
182
183       if( secs < 10 )
184          buf.append( "0" + secs );
185       else
186          buf.append( "" + secs );
187
188       return buf.toString();
189    }
190
191    public static String JavaDoc prettyPrintFileSize( long size )
192    {
193       if( size == -1 )
194          return "n/a";
195
196       if( size == 0 )
197          return "0 k";
198
199       // use kilobytes only (1028 bytes) for now
200
if( size < 1028 )
201       {
202          return "1 k";
203          // return form.format( size ) + " bytes";
204
}
205
206       NumberFormat form = NumberFormat.getNumberInstance( Locale.US );
207
208       size = size / 1028;
209
210       return form.format( size ) + " k";
211    }
212
213    public static String JavaDoc removeTrailingNewlines( String JavaDoc line )
214    {
215       // remove trailing newlines
216
int cutoffPoint = line.length();
217       while( cutoffPoint > 0
218              && ( line.charAt( cutoffPoint - 1 ) == 10
219              || line.charAt( cutoffPoint - 1 ) == 13 ) )
220       {
221          cutoffPoint--;
222       }
223
224       return line.substring( 0, cutoffPoint );
225    }
226
227    // convienience method
228
public static String JavaDoc[] split( String JavaDoc line )
229    {
230       return split( line, ",;:|" );
231    }
232
233    public static String JavaDoc[] split( String JavaDoc line, String JavaDoc delim )
234    {
235       ArrayList l = new ArrayList();
236       StringTokenizer t = new StringTokenizer( line, delim );
237       while( t.hasMoreTokens() )
238          l.add( t.nextToken() );
239
240       return ( String JavaDoc[] ) l.toArray( new String JavaDoc[0] );
241    }
242
243    /*
244     * Perform a series of substitutions. The substitions
245     * are performed by replacing $variable in the target
246     * string with the value of provided by the key "variable"
247     * in the provided hashtable.
248     *
249     * @param String target string
250     * @param Hashtable name/value pairs used for substitution
251     * @return String target string with replacements.
252     */

253    public static String JavaDoc stringSubstitution(
254          Properties vars, String JavaDoc argStr )
255    {
256       return fillTemplate( vars, argStr );
257    }
258
259 }
260
Popular Tags