Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a CLI command to list Metapath functions #39

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void registerFunctionByName(@NonNull IFunction function) {
}

@Override
public Stream<IFunction> getFunctionsAsStream() {
public Stream<IFunction> stream() {
synchronized (this) {
return ObjectUtils.notNull(libraryByQName.values().stream().flatMap(NamedFunctionSet::getFunctionsAsStream));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.stream.Stream;

import javax.xml.namespace.QName;

Expand Down Expand Up @@ -64,7 +65,7 @@ public FunctionService() {
FunctionLibrary functionLibrary = new FunctionLibrary();
loader.stream()
.map(Provider<IFunctionLibrary>::get)
.flatMap(IFunctionLibrary::getFunctionsAsStream)
.flatMap(IFunctionLibrary::stream)
.forEachOrdered(function -> functionLibrary.registerFunction(ObjectUtils.notNull(function)));
this.library = functionLibrary;
}
Expand All @@ -79,6 +80,10 @@ private ServiceLoader<IFunctionLibrary> getLoader() {
return loader;
}

public Stream<IFunction> stream() {
return this.library.stream();
}

/**
* Retrieve the function with the provided name that supports the signature of
* the provided methods, if such a function exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ default String toSignature() {
return ObjectUtils.notNull(String.format("Q{%s}%s(%s) as %s",
getNamespace(),
getName(),
getArguments().isEmpty() ? "()"
getArguments().isEmpty() ? ""
: getArguments().stream().map(IArgument::toSignature).collect(Collectors.joining(","))
+ (isArityUnbounded() ? ", ..." : ""),
getResult().toSignature()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface IFunctionLibrary {
* @return a stream of function signatures
*/
@NonNull
Stream<IFunction> getFunctionsAsStream();
Stream<IFunction> stream();

/**
* Determine if there is a function with the provided name that supports the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,72 @@ default <CLASS> void convert(
ISerializer<CLASS> serializer = getBindingContext().newSerializer(toFormat, rootClass);
serializer.serialize(object, os);
}

/**
* Auto convert the provided {@code source} to the provided {@code toFormat}.
* Write the converted content to the provided {@code destination}.
* <p>
* The format of the source is expected to be auto detected using
* {@link #detectFormat(Path)}.
*
* @param <CLASS>
* the Java type to load data into
* @param source
* the resource to convert
* @param destination
* the resource to write converted content to
* @param toFormat
* the format to convert to
* @param rootClass
* the class for the Java type to load data into
* @throws FileNotFoundException
* the the provided source file was not found
* @throws IOException
* if an error occurred while loading the data from the specified
* resource or writing the converted data to the specified destination
*/
default <CLASS> void convert(
@NonNull URI source,
@NonNull Path destination,
@NonNull Format toFormat,
@NonNull Class<CLASS> rootClass) throws FileNotFoundException, IOException {
CLASS object = load(rootClass, source);

ISerializer<CLASS> serializer = getBindingContext().newSerializer(toFormat, rootClass);
serializer.serialize(object, destination);
}

/**
* Auto convert the provided {@code source} to the provided {@code toFormat}.
* Write the converted content to the provided {@code destination}.
* <p>
* The format of the source is expected to be auto detected using
* {@link #detectFormat(Path)}.
*
* @param <CLASS>
* the Java type to load data into
* @param source
* the resource to convert
* @param os
* the output stream to write converted content to
* @param toFormat
* the format to convert to
* @param rootClass
* the class for the Java type to load data into
* @throws FileNotFoundException
* the the provided source file was not found
* @throws IOException
* if an error occurred while loading the data from the specified
* resource or writing the converted data to the specified destination
*/
default <CLASS> void convert(
@NonNull URI source,
@NonNull OutputStream os,
@NonNull Format toFormat,
@NonNull Class<CLASS> rootClass) throws FileNotFoundException, IOException {
CLASS object = load(rootClass, source);

ISerializer<CLASS> serializer = getBindingContext().newSerializer(toFormat, rootClass);
serializer.serialize(object, os);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

package gov.nist.secauto.metaschema.cli;

import gov.nist.secauto.metaschema.cli.commands.GenerateSchemaCommand;
import gov.nist.secauto.metaschema.cli.commands.ValidateContentUsingModuleCommand;
import gov.nist.secauto.metaschema.cli.commands.ValidateModuleCommand;
import gov.nist.secauto.metaschema.cli.commands.MetaschemaCommands;
import gov.nist.secauto.metaschema.cli.processor.CLIProcessor;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
import gov.nist.secauto.metaschema.cli.processor.command.CommandService;
Expand Down Expand Up @@ -56,9 +54,7 @@ public static ExitStatus runCli(String... args) {
new MetaschemaJavaVersion(),
new MetaschemaVersion()));
CLIProcessor processor = new CLIProcessor("metaschema-cli", versions);
processor.addCommandHandler(new ValidateModuleCommand());
processor.addCommandHandler(new GenerateSchemaCommand());
processor.addCommandHandler(new ValidateContentUsingModuleCommand());
MetaschemaCommands.COMMANDS.forEach(processor::addCommandHandler);

CommandService.getInstance().getCommands().stream().forEach(command -> {
assert command != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument;
import gov.nist.secauto.metaschema.core.util.CustomCollectors;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.core.util.UriUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.io.Format;
import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
Expand All @@ -47,6 +48,8 @@
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -64,8 +67,8 @@ public abstract class AbstractConvertSubcommand
private static final String COMMAND = "convert";
@NonNull
private static final List<ExtraArgument> EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
new DefaultExtraArgument("source file", true),
new DefaultExtraArgument("destination file", false)));
new DefaultExtraArgument("source-file-or-URL", true),
new DefaultExtraArgument("destination-file", false)));

@NonNull
private static final Option OVERWRITE_OPTION = ObjectUtils.notNull(
Expand Down Expand Up @@ -121,14 +124,6 @@ public void validateOptions(CallingContext callingContext, CommandLine cmdLine)
if (extraArgs.isEmpty() || extraArgs.size() > 2) {
throw new InvalidArgumentException("Illegal number of arguments.");
}

Path source = Paths.get(extraArgs.get(0));
if (!Files.exists(source)) {
throw new InvalidArgumentException("The provided source '" + source + "' does not exist.");
}
if (!Files.isReadable(source)) {
throw new InvalidArgumentException("The provided source '" + source + "' is not readable.");
}
}

protected abstract static class AbstractConversionCommandExecutor
Expand Down Expand Up @@ -185,7 +180,15 @@ public ExitStatus execute() {
}
}

