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 *******************************************************************************/
10 package it.polito.escape.verify.service;
13 import java.io.IOException;
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;
25 public class ValidationUtils {
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";
30 public static JsonNode getJsonNode(String jsonText) throws IOException {
31 return JsonLoader.fromString(jsonText);
32 } // getJsonNode(text) ends
34 public static JsonNode getJsonNode(File jsonFile) throws IOException {
35 return JsonLoader.fromFile(jsonFile);
36 } // getJsonNode(File) ends
38 public static JsonNode getJsonNode(URL url) throws IOException {
39 return JsonLoader.fromURL(url);
40 } // getJsonNode(URL) ends
42 public static JsonNode getJsonNodeFromResource(String resource) throws IOException {
43 return JsonLoader.fromResource(resource);
44 } // getJsonNode(Resource) ends
46 public static JsonSchema getSchemaNode(String schemaText) throws IOException, ProcessingException {
47 final JsonNode schemaNode = getJsonNode(schemaText);
48 return _getSchemaNode(schemaNode);
49 } // getSchemaNode(text) ends
51 public static JsonSchema getSchemaNode(File schemaFile) throws IOException, ProcessingException {
52 final JsonNode schemaNode = getJsonNode(schemaFile);
53 return _getSchemaNode(schemaNode);
54 } // getSchemaNode(File) ends
56 public static JsonSchema getSchemaNode(URL schemaFile) throws IOException, ProcessingException {
57 final JsonNode schemaNode = getJsonNode(schemaFile);
58 return _getSchemaNode(schemaNode);
59 } // getSchemaNode(URL) ends
61 public static JsonSchema getSchemaNodeFromResource(String resource) throws IOException, ProcessingException {
62 final JsonNode schemaNode = getJsonNodeFromResource(resource);
63 return _getSchemaNode(schemaNode);
64 } // getSchemaNode() ends
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);
73 } // validateJson(Node) ends
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
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
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
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
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
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
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
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
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);
129 final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
130 return factory.getJsonSchema(jsonNode);
131 } // _getSchemaNode() ends