1 package org.apache.velocity.app.tools; 2 3 58 59 import java.util.*; 61 import java.text.*; 62 import java.lang.reflect.Array ; 63 64 import org.apache.velocity.context.*; 66 67 84 public class VelocityFormatter 85 { 86 Context context = null; 87 NumberFormat nf = NumberFormat.getInstance(); 88 89 94 public VelocityFormatter(Context context) 95 { 96 this.context = context; 97 } 98 99 105 public String formatShortDate(Date date) 106 { 107 return DateFormat.getDateInstance(DateFormat.SHORT).format(date); 108 } 109 110 116 public String formatLongDate(Date date) 117 { 118 return DateFormat.getDateInstance(DateFormat.LONG).format(date); 119 } 120 121 127 public String formatShortDateTime(Date date) 128 { 129 return DateFormat 130 .getDateTimeInstance(DateFormat.SHORT, 131 DateFormat.SHORT).format(date); 132 } 133 134 140 public String formatLongDateTime(Date date) 141 { 142 return DateFormat.getDateTimeInstance( 143 DateFormat.LONG, DateFormat.LONG).format(date); 144 } 145 146 152 public String formatArray(Object array) 153 { 154 return formatArray(array, ", ", " and "); 155 } 156 157 165 public String formatArray(Object array, 166 String delim) 167 { 168 return formatArray(array, delim, delim); 169 } 170 171 180 public String formatArray(Object array, 181 String delim, 182 String finaldelim) 183 { 184 StringBuffer sb = new StringBuffer (); 185 int arrayLen = Array.getLength(array); 186 for (int i = 0; i < arrayLen; i++) 187 { 188 sb.append(Array.get(array, i).toString()); 192 if (i < arrayLen - 2) 193 { 194 sb.append(delim); 195 } 196 else if (i < arrayLen - 1) 197 { 198 sb.append(finaldelim); 199 } 200 } 201 return sb.toString(); 202 } 203 204 210 public String formatVector(List list) 211 { 212 return formatVector(list, ", ", " and "); 213 } 214 215 222 public String formatVector(List list, 223 String delim) 224 { 225 return formatVector(list, delim, delim); 226 } 227 228 237 public String formatVector(List list, 238 String delim, 239 String finaldelim) 240 { 241 StringBuffer sb = new StringBuffer (); 242 int size = list.size(); 243 for (int i = 0; i < size; i++) 244 { 245 sb.append(list.get(i)); 246 if (i < size - 2) 247 { 248 sb.append(delim); 249 } 250 else if (i < size - 1) 251 { 252 sb.append(finaldelim); 253 } 254 } 255 return sb.toString(); 256 } 257 258 266 public String limitLen(int maxlen, 267 String string) 268 { 269 return limitLen(maxlen, string, "..."); 270 } 271 272 281 public String limitLen(int maxlen, 282 String string, 283 String suffix) 284 { 285 String ret = string; 286 if (string.length() > maxlen) 287 { 288 ret = string.substring(0, maxlen - suffix.length()) + suffix; 289 } 290 return ret; 291 } 292 293 301 public class VelocityAlternator 302 { 303 protected String [] alternates = null; 304 protected int current = 0; 305 306 311 public VelocityAlternator(String [] alternates) 312 { 313 this.alternates = alternates; 314 } 315 316 321 public String alternate() 322 { 323 current++; 324 current %= alternates.length; 325 return ""; 326 } 327 328 333 public String toString() 334 { 335 return alternates[current]; 336 } 337 } 338 339 343 public class VelocityAutoAlternator extends VelocityAlternator 344 { 345 350 public VelocityAutoAlternator(String [] alternates) 351 { 352 super(alternates); 353 } 354 355 362 public final String toString() 363 { 364 String s = alternates[current]; 365 alternate(); 366 return s; 367 } 368 } 369 370 391 public String makeAlternator(String name, 392 String alt1, 393 String alt2) 394 { 395 String [] alternates = { alt1, alt2 }; 396 context.put(name, new VelocityAlternator(alternates)); 397 return ""; 398 } 399 400 406 public String makeAlternator(String name, 407 String alt1, 408 String alt2, 409 String alt3) 410 { 411 String [] alternates = { alt1, alt2, alt3 }; 412 context.put(name, new VelocityAlternator(alternates)); 413 return ""; 414 } 415 416 421 public String makeAlternator(String name, String alt1, String alt2, 422 String alt3, String alt4) 423 { 424 String [] alternates = { alt1, alt2, alt3, alt4 }; 425 context.put(name, new VelocityAlternator(alternates)); 426 return ""; 427 } 428 429 435 public String makeAutoAlternator(String name, String alt1, String alt2) 436 { 437 String [] alternates = { alt1, alt2 }; 438 context.put(name, new VelocityAutoAlternator(alternates)); 439 return ""; 440 } 441 442 445 public Object isNull(Object o, Object dflt) 446 { 447 if ( o == null ) 448 { 449 return dflt; 450 } 451 else 452 { 453 return o; 454 } 455 } 456 } 457 | Popular Tags |