Class MetaschemaCommands

java.lang.Object
gov.nist.secauto.metaschema.cli.commands.MetaschemaCommands

public final class MetaschemaCommands extends Object
This class provides a variety of utility methods for processing Metaschema-related commands.

These methods handle the errors produced using the CommandExecutionException, which will return an exceptional result to the command line interface (CLI) processor. This approach keeps the command implementations fairly clean and simple.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.apache.commons.cli.Option
    Used by commands to identify the source format for a content-related operation.
    static final org.apache.commons.cli.Option
    Used by commands that produce schemas to identify the schema format to produce.
    static final List<ICommand>
    A list of the Metaschema-related command pathways, for reuse in this and other CLI applications.
    static final org.apache.commons.cli.Option
    Used by commands to declare an optional Metaschema module for processing.
    static final org.apache.commons.cli.Option
    Used by commands to declare a required Metaschema module for processing.
    static final org.apache.commons.cli.Option
    Used by commands to protect existing files from being overwritten, unless this option is provided.
    static final org.apache.commons.cli.Option
    Used by commands to identify the target format for a content conversion operation.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Format
    determineSourceFormat(org.apache.commons.cli.CommandLine commandLine, org.apache.commons.cli.Option option, IBoundLoader loader, URI resource)
    Detect the source format for content identified using the provided option.
    static Format
    getFormat(org.apache.commons.cli.CommandLine commandLine, org.apache.commons.cli.Option option)
    Parse the command line options to get the selected format.
    static URI
    getResourceUri(String location, URI currentWorkingDirectory)
    For a given resource location, resolve the location into an absolute URI.
    getSchemaFormat(org.apache.commons.cli.CommandLine commandLine, org.apache.commons.cli.Option option)
    Parse the command line options to get the selected schema format.
    static Path
    handleDestination(String path, org.apache.commons.cli.CommandLine commandLine)
    Get the provided destination path as an absolute Path for the resource.
    static URI
    handleSource(String pathOrUri, URI currentWorkingDirectory)
    Get the provided source path or URI string as an absolute URI for the resource.
    static Set<gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet>
    loadConstraintSets(org.apache.commons.cli.CommandLine commandLine, org.apache.commons.cli.Option option, URI currentWorkingDirectory)
    Load a set of external Metaschema module constraints based on the provided command line option.
    static gov.nist.secauto.metaschema.core.model.IModule
    loadModule(String moduleResource, URI currentWorkingDirectory, IBindingContext bindingContext)
    Load a Metaschema module from the provided relative resource path.
    static gov.nist.secauto.metaschema.core.model.IModule
    loadModule(URI moduleResource, IBindingContext bindingContext)
    Load a Metaschema module from the provided resource path.
    static gov.nist.secauto.metaschema.core.model.IModule
    loadModule(org.apache.commons.cli.CommandLine commandLine, org.apache.commons.cli.Option option, URI currentWorkingDirectory, IBindingContext bindingContext)
    Load a Metaschema module based on the provided command line option.
    Create a new IBindingContext that is configured for dynamic compilation.
    newBindingContextWithDynamicCompilation(Set<gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet> constraintSets)
    Create a new IBindingContext that is configured for dynamic compilation and to use the provided constraints.
    static Path
    Create a temporary directory for ephemeral files that will be deleted on shutdown.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • COMMANDS

      @NonNull public static final List<ICommand> COMMANDS
      A list of the Metaschema-related command pathways, for reuse in this and other CLI applications.
    • METASCHEMA_REQUIRED_OPTION

      @NonNull public static final org.apache.commons.cli.Option METASCHEMA_REQUIRED_OPTION
      Used by commands to declare a required Metaschema module for processing.
      Since:
      2.0.0
    • METASCHEMA_OPTIONAL_OPTION

      @NonNull public static final org.apache.commons.cli.Option METASCHEMA_OPTIONAL_OPTION
      Used by commands to declare an optional Metaschema module for processing.
      Since:
      2.0.0
    • OVERWRITE_OPTION

      @NonNull public static final org.apache.commons.cli.Option OVERWRITE_OPTION
      Used by commands to protect existing files from being overwritten, unless this option is provided.
    • TO_OPTION

      @NonNull public static final org.apache.commons.cli.Option TO_OPTION
      Used by commands to identify the target format for a content conversion operation.
      Since:
      2.0.0
    • AS_FORMAT_OPTION

      @NonNull public static final org.apache.commons.cli.Option AS_FORMAT_OPTION
      Used by commands to identify the source format for a content-related operation.
      Since:
      2.0.0
    • AS_SCHEMA_FORMAT_OPTION

      @NonNull public static final org.apache.commons.cli.Option AS_SCHEMA_FORMAT_OPTION
      Used by commands that produce schemas to identify the schema format to produce.
      Since:
      2.0.0
  • Method Details

    • handleSource

      @NonNull public static URI handleSource(@NonNull String pathOrUri, @NonNull URI currentWorkingDirectory) throws CommandExecutionException
      Get the provided source path or URI string as an absolute URI for the resource.
      Parameters:
      pathOrUri - the resource
      currentWorkingDirectory - the current working directory the URI will be resolved against to ensure it is absolute
      Returns:
      the absolute URI for the resource
      Throws:
      CommandExecutionException - if the resulting URI is not a well-formed URI
      Since:
      2.0.0
    • handleDestination

      public static Path handleDestination(@NonNull String path, @NonNull org.apache.commons.cli.CommandLine commandLine) throws CommandExecutionException
      Get the provided destination path as an absolute Path for the resource.

      This method checks if the path exists and if so, if the overwrite option is set. The method also ensures that the parent directory is created, if it doesn't already exist.

      Parameters:
      path - the resource
      commandLine - the provided command line argument information
      Returns:
      the absolute URI for the resource
      Throws:
      CommandExecutionException - if the path exists and cannot be overwritten or is not writable
      Since:
      2.0.0
    • getFormat

      @NonNull public static Format getFormat(@NonNull org.apache.commons.cli.CommandLine commandLine, @NonNull org.apache.commons.cli.Option option) throws CommandExecutionException
      Parse the command line options to get the selected format.
      Parameters:
      commandLine - the provided command line argument information
      option - the option specifying the format, which must be present on the command line
      Returns:
      the format
      Throws:
      CommandExecutionException - if the format option was not provided or was an invalid choice
      Since:
      2.0.0
    • getSchemaFormat

      @NonNull public static ISchemaGenerator.SchemaFormat getSchemaFormat(@NonNull org.apache.commons.cli.CommandLine commandLine, @NonNull org.apache.commons.cli.Option option) throws CommandExecutionException
      Parse the command line options to get the selected schema format.
      Parameters:
      commandLine - the provided command line argument information
      option - the option specifying the format, which must be present on the command line
      Returns:
      the format
      Throws:
      CommandExecutionException - if the format option was not provided or was an invalid choice
      Since:
      2.0.0
    • determineSourceFormat

      @NonNull public static Format determineSourceFormat(@NonNull org.apache.commons.cli.CommandLine commandLine, @NonNull org.apache.commons.cli.Option option, @NonNull IBoundLoader loader, @NonNull URI resource) throws CommandExecutionException
      Detect the source format for content identified using the provided option.

      This method will first check if the source format is explicitly declared on the command line. If so, this format will be returned.

      If not, then the content will be analyzed to determine the format.

      Parameters:
      commandLine - the provided command line argument information
      option - the option specifying the format, which must be present on the command line
      loader - the content loader to use to load the content instance
      resource - the resource to load
      Returns:
      the identified content format
      Throws:
      CommandExecutionException - if an error occurred while determining the source format
      Since:
      2.0.0
    • loadModule

      @NonNull public static gov.nist.secauto.metaschema.core.model.IModule loadModule(@NonNull org.apache.commons.cli.CommandLine commandLine, @NonNull org.apache.commons.cli.Option option, @NonNull URI currentWorkingDirectory, @NonNull IBindingContext bindingContext) throws CommandExecutionException
      Load a Metaschema module based on the provided command line option.
      Parameters:
      commandLine - the provided command line argument information
      option - the option specifying the module to load, which must be present on the command line
      currentWorkingDirectory - the URI of the current working directory
      bindingContext - the context used to access Metaschema module information based on Java class bindings
      Returns:
      the loaded module
      Throws:
      CommandExecutionException - if an error occurred while loading the module
      Since:
      2.0.0
    • loadModule

      @NonNull public static gov.nist.secauto.metaschema.core.model.IModule loadModule(@NonNull String moduleResource, @NonNull URI currentWorkingDirectory, @NonNull IBindingContext bindingContext) throws CommandExecutionException
      Load a Metaschema module from the provided relative resource path.

      This method will resolve the provided resource against the current working directory to create an absolute URI.

      Parameters:
      moduleResource - the relative path to the module resource to load
      currentWorkingDirectory - the URI of the current working directory
      bindingContext - the context used to access Metaschema module information based on Java class bindings
      Returns:
      the loaded module
      Throws:
      CommandExecutionException - if an error occurred while loading the module
      Since:
      2.0.0
    • loadModule

      @NonNull public static gov.nist.secauto.metaschema.core.model.IModule loadModule(@NonNull URI moduleResource, @NonNull IBindingContext bindingContext) throws CommandExecutionException
      Load a Metaschema module from the provided resource path.
      Parameters:
      moduleResource - the absolute path to the module resource to load
      bindingContext - the context used to access Metaschema module information based on Java class bindings
      Returns:
      the loaded module
      Throws:
      CommandExecutionException - if an error occurred while loading the module
      Since:
      2.0.0
    • getResourceUri

      @NonNull public static URI getResourceUri(@NonNull String location, @NonNull URI currentWorkingDirectory) throws URISyntaxException
      For a given resource location, resolve the location into an absolute URI.
      Parameters:
      location - the resource location
      currentWorkingDirectory - the URI of the current working directory
      Returns:
      the resolved URI
      Throws:
      URISyntaxException - if the location is not a valid URI
    • loadConstraintSets

      @NonNull public static Set<gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet> loadConstraintSets(@NonNull org.apache.commons.cli.CommandLine commandLine, @NonNull org.apache.commons.cli.Option option, @NonNull URI currentWorkingDirectory) throws CommandExecutionException
      Load a set of external Metaschema module constraints based on the provided command line option.
      Parameters:
      commandLine - the provided command line argument information
      option - the option specifying the constraints to load, which must be present on the command line
      currentWorkingDirectory - the URI of the current working directory
      Returns:
      the set of loaded constraints
      Throws:
      CommandExecutionException - if an error occurred while loading the module
      Since:
      2.0.0
    • newTempDir

      @NonNull public static Path newTempDir() throws IOException
      Create a temporary directory for ephemeral files that will be deleted on shutdown.
      Returns:
      the temp directory path
      Throws:
      IOException - if an error occurred while creating the temporary directory
    • newBindingContextWithDynamicCompilation

      Create a new IBindingContext that is configured for dynamic compilation.
      Returns:
      the binding context
      Throws:
      CommandExecutionException - if an error occurred while creating the binding context
      Since:
      2.0.0
    • newBindingContextWithDynamicCompilation

      @NonNull public static IBindingContext newBindingContextWithDynamicCompilation(@NonNull Set<gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet> constraintSets) throws CommandExecutionException
      Create a new IBindingContext that is configured for dynamic compilation and to use the provided constraints.
      Parameters:
      constraintSets - the Metaschema module constraints to dynamicly bind to loaded modules
      Returns:
      the binding context
      Throws:
      CommandExecutionException - if an error occurred while creating the binding context
      Since:
      2.0.0