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
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
35
36
37
38
39
40
41
42 public class HtmlMessagesRenderer
43 extends HtmlMessagesRendererBase
44 {
45
46
47
48 public void encodeEnd(FacesContext facesContext, UIComponent component)
49 throws IOException
50 {
51 super.encodeEnd(facesContext, component);
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 }