1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
41
42
43
44
45
46
47
48 public class HtmlMessagesRenderer
49 extends HtmlMessagesRendererBase
50 {
51
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);
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
87
88
89
90
91
92
93
94
95
96
97
98
99
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 }