View Javadoc
1 package org.apache.turbine.util.webmacro; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.lang.reflect.Array; 58 59 import java.text.DateFormat; 60 import java.text.NumberFormat; 61 62 import java.util.Date; 63 import java.util.Vector; 64 65 import org.apache.turbine.util.ObjectUtils; 66 67 import org.webmacro.Context; 68 69 /*** 70 * Formatting tool for inserting into the WebMacro WebContext. Can 71 * format dates or lists of objects. 72 * 73 * <p>Here's an example of some uses: 74 * 75 * <code><pre> 76 * $formatter.formatShortDate($object.Date) 77 * $formatter.formatLongDate($db.getRecord(232).getDate()) 78 * $formatter.formatArray($array) 79 * $formatter.limitLen(30, $object.Description) 80 * </pre></code> 81 * 82 * @author <a href="sean@somacity.com">Sean Legassick</a> 83 * @version $Id: WebMacroFormatter.java,v 1.1.1.1 2001/08/16 05:10:05 jvanzyl Exp $ 84 * @deprecated you should use velocity 85 */ 86 public class WebMacroFormatter 87 { 88 Context context = null; 89 NumberFormat nf = NumberFormat.getInstance(); 90 91 /*** 92 * Constructor needs a backpointer to the context. 93 * 94 * @param context A Context. 95 */ 96 public WebMacroFormatter(Context context) 97 { 98 this.context = context; 99 } 100 101 /*** 102 * Formats a date in 'short' style. 103 * 104 * @param date A Date. 105 * @return A String. 106 */ 107 public String formatShortDate(Date date) 108 { 109 return DateFormat 110 .getDateInstance(DateFormat.SHORT).format(date); 111 } 112 113 /*** 114 * Formats a date in 'long' style. 115 * 116 * @param date A Date. 117 * @return A String. 118 */ 119 public String formatLongDate(Date date) 120 { 121 return DateFormat 122 .getDateInstance(DateFormat.LONG).format(date); 123 } 124 125 /*** 126 * Formats a date/time in 'short' style. 127 * 128 * @param date A Date. 129 * @return A String. 130 */ 131 public String formatShortDateTime(Date date) 132 { 133 return DateFormat 134 .getDateTimeInstance(DateFormat.SHORT, 135 DateFormat.SHORT).format(date); 136 } 137 138 /*** 139 * Formats a date/time in 'long' style. 140 * 141 * @param date A Date. 142 * @return A String. 143 */ 144 public String formatLongDateTime(Date date) 145 { 146 return DateFormat.getDateTimeInstance( 147 DateFormat.LONG, DateFormat.LONG).format(date); 148 } 149 150 /*** 151 * Formats an array into the form "A, B and C". 152 * 153 * @param array An Object. 154 * @return A String. 155 */ 156 public String formatArray(Object array) 157 { 158 return formatArray(array, ", ", " and "); 159 } 160 161 /*** 162 * Formats an array into the form 163 * "A<delim>B<delim>C". 164 * 165 * @param array An Object. 166 * @param delim A String. 167 * @return A String. 168 */ 169 public String formatArray(Object array, 170 String delim) 171 { 172 return formatArray(array, delim, delim); 173 } 174 175 /*** 176 * Formats an array into the form 177 * "A<delim>B<finaldelim>C". 178 * 179 * @param array An Object. 180 * @param delim A String. 181 * @param finalDelim A String. 182 * @return A String. 183 */ 184 public String formatArray(Object array, 185 String delim, 186 String finaldelim) 187 { 188 StringBuffer sb = new StringBuffer(); 189 int arrayLen = Array.getLength(array); 190 for (int i = 0; i < arrayLen; i++) 191 { 192 // Use the Array.get method as this will automatically 193 // wrap primitive types in a suitable Object-derived 194 // wrapper if necessary. 195 sb.append(Array.get(array, i).toString()); 196 if (i < arrayLen - 2) 197 { 198 sb.append(delim); 199 } 200 else if (i < arrayLen - 1) 201 { 202 sb.append(finaldelim); 203 } 204 } 205 return sb.toString(); 206 } 207 208 /*** 209 * Formats a vector into the form "A, B and C". 210 * 211 * @param vector A Vector. 212 * @return A String. 213 */ 214 public String formatVector(Vector vector) 215 { 216 return formatVector(vector, ", ", " and "); 217 } 218 219 /*** 220 * Formats a vector into the form "A<delim>B<delim>C". 221 * 222 * @param vector A Vector. 223 * @param delim A String. 224 * @return A String. 225 */ 226 public String formatVector(Vector vector, 227 String delim) 228 { 229 return formatVector(vector, delim, delim); 230 } 231 232 /*** 233 * Formats a vector into the form 234 * "Adelim>B<finaldelim>C". 235 * 236 * @param vector A Vector. 237 * @param delim A String. 238 * @param finalDelim A String. 239 * @return A String. 240 */ 241 public String formatVector(Vector vector, 242 String delim, 243 String finaldelim) 244 { 245 StringBuffer sb = new StringBuffer(); 246 for (int i = 0; i < vector.size(); i++) 247 { 248 sb.append(vector.elementAt(i).toString()); 249 if (i < vector.size() - 2) 250 { 251 sb.append(delim); 252 } 253 else if (i < vector.size() - 1) 254 { 255 sb.append(finaldelim); 256 } 257 } 258 return sb.toString(); 259 } 260 261 /*** 262 * Limits 'string' to 'maxlen' characters. If the string gets 263 * curtailed, "..." is appended to it. 264 * 265 * @param maxlen An int with the maximum length. 266 * @param string A String. 267 * @return A String. 268 */ 269 public String limitLen(int maxlen, 270 String string) 271 { 272 return limitLen(maxlen, string, "..."); 273 } 274 275 /*** 276 * Limits 'string' to 'maxlen' character. If the string gets 277 * curtailed, 'suffix' is appended to it. 278 * 279 * @param maxlen An int with the maximum length. 280 * @param string A String. 281 * @param suffix A String. 282 * @return A String. 283 */ 284 public String limitLen(int maxlen, 285 String string, 286 String suffix) 287 { 288 String ret = string; 289 if (string.length() > maxlen) 290 { 291 ret = string.substring(0, maxlen - suffix.length()) + suffix; 292 } 293 return ret; 294 } 295 296 /*** 297 * Class that returns alternating values in a template. It stores 298 * a list of alternate Strings, whenever alternate() is called it 299 * switches to the next in the list. The current alternate is 300 * retrieved through toString() - i.e. just by referencing the 301 * object in a webmacro template. For an example of usage see the 302 * makeAlternator() method below. 303 */ 304 public class WebMacroAlternator 305 { 306 String[] alternates = null; 307 int current = 0; 308 309 /*** 310 * Constructor takes an array of Strings. 311 * 312 * @param alternates A String[]. 313 */ 314 public WebMacroAlternator(String[] alternates) 315 { 316 this.alternates = alternates; 317 } 318 319 /*** 320 * Alternates to the next in the list. 321 * 322 * @return A String. 323 */ 324 public String alternate() 325 { 326 current++; 327 current %= alternates.length; 328 return ""; 329 } 330 331 /*** 332 * Returns the current alternate. 333 * 334 * @return A String. 335 */ 336 public String toString() 337 { 338 return alternates[current]; 339 } 340 } 341 342 /*** 343 * Makes an alternator object that alternates between two values. 344 * 345 * <p>Example usage in a WebMacro template: 346 * 347 * <code><pre> 348 * <table> 349 * $formatter.makeAlternator(rowcolor, "#c0c0c0", "#e0e0e0") 350 * #foreach $item in $items 351 * #begin 352 * <tr><td bgcolor="$rowcolor">$item.Name</td></tr> 353 * $rowcolor.alternate() 354 * #end 355 * </table> 356 * </pre></code> 357 * 358 * @param name A String. 359 * @param alt1 A String. 360 * @param alt2 A String. 361 * @return A String. 362 */ 363 public String makeAlternator(String name, 364 String alt1, 365 String alt2) 366 { 367 String[] alternates = { alt1, alt2 }; 368 context.put(name, new WebMacroAlternator(alternates)); 369 return ""; 370 } 371 372 /*** 373 * Makes an alternator object that alternates between three 374 * values. 375 * 376 * @param name A String. 377 * @param alt1 A String. 378 * @param alt2 A String. 379 * @param alt3 A String. 380 * @return A String. 381 */ 382 public String makeAlternator(String name, 383 String alt1, 384 String alt2, 385 String alt3) 386 { 387 String[] alternates = { alt1, alt2, alt3 }; 388 context.put(name, new WebMacroAlternator(alternates)); 389 return ""; 390 } 391 392 /*** 393 * Makes an alternator object that alternates between four values. 394 * 395 * @param name A String. 396 * @param alt1 A String. 397 * @param alt2 A String. 398 * @param alt3 A String. 399 * @param alt4 A String. 400 * @return A String. 401 */ 402 String makeAlternator(String name, 403 String alt1, 404 String alt2, 405 String alt3, 406 String alt4) 407 { 408 String[] alternates = { alt1, alt2, alt3, alt4 }; 409 context.put(name, new WebMacroAlternator(alternates)); 410 return ""; 411 } 412 413 /*** 414 * Returns a default value if the object passed is null. 415 */ 416 public Object isNull(Object o, Object dflt) 417 { 418 return ObjectUtils.isNull(o, dflt); 419 } 420 }

This page was automatically generated by Maven