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.myfaces.shared.renderkit.JSFAttr;
22  import org.apache.myfaces.shared.renderkit.RendererUtils;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIInput;
26  import javax.faces.component.UIOutput;
27  import javax.faces.component.html.HtmlInputSecret;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  import javax.faces.convert.ConverterException;
31  import java.io.IOException;
32  
33  
34  /***
35   * see Spec.1.0 EA - JSF.7.6.4 Renderer Types for UIInput Components
36   * @author Manfred Geiler (latest modification by $Author: matzew $)
37   * @author Thomas Spiegl
38   * @author Anton Koinov
39   * @version $Revision: 557350 $ $Date: 2007-07-18 13:19:50 -0500 (Wed, 18 Jul 2007) $
40   */
41  public class HtmlSecretRendererBase
42          extends HtmlRenderer
43  {
44      private static final String AUTOCOMPLETE_VALUE_OFF = "off";
45  
46      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
47              throws IOException
48      {
49          RendererUtils.checkParamValidity(facesContext, uiComponent, UIInput.class);
50  
51          ResponseWriter writer = facesContext.getResponseWriter();
52          writer.startElement(HTML.INPUT_ELEM, uiComponent);
53          writer.writeAttribute(HTML.TYPE_ATTR, org.apache.myfaces.shared.renderkit.html.HTML.INPUT_TYPE_PASSWORD, null);
54  
55          String clientId = uiComponent.getClientId(facesContext);
56  
57          HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
58          writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
59  
60          boolean isRedisplay;
61          if (uiComponent instanceof HtmlInputSecret)
62          {
63              isRedisplay = ((HtmlInputSecret)uiComponent).isRedisplay();
64          }
65          else
66          {
67              isRedisplay = org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(uiComponent, JSFAttr.REDISPLAY_ATTR, false);
68          }
69          if (isRedisplay)
70          {
71              String strValue = RendererUtils.getStringValue(facesContext, uiComponent);
72              writer.writeAttribute(HTML.VALUE_ATTR, strValue, JSFAttr.VALUE_ATTR);
73          }
74  
75          HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
76          if (isDisabled(facesContext, uiComponent))
77          {
78              writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
79          }
80  
81          if (isAutocompleteOff(facesContext, uiComponent))
82          {
83              writer.writeAttribute(HTML.AUTOCOMPLETE_ATTR, AUTOCOMPLETE_VALUE_OFF, HTML.AUTOCOMPLETE_ATTR);
84          }
85  
86          writer.endElement(HTML.INPUT_ELEM);
87      }
88  
89  
90      protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
91      {
92          //TODO: overwrite in extended HtmlSecretRenderer and check for enabledOnUserRole
93          if (uiComponent instanceof HtmlInputSecret)
94          {
95              return ((HtmlInputSecret)uiComponent).isDisabled();
96          }
97  
98          return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false);
99      }
100 
101     /***
102      * If autocomplete is "on" or not set, do not render it
103      */
104     protected boolean isAutocompleteOff(FacesContext facesContext, UIComponent component)
105     {
106         if (component instanceof HtmlInputSecret)
107         {
108             String autocomplete = ((HtmlInputSecret)component).getAutocomplete();
109             if (autocomplete != null)
110             {
111                 return autocomplete.equals(AUTOCOMPLETE_VALUE_OFF);
112             }
113         }
114 
115         return false;
116     }
117 
118     public void decode(FacesContext facesContext, UIComponent component)
119     {
120         org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIInput.class);
121         HtmlRendererUtils.decodeUIInput(facesContext, component);
122     }
123 
124     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException
125     {
126         RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
127         return RendererUtils.getConvertedUIOutputValue(facesContext,
128                                                        (UIOutput)uiComponent,
129                                                        submittedValue);
130     }
131 
132 }