Path source = Paths.get(extraArgs.get(0));
String sourceName = extraArgs.get(0);
URI source;
URI cwd = Paths.get("").toAbsolutePath().toUri();
try {
source = UriUtils.toUri(sourceName, cwd);
} catch (URISyntaxException ex) {
return ExitCode.IO_ERROR.exitMessage("Cannot load source '%s' as it is not a valid file or URI.")
.withThrowable(ex);
}
assert source != null;

String toFormatText = cmdLine.getOptionValue(TO_OPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public abstract class AbstractValidateContentCommand
private static final String COMMAND = "validate";
@NonNull
private static final List<ExtraArgument> EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
new DefaultExtraArgument("file to validate", true)));
new DefaultExtraArgument("file-or-URI-to-validate", true)));

@NonNull
private static final Option AS_OPTION = ObjectUtils.notNull(
Expand Down Expand Up @@ -189,7 +189,7 @@ public ExitStatus execute() {
IBoundLoader loader = bindingContext.newBoundLoader();

List<String> extraArgs = cmdLine.getArgList();
// @SuppressWarnings("null")

String sourceName = extraArgs.get(0);
URI source;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class GenerateSchemaCommand

static {
EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
new DefaultExtraArgument("metaschema-module-file", true),
new DefaultExtraArgument("metaschema-module-file-or-URL", true),
new DefaultExtraArgument("destination-schema-file", false)));
}

Expand Down Expand Up @@ -166,7 +166,6 @@ protected ExitStatus executeCommand(
@NonNull CallingContext callingContext,
@NonNull CommandLine cmdLine) {
List<String> extraArgs = cmdLine.getArgList();
URI cwd = Paths.get("").toAbsolutePath().toUri();

Path destination = null;
if (extraArgs.size() > 1) {
Expand Down Expand Up @@ -212,6 +211,7 @@ protected ExitStatus executeCommand(

URI input;
String inputName = extraArgs.get(0);
URI cwd = Paths.get("").toAbsolutePath().toUri();

try {
input = UriUtils.toUri(extraArgs.get(0), cwd);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.cli.commands;

import gov.nist.secauto.metaschema.cli.commands.metapath.MetapathCommand;
import gov.nist.secauto.metaschema.cli.processor.command.ICommand;

import java.util.List;

public final class MetaschemaCommands {
public static final List<ICommand> COMMANDS = List.of(
new ValidateModuleCommand(),
new GenerateSchemaCommand(),
new ValidateContentUsingModuleCommand(),
new MetapathCommand());

private MetaschemaCommands() {
// disable construction
}
}
Loading