1 /*
2 * SPDX-FileCopyrightText: none
3 * SPDX-License-Identifier: CC0-1.0
4 */
5
6 package dev.metaschema.core.model.validation;
7
8 import org.json.JSONObject;
9 import org.xml.sax.SAXException;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.util.List;
19
20 import javax.xml.transform.Source;
21
22 import dev.metaschema.core.model.util.JsonUtil;
23 import dev.metaschema.core.util.ObjectUtils;
24 import edu.umd.cs.findbugs.annotations.NonNull;
25
26 /**
27 * A common interface for validation of Metaschema module-based content.
28 */
29 public interface IContentValidator {
30 /**
31 * Validate the resource at provided {@code path}.
32 *
33 * @param path
34 * the resource to validate
35 * @return the result of the validation
36 * @throws IOException
37 * if an error occurred while performing validation
38 */
39 @NonNull
40 default IValidationResult validate(@NonNull Path path) throws IOException {
41 try (InputStream is = ObjectUtils.notNull(Files.newInputStream(path))) {
42 return validate(is, ObjectUtils.notNull(path.toUri()));
43 }
44 }
45
46 /**
47 * Validate the resource at the provided {@code url}.
48 *
49 * @param url
50 * the resource to validate
51 * @return the result of the validation
52 * @throws IOException
53 * if an error occurred while performing validation
54 * @throws URISyntaxException
55 * if there is a problem with the provided {@code url}
56 */
57 @NonNull
58 default IValidationResult validate(@NonNull URL url) throws IOException, URISyntaxException {
59 return validate(ObjectUtils.notNull(url.toURI()));
60 }
61
62 /**
63 * Validate the resource identified by the provided {@code uri}.
64 *
65 * @param uri
66 * the resource to validate
67 * @return the result of the validation
68 * @throws IOException
69 * if an error occurred while performing validation
70 */
71 @NonNull
72 IValidationResult validate(@NonNull URI uri) throws IOException;
73
74 /**
75 * Validate the resource associated with the provided input stream {@code is}.
76 *
77 * @param is
78 * an input stream to access the resource
79 * @param documentUri
80 * the URI of the resource to validate
81 * @return the result of the validation
82 * @throws IOException
83 * if an error occurred while performing validation
84 */
85 @NonNull
86 IValidationResult validate(@NonNull InputStream is, @NonNull URI documentUri) throws IOException;
87
88 /**
89 * Validate the target using the provided XML schemas.
90 *
91 * @param target
92 * the target to validate
93 * @param schemaSources
94 * the XML schema sources to validate with
95 * @return the validation result
96 * @throws IOException
97 * if an error occurred while performing validation
98 * @throws SAXException
99 * if an error occurred while parsing the XML target or schema
100 */
101 @NonNull
102 static IValidationResult validateWithXmlSchema(@NonNull URI target, @NonNull List<Source> schemaSources)
103 throws IOException, SAXException {
104 return new XmlSchemaContentValidator(schemaSources).validate(target);
105 }
106
107 /**
108 * Validate the target using the provided JSON schema.
109 *
110 * @param target
111 * the target to validate
112 * @param schema
113 * the JSON schema to validate with
114 * @return the validation result
115 * @throws IOException
116 * if an error occurred while performing validation
117 * @see JsonUtil#toJsonObject(InputStream)
118 * @see JsonUtil#toJsonObject(java.io.Reader)
119 */
120 @NonNull
121 static IValidationResult validateWithJsonSchema(@NonNull URI target, @NonNull JSONObject schema)
122 throws IOException {
123 return new JsonSchemaContentValidator(schema).validate(target);
124 }
125 }