View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.shared_orchestra.util;
    20
    21  import java.text.MessageFormat;
    22  import java.util.Locale;
    23
    24  import javax.faces.application.FacesMessage;
    25  import javax.faces.context.FacesContext;
    26  import javax.faces.el.ValueBinding;
    27
    28  /**
    29   * This class encapsulates a FacesMessage to evaluate the label
    30   * expression on render response, where f:loadBundle is available
    31   *
    32   * @author Leonardo Uribe (latest modification by $Author: skitching $)
    33   * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Dom, 13 Jul 2008) $
    34   */
    35  public class ParametrizableFacesMessage extends FacesMessage
    36  {
    37      /**
    38       *
    39       */
    40      private static final long serialVersionUID = 7792947730961657948L;
    41
    42      private final Object _args[];
    43      private String _evaluatedDetail;
    44      private String _evaluatedSummary;
    45      private transient Object _evaluatedArgs[];
    46      private Locale _locale;
    47
    48      public ParametrizableFacesMessage(
    49              String summary, String detail, Object[] args, Locale locale)
    50      {
    51          super(summary, detail);
    52          if(locale == null) throw new NullPointerException("locale");
    53          _locale = locale;
    54          _args = args;
    55      }
    56
    57      public ParametrizableFacesMessage(FacesMessage.Severity severity,
    58              String summary, String detail, Object[] args, Locale locale)
    59      {
    60          super(severity, summary, detail);
    61          if(locale == null) throw new NullPointerException("locale");
    62          _locale = locale;
    63          _args = args;
    64      }
    65
    66      public String getDetail()
    67      {
    68          if (_evaluatedArgs == null && _args != null)
    69          {
    70              evaluateArgs();
    71          }
    72          if (_evaluatedDetail == null)
    73          {
    74              MessageFormat format = new MessageFormat(super.getDetail(), _locale);
    75              _evaluatedDetail = format.format(_evaluatedArgs);
    76          }
    77          return _evaluatedDetail;
    78      }
    79
    80      public void setDetail(String detail)
    81      {
    82          super.setDetail(detail);
    83          _evaluatedDetail = null;
    84      }
    85
    86      public String getUnformattedDetail()
    87      {
    88          return super.getDetail();
    89      }
    90
    91      public String getSummary()
    92      {
    93          if (_evaluatedArgs == null && _args != null)
    94          {
    95              evaluateArgs();
    96          }
    97          if (_evaluatedSummary == null)
    98          {
    99              MessageFormat format = new MessageFormat(super.getSummary(), _locale);
    100             _evaluatedSummary = format.format(_evaluatedArgs);
    101         }
    102         return _evaluatedSummary;
    103     }
    104
    105     public void setSummary(String summary)
    106     {
    107         super.setSummary(summary);
    108         _evaluatedSummary = null;
    109     }
    110
    111     public String getUnformattedSummary()
    112     {
    113         return super.getSummary();
    114     }
    115
    116     private void evaluateArgs()
    117     {
    118         _evaluatedArgs = new Object[_args.length];
    119         FacesContext facesContext = null;
    120         for (int i = 0; i < _args.length; i++)
    121         {
    122             if (_args[i] == null)
    123             {
    124                 continue;
    125             }
    126             else if (_args[i] instanceof ValueBinding)
    127             {
    128                 if (facesContext == null)
    129                 {
    130                     facesContext = FacesContext.getCurrentInstance();
    131                 }
    132                 _evaluatedArgs[i] = ((ValueBinding)_args[i]).getValue(facesContext);
    133             }
    134             else
    135             {
    136                 _evaluatedArgs[i] = _args[i];
    137             }
    138         }
    139     }
    140 }