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.AbstractConvertSubcommand;
009import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
010import gov.nist.secauto.metaschema.cli.processor.command.CommandExecutionException;
011import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
012import gov.nist.secauto.metaschema.core.model.IBoundObject;
013import gov.nist.secauto.metaschema.databind.IBindingContext;
014import gov.nist.secauto.metaschema.databind.io.Format;
015import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
016import gov.nist.secauto.oscal.lib.OscalBindingContext;
017
018import org.apache.commons.cli.CommandLine;
019import org.apache.logging.log4j.LogManager;
020import org.apache.logging.log4j.Logger;
021
022import java.io.FileNotFoundException;
023import java.io.IOException;
024import java.io.Writer;
025import java.net.URI;
026
027import edu.umd.cs.findbugs.annotations.NonNull;
028
029/**
030 * Used by implementing classes to provide an OSCAL content conversion command.
031 * <p>
032 * This executor provides user feedback about extending command being deprecated
033 * in favor of the {@link ConvertCommand}.
034 */
035public abstract class AbstractOscalConvertCommand
036    extends AbstractConvertSubcommand {
037  private static final Logger LOGGER = LogManager.getLogger(AbstractOscalConvertCommand.class);
038
039  /**
040   * Get the bound object class for the assembly associated with the command.
041   *
042   * @return the bound object class for the associated assembly
043   */
044  @NonNull
045  public abstract Class<? extends IBoundObject> getOscalClass();
046
047  @Override
048  public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
049    return new CommandExecutor(callingContext, commandLine);
050  }
051
052  private final class CommandExecutor
053      extends AbstractConversionCommandExecutor {
054
055    private CommandExecutor(
056        @NonNull CallingContext callingContext,
057        @NonNull CommandLine commandLine) {
058      super(callingContext, commandLine);
059    }
060
061    @Override
062    protected IBindingContext getBindingContext() {
063      return OscalBindingContext.instance();
064    }
065
066    @Override
067    public void execute() throws CommandExecutionException {
068      LOGGER.atWarn().log("This command path is deprecated. Please use 'convert'.");
069
070      super.execute();
071    }
072
073    @Override
074    protected void handleConversion(URI source, Format toFormat, Writer writer, IBoundLoader loader)
075        throws FileNotFoundException, IOException {
076      Class<? extends IBoundObject> clazz = getOscalClass();
077      loader.convert(source, writer, toFormat, clazz);
078    }
079  }
080}