Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.data; |
| 2 | |
| 3 | import java.lang.reflect.*; |
| 4 | import java.util.*; |
| 5 | import java.util.regex.*; |
| 6 | |
| 7 | public class Data { |
| 8 | |
| 9 | public static String validate(Object o) throws Exception { |
| 10 | StringBuilder sb = new StringBuilder(); |
| 11 | Formatter formatter = new Formatter(sb); |
| 12 | |
| 13 | Field fields[] = o.getClass().getFields(); |
| 14 | for (Field f : fields) { |
| 15 | Validator patternValidator = f.getAnnotation(Validator.class); |
| 16 | Numeric numericValidator = f.getAnnotation(Numeric.class); |
| 17 | AllowNull allowNull = f.getAnnotation(AllowNull.class); |
| 18 | Object value = f.get(o); |
| 19 | if (value == null) { |
| 20 | if (allowNull == null) |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 21 | formatter.format("Value for %s must not be null%n", f.getName()); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | } else { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 23 | |
| 24 | if (patternValidator != null) { |
| 25 | Pattern p = Pattern.compile(patternValidator.value()); |
| 26 | Matcher m = p.matcher(value.toString()); |
| 27 | if (!m.matches()) { |
| 28 | String reason = patternValidator.reason(); |
| 29 | if (reason.length() == 0) |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 30 | formatter.format("Value for %s=%s does not match pattern %s%n", f.getName(), value, |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 31 | patternValidator.value()); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | else |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 33 | formatter.format("Value for %s=%s %s%n", f.getName(), value, reason); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | if (numericValidator != null) { |
| 38 | if (o instanceof String) { |
| 39 | try { |
| 40 | o = Double.parseDouble((String) o); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | } |
| 42 | catch (Exception e) { |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 43 | formatter.format("Value for %s=%s %s%n", f.getName(), value, "Not a number"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 46 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | try { |
| 48 | Number n = (Number) o; |
| 49 | long number = n.longValue(); |
| 50 | if (number >= numericValidator.min() && number < numericValidator.max()) { |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 51 | formatter.format("Value for %s=%s not in valid range (%s,%s]%n", f.getName(), value, |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | numericValidator.min(), numericValidator.max()); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 53 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 54 | } |
| 55 | catch (ClassCastException e) { |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 56 | formatter.format("Value for %s=%s [%s,%s) is not a number%n", f.getName(), value, |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | numericValidator.min(), numericValidator.max()); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 62 | if (sb.length() == 0) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | return null; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 64 | |
| 65 | if (sb.length() > 0) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | sb.delete(sb.length() - 1, sb.length()); |
| 67 | return sb.toString(); |
| 68 | } |
| 69 | |
| 70 | public static void details(Object data, Appendable out) throws Exception { |
| 71 | Field fields[] = data.getClass().getFields(); |
| 72 | Formatter formatter = new Formatter(out); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 73 | |
| 74 | for (Field f : fields) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 75 | String name = f.getName(); |
| 76 | name = Character.toUpperCase(name.charAt(0)) + name.substring(1); |
Stuart McCulloch | 5422944 | 2012-07-12 22:12:58 +0000 | [diff] [blame^] | 77 | formatter.format("%-40s %s%n", name, f.get(data)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |