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.validator;
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: bommel $)
34   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 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) throw new NullPointerException("locale");
54          _locale = locale;
55          _args = args;
56      }
57  
58      public _ParametrizableFacesMessage(FacesMessage.Severity severity,
59              String summary, String detail, Object[] args, Locale locale)
60      {
61          super(severity, summary, detail);
62          if(locale == null) throw new NullPointerException("locale");
63          _locale = locale;
64          _args = args;
65      }
66  
67      @Override
68      public String getDetail()
69      {
70          if (_evaluatedArgs == null && _args != null)
71          {
72              evaluateArgs();
73          }
74          if (_evaluatedDetail == null)
75          {
76              MessageFormat format = new MessageFormat(super.getDetail(), _locale);
77              _evaluatedDetail = format.format(_evaluatedArgs);
78          }
79          return _evaluatedDetail;
80      }
81  
82      @Override
83      public void setDetail(String detail)
84      {
85          super.setDetail(detail);
86          _evaluatedDetail = null;
87      }
88      
89      public String getUnformattedDetail()
90      {
91          return super.getDetail();
92      }
93  
94      @Override
95      public String getSummary()
96      {
97          if (_evaluatedArgs == null && _args != null)
98          {
99              evaluateArgs();
100         }
101         if (_evaluatedSummary == null)
102         {
103             MessageFormat format = new MessageFormat(super.getSummary(), _locale);
104             _evaluatedSummary = format.format(_evaluatedArgs);
105         }
106         return _evaluatedSummary;
107     }
108 
109     @Override
110     public void setSummary(String summary)
111     {
112         super.setSummary(summary);
113         _evaluatedSummary = null;
114     }
115     
116     public String getUnformattedSummary()
117     {
118         return super.getSummary();
119     }
120 
121     private void evaluateArgs()
122     {
123         _evaluatedArgs = new Object[_args.length];
124         FacesContext facesContext = null;
125         for (int i = 0; i < _args.length; i++)
126         {
127             if (_args[i] == null)
128             {
129                 continue;
130             }
131             else if (_args[i] instanceof ValueBinding)
132             {
133                 if (facesContext == null)
134                 {
135                     facesContext = FacesContext.getCurrentInstance();
136                 }
137                 _evaluatedArgs[i] = ((ValueBinding)_args[i]).getValue(facesContext);
138             }
139             else if (_args[i] instanceof ValueExpression)
140             {
141                 if (facesContext == null)
142                 {
143                     facesContext = FacesContext.getCurrentInstance();
144                 }
145                 _evaluatedArgs[i] = ((ValueExpression)_args[i]).getValue(facesContext.getELContext());
146             }
147             else 
148             {
149                 _evaluatedArgs[i] = _args[i];
150             }
151         }
152     }
153 }