IContentValidator.java

  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. package gov.nist.secauto.metaschema.core.model.validation;

  27. import gov.nist.secauto.metaschema.core.model.util.JsonUtil;
  28. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  29. import org.json.JSONObject;
  30. import org.xml.sax.SAXException;

  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.net.URI;
  34. import java.net.URISyntaxException;
  35. import java.net.URL;
  36. import java.nio.file.Files;
  37. import java.nio.file.Path;
  38. import java.util.List;

  39. import javax.xml.transform.Source;

  40. import edu.umd.cs.findbugs.annotations.NonNull;

  41. /**
  42.  * A common interface for validation of Metaschema module-based content.
  43.  */
  44. public interface IContentValidator {
  45.   /**
  46.    * Validate the resource at provided {@code path}.
  47.    *
  48.    * @param path
  49.    *          the resource to validate
  50.    * @return the result of the validation
  51.    * @throws IOException
  52.    *           if an error occurred while performing validation
  53.    */
  54.   @NonNull
  55.   default IValidationResult validate(@NonNull Path path) throws IOException {
  56.     try (InputStream is = ObjectUtils.notNull(Files.newInputStream(path))) {
  57.       return validate(is, ObjectUtils.notNull(path.toUri()));
  58.     }
  59.   }

  60.   /**
  61.    * Validate the resource at the provided {@code url}.
  62.    *
  63.    * @param url
  64.    *          the resource to validate
  65.    * @return the result of the validation
  66.    * @throws IOException
  67.    *           if an error occurred while performing validation
  68.    * @throws URISyntaxException
  69.    *           if there is a problem with the provided {@code url}
  70.    */
  71.   @NonNull
  72.   default IValidationResult validate(@NonNull URL url) throws IOException, URISyntaxException {
  73.     return validate(ObjectUtils.notNull(url.toURI()));
  74.   }

  75.   /**
  76.    * Validate the resource identified by the provided {@code uri}.
  77.    *
  78.    * @param uri
  79.    *          the resource to validate
  80.    * @return the result of the validation
  81.    * @throws IOException
  82.    *           if an error occurred while performing validation
  83.    */
  84.   @NonNull
  85.   IValidationResult validate(@NonNull URI uri) throws IOException;

  86.   /**
  87.    * Validate the resource associated with the provided input stream {@code is}.
  88.    *
  89.    * @param is
  90.    *          an input stream to access the resource
  91.    * @param documentUri
  92.    *          the URI of the resource to validate
  93.    * @return the result of the validation
  94.    * @throws IOException
  95.    *           if an error occurred while performing validation
  96.    */
  97.   @NonNull
  98.   IValidationResult validate(@NonNull InputStream is, @NonNull URI documentUri) throws IOException;

  99.   /**
  100.    * Validate the target using the provided XML schemas.
  101.    *
  102.    * @param target
  103.    *          the target to validate
  104.    * @param schemaSources
  105.    *          the XML schema sources to validate with
  106.    * @return the validation result
  107.    * @throws IOException
  108.    *           if an error occurred while performing validation
  109.    * @throws SAXException
  110.    *           if an error occurred while parsing the XML target or schema
  111.    */
  112.   @NonNull
  113.   static IValidationResult validateWithXmlSchema(@NonNull URI target, @NonNull List<Source> schemaSources)
  114.       throws IOException, SAXException {
  115.     return new XmlSchemaContentValidator(schemaSources).validate(target);
  116.   }

  117.   /**
  118.    * Validate the target using the provided JSON schema.
  119.    *
  120.    * @param target
  121.    *          the target to validate
  122.    * @param schema
  123.    *          the JSON schema to validate with
  124.    * @return the validation result
  125.    * @throws IOException
  126.    *           if an error occurred while performing validation
  127.    * @see JsonUtil#toJsonObject(InputStream)
  128.    * @see JsonUtil#toJsonObject(java.io.Reader)
  129.    */
  130.   @NonNull
  131.   static IValidationResult validateWithJsonSchema(@NonNull URI target, @NonNull JSONObject schema)
  132.       throws IOException {
  133.     return new JsonSchemaContentValidator(schema).validate(target);
  134.   }
  135. }