001    package org.apache.myfaces.tobago.application;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.util.MessageUtils;
021    
022    import javax.faces.application.FacesMessage;
023    import javax.faces.context.FacesContext;
024    import javax.faces.el.ValueBinding;
025    import javax.faces.webapp.UIComponentTag;
026    import java.util.Locale;
027    
028    @SuppressWarnings("deprecation")
029    public class LabelValueBindingFacesMessage extends FacesMessage {
030      private Locale locale;
031      private Object[] args;
032    
033      public LabelValueBindingFacesMessage() {
034        super();
035      }
036    
037      public LabelValueBindingFacesMessage(FacesMessage.Severity severity, String summary, String detail,
038          Locale locale, Object... args) {
039        super(severity, summary, detail);
040        this.locale = locale;
041        this.args = args;
042      }
043    
044      public LabelValueBindingFacesMessage(String summary, String detail) {
045        super(summary, detail);
046      }
047    
048      public LabelValueBindingFacesMessage(String summary) {
049        super(summary);
050      }
051    
052      @Override
053      public String getDetail() {
054        String detail = super.getDetail();
055        if (args != null && args.length > 0) {
056          if (args.length == 1 && UIComponentTag.isValueReference(args[0].toString())) {
057            FacesContext facesContext = FacesContext.getCurrentInstance();
058            ValueBinding value = facesContext.getApplication().createValueBinding(detail);
059            return MessageUtils.getFormatedMessage(detail, locale, value.getValue(facesContext));
060          }
061          return MessageUtils.getFormatedMessage(detail, locale, args);
062        }
063        return detail;
064      }
065    
066      @Override
067      public String getSummary() {
068        String summary = super.getSummary();
069        if (args != null && args.length > 0) {
070          if (args.length == 1 && UIComponentTag.isValueReference(args[0].toString())) {
071            FacesContext facesContext = FacesContext.getCurrentInstance();
072            ValueBinding value = facesContext.getApplication().createValueBinding(summary);
073            return MessageUtils.getFormatedMessage(summary, locale, value.getValue(facesContext));
074          }
075          return MessageUtils.getFormatedMessage(summary, locale, args);
076        }
077        return summary;
078      }
079    
080    }