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.trinidad.util;
20  
21  import javax.el.ValueExpression;
22  
23  import java.io.IOException;
24  
25  import javax.faces.application.FacesMessage;
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.ValueBinding;
28  
29  /**
30   * Extension to FacesMessage which keeps track of the label on the component
31   * that generated the message.  This implementation supports a label that is either
32   * a valueBinding or a String.  If a valueBinding is provided, then the ability to
33   * serialize this FacesMessage may be suspect.  We should not change this for historical
34   * reasons.
35   * <p/>
36   * If serialization is a must, and the ValueBindings need to be serialized BEFORE
37   * they are exchanged, then a version of the LabeledFacesMessage must be obtained
38   * using the {@link #getLabeledFacesMessageWithLabelString} method.
39   * 
40   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/util/LabeledFacesMessage.java#0 $) $Date: 10-nov-2005.19:08:38 $
41   */
42  public class LabeledFacesMessage extends FacesMessage
43  {
44    public LabeledFacesMessage()
45    {
46    }
47  
48    /**
49     * Creates a LabeledFacesMessage without a pre-set label.
50     * @param severity the severity of the message
51     * @param summary the message summary
52     * @param detail the message detail
53     */
54    public LabeledFacesMessage(
55      FacesMessage.Severity severity,
56      String summary,
57      String detail)
58    {
59      super(severity, summary, detail);
60    }
61  
62  
63    /**
64     * Creates a LabeledFacesMessage with a label.
65     * @param severity the severity of the message
66     * @param summary the message summary
67     * @param detail the message detail
68     * @param label the message label - either a String or a ValueBinding
69     */
70    public LabeledFacesMessage(
71      FacesMessage.Severity severity,
72      String summary,
73      String detail,
74      Object label)
75    {
76      super(severity, summary, detail);
77      _label = label;
78    }
79  
80    /**
81     * Returns the label, which can be either a String or a ValueBinding.
82     */
83    public Object getLabel()
84    {
85      return _label;
86    }
87  
88    /**
89     * Sets the label, which can be either a String or a ValueBinding.
90     */
91    public void setLabel(Object label)
92    {
93      _label = label;
94    }
95  
96    /**
97     * Gets a string representation of the label.  If the label
98     * is a ValueBinding, the expression is evaluated and the string
99     * value returned.
100    */
101   public String getLabelAsString(FacesContext context)
102   {
103     Object label = getLabel();
104 
105     if (label instanceof ValueExpression)
106     {
107       label = ((ValueExpression) label).getValue(context.getELContext());          
108     }
109     else if (label instanceof ValueBinding)
110     {
111       label = ((ValueBinding) label).getValue(context);
112     }
113 
114     if (label == null)
115       return null;
116 
117     return label.toString();
118   }
119 
120   /**
121    * Returns a LabeledFacesMessage object that is guarenteed to have a String as
122    * the label.  If the label is already a String, this object will be returned.
123    * If it is not then a new LabeledFacesMessage will be created.
124    * <p/>
125    * By default the new LabeledFacesMessage will be created by calling
126    * 
127    * @param context
128    * @return
129    */
130   public LabeledFacesMessage getLabeledFacesMessageWithLabelString(FacesContext context)
131   {
132     Object label = getLabel();
133 
134     if(null == label || label instanceof String)
135     {
136       return this;
137     }
138     
139     return new LabeledFacesMessage(getSeverity(), getSummary(), getDetail(), getLabelAsString(context));
140   }
141   
142   private Object _label;
143   
144   @SuppressWarnings("compatibility:-4615739861775811173")
145   private static final long serialVersionUID = 1L;
146 }