1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli.commands;
7   
8   import gov.nist.secauto.metaschema.cli.commands.metapath.MetapathCommand;
9   import gov.nist.secauto.metaschema.cli.processor.command.ICommand;
10  import gov.nist.secauto.metaschema.core.model.IModule;
11  import gov.nist.secauto.metaschema.core.model.MetaschemaException;
12  import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
13  import gov.nist.secauto.metaschema.core.model.xml.ExternalConstraintsModulePostProcessor;
14  import gov.nist.secauto.metaschema.core.model.xml.ModuleLoader;
15  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
16  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
17  import gov.nist.secauto.metaschema.core.util.UriUtils;
18  
19  import org.apache.commons.cli.CommandLine;
20  import org.apache.commons.cli.Option;
21  
22  import java.io.IOException;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import edu.umd.cs.findbugs.annotations.NonNull;
29  
30  public final class MetaschemaCommands {
31    @NonNull
32    public static final List<ICommand> COMMANDS = ObjectUtils.notNull(List.of(
33        new ValidateModuleCommand(),
34        new GenerateSchemaCommand(),
35        new GenerateDiagramCommand(),
36        new ValidateContentUsingModuleCommand(),
37        new MetapathCommand()));
38  
39    @NonNull
40    public static final Option METASCHEMA_OPTION = ObjectUtils.notNull(
41        Option.builder("m")
42            .hasArg()
43            .argName("FILE_OR_URL")
44            .required()
45            .desc("metaschema resource")
46            .build());
47    @NonNull
48    public static final Option OVERWRITE_OPTION = ObjectUtils.notNull(
49        Option.builder()
50            .longOpt("overwrite")
51            .desc("overwrite the destination if it exists")
52            .build());
53  
54    @NonNull
55    public static IModule handleModule(
56        @NonNull CommandLine commandLine,
57        @NonNull URI cwd,
58        @NonNull Collection<IConstraintSet> constraintSets) throws URISyntaxException, IOException, MetaschemaException {
59      String moduleName
60          = ObjectUtils.requireNonNull(commandLine.getOptionValue(MetaschemaCommands.METASCHEMA_OPTION));
61      URI moduleUri = UriUtils.toUri(moduleName, cwd);
62      return handleModule(moduleUri, constraintSets);
63    }
64  
65    @NonNull
66    public static IModule handleModule(
67        @NonNull URI moduleResource,
68        @NonNull Collection<IConstraintSet> constraintSets) throws IOException, MetaschemaException {
69      ExternalConstraintsModulePostProcessor postProcessor
70          = new ExternalConstraintsModulePostProcessor(constraintSets);
71  
72      ModuleLoader loader = new ModuleLoader(CollectionUtil.singletonList(postProcessor));
73  
74      // BindingModuleLoader loader
75      // = new BindingModuleLoader(new DefaultBindingContext(),
76      // CollectionUtil.singletonList(postProcessor));
77  
78      loader.allowEntityResolution();
79      return loader.load(moduleResource);
80    }
81  
82    @NonNull
83    public static URI handleResource(
84        @NonNull String location,
85        @NonNull URI cwd) throws IOException {
86      try {
87        return UriUtils.toUri(location, cwd);
88      } catch (URISyntaxException ex) {
89        IOException newEx = new IOException( // NOPMD - intentional
90            String.format("Cannot load module as '%s' is not a valid file or URL.", location));
91        newEx.addSuppressed(ex);
92        throw newEx;
93      }
94    }
95  
96    private MetaschemaCommands() {
97      // disable construction
98    }
99  }