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.shared.renderkit.html;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.shared.component.EscapeCapable;
24  import org.apache.myfaces.shared.renderkit.JSFAttr;
25  import org.apache.myfaces.shared.renderkit.RendererUtils;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIInput;
29  import javax.faces.component.UIOutput;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.component.html.HtmlInputText;
32  import javax.faces.component.html.HtmlOutputText;
33  import javax.faces.context.FacesContext;
34  import javax.faces.context.ResponseWriter;
35  import javax.faces.convert.ConverterException;
36  import java.io.IOException;
37  
38  /***
39   * @author Thomas Spiegl (latest modification by $Author: skitching $)
40   * @author Manfred Geiler
41   * @version $Revision: 673827 $ $Date: 2008-07-03 16:46:23 -0500 (Thu, 03 Jul 2008) $
42   */
43  public class HtmlTextRendererBase
44          extends HtmlRenderer
45  {
46      private static final Log log = LogFactory.getLog(HtmlTextRendererBase.class);
47  
48      private static final String AUTOCOMPLETE_VALUE_OFF = "off";
49  
50      public void encodeEnd(FacesContext facesContext, UIComponent component)
51          throws IOException
52      {
53          org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext,component,null);
54          
55          if (component instanceof UIInput)
56          {
57              renderInput(facesContext, component);
58          }
59          else if (component instanceof UIOutput)
60          {
61              renderOutput(facesContext, component);
62          }
63          else
64          {
65              throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
66          }
67      }
68  
69  
70      protected static void renderOutput(FacesContext facesContext, UIComponent component)
71          throws IOException
72      {
73          
74          String text = org.apache.myfaces.shared.renderkit.RendererUtils.getStringValue(facesContext, component);
75          if (log.isDebugEnabled()) log.debug("renderOutput '" + text + "'");
76          boolean escape;
77          if (component instanceof HtmlOutputText || component instanceof EscapeCapable)
78          {
79              escape = ((HtmlOutputText)component).isEscape();
80          }
81          else
82          {
83              escape = RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared.renderkit.JSFAttr.ESCAPE_ATTR,
84                                                         true); //default is to escape
85          }
86          renderOutputText(facesContext, component, text, escape);
87      }
88  
89  
90      public static void renderOutputText(FacesContext facesContext,
91                                          UIComponent component,
92                                          String text,
93                                          boolean escape)
94          throws IOException
95      {
96          if (text != null)
97          {
98              ResponseWriter writer = facesContext.getResponseWriter();
99              boolean span = false;
100 
101             if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
102             {
103                 span = true;
104 
105                 writer.startElement(HTML.SPAN_ELEM, component);
106 
107                 HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
108 
109                 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
110 
111             }
112             else
113             {
114                 span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,component,
115                         HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES);
116             }
117 
118             if (escape)
119             {
120                 if (log.isDebugEnabled()) log.debug("renderOutputText writing '" + text + "'");
121                 writer.writeText(text, org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR);
122             }
123             else
124             {
125                 writer.write(text);
126             }
127 
128             if(span)
129             {
130                 writer.endElement(org.apache.myfaces.shared.renderkit.html.HTML.SPAN_ELEM);
131             }
132         }
133     }
134 
135 
136     protected void renderInput(FacesContext facesContext, UIComponent component)
137         throws IOException
138     {
139         ResponseWriter writer = facesContext.getResponseWriter();
140 
141         String clientId = component.getClientId(facesContext);
142         String value = org.apache.myfaces.shared.renderkit.RendererUtils.getStringValue(facesContext, component);
143         if (log.isDebugEnabled()) log.debug("renderInput '" + value + "'");
144         writer.startElement(HTML.INPUT_ELEM, component);
145         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
146         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
147         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_TEXT, null);
148         if (value != null)
149         {
150             writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
151         }
152 
153         HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
154         if (isDisabled(facesContext, component))
155         {
156             writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
157         }
158 
159         if (isAutocompleteOff(facesContext, component))
160         {
161             writer.writeAttribute(HTML.AUTOCOMPLETE_ATTR, AUTOCOMPLETE_VALUE_OFF, HTML.AUTOCOMPLETE_ATTR);
162         }
163 
164         writer.endElement(HTML.INPUT_ELEM);
165     }
166 
167     protected boolean isDisabled(FacesContext facesContext, UIComponent component)
168     {
169         //TODO: overwrite in extended HtmlTextRenderer and check for enabledOnUserRole
170         if (component instanceof HtmlInputText)
171         {
172             return ((HtmlInputText)component).isDisabled();
173         }
174 
175         return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(component, HTML.DISABLED_ATTR, false);
176         
177     }
178 
179     /***
180      * If autocomplete is "on" or not set, do not render it
181      */
182     protected boolean isAutocompleteOff(FacesContext facesContext, UIComponent component)
183     {
184         if (component instanceof HtmlInputText)
185         {
186             String autocomplete = ((HtmlInputText)component).getAutocomplete();
187             if (autocomplete != null)
188             {
189                 return autocomplete.equals(AUTOCOMPLETE_VALUE_OFF);
190             }
191         }
192 
193         return false;
194     }
195 
196 
197     public void decode(FacesContext facesContext, UIComponent component)
198     {
199         RendererUtils.checkParamValidity(facesContext,component,null);
200 
201         if (component instanceof UIInput)
202         {
203             HtmlRendererUtils.decodeUIInput(facesContext, component);
204         }
205         else if (component instanceof UIOutput)
206         {
207             //nothing to decode
208         }
209         else
210         {
211             throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
212         }
213     }
214 
215 
216     public Object getConvertedValue(FacesContext facesContext, UIComponent component, Object submittedValue) throws ConverterException
217     {
218         org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIOutput.class);
219         return RendererUtils.getConvertedUIOutputValue(facesContext,
220                                                        (UIOutput)component,
221                                                        submittedValue);
222     }
223 
224 }