1 16 package org.apache.myfaces.renderkit.html.ext; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.myfaces.component.html.ext.HtmlMessage; 21 import org.apache.myfaces.component.html.ext.HtmlMessages; 22 import org.apache.myfaces.renderkit.RendererUtils; 23 import org.apache.myfaces.renderkit.html.HtmlMessageRendererBase; 24 25 import javax.faces.application.FacesMessage; 26 import javax.faces.component.UIColumn; 27 import javax.faces.component.UIComponent; 28 import javax.faces.component.ValueHolder; 29 import javax.faces.component.html.HtmlOutputLabel; 30 import javax.faces.component.html.HtmlOutputText; 31 import javax.faces.context.FacesContext; 32 import java.io.IOException ; 33 import java.text.MessageFormat ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 39 74 public class HtmlMessageRenderer 75 extends HtmlMessageRendererBase 76 { 77 private static final Log log = LogFactory.getLog(HtmlMessageRenderer.class); 78 79 private static final String OUTPUT_LABEL_MAP = HtmlMessageRenderer.class.getName() + ".OUTPUT_LABEL_MAP"; 80 81 public void encodeEnd(FacesContext facesContext, UIComponent component) 82 throws IOException 83 { 84 super.encodeEnd(facesContext, component); renderMessage(facesContext, component); 86 } 87 88 protected String getSummary(FacesContext facesContext, 89 UIComponent message, 90 FacesMessage facesMessage, 91 String msgClientId) 92 { 93 String msgSummary = facesMessage.getSummary(); 94 if (msgSummary == null) return null; 95 96 String inputLabel = null; 97 if (msgClientId != null) inputLabel = findInputLabel(facesContext, msgClientId); 98 if (inputLabel == null) inputLabel = ""; 99 100 if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) || 101 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&& 102 inputLabel.length()!=0) 103 msgSummary = msgSummary.replaceAll(findInputId(facesContext, msgClientId),inputLabel); 104 105 106 String summaryFormat; 107 if (message instanceof HtmlMessage) 108 { 109 summaryFormat = ((HtmlMessage)message).getSummaryFormat(); 110 } 111 else 112 { 113 summaryFormat = (String )message.getAttributes().get("summaryFormat"); 114 } 115 116 if (summaryFormat == null) return msgSummary; 117 118 MessageFormat format = new MessageFormat (summaryFormat, facesContext.getViewRoot().getLocale()); 119 120 return format.format(new Object [] {msgSummary, inputLabel}); 121 } 122 123 protected String getDetail(FacesContext facesContext, 124 UIComponent message, 125 FacesMessage facesMessage, 126 String msgClientId) 127 { 128 String msgDetail = facesMessage.getDetail(); 129 if (msgDetail == null) return null; 130 131 String inputLabel = null; 132 if (msgClientId != null) inputLabel = findInputLabel(facesContext, msgClientId); 133 if (inputLabel == null) inputLabel = ""; 134 135 if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) || 136 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&& 137 inputLabel.length()!=0) 138 msgDetail = msgDetail.replaceAll(findInputId(facesContext, msgClientId),inputLabel); 139 140 String detailFormat; 141 if (message instanceof HtmlMessage) 142 { 143 detailFormat = ((HtmlMessage)message).getDetailFormat(); 144 } 145 else 146 { 147 detailFormat = (String )message.getAttributes().get("detailFormat"); 148 } 149 150 if (detailFormat == null) return msgDetail; 151 152 MessageFormat format = new MessageFormat (detailFormat, facesContext.getViewRoot().getLocale()); 153 return format.format(new Object [] {msgDetail, inputLabel}); 154 } 155 156 157 public static String findInputLabel(FacesContext facesContext, String inputClientId) 158 { 159 Map outputLabelMap = getOutputLabelMap(facesContext); 160 MessageLabelInfo info = ((MessageLabelInfo)outputLabelMap.get(inputClientId)); 161 162 if(info == null) 163 { 164 UIComponent comp = facesContext.getViewRoot().findComponent(inputClientId); 165 166 UIComponent parent=comp; 167 168 while(parent != null && !((parent=parent.getParent())instanceof UIColumn)); 169 170 if(parent != null) 171 { 172 UIColumn column = (UIColumn) parent; 173 174 if(column.getHeader()!=null) 175 { 176 UIComponent header = column.getHeader(); 177 178 return getComponentText(facesContext, header); 179 } 180 } 181 } 182 183 return info==null?null:info.getText(); 184 } 185 186 public static String findInputId(FacesContext facesContext, String inputClientId) 187 { 188 Map outputLabelMap = getOutputLabelMap(facesContext); 189 MessageLabelInfo info = ((MessageLabelInfo)outputLabelMap.get(inputClientId)); 190 191 if(info == null) 192 { 193 UIComponent comp = facesContext.getViewRoot().findComponent(inputClientId); 194 195 if(comp!=null) 196 { 197 return comp.getId(); 198 } 199 } 200 201 return info==null?null:(info.getForComponent()==null?null:info.getForComponent().getId()); 202 } 203 204 209 private static Map getOutputLabelMap(FacesContext facesContext) 210 { 211 Map map = (Map )facesContext.getExternalContext().getRequestMap().get(OUTPUT_LABEL_MAP); 212 if (map == null) 213 { 214 map = new HashMap (); 215 createOutputLabelMap(facesContext, facesContext.getViewRoot(), map); 216 facesContext.getExternalContext().getRequestMap().put(OUTPUT_LABEL_MAP, map); 217 } 218 return map; 219 } 220 221 private static void createOutputLabelMap(FacesContext facesContext, 222 UIComponent root, 223 Map map) 224 { 225 for (Iterator it = root.getFacetsAndChildren(); it.hasNext(); ) 226 { 227 UIComponent child = (UIComponent)it.next(); 228 if (child instanceof HtmlOutputLabel) 229 { 230 String forAttr = ((HtmlOutputLabel)child).getFor(); 231 UIComponent input = child.findComponent(forAttr); 232 if (input == null) 233 { 234 log.warn("Unable to find component '" + forAttr + "' (calling findComponent on component '" + child.getClientId(facesContext) + "')"); 235 } 236 else 237 { 238 map.put(input.getClientId(facesContext), 239 new MessageLabelInfo( 240 input,getComponentText(facesContext, (HtmlOutputLabel)child))); 241 } 242 } 243 else 244 { 245 createOutputLabelMap(facesContext, child, map); 246 } 247 } 248 } 249 250 private static String getComponentText(FacesContext facesContext, UIComponent component) 251 { 252 String text = null; 253 254 if(component instanceof ValueHolder) 255 { 256 text=RendererUtils.getStringValue(facesContext, component); 257 } 258 259 if (text == null) 260 { 261 StringBuffer buf = new StringBuffer (); 262 List li = component.getChildren(); 263 264 for (int i = 0; i < li.size(); i++) 265 { 266 UIComponent child = (UIComponent) li.get(i); 267 268 if(child instanceof HtmlOutputText) 269 { 270 String str = RendererUtils.getStringValue(facesContext, child); 271 272 if(str!=null) 273 buf.append(str); 274 } 275 } 276 277 text = buf.toString(); 278 } 279 return text; 280 } 281 282 public static class MessageLabelInfo 283 { 284 private UIComponent _forComponent; 285 private String _text; 286 287 public MessageLabelInfo(UIComponent forComponent, String text) 288 { 289 _forComponent = forComponent; 290 _text = text; 291 } 292 293 public UIComponent getForComponent() 294 { 295 return _forComponent; 296 } 297 298 public void setForComponent(UIComponent forComponent) 299 { 300 _forComponent = forComponent; 301 } 302 303 public String getText() 304 { 305 return _text; 306 } 307 308 public void setText(String text) 309 { 310 _text = text; 311 } 312 } 313 } 314 | Popular Tags |