Use local copy of latest bndlib code for pre-release testing purposes
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1347815 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/lib/data/AllowNull.java b/bundleplugin/src/main/java/aQute/lib/data/AllowNull.java
new file mode 100644
index 0000000..2bbf253
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/data/AllowNull.java
@@ -0,0 +1,9 @@
+package aQute.lib.data;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(value={ElementType.FIELD})
+public @interface AllowNull {
+ String reason() default "";
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/data/Data.java b/bundleplugin/src/main/java/aQute/lib/data/Data.java
new file mode 100644
index 0000000..d790d2e
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/data/Data.java
@@ -0,0 +1,79 @@
+package aQute.lib.data;
+
+import java.lang.reflect.*;
+import java.util.*;
+import java.util.regex.*;
+
+public class Data {
+
+ public static String validate(Object o) throws Exception {
+ StringBuilder sb = new StringBuilder();
+ Formatter formatter = new Formatter(sb);
+
+ Field fields[] = o.getClass().getFields();
+ for (Field f : fields) {
+ Validator patternValidator = f.getAnnotation(Validator.class);
+ Numeric numericValidator = f.getAnnotation(Numeric.class);
+ AllowNull allowNull = f.getAnnotation(AllowNull.class);
+ Object value = f.get(o);
+ if (value == null) {
+ if (allowNull == null)
+ formatter.format("Value for %s must not be null\n", f.getName());
+ } else {
+
+
+ if (patternValidator != null) {
+ Pattern p = Pattern.compile(patternValidator.value());
+ Matcher m = p.matcher(value.toString());
+ if (!m.matches()) {
+ String reason = patternValidator.reason();
+ if (reason.length() == 0)
+ formatter.format("Value for %s=%s does not match pattern %s\n",
+ f.getName(), value, patternValidator.value());
+ else
+ formatter.format("Value for %s=%s %s\n", f.getName(), value, reason);
+ }
+ }
+
+ if (numericValidator != null) {
+ if (o instanceof String) {
+ try {
+ o = Double.parseDouble((String) o);
+ } catch (Exception e) {
+ formatter.format("Value for %s=%s %s\n", f.getName(), value, "Not a number");
+ }
+ }
+
+ try {
+ Number n = (Number) o;
+ long number = n.longValue();
+ if (number >= numericValidator.min() && number < numericValidator.max()) {
+ formatter.format("Value for %s=%s not in valid range (%s,%s]\n",
+ f.getName(), value, numericValidator.min(), numericValidator.max());
+ }
+ } catch (ClassCastException e) {
+ formatter.format("Value for %s=%s [%s,%s) is not a number\n", f.getName(), value,
+ numericValidator.min(), numericValidator.max());
+ }
+ }
+ }
+ }
+ if ( sb.length() == 0)
+ return null;
+
+ if ( sb.length() > 0)
+ sb.delete(sb.length() - 1, sb.length());
+ return sb.toString();
+ }
+
+ public static void details(Object data, Appendable out) throws Exception {
+ Field fields[] = data.getClass().getFields();
+ Formatter formatter = new Formatter(out);
+
+ for ( Field f : fields ) {
+ String name = f.getName();
+ name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+ formatter.format("%-40s %s\n", name, f.get(data));
+ }
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/data/Numeric.java b/bundleplugin/src/main/java/aQute/lib/data/Numeric.java
new file mode 100644
index 0000000..19d60cf
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/data/Numeric.java
@@ -0,0 +1,11 @@
+package aQute.lib.data;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(value={ElementType.FIELD})
+public @interface Numeric {
+ long min() default Long.MIN_VALUE;
+ long max() default Long.MAX_VALUE;
+ String reason() default "";
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/data/Validator.java b/bundleplugin/src/main/java/aQute/lib/data/Validator.java
new file mode 100644
index 0000000..e8a8d4f
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/data/Validator.java
@@ -0,0 +1,10 @@
+package aQute.lib.data;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(value={ElementType.FIELD})
+public @interface Validator {
+ String value();
+ String reason() default "";
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/data/packageinfo b/bundleplugin/src/main/java/aQute/lib/data/packageinfo
new file mode 100644
index 0000000..9ad81f6
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/data/packageinfo
@@ -0,0 +1 @@
+version 1.0.0