001/* 002 * Portions of this software was developed by employees of the National Institute 003 * of Standards and Technology (NIST), an agency of the Federal Government and is 004 * being made available as a public service. Pursuant to title 17 United States 005 * Code Section 105, works of NIST employees are not subject to copyright 006 * protection in the United States. This software may be subject to foreign 007 * copyright. Permission in the United States and in foreign countries, to the 008 * extent that NIST may hold copyright, to use, copy, modify, create derivative 009 * works, and distribute this software and its documentation without fee is hereby 010 * granted on a non-exclusive basis, provided that this notice and disclaimer 011 * of warranty appears in all copies. 012 * 013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER 014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY 015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF 016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM 017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE 018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT 019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, 020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, 021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, 022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR 023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT 024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. 025 */ 026 027package gov.nist.secauto.metaschema.core.model.validation; 028 029import gov.nist.secauto.metaschema.core.model.util.JsonUtil; 030import gov.nist.secauto.metaschema.core.util.ObjectUtils; 031 032import org.json.JSONObject; 033import org.xml.sax.SAXException; 034 035import java.io.IOException; 036import java.io.InputStream; 037import java.net.URI; 038import java.net.URISyntaxException; 039import java.net.URL; 040import java.nio.file.Files; 041import java.nio.file.Path; 042import java.util.List; 043 044import javax.xml.transform.Source; 045 046import edu.umd.cs.findbugs.annotations.NonNull; 047 048/** 049 * A common interface for validation of Metaschema module-based content. 050 */ 051public interface IContentValidator { 052 /** 053 * Validate the resource at provided {@code path}. 054 * 055 * @param path 056 * the resource to validate 057 * @return the result of the validation 058 * @throws IOException 059 * if an error occurred while performing validation 060 */ 061 @NonNull 062 default IValidationResult validate(@NonNull Path path) throws IOException { 063 try (InputStream is = ObjectUtils.notNull(Files.newInputStream(path))) { 064 return validate(is, ObjectUtils.notNull(path.toUri())); 065 } 066 } 067 068 /** 069 * Validate the resource at the provided {@code url}. 070 * 071 * @param url 072 * the resource to validate 073 * @return the result of the validation 074 * @throws IOException 075 * if an error occurred while performing validation 076 * @throws URISyntaxException 077 * if there is a problem with the provided {@code url} 078 */ 079 @NonNull 080 default IValidationResult validate(@NonNull URL url) throws IOException, URISyntaxException { 081 return validate(ObjectUtils.notNull(url.toURI())); 082 } 083 084 /** 085 * Validate the resource identified by the provided {@code uri}. 086 * 087 * @param uri 088 * the resource to validate 089 * @return the result of the validation 090 * @throws IOException 091 * if an error occurred while performing validation 092 */ 093 @NonNull 094 IValidationResult validate(@NonNull URI uri) throws IOException; 095 096 /** 097 * Validate the resource associated with the provided input stream {@code is}. 098 * 099 * @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}