Save This Page
Home » displaytag-1.1.1-src » org » displaytag » decorator » [javadoc | source]
    1   /**
    2    * Licensed under the Artistic License; you may not use this file
    3    * except in compliance with the License.
    4    * You may obtain a copy of the License at
    5    *
    6    *      http://displaytag.sourceforge.net/license.html
    7    *
    8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
    9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   11    */
   12   package org.displaytag.decorator;
   13   
   14   import java.text.MessageFormat;
   15   import java.util.Locale;
   16   
   17   import javax.servlet.jsp.PageContext;
   18   
   19   import org.apache.commons.logging.Log;
   20   import org.apache.commons.logging.LogFactory;
   21   import org.displaytag.Messages;
   22   import org.displaytag.properties.MediaTypeEnum;
   23   
   24   
   25   /**
   26    * A decorator that simply formats input Objects using a <code>java.text.messageFormat</code>. By design, this
   27    * implementations handle MessageFormat errors by returning the unformatted value and logging the exception.
   28    * @author Fabrizio Giustina
   29    * @version $Id$
   30    */
   31   public class MessageFormatColumnDecorator implements DisplaytagColumnDecorator
   32   {
   33   
   34       /**
   35        * Logger.
   36        */
   37       private static Log log = LogFactory.getLog(MessageFormatColumnDecorator.class);
   38   
   39       /**
   40        * Pre-compiled messageFormat.
   41        */
   42       private MessageFormat format;
   43   
   44       /**
   45        * Instantiates a new MessageFormatColumnDecorator with a given pattern and locale.
   46        * @param pattern see <code>java.text.messageFormat</code>
   47        * @param locale current locale
   48        * @see java.text.messageFormat
   49        */
   50       public MessageFormatColumnDecorator(String pattern, Locale locale)
   51       {
   52           this.format = new MessageFormat(pattern, locale);
   53       }
   54   
   55       /**
   56        * @see org.displaytag.decorator.DisplaytagColumnDecorator#decorate(Object, PageContext, MediaTypeEnum)
   57        */
   58       public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media)
   59       {
   60           try
   61           {
   62               return this.format.format(new Object[]{columnValue});
   63           }
   64           catch (IllegalArgumentException e)
   65           {
   66               log.error(Messages.getString("MessageFormatColumnDecorator.invalidArgument", new Object[]{ //$NON-NLS-1$
   67                   this.format.toPattern(), columnValue != null ? columnValue.getClass().getName() : "null"})); //$NON-NLS-1$
   68   
   69               return columnValue;
   70           }
   71       }
   72   }

Save This Page
Home » displaytag-1.1.1-src » org » displaytag » decorator » [javadoc | source]