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.AbstractValidateContentCommand;
9   import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
10  import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
11  import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
12  import gov.nist.secauto.metaschema.core.model.xml.ExternalConstraintsModulePostProcessor;
13  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
14  import gov.nist.secauto.metaschema.databind.IBindingContext;
15  import gov.nist.secauto.oscal.lib.OscalBindingContext;
16  
17  import org.apache.commons.cli.CommandLine;
18  import org.json.JSONObject;
19  
20  import java.io.IOException;
21  import java.net.URL;
22  import java.util.List;
23  import java.util.Set;
24  
25  import javax.xml.transform.Source;
26  
27  import edu.umd.cs.findbugs.annotations.NonNull;
28  
29  public abstract class AbstractOscalValidationCommand
30      extends AbstractValidateContentCommand {
31  
32    @NonNull
33    protected abstract List<Source> getOscalXmlSchemas() throws IOException;
34  
35    @NonNull
36    protected abstract JSONObject getOscalJsonSchema() throws IOException;
37  
38    @Override
39    public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
40      return new OscalCommandExecutor(callingContext, commandLine);
41    }
42  
43    protected class OscalCommandExecutor
44        extends AbstractValidationCommandExecutor {
45  
46      protected OscalCommandExecutor(
47          @NonNull CallingContext callingContext,
48          @NonNull CommandLine commandLine) {
49        super(callingContext, commandLine);
50      }
51  
52      @Override
53      protected IBindingContext getBindingContext(@NonNull Set<IConstraintSet> constraintSets) {
54        IBindingContext retval;
55        if (constraintSets.isEmpty()) {
56          retval = OscalBindingContext.instance();
57        } else {
58          ExternalConstraintsModulePostProcessor postProcessor
59              = new ExternalConstraintsModulePostProcessor(constraintSets);
60  
61          retval = new OscalBindingContext(CollectionUtil.singletonList(postProcessor));
62        }
63        return retval;
64      }
65  
66      @Override
67      @NonNull
68      public List<Source> getXmlSchemas(URL targetResource) throws IOException {
69        return getOscalXmlSchemas();
70      }
71  
72      @Override
73      @NonNull
74      public JSONObject getJsonSchema(JSONObject json) throws IOException {
75        return getOscalJsonSchema();
76      }
77    }
78  }