1 package org.apache.turbine.util.validation; 2 3 18 19 24 public abstract class InputValidator 25 { 26 public static final boolean AllowNullInput = true; public static final int NoMaxSize = -1; public static final String EmptyArgv = ""; 30 private static String NullInputError = "Null Input Not Allowed"; 32 private static String MaxSizeExceededError = "Maximum Size Exceeded"; 33 34 private boolean allowNullInput; 35 private int maxSize; 36 private String argv; 37 38 41 public InputValidator() 42 { 43 this(AllowNullInput, NoMaxSize, EmptyArgv); 44 } 45 46 52 public InputValidator(boolean allowNullInput, 53 int maxSize, 54 String argv) 55 { 56 this.allowNullInput = allowNullInput; 57 this.maxSize = maxSize; 58 this.argv = argv; 59 } 60 61 64 public void setAllowNullInput(boolean allowNullInput) 65 { 66 this.allowNullInput = allowNullInput; 67 } 68 69 72 public void setMaxSize(int maxSize) 73 { 74 this.maxSize = maxSize; 75 } 76 77 80 public void setArgv(String argv) 81 { 82 this.argv = argv; 83 } 84 85 89 public boolean isValid(String input) 90 { 91 try 92 { 93 checkInput(input); 94 return true; 95 } 96 catch (Exception e) 97 { 98 return false; 99 } 100 } 101 102 106 public String getErrorMessage(String input) 107 { 108 try 109 { 110 checkInput(input); 111 } 112 catch (Exception e) 113 { 114 return e.toString(); 115 } 116 117 return null; 119 } 120 121 125 public void checkInput(String value) 126 throws Exception 127 { 128 int size = 0; 129 if (value != null) 130 { 131 value = value.trim(); 132 size = value.length(); 133 } 134 135 if (!allowNullInput && value == null) 136 { 137 throw new Exception (NullInputError); 138 } 139 140 if (maxSize != NoMaxSize && size > maxSize) 141 { 142 throw new Exception (MaxSizeExceededError); 143 } 144 145 check(value); 147 } 148 149 152 public abstract String getExpectedFormat(); 153 154 158 protected abstract void check(String input) 159 throws Exception ; 160 161 } 162 | Popular Tags |