Class MetaschemaCommands
java.lang.Object
gov.nist.secauto.metaschema.cli.commands.MetaschemaCommands
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
Modifier and TypeFieldDescriptionstatic 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.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 TypeMethodDescriptionstatic 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 absolutePath
for the resource.static URI
handleSource
(String pathOrUri, URI currentWorkingDirectory) Get the provided source path or URI string as an absoluteURI
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.static IBindingContext
Create a newIBindingContext
that is configured for dynamic compilation.static IBindingContext
newBindingContextWithDynamicCompilation
(Set<gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet> constraintSets) Create a newIBindingContext
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.
-
Field Details
-
COMMANDS
A list of the Metaschema-related command pathways, for reuse in this and other CLI applications. -
METASCHEMA_REQUIRED_OPTION
Used by commands to declare a required Metaschema module for processing.- Since:
- 2.0.0
-
METASCHEMA_OPTIONAL_OPTION
Used by commands to declare an optional Metaschema module for processing.- Since:
- 2.0.0
-
OVERWRITE_OPTION
Used by commands to protect existing files from being overwritten, unless this option is provided. -
TO_OPTION
Used by commands to identify the target format for a content conversion operation.- Since:
- 2.0.0
-
AS_FORMAT_OPTION
Used by commands to identify the source format for a content-related operation.- Since:
- 2.0.0
-
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 absoluteURI
for the resource.- Parameters:
pathOrUri
- the resourcecurrentWorkingDirectory
- 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 absolutePath
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 resourcecommandLine
- 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 informationoption
- 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 informationoption
- 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 informationoption
- the option specifying the format, which must be present on the command lineloader
- the content loader to use to load the content instanceresource
- 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 informationoption
- the option specifying the module to load, which must be present on the command linecurrentWorkingDirectory
- the URI of the current working directorybindingContext
- 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 loadcurrentWorkingDirectory
- the URI of the current working directorybindingContext
- 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 loadbindingContext
- 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 locationcurrentWorkingDirectory
- 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 informationoption
- the option specifying the constraints to load, which must be present on the command linecurrentWorkingDirectory
- 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
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
@NonNull public static IBindingContext newBindingContextWithDynamicCompilation() throws CommandExecutionExceptionCreate a newIBindingContext
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 newIBindingContext
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
-