Skip to content

Commit

Permalink
Custom constraint messages (metaschema-framework#218)
Browse files Browse the repository at this point in the history
* Added message support for all classes supporting reading and writing constraints in various forms.
* Relocated classes in gov/nist/secauto/metaschema/databind/model/binding/metaschema to gov/nist/secauto/metaschema/databind/model/metaschema/binding.
* Improved Javadocs.
* Added support for custom constraint messages in all but allowed values constraints (see metaschema-framework/metaschema#43). Resolves metaschema-framework#215.
* Enhanced logging to include the constraint identifier if it exists.

Co-authored-by: A.J. Stein <[email protected]>
Co-authored-by: A.J. Stein <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2024
1 parent 5114dc5 commit d85e17b
Show file tree
Hide file tree
Showing 128 changed files with 1,386 additions and 540 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static void handleQuiet() {
}

protected void showVersion() {
@SuppressWarnings("resource")
PrintStream out = AnsiConsole.out(); // NOPMD - not owner
@SuppressWarnings("resource") PrintStream out = AnsiConsole.out(); // NOPMD - not owner
getVersionInfos().values().stream().forEach(info -> {
out.println(ansi()
.bold().a(info.getName()).boldOff()
Expand Down Expand Up @@ -254,8 +253,7 @@ public class CallingContext {

@SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields")
public CallingContext(@NonNull List<String> args) {
@SuppressWarnings("PMD.LooseCoupling")
LinkedList<ICommand> calledCommands = new LinkedList<>();
@SuppressWarnings("PMD.LooseCoupling") LinkedList<ICommand> calledCommands = new LinkedList<>();
List<Option> options = new LinkedList<>(OPTIONS);
List<String> extraArgs = new LinkedList<>();

Expand Down Expand Up @@ -633,8 +631,7 @@ public void showHelp() {
HelpFormatter formatter = new HelpFormatter();
formatter.setLongOptSeparator("=");

@SuppressWarnings("resource")
AnsiPrintStream out = AnsiConsole.out();
@SuppressWarnings("resource") AnsiPrintStream out = AnsiConsole.out();

try (PrintWriter writer = new PrintWriter( // NOPMD not owned
AutoCloser.preventClose(out),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public CommandExecutionException(@NonNull ExitCode code, String message) {
}

/**
* Constructs a new exception with no message and the provided {@code code}
* and {@code cause}.
* Constructs a new exception with no message and the provided {@code code} and
* {@code cause}.
*
* @param code
* the exit code associated with this error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public final QName getDefinitionQName() {
return definitionQName.get();
}

@Override
public boolean isInline() {
// never inline
return false;
}

@Override
public final INSTANCE getInlineInstance() {
// never inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public final DEFINITION getDefinition() {
return ObjectUtils.asType(this);
}

@Override
public boolean isInline() {
return true;
}

@Override
@NonNull
public final INSTANCE getInlineInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public final DEFINITION getDefinition() {
return ObjectUtils.asType(this);
}

@Override
public boolean isInline() {
return true;
}

@Override
@NonNull
public final INSTANCE getInlineInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ default String getRootJsonName() {
return getRootName();
}

@Override
default boolean isInline() {
// not inline by default
return false;
}

@Override
default IAssemblyInstance getInlineInstance() {
// not inline by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ default QName getDefinitionQName() {
/**
* Determine if the definition is defined inline, meaning the definition is
* declared where it is used.
* <p>
* If this method returns {@code false}, then {@link #getInlineInstance()} must
* return {@code null}.
*
* @return {@code true} if the definition is declared inline or {@code false} if
* the definition is able to be globally referenced
* @see #getInlineInstance()
*/
default boolean isInline() {
return getInlineInstance() != null;
Expand All @@ -71,8 +75,12 @@ default boolean isInline() {
/**
* If {@link #isInline()} is {@code true}, return the instance the definition is
* inlined for.
* <p>
* If this method returns {@code null}, then {@link #getInlineInstance()} must
* return {@code false}.
*
* @return the instance or {@code null} otherwise
* @see #isInline()
*/
INamedInstance getInlineInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public interface IFeatureDefinitionInstanceInlined<
DEFINITION extends IDefinition,
INSTANCE extends INamedInstance>
extends IDefinition, INamedInstance {
@Override
default boolean isInline() {
// has to be inline
return true;
}

@Override
default QName getDefinitionQName() {
Expand All @@ -44,6 +39,12 @@ default DEFINITION getDefinition() {
return ObjectUtils.asType(this);
}

@Override
default boolean isInline() {
// has to be inline
return true;
}

@Override
@NonNull
default INSTANCE getInlineInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import edu.umd.cs.findbugs.annotations.Nullable;

public interface IFieldDefinition extends IModelDefinition, IValuedDefinition, IField {

@Override
default boolean isInline() {
// not inline by default
return false;
}

@Override
default IFieldInstance getInlineInstance() {
// not inline by default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.model.constraint;

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

/**
* Provides builder methods for the core data elements of an
* {@link IConstraint}.
* <p>
* The base class of all constraint builders.
*
* @param <T>
* the Java type of the implementing builder
* @param <R>
* the Java type of the resulting built object
* @since 2.0.0
*/
public abstract class AbstractConfigurableMessageConstraintBuilder<
T extends AbstractConfigurableMessageConstraintBuilder<T, R>,
R extends IConfigurableMessageConstraint>
extends AbstractConstraintBuilder<T, R> {
private String message;

/**
* A message to emit when the constraint is violated. Allows embedded Metapath
* expressions using the syntax {@code \{ metapath \}}.
*
* @param message
* the message if defined or {@code null} otherwise
* @return this builder
*/
@NonNull
public T message(@NonNull String message) {
this.message = message;
return getThis();
}

/**
* Get the constraint message provided to the builder.
*
* @return the message or {@code null} if no message is set
*/
@Nullable
protected String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import edu.umd.cs.findbugs.annotations.Nullable;

/**
* The base class of all constraint builders.
* <p>
* Provides builder methods for the core data elements of an
* {@link IConstraint}.
* <p>
* The base class of all constraint builders.
*
* @param <T>
* the Java type of the implementing builder
Expand Down
Loading

0 comments on commit d85e17b

Please sign in to comment.