Skip to content

Commit

Permalink
Refactored code to reduce warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 2, 2024
1 parent 9c7c850 commit 3eee189
Show file tree
Hide file tree
Showing 26 changed files with 555 additions and 264 deletions.
4 changes: 0 additions & 4 deletions cli-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
<groupId>nl.talsmasoftware</groupId>
<artifactId>lazy4j</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument;
import gov.nist.secauto.metaschema.cli.processor.command.ICommand;
import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
import gov.nist.secauto.metaschema.core.util.AutoCloser;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.IVersionInfo;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
Expand Down Expand Up @@ -624,31 +625,33 @@ protected String buildHelpCliSyntax() {
return retval;
}

/**
* Output the help text to the console.
*/
public void showHelp() {

HelpFormatter formatter = new HelpFormatter();
formatter.setLongOptSeparator("=");

@SuppressWarnings("resource")
AnsiPrintStream out = AnsiConsole.out();
int terminalWidth = Math.max(out.getTerminalWidth(), 40);

@SuppressWarnings("resource")
PrintWriter writer = new PrintWriter( // NOPMD not owned
out,
try (PrintWriter writer = new PrintWriter( // NOPMD not owned
AutoCloser.preventClose(out),
true,
StandardCharsets.UTF_8);
formatter.printHelp(
writer,
terminalWidth,
buildHelpCliSyntax(),
buildHelpHeader(),
toOptions(),
HelpFormatter.DEFAULT_LEFT_PAD,
HelpFormatter.DEFAULT_DESC_PAD,
buildHelpFooter(),
false);
writer.flush();
StandardCharsets.UTF_8)) {
formatter.printHelp(
writer,
Math.max(out.getTerminalWidth(), 50),
buildHelpCliSyntax(),
buildHelpHeader(),
toOptions(),
HelpFormatter.DEFAULT_LEFT_PAD,
HelpFormatter.DEFAULT_DESC_PAD,
buildHelpFooter(),
false);
writer.flush();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ protected static Path getCurrentWorkingDirectory() {

@NonNull
protected static Path resolveAgainstCWD(@NonNull Path path) {
return getCurrentWorkingDirectory().resolve(path).normalize();
return ObjectUtils.notNull(getCurrentWorkingDirectory().resolve(path).normalize());
}

@NonNull
protected static URI resolveAgainstCWD(@NonNull URI uri) {
return getCurrentWorkingDirectory().toUri().resolve(uri.normalize());
return ObjectUtils.notNull(getCurrentWorkingDirectory().toUri().resolve(uri.normalize()));
}

@NonNull
protected static URI resolveAgainstCWD(@NonNull String uri) throws URISyntaxException {
return UriUtils.toUri(uri, getCurrentWorkingDirectory().toUri());
return UriUtils.toUri(uri, ObjectUtils.notNull(getCurrentWorkingDirectory().toUri()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.cli.processor.command;

import gov.nist.secauto.metaschema.cli.processor.ExitCode;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* For use in commands to short-circut command execution.
*/
public class CommandExecutionException
extends Exception {
private final ExitCode exitCode;

/**
* the serial version UID.
*/
private static final long serialVersionUID = 1L;

/**
* Constructs a new exception with the provided {@code code}, and no message or
* cause.
*
* @param code
* the exit code associated with this error
*/
public CommandExecutionException(@NonNull ExitCode code) {
this.exitCode = code;
}

/**
* Constructs a new exception with the provided {@code code}, {@code message},
* and no cause.
*
* @param code
* the exit code associated with this error
* @param message
* the exception message
*/
public CommandExecutionException(@NonNull ExitCode code, String message) {
super(message);
this.exitCode = code;
}

/**
* Constructs a new exception with the no message and provided the {@code code}
* and {@code cause}.
*
* @param code
* the exit code associated with this error
* @param cause
* the original exception cause
*/
public CommandExecutionException(@NonNull ExitCode code, Throwable cause) {
super(cause);
this.exitCode = code;
}

/**
* Constructs a new exception with the provided {@code code}, {@code message},
* and {@code cause}.
*
* @param code
* the exit code associated with this error
* @param message
* the exception message
* @param cause
* the original exception cause
*/
public CommandExecutionException(@NonNull ExitCode code, String message, Throwable cause) {
super(message, cause);
this.exitCode = code;
}

/**
* Generate an {@link ExitStatus} based on this exception.
*
* @return the exit status
*/
@NonNull
public ExitStatus toExitStatus() {
String message = getLocalizedMessage();

ExitStatus retval = message == null
? exitCode.exit()
: exitCode.exitMessage(message);

Throwable cause = getCause();
if (cause != null) {
retval.withThrowable(cause);
}
return retval;
}
}
5 changes: 0 additions & 5 deletions databind-metaschema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
<artifactId>metaschema-databind</artifactId>
</dependency>

<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
</dependency>

<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ private void addConstraintValidationFinding(@NonNull ConstraintValidationFinding
results.add(new ConstraintResult(finding));
}

public void write(@NonNull Path outputFile) throws IOException {
public void write(
@NonNull Path outputFile,
@NonNull IBindingContext bindingContext) throws IOException {

URI output = ObjectUtils.notNull(outputFile.toUri());

Expand Down Expand Up @@ -246,7 +248,6 @@ public void write(@NonNull Path outputFile) throws IOException {
run.setTool(tool);
}

IBindingContext bindingContext = IBindingContext.newInstance();
bindingContext.registerModule(SarifModule.class);
bindingContext.newSerializer(Format.JSON, Sarif.class)
.disableFeature(SerializationFeature.SERIALIZE_ROOT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import gov.nist.secauto.metaschema.core.model.validation.IValidationFinding;
import gov.nist.secauto.metaschema.core.util.IVersionInfo;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;

import org.jmock.Expectations;
import org.jmock.junit5.JUnit5Mockery;
Expand Down Expand Up @@ -110,7 +111,7 @@ void testValid() throws IOException {

// no need to cleanup this file, since it is created in the target directory
Path sarifFile = ObjectUtils.requireNonNull(Paths.get("target/test.sarif"));
handler.write(sarifFile);
handler.write(sarifFile, IBindingContext.newInstance());

Path sarifSchema = Paths.get("modules/sarif/sarif-schema-2.1.0.json");

Expand Down
3 changes: 1 addition & 2 deletions metaschema-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@

<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>consolecaptor</artifactId>
<version>1.0.3</version>
<artifactId>logcaptor</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* The main entry point for the CLI application.
*/
@SuppressWarnings("PMD.ShortClassName")
public final class CLI {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import gov.nist.secauto.metaschema.cli.processor.command.CommandExecutionException;
import gov.nist.secauto.metaschema.cli.processor.command.DefaultExtraArgument;
import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument;
import gov.nist.secauto.metaschema.core.model.MetaschemaException;
import gov.nist.secauto.metaschema.core.util.AutoCloser;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;
Expand All @@ -39,7 +38,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Used by implementing classes to declare a content conversion command.
* Used by implementing classes to provide a content conversion command.
*/
public abstract class AbstractConvertSubcommand
extends AbstractTerminalCommand {
Expand Down Expand Up @@ -94,11 +93,8 @@ protected AbstractConversionCommandExecutor(
* Get the binding context to use for data processing.
*
* @return the context
* @throws MetaschemaException
* if a Metaschema error occurred
* @throws IOException
* if an error occurred while reading data
* @throws CommandExecutionException
* if an error occurred getting the binding context
*/
@NonNull
protected abstract IBindingContext getBindingContext() throws CommandExecutionException;
Expand Down
Loading

0 comments on commit 3eee189

Please sign in to comment.