blob: 9d318b86bada55790609e2b2cfb4082ac9d1fb79 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.data;
2
3import java.lang.reflect.*;
4import java.util.*;
5import java.util.regex.*;
6
7public 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 McCulloch54229442012-07-12 22:12:58 +000021 formatter.format("Value for %s must not be null%n", f.getName());
Stuart McCullochf3173222012-06-07 21:57:32 +000022 } else {
Stuart McCullochf3173222012-06-07 21:57:32 +000023
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 McCulloch54229442012-07-12 22:12:58 +000030 formatter.format("Value for %s=%s does not match pattern %s%n", f.getName(), value,
Stuart McCulloch4482c702012-06-15 13:27:53 +000031 patternValidator.value());
Stuart McCullochf3173222012-06-07 21:57:32 +000032 else
Stuart McCulloch54229442012-07-12 22:12:58 +000033 formatter.format("Value for %s=%s %s%n", f.getName(), value, reason);
Stuart McCullochf3173222012-06-07 21:57:32 +000034 }
35 }
36
37 if (numericValidator != null) {
38 if (o instanceof String) {
39 try {
40 o = Double.parseDouble((String) o);
Stuart McCulloch4482c702012-06-15 13:27:53 +000041 }
42 catch (Exception e) {
Stuart McCulloch54229442012-07-12 22:12:58 +000043 formatter.format("Value for %s=%s %s%n", f.getName(), value, "Not a number");
Stuart McCullochf3173222012-06-07 21:57:32 +000044 }
45 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000046
Stuart McCullochf3173222012-06-07 21:57:32 +000047 try {
48 Number n = (Number) o;
49 long number = n.longValue();
50 if (number >= numericValidator.min() && number < numericValidator.max()) {
Stuart McCulloch54229442012-07-12 22:12:58 +000051 formatter.format("Value for %s=%s not in valid range (%s,%s]%n", f.getName(), value,
Stuart McCulloch4482c702012-06-15 13:27:53 +000052 numericValidator.min(), numericValidator.max());
Stuart McCullochf3173222012-06-07 21:57:32 +000053 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000054 }
55 catch (ClassCastException e) {
Stuart McCulloch54229442012-07-12 22:12:58 +000056 formatter.format("Value for %s=%s [%s,%s) is not a number%n", f.getName(), value,
Stuart McCullochf3173222012-06-07 21:57:32 +000057 numericValidator.min(), numericValidator.max());
58 }
59 }
60 }
61 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000062 if (sb.length() == 0)
Stuart McCullochf3173222012-06-07 21:57:32 +000063 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000064
65 if (sb.length() > 0)
Stuart McCullochf3173222012-06-07 21:57:32 +000066 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 McCulloch4482c702012-06-15 13:27:53 +000073
74 for (Field f : fields) {
Stuart McCullochf3173222012-06-07 21:57:32 +000075 String name = f.getName();
76 name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
Stuart McCulloch54229442012-07-12 22:12:58 +000077 formatter.format("%-40s %s%n", name, f.get(data));
Stuart McCullochf3173222012-06-07 21:57:32 +000078 }
79 }
80}