1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.oscal.tools.cli.core.commands;
7   
8   import gov.nist.secauto.metaschema.cli.commands.AbstractConvertSubcommand;
9   import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
10  import gov.nist.secauto.metaschema.cli.processor.command.CommandExecutionException;
11  import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
12  import gov.nist.secauto.metaschema.core.model.IBoundObject;
13  import gov.nist.secauto.metaschema.databind.IBindingContext;
14  import gov.nist.secauto.metaschema.databind.io.Format;
15  import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
16  import gov.nist.secauto.oscal.lib.OscalBindingContext;
17  
18  import org.apache.commons.cli.CommandLine;
19  import org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.Logger;
21  
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.net.URI;
26  
27  import edu.umd.cs.findbugs.annotations.NonNull;
28  
29  /**
30   * Used by implementing classes to provide an OSCAL content conversion command.
31   * <p>
32   * This executor provides user feedback about extending command being deprecated
33   * in favor of the {@link ConvertCommand}.
34   */
35  public abstract class AbstractOscalConvertCommand
36      extends AbstractConvertSubcommand {
37    private static final Logger LOGGER = LogManager.getLogger(AbstractOscalConvertCommand.class);
38  
39    /**
40     * Get the bound object class for the assembly associated with the command.
41     *
42     * @return the bound object class for the associated assembly
43     */
44    @NonNull
45    public abstract Class<? extends IBoundObject> getOscalClass();
46  
47    @Override
48    public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
49      return new CommandExecutor(callingContext, commandLine);
50    }
51  
52    private final class CommandExecutor
53        extends AbstractConversionCommandExecutor {
54  
55      private CommandExecutor(
56          @NonNull CallingContext callingContext,
57          @NonNull CommandLine commandLine) {
58        super(callingContext, commandLine);
59      }
60  
61      @Override
62      protected IBindingContext getBindingContext() {
63        return OscalBindingContext.instance();
64      }
65  
66      @Override
67      public void execute() throws CommandExecutionException {
68        LOGGER.atWarn().log("This command path is deprecated. Please use 'convert'.");
69  
70        super.execute();
71      }
72  
73      @Override
74      protected void handleConversion(URI source, Format toFormat, Writer writer, IBoundLoader loader)
75          throws FileNotFoundException, IOException {
76        Class<? extends IBoundObject> clazz = getOscalClass();
77        loader.convert(source, writer, toFormat, clazz);
78      }
79    }
80  }