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