Merge "Add verigraph code base"
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / service / ValidationUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.escape.verify.service;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URL;
15
16 import com.fasterxml.jackson.databind.JsonNode;
17 import com.fasterxml.jackson.databind.node.ObjectNode;
18 import com.github.fge.jackson.JsonLoader;
19 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
20 import com.github.fge.jsonschema.core.report.ProcessingMessage;
21 import com.github.fge.jsonschema.core.report.ProcessingReport;
22 import com.github.fge.jsonschema.main.JsonSchema;
23 import com.github.fge.jsonschema.main.JsonSchemaFactory;
24
25 public class ValidationUtils {
26
27         public static final String      JSON_V4_SCHEMA_IDENTIFIER               = "http://json-schema.org/draft-04/schema#";
28         public static final String      JSON_SCHEMA_IDENTIFIER_ELEMENT  = "$schema";
29
30         public static JsonNode getJsonNode(String jsonText) throws IOException {
31                 return JsonLoader.fromString(jsonText);
32         } // getJsonNode(text) ends
33
34         public static JsonNode getJsonNode(File jsonFile) throws IOException {
35                 return JsonLoader.fromFile(jsonFile);
36         } // getJsonNode(File) ends
37
38         public static JsonNode getJsonNode(URL url) throws IOException {
39                 return JsonLoader.fromURL(url);
40         } // getJsonNode(URL) ends
41
42         public static JsonNode getJsonNodeFromResource(String resource) throws IOException {
43                 return JsonLoader.fromResource(resource);
44         } // getJsonNode(Resource) ends
45
46         public static JsonSchema getSchemaNode(String schemaText) throws IOException, ProcessingException {
47                 final JsonNode schemaNode = getJsonNode(schemaText);
48                 return _getSchemaNode(schemaNode);
49         } // getSchemaNode(text) ends
50
51         public static JsonSchema getSchemaNode(File schemaFile) throws IOException, ProcessingException {
52                 final JsonNode schemaNode = getJsonNode(schemaFile);
53                 return _getSchemaNode(schemaNode);
54         } // getSchemaNode(File) ends
55
56         public static JsonSchema getSchemaNode(URL schemaFile) throws IOException, ProcessingException {
57                 final JsonNode schemaNode = getJsonNode(schemaFile);
58                 return _getSchemaNode(schemaNode);
59         } // getSchemaNode(URL) ends
60
61         public static JsonSchema getSchemaNodeFromResource(String resource) throws IOException, ProcessingException {
62                 final JsonNode schemaNode = getJsonNodeFromResource(resource);
63                 return _getSchemaNode(schemaNode);
64         } // getSchemaNode() ends
65
66         public static void validateJson(JsonSchema jsonSchemaNode, JsonNode jsonNode) throws ProcessingException {
67                 ProcessingReport report = jsonSchemaNode.validate(jsonNode);
68                 if (!report.isSuccess()) {
69                         for (ProcessingMessage processingMessage : report) {
70                                 throw new ProcessingException(processingMessage);
71                         }
72                 }
73         } // validateJson(Node) ends
74
75         public static boolean isJsonValid(JsonSchema jsonSchemaNode, JsonNode jsonNode) throws ProcessingException {
76                 ProcessingReport report = jsonSchemaNode.validate(jsonNode);
77                 return report.isSuccess();
78         } // validateJson(Node) ends
79
80         public static boolean isJsonValid(String schemaText, String jsonText) throws ProcessingException, IOException {
81                 final JsonSchema schemaNode = getSchemaNode(schemaText);
82                 final JsonNode jsonNode = getJsonNode(jsonText);
83                 return isJsonValid(schemaNode, jsonNode);
84         } // validateJson(Node) ends
85
86         public static boolean isJsonValid(File schemaFile, File jsonFile) throws ProcessingException, IOException {
87                 final JsonSchema schemaNode = getSchemaNode(schemaFile);
88                 final JsonNode jsonNode = getJsonNode(jsonFile);
89                 return isJsonValid(schemaNode, jsonNode);
90         } // validateJson(Node) ends
91
92         public static boolean isJsonValid(URL schemaURL, URL jsonURL) throws ProcessingException, IOException {
93                 final JsonSchema schemaNode = getSchemaNode(schemaURL);
94                 final JsonNode jsonNode = getJsonNode(jsonURL);
95                 return isJsonValid(schemaNode, jsonNode);
96         } // validateJson(Node) ends
97
98         public static void validateJson(String schemaText, String jsonText) throws IOException, ProcessingException {
99                 final JsonSchema schemaNode = getSchemaNode(schemaText);
100                 final JsonNode jsonNode = getJsonNode(jsonText);
101                 validateJson(schemaNode, jsonNode);
102         } // validateJson(text) ends
103
104         public static void validateJson(File schemaFile, File jsonFile) throws IOException, ProcessingException {
105                 final JsonSchema schemaNode = getSchemaNode(schemaFile);
106                 final JsonNode jsonNode = getJsonNode(jsonFile);
107                 validateJson(schemaNode, jsonNode);
108         } // validateJson(File) ends
109
110         public static void validateJson(URL schemaDocument, URL jsonDocument) throws IOException, ProcessingException {
111                 final JsonSchema schemaNode = getSchemaNode(schemaDocument);
112                 final JsonNode jsonNode = getJsonNode(jsonDocument);
113                 validateJson(schemaNode, jsonNode);
114         } // validateJson(URL) ends
115
116         public static void validateJsonResource(String schemaResource, String jsonResource)     throws IOException,
117                                                                                                                                                                                 ProcessingException {
118                 final JsonSchema schemaNode = getSchemaNode(schemaResource);
119                 final JsonNode jsonNode = getJsonNodeFromResource(jsonResource);
120                 validateJson(schemaNode, jsonNode);
121         } // validateJsonResource() ends
122
123         private static JsonSchema _getSchemaNode(JsonNode jsonNode) throws ProcessingException {
124                 final JsonNode schemaIdentifier = jsonNode.get(JSON_SCHEMA_IDENTIFIER_ELEMENT);
125                 if (null == schemaIdentifier) {
126                         ((ObjectNode) jsonNode).put(JSON_SCHEMA_IDENTIFIER_ELEMENT, JSON_V4_SCHEMA_IDENTIFIER);
127                 }
128
129                 final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
130                 return factory.getJsonSchema(jsonNode);
131         } // _getSchemaNode() ends
132 }