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.renderkit.html.ext;
20  
21  import org.apache.myfaces.component.html.ext.HtmlMessage;
22  import org.apache.myfaces.component.html.ext.HtmlMessages;
23  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlMessagesRendererBase;
24  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
25  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
26  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
27  
28  import javax.faces.application.FacesMessage;
29  import javax.faces.component.UIComponent;
30  import javax.faces.component.behavior.ClientBehavior;
31  import javax.faces.component.behavior.ClientBehaviorHolder;
32  import javax.faces.context.FacesContext;
33  import javax.faces.context.ResponseWriter;
34  import java.io.IOException;
35  import java.text.MessageFormat;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * @JSFRenderer
41   *   renderKitId = "HTML_BASIC"
42   *   family = "javax.faces.Messages"
43   *   type = "org.apache.myfaces.Messages"
44   * 
45   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
46   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (sáb, 24 may 2008) $
47   */
48  public class HtmlMessagesRenderer
49          extends HtmlMessagesRendererBase
50  {
51      //private static final Log log = LogFactory.getLog(HtmlMessagesRenderer.class);
52  
53      @Override
54      public void decode(FacesContext context, UIComponent component)
55      {
56          super.decode(context, component);
57          
58          HtmlRendererUtils.decodeClientBehaviors(context, component);
59      }
60      
61      public void encodeEnd(FacesContext facesContext, UIComponent component)
62              throws IOException
63      {
64          super.encodeEnd(facesContext, component);   //check for NP
65          
66          Map<String, List<ClientBehavior>> behaviors = null;
67          if (component instanceof ClientBehaviorHolder)
68          {
69              behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
70              if (!behaviors.isEmpty())
71              {
72                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
73              }
74          }
75  
76          boolean forceSpan = false;
77          if (component instanceof HtmlMessages
78                  && ((HtmlMessages) component).getForceSpan())
79          {
80              forceSpan = true;
81          }
82          
83          renderMessages(facesContext, component, forceSpan, true);
84  
85          /*
86          if (component instanceof HtmlMessages
87                  && ((HtmlMessages) component).getForceSpan())
88          {
89              ResponseWriter writer = facesContext.getResponseWriter();
90  
91              HtmlMessages htmlMessages = (HtmlMessages) component;
92  
93              writer.startElement(HTML.SPAN_ELEM, null);
94              writer.writeAttribute(HTML.ID_ATTR,component.getClientId(facesContext),null);
95              if(htmlMessages.getStyleClass()!=null)
96              writer.writeAttribute(HTML.CLASS_ATTR,htmlMessages.getStyleClass(),null);
97              if(htmlMessages.getStyle()!=null)
98              writer.writeAttribute(HTML.STYLE_ATTR,htmlMessages.getStyle(),null);
99              writer.endElement(HTML.SPAN_ELEM);
100         }*/
101     }
102 
103     protected String getSummary(FacesContext facesContext,
104                                 UIComponent message,
105                                 FacesMessage facesMessage,
106                                 String msgClientId)
107     {
108         String msgSummary = facesMessage.getSummary();
109         if (msgSummary == null) return null;
110 
111         String inputLabel = null;
112         if (msgClientId != null) inputLabel = HtmlMessageRenderer.findInputLabel(facesContext, msgClientId);
113         if (inputLabel == null) inputLabel = "";
114 
115         if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) ||
116                 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&&
117                 inputLabel.length()!=0)
118             msgSummary = msgSummary.replaceAll(HtmlMessageRenderer.findInputId(facesContext, msgClientId),inputLabel);
119 
120 
121         String summaryFormat;
122         if (msgClientId == null)
123         {
124             summaryFormat = getGlobalSummaryFormat(message);
125             if (summaryFormat == null)
126             {
127                 summaryFormat = getSummaryFormat(message);
128             }
129         }
130         else
131         {
132             summaryFormat = getSummaryFormat(message);
133         }
134         if (summaryFormat == null) return msgSummary;
135 
136         MessageFormat format = new MessageFormat(summaryFormat, facesContext.getViewRoot().getLocale());
137         return format.format(new Object[] {msgSummary, inputLabel});
138     }
139 
140 
141     private String getSummaryFormat(UIComponent message)
142     {
143         if (message instanceof HtmlMessages)
144         {
145             return ((HtmlMessages)message).getSummaryFormat();
146         }
147         else
148         {
149             return (String)message.getAttributes().get("summaryFormat");
150         }
151     }
152 
153     private String getGlobalSummaryFormat(UIComponent message)
154     {
155         if (message instanceof HtmlMessages)
156         {
157             return ((HtmlMessages)message).getGlobalSummaryFormat();
158         }
159         else
160         {
161             return (String)message.getAttributes().get("globalSummaryFormat");
162         }
163     }
164 
165     protected String getDetail(FacesContext facesContext,
166                                UIComponent message,
167                                FacesMessage facesMessage,
168                                String msgClientId)
169     {
170         String msgDetail = facesMessage.getDetail();
171         if (msgDetail == null) return null;
172 
173         String inputLabel = null;
174         if (msgClientId != null) inputLabel = HtmlMessageRenderer.findInputLabel(facesContext, msgClientId);
175         if (inputLabel == null) inputLabel = "";
176 
177         if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) ||
178                 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&&
179                 inputLabel.length()!=0)
180             msgDetail = msgDetail.replaceAll(HtmlMessageRenderer.findInputId(facesContext, msgClientId),inputLabel);
181 
182         String detailFormat;
183         if (message instanceof HtmlMessage)
184         {
185             detailFormat = ((HtmlMessage)message).getDetailFormat();
186         }
187         else
188         {
189             detailFormat = (String)message.getAttributes().get("detailFormat");
190         }
191 
192         if (detailFormat == null) return msgDetail;
193 
194         MessageFormat format = new MessageFormat(detailFormat, facesContext.getViewRoot().getLocale());
195         return format.format(new Object[] {msgDetail, inputLabel});
196     }
197 
198 
199 }