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.ExitStatus;
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  public abstract class AbstractOscalConvertCommand
30      extends AbstractConvertSubcommand {
31    private static final Logger LOGGER = LogManager.getLogger(AbstractOscalConvertCommand.class);
32  
33    @NonNull
34    public abstract Class<? extends IBoundObject> getOscalClass();
35  
36    @Override
37    public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
38      return new OscalCommandExecutor(callingContext, commandLine);
39    }
40  
41    private final class OscalCommandExecutor
42        extends AbstractConversionCommandExecutor {
43  
44      private OscalCommandExecutor(
45          @NonNull CallingContext callingContext,
46          @NonNull CommandLine commandLine) {
47        super(callingContext, commandLine);
48      }
49  
50      @Override
51      protected IBindingContext getBindingContext() {
52        return OscalBindingContext.instance();
53      }
54  
55      @Override
56      public ExitStatus execute() {
57        LOGGER.atWarn().log("This command path is deprecated. Please use 'convert'.");
58  
59        return super.execute();
60      }
61  
62      @Override
63      protected void handleConversion(URI source, Format toFormat, Writer writer, IBoundLoader loader)
64          throws FileNotFoundException, IOException {
65        Class<? extends IBoundObject> clazz = getOscalClass();
66        loader.convert(source, writer, toFormat, clazz);
67      }
68    }
69  }