33import com .fasterxml .jackson .core .JsonProcessingException ;
44import com .fasterxml .jackson .databind .JsonNode ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
6- import com .networknt .schema .JsonSchema ;
7- import com .networknt .schema .JsonSchemaFactory ;
8- import com .networknt .schema .SpecVersion ;
9- import com .networknt .schema .ValidationMessage ;
10-
6+ import com .networknt .schema .AbsoluteIri ;
7+ import com .networknt .schema .Error ;
8+ import com .networknt .schema .Schema ;
9+ import com .networknt .schema .SchemaLocation ;
10+ import com .networknt .schema .SchemaRegistry ;
11+ import com .networknt .schema .dialect .DialectId ;
1112import edu .kit .datamanager .ro_crate .objectmapper .MyObjectMapper ;
1213
13- import java .net .URISyntaxException ;
14+ import java .io .IOException ;
15+ import java .io .InputStream ;
1416import java .net .URL ;
17+ import java .util .Collection ;
1518import java .util .Objects ;
16- import java .util .Set ;
1719
1820/**
1921 * Implementation of the entity validation strategy that uses json schema.
@@ -27,19 +29,22 @@ public class JsonSchemaValidation implements EntityValidationStrategy {
2729 = Objects .requireNonNull (JsonSchemaValidation .class .getClassLoader ()
2830 .getResource ("json_schemas/entity_field_structure_schema.json" ));
2931
30- private JsonSchema entitySchema ;
31- private JsonSchema entityFieldSchema ;
32+ private Schema entitySchema ;
33+ private Schema entityFieldSchema ;
3234
3335 /**
3436 * Default constructor that uses the default schemas.
3537 */
3638 public JsonSchemaValidation () {
37- JsonSchemaFactory factory = JsonSchemaFactory .getInstance (SpecVersion .VersionFlag .V201909 );
38- try {
39- this .entitySchema = factory .getSchema (entitySchemaDefault .toURI ());
40- this .entityFieldSchema = factory .getSchema (fieldSchemaDefault .toURI ());
41- } catch (URISyntaxException e ) {
42- e .printStackTrace ();
39+ SchemaRegistry factory = new SchemaRegistry .Builder ()
40+ .defaultDialectId (DialectId .DRAFT_2019_09 )
41+ .build ();
42+
43+ try (InputStream entitySchemaStream = entitySchemaDefault .openStream (); InputStream fieldSchemaStream = fieldSchemaDefault .openStream ()) {
44+ this .entitySchema = factory .getSchema (entitySchemaStream );
45+ this .entityFieldSchema = factory .getSchema (fieldSchemaStream );
46+ } catch (IOException e ) {
47+ throw new RuntimeException (e );
4348 }
4449 }
4550
@@ -50,16 +55,20 @@ public JsonSchemaValidation() {
5055 * @param fieldSchema schema for the field validation.
5156 */
5257 public JsonSchemaValidation (JsonNode entitySchema , JsonNode fieldSchema ) {
53- JsonSchemaFactory factory = JsonSchemaFactory .getInstance (SpecVersion .VersionFlag .V201909 );
58+ SchemaRegistry factory = new SchemaRegistry .Builder ()
59+ .defaultDialectId (DialectId .DRAFT_2019_09 )
60+ .build ();
5461 this .entitySchema = factory .getSchema (entitySchema );
5562 this .entityFieldSchema = factory .getSchema (fieldSchema );
5663 }
5764
5865 @ Override
5966 public boolean validateEntity (JsonNode entity ) {
60- Set <ValidationMessage > errors = this .entitySchema .validate (entity );
61- if (errors .size () != 0 ) {
62- System .err .println ("This entity does not comply to the basic RO-Crate entity structure." );
67+ Collection <Error > errors = this .entitySchema .validate (entity );
68+ if (!errors .isEmpty ()) {
69+ System .err .println (
70+ "This entity does not comply to the basic RO-Crate entity structure."
71+ );
6372 errors .forEach (error -> System .err .println (error .getMessage ()));
6473 return false ;
6574 }
@@ -68,17 +77,23 @@ public boolean validateEntity(JsonNode entity) {
6877
6978 @ Override
7079 public boolean validateFieldOfEntity (JsonNode field ) {
71- Set < ValidationMessage > errors = this .entityFieldSchema .validate (field );
80+ Collection < Error > errors = this .entityFieldSchema .validate (field );
7281 if (!errors .isEmpty ()) {
7382 ObjectMapper objectMapper = MyObjectMapper .getMapper ();
7483 System .err .println ("The property: " );
7584 try {
76- System .err .println (objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (field ));
85+ System .err .println (
86+ objectMapper
87+ .writerWithDefaultPrettyPrinter ()
88+ .writeValueAsString (field )
89+ );
7790 } catch (JsonProcessingException e ) {
7891 e .printStackTrace ();
7992 }
80- System .err .println ("does not comply with the flattened structure"
81- + " of the RO-Crate json document." );
93+ System .err .println (
94+ "does not comply with the flattened structure" +
95+ " of the RO-Crate json document."
96+ );
8297 return false ;
8398 }
8499 return true ;
0 commit comments