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.component.html;
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  class _ParametrizableFacesMessage extends FacesMessage
34  {
35      /**
36       * 
37       */
38      private static final long serialVersionUID = 7792947730961657948L;
39  
40      private final Object _args[];
41      private String _evaluatedDetail;
42      private String _evaluatedSummary;
43      private transient Object _evaluatedArgs[];
44      private Locale _locale;
45  
46      public _ParametrizableFacesMessage(
47              String summary, String detail, Object[] args, Locale locale)
48      {
49          super(summary, detail);
50          if(locale == null)
51          {
52              throw new NullPointerException("locale");
53          }
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)
63          {
64              throw new NullPointerException("locale");
65          }
66          _locale = locale;
67          _args = args;
68      }
69  
70      @Override
71      public String getDetail()
72      {
73          if (_evaluatedArgs == null && _args != null)
74          {
75              evaluateArgs();
76          }
77          if (_evaluatedDetail == null)
78          {
79              MessageFormat format = new MessageFormat(super.getDetail(), _locale);
80              _evaluatedDetail = format.format(_evaluatedArgs);
81          }
82          return _evaluatedDetail;
83      }
84  
85      @Override
86      public void setDetail(String detail)
87      {
88          super.setDetail(detail);
89          _evaluatedDetail = null;
90      }
91      
92      public String getUnformattedDetail()
93      {
94          return super.getDetail();
95      }
96  
97      @Override
98      public String getSummary()
99      {
100         if (_evaluatedArgs == null && _args != null)
101         {
102             evaluateArgs();
103         }
104         if (_evaluatedSummary == null)
105         {
106             MessageFormat format = new MessageFormat(super.getSummary(), _locale);
107             _evaluatedSummary = format.format(_evaluatedArgs);
108         }
109         return _evaluatedSummary;
110     }
111 
112     @Override
113     public void setSummary(String summary)
114     {
115         super.setSummary(summary);
116         _evaluatedSummary = null;
117     }
118     
119     public String getUnformattedSummary()
120     {
121         return super.getSummary();
122     }
123 
124     private void evaluateArgs()
125     {
126         _evaluatedArgs = new Object[_args.length];
127         FacesContext facesContext = null;
128         for (int i = 0; i < _args.length; i++)
129         {
130             if (_args[i] == null)
131             {
132                 continue;
133             }
134             else if (_args[i] instanceof ValueBinding)
135             {
136                 if (facesContext == null)
137                 {
138                     facesContext = FacesContext.getCurrentInstance();
139                 }
140                 _evaluatedArgs[i] = ((ValueBinding)_args[i]).getValue(facesContext);
141             }
142             else if (_args[i] instanceof ValueExpression)
143             {
144                 if (facesContext == null)
145                 {
146                     facesContext = FacesContext.getCurrentInstance();
147                 }
148                 _evaluatedArgs[i] = ((ValueExpression)_args[i]).getValue(facesContext.getELContext());
149             }
150             else 
151             {
152                 _evaluatedArgs[i] = _args[i];
153             }
154         }
155     }
156 }