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