1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.renderkit.JSFAttr; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.component.UINamingContainer; 26 import javax.faces.component.ValueHolder; 27 import javax.faces.component.html.HtmlOutputLabel; 28 import javax.faces.context.FacesContext; 29 import javax.faces.context.ResponseWriter; 30 import java.io.IOException ; 31 32 33 67 public class HtmlLabelRenderer 68 extends HtmlRenderer 69 { 70 private static final Log log = LogFactory.getLog(HtmlLabelRenderer.class); 71 72 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 73 throws IOException 74 { 75 super.encodeBegin(facesContext, uiComponent); 77 ResponseWriter writer = facesContext.getResponseWriter(); 78 79 encodeBefore(facesContext, writer, uiComponent); 80 81 writer.startElement(HTML.LABEL_ELEM, uiComponent); 82 HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext); 83 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.LABEL_PASSTHROUGH_ATTRIBUTES); 84 85 String forAttr = getFor(uiComponent); 86 if (forAttr == null) 87 { 88 throw new NullPointerException ("Attribute 'for' of label component with id " + uiComponent.getClientId(facesContext)); 89 } 90 91 UIComponent forComponent = uiComponent.findComponent(forAttr); 92 if (forComponent == null) 93 { 94 if (log.isWarnEnabled()) 95 { 96 log.warn("Unable to find component '" + forAttr + "' (calling findComponent on component '" + uiComponent.getClientId(facesContext) + "')"); 97 } 98 if (forAttr.length() > 0 && forAttr.charAt(0) == UINamingContainer.SEPARATOR_CHAR) 99 { 100 writer.writeAttribute(HTML.FOR_ATTR, forAttr.substring(1), JSFAttr.FOR_ATTR); 102 } 103 else 104 { 105 String labelClientId = uiComponent.getClientId(facesContext); 107 int colon = labelClientId.lastIndexOf(UINamingContainer.SEPARATOR_CHAR); 108 if (colon == -1) 109 { 110 writer.writeAttribute(HTML.FOR_ATTR, forAttr, JSFAttr.FOR_ATTR); 111 } 112 else 113 { 114 writer.writeAttribute(HTML.FOR_ATTR, labelClientId.substring(0, colon + 1) + forAttr, JSFAttr.FOR_ATTR); 115 } 116 } 117 } 118 else 119 { 120 writer.writeAttribute(HTML.FOR_ATTR, forComponent.getClientId(facesContext), JSFAttr.FOR_ATTR); 121 } 122 123 124 if (uiComponent instanceof ValueHolder) 127 { 128 String text = RendererUtils.getStringValue(facesContext, uiComponent); 129 if(text != null) 130 { 131 writer.writeText(text, "value"); 132 } 133 } 134 135 writer.flush(); 137 encodeAfterStart(facesContext,writer,uiComponent); 138 } 139 140 protected void encodeAfterStart(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) 141 throws IOException 142 { 143 } 144 145 protected void encodeBefore(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) 146 throws IOException 147 { 148 } 149 150 151 protected String getFor(UIComponent component) 152 { 153 if (component instanceof HtmlOutputLabel) 154 { 155 return ((HtmlOutputLabel)component).getFor(); 156 } 157 else 158 { 159 return (String )component.getAttributes().get(JSFAttr.FOR_ATTR); 160 } 161 } 162 163 164 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 165 throws IOException 166 { 167 super.encodeEnd(facesContext, uiComponent); 169 ResponseWriter writer = facesContext.getResponseWriter(); 170 171 encodeBeforeEnd(facesContext, writer, uiComponent); 172 173 writer.endElement(HTML.LABEL_ELEM); 174 175 encodeAfter(facesContext, writer, uiComponent); 176 } 177 178 protected void encodeBeforeEnd(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) 179 throws IOException 180 { 181 } 182 183 protected void encodeAfter(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) 184 throws IOException 185 { 186 } 187 } 188 | Popular Tags |