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 javax.faces.convert;
20  
21  import java.text.MessageFormat;
22  import java.util.Locale;
23  
24  import javax.el.ValueExpression;
25  import javax.faces.application.FacesMessage;
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.ValueBinding;
28  
29  /** 
30   * This class encapsulates a FacesMessage to evaluate the label
31   * expression on render response, where f:loadBundle is available
32   * 
33   * @author Leonardo Uribe (latest modification by $Author: struberg $)
34   * @version $Revision: 1188235 $ $Date: 2011-10-24 12:09:33 -0500 (Mon, 24 Oct 2011) $
35   */
36  class _ParametrizableFacesMessage extends FacesMessage
37  {
38      /**
39       * 
40       */
41      private static final long serialVersionUID = 7792947730961657948L;
42  
43      private final Object _args[];
44      private String _evaluatedDetail;
45      private String _evaluatedSummary;
46      private transient Object _evaluatedArgs[];
47      private Locale _locale;
48  
49      public _ParametrizableFacesMessage(
50              String summary, String detail, Object[] args, Locale locale)
51      {
52          super(summary, detail);
53          if(locale == null)
54          {
55              throw new NullPointerException("locale");
56          }
57          _locale = locale;
58          _args = args;
59      }
60  
61      public _ParametrizableFacesMessage(FacesMessage.Severity severity,
62              String summary, String detail, Object[] args, Locale locale)
63      {
64          super(severity, summary, detail);
65          if(locale == null)
66          {
67              throw new NullPointerException("locale");
68          }
69          _locale = locale;
70          _args = args;
71      }
72  
73      @Override
74      public String getDetail()
75      {
76          if (_evaluatedArgs == null && _args != null)
77          {
78              evaluateArgs();
79          }
80          if (_evaluatedDetail == null)
81          {
82              MessageFormat format = new MessageFormat(super.getDetail(), _locale);
83              _evaluatedDetail = format.format(_evaluatedArgs);
84          }
85          return _evaluatedDetail;
86      }
87  
88      @Override
89      public void setDetail(String detail)
90      {
91          super.setDetail(detail);
92          _evaluatedDetail = null;
93      }
94      
95      public String getUnformattedDetail()
96      {
97          return super.getDetail();
98      }
99  
100     @Override
101     public String getSummary()
102     {
103         if (_evaluatedArgs == null && _args != null)
104         {
105             evaluateArgs();
106         }
107         if (_evaluatedSummary == null)
108         {
109             MessageFormat format = new MessageFormat(super.getSummary(), _locale);
110             _evaluatedSummary = format.format(_evaluatedArgs);
111         }
112         return _evaluatedSummary;
113     }
114 
115     @Override
116     public void setSummary(String summary)
117     {
118         super.setSummary(summary);
119         _evaluatedSummary = null;
120     }
121     
122     public String getUnformattedSummary()
123     {
124         return super.getSummary();
125     }
126 
127     private void evaluateArgs()
128     {
129         _evaluatedArgs = new Object[_args.length];
130         FacesContext facesContext = null;
131         for (int i = 0; i < _args.length; i++)
132         {
133             if (_args[i] == null)
134             {
135                 continue;
136             }
137             else if (_args[i] instanceof ValueBinding)
138             {
139                 if (facesContext == null)
140                 {
141                     facesContext = FacesContext.getCurrentInstance();
142                 }
143                 _evaluatedArgs[i] = ((ValueBinding)_args[i]).getValue(facesContext);
144             }
145             else if (_args[i] instanceof ValueExpression)
146             {
147                 if (facesContext == null)
148                 {
149                     facesContext = FacesContext.getCurrentInstance();
150                 }
151                 _evaluatedArgs[i] = ((ValueExpression)_args[i]).getValue(facesContext.getELContext());
152             }
153             else 
154             {
155                 _evaluatedArgs[i] = _args[i];
156             }
157         }
158     }
159 }