View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.model.validation;
28  
29  import gov.nist.secauto.metaschema.core.model.util.JsonUtil;
30  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
31  
32  import org.json.JSONObject;
33  import org.xml.sax.SAXException;
34  
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.net.URI;
38  import java.net.URISyntaxException;
39  import java.net.URL;
40  import java.nio.file.Files;
41  import java.nio.file.Path;
42  import java.util.List;
43  
44  import javax.xml.transform.Source;
45  
46  import edu.umd.cs.findbugs.annotations.NonNull;
47  
48  /**
49   * A common interface for validation of Metaschema module-based content.
50   */
51  public interface IContentValidator {
52    /**
53     * Validate the resource at provided {@code path}.
54     *
55     * @param path
56     *          the resource to validate
57     * @return the result of the validation
58     * @throws IOException
59     *           if an error occurred while performing validation
60     */
61    @NonNull
62    default IValidationResult validate(@NonNull Path path) throws IOException {
63      try (InputStream is = ObjectUtils.notNull(Files.newInputStream(path))) {
64        return validate(is, ObjectUtils.notNull(path.toUri()));
65      }
66    }
67  
68    /**
69     * Validate the resource at the provided {@code url}.
70     *
71     * @param url
72     *          the resource to validate
73     * @return the result of the validation
74     * @throws IOException
75     *           if an error occurred while performing validation
76     * @throws URISyntaxException
77     *           if there is a problem with the provided {@code url}
78     */
79    @NonNull
80    default IValidationResult validate(@NonNull URL url) throws IOException, URISyntaxException {
81      return validate(ObjectUtils.notNull(url.toURI()));
82    }
83  
84    /**
85     * Validate the resource identified by the provided {@code uri}.
86     *
87     * @param uri
88     *          the resource to validate
89     * @return the result of the validation
90     * @throws IOException
91     *           if an error occurred while performing validation
92     */
93    @NonNull
94    IValidationResult validate(@NonNull URI uri) throws IOException;
95  
96    /**
97     * Validate the resource associated with the provided input stream {@code is}.
98     *
99     * @param is
100    *          an input stream to access the resource
101    * @param documentUri
102    *          the URI of the resource to validate
103    * @return the result of the validation
104    * @throws IOException
105    *           if an error occurred while performing validation
106    */
107   @NonNull
108   IValidationResult validate(@NonNull InputStream is, @NonNull URI documentUri) throws IOException;
109 
110   /**
111    * Validate the target using the provided XML schemas.
112    *
113    * @param target
114    *          the target to validate
115    * @param schemaSources
116    *          the XML schema sources to validate with
117    * @return the validation result
118    * @throws IOException
119    *           if an error occurred while performing validation
120    * @throws SAXException
121    *           if an error occurred while parsing the XML target or schema
122    */
123   @NonNull
124   static IValidationResult validateWithXmlSchema(@NonNull URI target, @NonNull List<Source> schemaSources)
125       throws IOException, SAXException {
126     return new XmlSchemaContentValidator(schemaSources).validate(target);
127   }
128 
129   /**
130    * Validate the target using the provided JSON schema.
131    *
132    * @param target
133    *          the target to validate
134    * @param schema
135    *          the JSON schema to validate with
136    * @return the validation result
137    * @throws IOException
138    *           if an error occurred while performing validation
139    * @see JsonUtil#toJsonObject(InputStream)
140    * @see JsonUtil#toJsonObject(java.io.Reader)
141    */
142   @NonNull
143   static IValidationResult validateWithJsonSchema(@NonNull URI target, @NonNull JSONObject schema)
144       throws IOException {
145     return new JsonSchemaContentValidator(schema).validate(target);
146   }
147 }