View Javadoc
1   package org.exoplatform.wcm.webui;
2   
3   import org.exoplatform.web.application.ApplicationMessage;
4   import org.exoplatform.webui.core.UIComponent;
5   import org.exoplatform.webui.exception.MessageException;
6   import org.exoplatform.webui.form.UIForm;
7   import org.exoplatform.webui.form.UIFormInput;
8   import org.exoplatform.webui.form.validator.Validator;
9   
10  /**
11   * Author : TAN DUNG DANG
12   * dzungdev@gmail.com
13   * Mar 30, 2009
14   */
15  public class ImageSizeValidator implements Validator {
16  
17    /* (non-Javadoc)
18     * @see org.exoplatform.webui.form.validator.Validator#validate(org.exoplatform.webui.form.UIFormInput)
19     */
20    @SuppressWarnings("unchecked")
21    public void validate(UIFormInput uiInput) throws Exception {
22      if (uiInput.getValue()==null || ((String)uiInput.getValue()).length()==0) return;
23      UIComponent uiComponent = (UIComponent) uiInput ;
24      UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class) ;
25      String label;
26      try{
27        label = uiForm.getLabel(uiInput.getName());
28      } catch(Exception e) {
29        label = uiInput.getName();
30      }
31      label = label.trim();
32      if(label.charAt(label.length() - 1) == ':') label = label.substring(0, label.length() - 1);
33      String s = (String)uiInput.getValue();
34      int size = s.length();
35      for(int i = 0; i < size; i ++){
36        char c = s.charAt(i);
37        if (Character.isDigit(c) || (s.charAt(0) == '-' && i == 0 && s.length() > 1)
38                                 || (s.charAt(size-1) == '%')){
39          continue;
40        }
41        Object[] args = { label, uiInput.getBindingField() };
42        throw new MessageException(new ApplicationMessage("NumberFormatValidator.msg.Invalid-number", args)) ;
43      }
44    }
45  }