1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
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 package it.polito.verigraph.service;
12 import java.io.IOException;
15 import com.fasterxml.jackson.databind.JsonNode;
16 import com.fasterxml.jackson.databind.node.ObjectNode;
17 import com.github.fge.jackson.JsonLoader;
18 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
19 import com.github.fge.jsonschema.core.report.ProcessingMessage;
20 import com.github.fge.jsonschema.core.report.ProcessingReport;
21 import com.github.fge.jsonschema.main.JsonSchema;
22 import com.github.fge.jsonschema.main.JsonSchemaFactory;
24 public class ValidationUtils {
26 public static final String JSON_V4_SCHEMA_IDENTIFIER= "http://json-schema.org/draft-04/schema#";
27 public static final String JSON_SCHEMA_IDENTIFIER_ELEMENT= "$schema";
29 public static JsonNode getJsonNode(String jsonText) throws IOException {
30 return JsonLoader.fromString(jsonText);
31 } // getJsonNode(text) ends
33 public static JsonNode getJsonNode(File jsonFile) throws IOException {
34 return JsonLoader.fromFile(jsonFile);
35 } // getJsonNode(File) ends
37 public static JsonNode getJsonNode(URL url) throws IOException {
38 return JsonLoader.fromURL(url);
39 } // getJsonNode(URL) ends
41 public static JsonNode getJsonNodeFromResource(String resource) throws IOException {
42 return JsonLoader.fromResource(resource);
43 } // getJsonNode(Resource) ends
45 public static JsonSchema getSchemaNode(String schemaText) throws IOException, ProcessingException {
46 final JsonNode schemaNode = getJsonNode(schemaText);
47 return _getSchemaNode(schemaNode);
48 } // getSchemaNode(text) ends
50 public static JsonSchema getSchemaNode(File schemaFile) throws IOException, ProcessingException {
51 final JsonNode schemaNode = getJsonNode(schemaFile);
52 return _getSchemaNode(schemaNode);
53 } // getSchemaNode(File) ends
55 public static JsonSchema getSchemaNode(URL schemaFile) throws IOException, ProcessingException {
56 final JsonNode schemaNode = getJsonNode(schemaFile);
57 return _getSchemaNode(schemaNode);
58 } // getSchemaNode(URL) ends
60 public static JsonSchema getSchemaNodeFromResource(String resource) throws IOException, ProcessingException {
61 final JsonNode schemaNode = getJsonNodeFromResource(resource);
62 return _getSchemaNode(schemaNode);
63 } // getSchemaNode() ends
65 public static void validateJson(JsonSchema jsonSchemaNode, JsonNode jsonNode) throws ProcessingException {
66 ProcessingReport report = jsonSchemaNode.validate(jsonNode);
67 if (!report.isSuccess()) {
68 for (ProcessingMessage processingMessage : report) {
69 throw new ProcessingException(processingMessage);
72 } // validateJson(Node) ends
74 public static boolean isJsonValid(JsonSchema jsonSchemaNode, JsonNode jsonNode) throws ProcessingException {
75 ProcessingReport report = jsonSchemaNode.validate(jsonNode);
76 return report.isSuccess();
77 } // validateJson(Node) ends
79 public static boolean isJsonValid(String schemaText, String jsonText) throws ProcessingException, IOException {
80 final JsonSchema schemaNode = getSchemaNode(schemaText);
81 final JsonNode jsonNode = getJsonNode(jsonText);
82 return isJsonValid(schemaNode, jsonNode);
83 } // validateJson(Node) ends
85 public static boolean isJsonValid(File schemaFile, File jsonFile) throws ProcessingException, IOException {
86 final JsonSchema schemaNode = getSchemaNode(schemaFile);
87 final JsonNode jsonNode = getJsonNode(jsonFile);
88 return isJsonValid(schemaNode, jsonNode);
89 } // validateJson(Node) ends
91 public static boolean isJsonValid(URL schemaURL, URL jsonURL) throws ProcessingException, IOException {
92 final JsonSchema schemaNode = getSchemaNode(schemaURL);
93 final JsonNode jsonNode = getJsonNode(jsonURL);
94 return isJsonValid(schemaNode, jsonNode);
95 } // validateJson(Node) ends
97 public static void validateJson(String schemaText, String jsonText) throws IOException, ProcessingException {
98 final JsonSchema schemaNode = getSchemaNode(schemaText);
99 final JsonNode jsonNode = getJsonNode(jsonText);
100 validateJson(schemaNode, jsonNode);
101 } // validateJson(text) ends
103 public static void validateJson(File schemaFile, File jsonFile) throws IOException, ProcessingException {
104 final JsonSchema schemaNode = getSchemaNode(schemaFile);
105 final JsonNode jsonNode = getJsonNode(jsonFile);
106 validateJson(schemaNode, jsonNode);
107 } // validateJson(File) ends
109 public static void validateJson(URL schemaDocument, URL jsonDocument) throws IOException, ProcessingException {
110 final JsonSchema schemaNode = getSchemaNode(schemaDocument);
111 final JsonNode jsonNode = getJsonNode(jsonDocument);
112 validateJson(schemaNode, jsonNode);
113 } // validateJson(URL) ends
115 public static void validateJsonResource(String schemaResource, String jsonResource)throws IOException,
116 ProcessingException {
117 final JsonSchema schemaNode = getSchemaNode(schemaResource);
118 final JsonNode jsonNode = getJsonNodeFromResource(jsonResource);
119 validateJson(schemaNode, jsonNode);
120 } // validateJsonResource() ends
122 private static JsonSchema _getSchemaNode(JsonNode jsonNode) throws ProcessingException {
123 final JsonNode schemaIdentifier = jsonNode.get(JSON_SCHEMA_IDENTIFIER_ELEMENT);
124 if (null == schemaIdentifier) {
125 ((ObjectNode) jsonNode).put(JSON_SCHEMA_IDENTIFIER_ELEMENT, JSON_V4_SCHEMA_IDENTIFIER);
128 final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
129 return factory.getJsonSchema(jsonNode);
130 } // _getSchemaNode() ends