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.commands.AbstractValidateContentCommand;
009import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
010import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
011import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
012import gov.nist.secauto.metaschema.core.model.xml.ExternalConstraintsModulePostProcessor;
013import gov.nist.secauto.metaschema.core.util.CollectionUtil;
014import gov.nist.secauto.metaschema.databind.IBindingContext;
015import gov.nist.secauto.oscal.lib.OscalBindingContext;
016
017import org.apache.commons.cli.CommandLine;
018import org.json.JSONObject;
019
020import java.io.IOException;
021import java.net.URL;
022import java.util.List;
023import java.util.Set;
024
025import javax.xml.transform.Source;
026
027import edu.umd.cs.findbugs.annotations.NonNull;
028
029public abstract class AbstractOscalValidationCommand
030    extends AbstractValidateContentCommand {
031
032  @NonNull
033  protected abstract List<Source> getOscalXmlSchemas() throws IOException;
034
035  @NonNull
036  protected abstract JSONObject getOscalJsonSchema() throws IOException;
037
038  @Override
039  public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
040    return new OscalCommandExecutor(callingContext, commandLine);
041  }
042
043  protected class OscalCommandExecutor
044      extends AbstractValidationCommandExecutor {
045
046    protected OscalCommandExecutor(
047        @NonNull CallingContext callingContext,
048        @NonNull CommandLine commandLine) {
049      super(callingContext, commandLine);
050    }
051
052    @Override
053    protected IBindingContext getBindingContext(@NonNull Set<IConstraintSet> constraintSets) {
054      IBindingContext retval;
055      if (constraintSets.isEmpty()) {
056        retval = OscalBindingContext.instance();
057      } else {
058        ExternalConstraintsModulePostProcessor postProcessor
059            = new ExternalConstraintsModulePostProcessor(constraintSets);
060
061        retval = new OscalBindingContext(CollectionUtil.singletonList(postProcessor));
062      }
063      return retval;
064    }
065
066    @Override
067    @NonNull
068    public List<Source> getXmlSchemas(URL targetResource) throws IOException {
069      return getOscalXmlSchemas();
070    }
071
072    @Override
073    @NonNull
074    public JSONObject getJsonSchema(JSONObject json) throws IOException {
075      return getOscalJsonSchema();
076    }
077  }
078}