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.commons.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      @Override
67      public String getDetail()
68      {
69          if (_evaluatedArgs == null && _args != null)
70          {
71              evaluateArgs();
72          }
73          if (_evaluatedDetail == null)
74          {
75              MessageFormat format = new MessageFormat(super.getDetail(), _locale);
76              _evaluatedDetail = format.format(_evaluatedArgs);
77          }
78          return _evaluatedDetail;
79      }
80  
81      @Override
82      public void setDetail(String detail)
83      {
84          super.setDetail(detail);
85          _evaluatedDetail = null;
86      }
87      
88      public String getUnformattedDetail()
89      {
90          return super.getDetail();
91      }
92  
93      @Override
94      public String getSummary()
95      {
96          if (_evaluatedArgs == null && _args != null)
97          {
98              evaluateArgs();
99          }
100         if (_evaluatedSummary == null)
101         {
102             MessageFormat format = new MessageFormat(super.getSummary(), _locale);
103             _evaluatedSummary = format.format(_evaluatedArgs);
104         }
105         return _evaluatedSummary;
106     }
107 
108     @Override
109     public void setSummary(String summary)
110     {
111         super.setSummary(summary);
112         _evaluatedSummary = null;
113     }
114     
115     public String getUnformattedSummary()
116     {
117         return super.getSummary();
118     }
119 
120     private void evaluateArgs()
121     {
122         _evaluatedArgs = new Object[_args.length];
123         FacesContext facesContext = null;
124         for (int i = 0; i < _args.length; i++)
125         {
126             if (_args[i] == null)
127             {
128                 continue;
129             }
130             else if (_args[i] instanceof ValueBinding)
131             {
132                 if (facesContext == null)
133                 {
134                     facesContext = FacesContext.getCurrentInstance();
135                 }
136                 _evaluatedArgs[i] = ((ValueBinding)_args[i]).getValue(facesContext);
137             }
138             else 
139             {
140                 _evaluatedArgs[i] = _args[i];
141             }
142         }
143     }
144 }