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 javax.faces.component.html;
20  
21  import javax.faces.component.UIInput;
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
25  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
26  
27  /**
28   * Renders a HTML input element.
29   *
30   */
31  @JSFComponent
32  (name = "h:inputFile",
33  clazz = "javax.faces.component.html.HtmlInputFile",template=true,
34  tagClass = "org.apache.myfaces.taglib.html.HtmlInputFileTag",
35  defaultRendererType = "javax.faces.File",
36  implementz = "javax.faces.component.behavior.ClientBehaviorHolder",
37  defaultEventName = "valueChange"
38  )
39  abstract class _HtmlInputFile extends UIInput implements _AccesskeyProperty,
40      _AltProperty, _UniversalProperties, _DisabledReadonlyProperties,
41      _FocusBlurProperties, _ChangeProperty, _SelectProperty,
42      _EventProperties, _StyleProperties, _TabindexProperty, _LabelProperty,
43      _RoleProperty
44  {
45  
46    static public final String COMPONENT_FAMILY =
47      "javax.faces.Input";
48    static public final String COMPONENT_TYPE =
49      "javax.faces.HtmlInputFile";
50  
51    /**
52     * HTML: The maximum number of characters allowed to be entered.
53     * 
54     * @JSFProperty
55     *   defaultValue = "Integer.MIN_VALUE"
56     */
57    public abstract int getMaxlength();
58  
59    /**
60     * HTML: The initial width of this control, in characters.
61     * 
62     * @JSFProperty
63     *   defaultValue = "Integer.MIN_VALUE"
64     */
65    public abstract int getSize();
66  
67    /**
68     * If the value of this attribute is "off", render "off" as the value of the attribute.
69     * This indicates that the browser should disable its autocomplete feature for this component.
70     * This is useful for components that perform autocompletion and do not want the browser interfering.
71     * If this attribute is not set or the value is "on", render nothing.
72     *
73     * @return  the new autocomplete value
74     */
75    @JSFProperty
76    public abstract String getAutocomplete();
77  
78      protected void validateValue(FacesContext context, Object convertedValue)
79      {
80          if (!isValid())
81          {
82              return;
83          }
84  
85          // If our value is empty, check the required property
86          boolean isEmpty = isEmptyValue(convertedValue); 
87  
88          if (isRequired() && isEmpty)
89          {
90              if (getRequiredMessage() != null)
91              {
92                  String requiredMessage = getRequiredMessage();
93                  context.addMessage(this.getClientId(context), new javax.faces.application.FacesMessage(
94                          javax.faces.application.FacesMessage.SEVERITY_ERROR,
95                      requiredMessage, requiredMessage));
96              }
97              else
98              {
99                  _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,
100                     new Object[] { _MessageUtils.getLabel(context, this) });
101             }
102             setValid(false);
103             return;
104         }
105 
106         if (!isEmpty)
107         {
108             super.validateValue(context, convertedValue);
109         }
110     }
111     
112     private static boolean isEmptyValue(Object value)
113     {
114         if (value == null)
115         {
116             return true;
117         }
118         else if (value instanceof String)
119         {
120             if ( ((String)value).trim().length() <= 0 )
121             {
122                 return true;
123             }
124         }
125         else if (value instanceof java.util.Collection)
126         {
127             if ( ((java.util.Collection)value).isEmpty())
128             {
129                 return true;
130             }
131         }
132         else if (value.getClass().isArray())
133         {
134             if (java.lang.reflect.Array.getLength(value) <= 0)
135             {
136                 return true;
137             }
138         }
139         else if (value instanceof java.util.Map)
140         {
141             if ( ((java.util.Map)value).isEmpty())
142             {
143                 return true;
144             }
145         }
146         else if (value instanceof javax.servlet.http.Part) 
147         {
148             if (((javax.servlet.http.Part)value).getSize() <= 0) 
149             {
150                 return true;
151             }
152         }
153         return false;
154     }
155 
156 }