View Javadoc

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