|
|
@@ -1,6 +1,8 @@
|
|
|
package org.leumasjaffe.json.schema.factory;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.function.Predicate;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
@@ -13,8 +15,13 @@ import org.leumasjaffe.json.schema.tester.FixedTester;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
|
|
|
|
|
+import lombok.AccessLevel;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.experimental.FieldDefaults;
|
|
|
|
|
|
+@NoArgsConstructor
|
|
|
public class SchemaFactory {
|
|
|
@AllArgsConstructor
|
|
|
static final class SimpleTester implements Tester {
|
|
|
@@ -32,7 +39,43 @@ public class SchemaFactory {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @RequiredArgsConstructor
|
|
|
+ @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
|
+ protected class Definitions {
|
|
|
+ JsonNode localJson;
|
|
|
+ Map<String, Tester> computed = new HashMap<>();
|
|
|
+
|
|
|
+ public Tester get(final String path) {
|
|
|
+ computed.computeIfAbsent(path, this::createTester);
|
|
|
+ return computed.get(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Tester createTester(final String path) {
|
|
|
+ if (path.startsWith("#")) {
|
|
|
+ JsonNode current = localJson;
|
|
|
+ final String[] tokens = path.substring(2).split("/");
|
|
|
+ for (final String tok : tokens) {
|
|
|
+ if (tok.matches("^\\d+$")) {
|
|
|
+ current = current.path(Integer.parseInt(tok));
|
|
|
+ } else {
|
|
|
+ current = current.path(tok);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return SchemaFactory.this.create(current);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Can't do URI searches yet...");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected Definitions defs = null;
|
|
|
+
|
|
|
+ protected SchemaFactory(Definitions defs) {
|
|
|
+ this.defs = defs;
|
|
|
+ }
|
|
|
+
|
|
|
public final Tester create(final JsonNode object) {
|
|
|
+ if (defs == null) { defs = this.new Definitions(object); }
|
|
|
switch (object.getNodeType()) {
|
|
|
case BOOLEAN:
|
|
|
return new Schema(object.asBoolean() ? FixedTester.ACCEPT : FixedTester.REJECT);
|
|
|
@@ -53,7 +96,7 @@ public class SchemaFactory {
|
|
|
return this;
|
|
|
} else {
|
|
|
switch (getVersionInt(version)) {
|
|
|
- case 6: return new SchemaV6Factory();
|
|
|
+ case 6: return new SchemaV6Factory(defs);
|
|
|
default:
|
|
|
throw new IllegalArgumentException("Unsupported schema version: " + version);
|
|
|
}
|