001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.oscal.tools.cli.core.commands;
007
008import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
009import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
010import gov.nist.secauto.metaschema.core.model.util.JsonUtil;
011import gov.nist.secauto.metaschema.core.model.util.XmlUtil;
012import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator;
013import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator;
014import gov.nist.secauto.metaschema.core.util.CollectionUtil;
015import gov.nist.secauto.metaschema.core.util.ObjectUtils;
016import gov.nist.secauto.oscal.lib.OscalBindingContext;
017
018import org.apache.commons.cli.CommandLine;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.LinkedList;
023import java.util.List;
024
025import javax.xml.transform.Source;
026
027import edu.umd.cs.findbugs.annotations.NonNull;
028
029/**
030 * A CLI command that supports validating that an OSCAL instance is valid.
031 */
032public class ValidateCommand
033    extends AbstractOscalValidationCommand {
034  @Override
035  public String getDescription() {
036    return "Check that the specified OSCAL instance is well-formed and valid to an OSCAL model.";
037  }
038
039  @Override
040  public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
041    return new AbstractOscalValidationCommand.OscalValidationCommandExecutor(callingContext, commandLine);
042  }
043
044  @Override
045  @NonNull
046  public XmlSchemaContentValidator getOscalXmlSchemas() throws IOException {
047    List<Source> retval = new LinkedList<>();
048    retval.add(
049        XmlUtil.getStreamSource(ObjectUtils.requireNonNull(
050            OscalBindingContext.class.getResource("/schema/xml/oscal-complete_schema.xsd"))));
051    return new XmlSchemaContentValidator(CollectionUtil.unmodifiableList(retval));
052  }
053
054  @Override
055  @NonNull
056  public JsonSchemaContentValidator getOscalJsonSchema() throws IOException {
057    try (InputStream is = ObjectUtils.requireNonNull(
058        OscalBindingContext.class.getResourceAsStream("/schema/json/oscal-complete_schema.json"))) {
059      return new JsonSchemaContentValidator(JsonUtil.toJsonObject(is));
060    }
061  }
062}