blob: f44d1a7d05e2fb5d37e50fa04c6c9f606fdc53dc [file] [log] [blame]
Stuart McCullochbb014372012-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)
21 formatter.format("Value for %s must not be null\n", f.getName());
22 } else {
Stuart McCullochbb014372012-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 McCulloch2286f232012-06-15 13:27:53 +000030 formatter.format("Value for %s=%s does not match pattern %s\n", f.getName(), value,
31 patternValidator.value());
Stuart McCullochbb014372012-06-07 21:57:32 +000032 else
33 formatter.format("Value for %s=%s %s\n", f.getName(), value, reason);
34 }
35 }
36
37 if (numericValidator != null) {
38 if (o instanceof String) {
39 try {
40 o = Double.parseDouble((String) o);
Stuart McCulloch2286f232012-06-15 13:27:53 +000041 }
42 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +000043 formatter.format("Value for %s=%s %s\n", f.getName(), value, "Not a number");
44 }
45 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000046
Stuart McCullochbb014372012-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 McCulloch2286f232012-06-15 13:27:53 +000051 formatter.format("Value for %s=%s not in valid range (%s,%s]\n", f.getName(), value,
52 numericValidator.min(), numericValidator.max());
Stuart McCullochbb014372012-06-07 21:57:32 +000053 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000054 }
55 catch (ClassCastException e) {
Stuart McCullochbb014372012-06-07 21:57:32 +000056 formatter.format("Value for %s=%s [%s,%s) is not a number\n", f.getName(), value,
57 numericValidator.min(), numericValidator.max());
58 }
59 }
60 }
61 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000062 if (sb.length() == 0)
Stuart McCullochbb014372012-06-07 21:57:32 +000063 return null;
Stuart McCulloch2286f232012-06-15 13:27:53 +000064
65 if (sb.length() > 0)
Stuart McCullochbb014372012-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 McCulloch2286f232012-06-15 13:27:53 +000073
74 for (Field f : fields) {
Stuart McCullochbb014372012-06-07 21:57:32 +000075 String name = f.getName();
76 name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
77 formatter.format("%-40s %s\n", name, f.get(data));
78 }
79 }
80}