1 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 getPrettyPrintedDate( Date date ) 38 { 39 Date now = new Date(); 40 41 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 52 return _dateTimeFormatter.format( date ); 53 } 54 } 55 56 public static String getPrettyPrintedDateDifference( Date date, boolean useDays ) 57 { 58 Date now = new Date(); 59 60 if( date.getDate() == now.getDate() 62 && date.getMonth() == now.getMonth() 63 && date.getYear() == now.getYear() ) 64 { 65 68 return ""; 69 } 70 else 71 { 72 StringBuffer buf = new StringBuffer (); 73 74 long diff = now.getTime() - date.getTime(); 75 76 if( diff > 0 ) 78 diff += ( 1000 * 60 * 60 * 24 ) - 1; 79 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 fillTemplate( 115 Properties vars, String argStr ) 116 { 117 StringBuffer argBuf = new StringBuffer (); 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 nameBuf = new StringBuffer (); 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 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 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 prettyPrintDuration( long duration_in_secs ) 172 { 173 long secs = duration_in_secs % 60; 174 long minutes = duration_in_secs / 60; 175 176 StringBuffer buf = new StringBuffer (); 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 prettyPrintFileSize( long size ) 192 { 193 if( size == -1 ) 194 return "n/a"; 195 196 if( size == 0 ) 197 return "0 k"; 198 199 if( size < 1028 ) 201 { 202 return "1 k"; 203 } 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 removeTrailingNewlines( String line ) 214 { 215 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 public static String [] split( String line ) 229 { 230 return split( line, ",;:|" ); 231 } 232 233 public static String [] split( String line, String 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 [] ) l.toArray( new String [0] ); 241 } 242 243 253 public static String stringSubstitution( 254 Properties vars, String argStr ) 255 { 256 return fillTemplate( vars, argStr ); 257 } 258 259 } 260 | Popular Tags |