Coverage Report - org.apache.myfaces.renderkit.html.HtmlLabelRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlLabelRenderer
0%
0/34
0%
0/10
0
 
 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;
 20  
 
 21  
 import java.io.IOException;
 22  
 
 23  
 import javax.faces.component.UIComponent;
 24  
 import javax.faces.component.ValueHolder;
 25  
 import javax.faces.component.html.HtmlOutputLabel;
 26  
 import javax.faces.context.FacesContext;
 27  
 import javax.faces.context.ResponseWriter;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
 32  
 import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
 33  
 import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
 34  
 import org.apache.myfaces.shared_impl.renderkit.html.HTML;
 35  
 import org.apache.myfaces.shared_impl.renderkit.html.HtmlRenderer;
 36  
 import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;
 37  
 
 38  
 
 39  
 /**
 40  
  *   
 41  
  * @author Thomas Spiegl (latest modification by $Author: lu4242 $)
 42  
  * @author Anton Koinov
 43  
  * @author Martin Marinschek
 44  
  * @version $Revision: 693358 $ $Date: 2008-09-08 22:54:29 -0500 (Mon, 08 Sep 2008) $
 45  
  */
 46  
 @JSFRenderer(
 47  
     renderKitId="HTML_BASIC",
 48  
     family="javax.faces.Output",
 49  
     type="javax.faces.Label")
 50  0
 public class HtmlLabelRenderer
 51  
 extends HtmlRenderer
 52  
 {
 53  0
     private static final Log log = LogFactory.getLog(HtmlLabelRenderer.class);
 54  
 
 55  
     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
 56  
             throws IOException
 57  
     {
 58  0
         super.encodeBegin(facesContext, uiComponent);   //check for NP
 59  
 
 60  0
         ResponseWriter writer = facesContext.getResponseWriter();
 61  
 
 62  0
         encodeBefore(facesContext, writer, uiComponent);
 63  
 
 64  0
         writer.startElement(HTML.LABEL_ELEM, uiComponent);
 65  0
         HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
 66  0
         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.LABEL_PASSTHROUGH_ATTRIBUTES);
 67  
 
 68  0
         String forAttr = getFor(uiComponent);
 69  
 
 70  0
         if (forAttr != null)
 71  
         {
 72  0
           writer.writeAttribute(HTML.FOR_ATTR,
 73  
                                 getClientId(facesContext, uiComponent, forAttr), JSFAttr.FOR_ATTR);
 74  
         }
 75  
         else
 76  
         {
 77  0
             if (log.isWarnEnabled()) {
 78  0
                 log.warn("Attribute 'for' of label component with id " + uiComponent.getClientId(facesContext)+" is not defined");
 79  
             }
 80  
         }
 81  
 
 82  
 
 83  
         //MyFaces extension: Render a label text given by value
 84  
         //TODO: Move to extended component
 85  0
         if (uiComponent instanceof ValueHolder)
 86  
         {
 87  0
             String text = RendererUtils.getStringValue(facesContext, uiComponent);
 88  0
             if(text != null)
 89  
             {
 90  0
                 writer.writeText(text, "value");
 91  
             }
 92  
         }
 93  
 
 94  0
         writer.flush(); // close start tag
 95  
 
 96  0
         encodeAfterStart(facesContext,writer,uiComponent);
 97  0
     }
 98  
 
 99  
     protected void encodeAfterStart(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
 100  
         throws IOException
 101  
     {
 102  0
     }
 103  
 
 104  
     protected void encodeBefore(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
 105  
         throws IOException
 106  
     {
 107  0
     }
 108  
 
 109  
 
 110  
     protected String getFor(UIComponent component)
 111  
     {
 112  0
         if (component instanceof HtmlOutputLabel)
 113  
         {
 114  0
             return ((HtmlOutputLabel)component).getFor();
 115  
         }
 116  
         
 117  0
         return (String) component.getAttributes().get(JSFAttr.FOR_ATTR);
 118  
         
 119  
     }
 120  
 
 121  
     protected String getClientId(FacesContext facesContext,
 122  
                                  UIComponent uiComponent, String forAttr)
 123  
     {
 124  0
         return RendererUtils.getClientId(facesContext, uiComponent, forAttr);
 125  
     }
 126  
 
 127  
 
 128  
     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
 129  
             throws IOException
 130  
     {
 131  0
         super.encodeEnd(facesContext, uiComponent); //check for NP
 132  
 
 133  0
         ResponseWriter writer = facesContext.getResponseWriter();
 134  
 
 135  0
         encodeBeforeEnd(facesContext, writer, uiComponent);
 136  
 
 137  0
         writer.endElement(HTML.LABEL_ELEM);
 138  
 
 139  0
         encodeAfter(facesContext, writer, uiComponent);
 140  0
     }
 141  
 
 142  
     protected void encodeBeforeEnd(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
 143  
         throws IOException
 144  
     {
 145  0
     }
 146  
 
 147  
     protected void encodeAfter(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
 148  
         throws IOException
 149  
     {
 150  0
     }
 151  
 }