Skip to content

Commit

Permalink
Updated oss-maven dependency to released version.
Browse files Browse the repository at this point in the history
Fixed a compile error in YamlOperations.java.
Updated Java code examples for using the binding implementation.
Applied code formatting.
  • Loading branch information
david-waltermire committed Jul 10, 2023
1 parent 42cb3c0 commit 968150f
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,8 @@ private static void handleNoColor() {
}

public static void handleQuiet() {
@SuppressWarnings("resource")
LoggerContext ctx = (LoggerContext) LogManager.getContext(false); // NOPMD not
// closable here
@SuppressWarnings("resource") LoggerContext ctx = (LoggerContext) LogManager.getContext(false); // NOPMD not
// closable here
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
Level oldLevel = loggerConfig.getLevel();
Expand All @@ -217,8 +216,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().stream().forEach((info) -> {
out.println(ansi()
.bold().a(info.getName()).boldOff()
Expand Down Expand Up @@ -568,10 +566,9 @@ public void showHelp() {
AnsiPrintStream out = AnsiConsole.out();
int terminalWidth = Math.max(out.getTerminalWidth(), 40);

@SuppressWarnings("resource")
PrintWriter writer = new PrintWriter(out, true, StandardCharsets.UTF_8); // NOPMD -
// not
// owned
@SuppressWarnings("resource") PrintWriter writer = new PrintWriter(out, true, StandardCharsets.UTF_8); // NOPMD -
// not
// owned
formatter.printHelp(
writer,
terminalWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ default void validateOptions(

boolean isSubCommandRequired();

@SuppressWarnings("unused")
@SuppressWarnings("unused")
default Command getSubCommandByName(@NonNull String name) {
// no sub commands by default
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public ExitStatus executeCommand(CallingContext callingContext, CommandLine cmdL
try {
IMetaschema metaschema = new MetaschemaLoader().load(input);
if (destination == null) {
@SuppressWarnings({"resource", "PMD.CloseResource"}) // not owned
@SuppressWarnings({ "resource", "PMD.CloseResource" }) // not owned
OutputStream os = ObjectUtils.notNull(System.out);
ISchemaGenerator.generateSchema(metaschema, os, asFormat, configuration);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

public final class LoggingValidationHandler {
private static final Logger LOGGER = LogManager.getLogger(LoggingValidationHandler.class);

private static final LoggingValidationHandler NO_LOG_EXCPTION_INSTANCE = new LoggingValidationHandler(false);
private static final LoggingValidationHandler LOG_EXCPTION_INSTANCE = new LoggingValidationHandler(true);

Expand All @@ -61,17 +61,15 @@ public static LoggingValidationHandler instance() {
public static LoggingValidationHandler instance(boolean logExceptions) {
return logExceptions ? LOG_EXCPTION_INSTANCE : NO_LOG_EXCPTION_INSTANCE;
}

protected LoggingValidationHandler(boolean logExceptions) {
this.logExceptions = logExceptions;
}


public boolean isLogExceptions() {
return logExceptions;
}


public boolean handleValidationResults(IValidationResult result) {
handleValidationFindings(result.getFindings());
return result.isPassing();
Expand Down Expand Up @@ -145,7 +143,7 @@ private LogBuilder getLogger(@NonNull IValidationFinding finding) {
}

assert retval != null;

if (finding.getCause() != null && isLogExceptions()) {
retval.withThrowable(finding.getCause());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.Tag;
Expand All @@ -48,9 +49,10 @@ public final class YamlOperations {
private static final Yaml YAML_PARSER;

static {
Constructor constructor = new Constructor();
Representer representer = new Representer();
YAML_PARSER = new Yaml(constructor, representer, new DumperOptions(), new Resolver() {
Constructor constructor = new Constructor(new LoaderOptions());
DumperOptions dumperOptions = new DumperOptions();
Representer representer = new Representer(dumperOptions);
YAML_PARSER = new Yaml(constructor, representer, dumperOptions, new Resolver() {
@Override
protected void addImplicitResolvers() {
addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.binding.io.yaml.impl;
package gov.nist.secauto.metaschema.binding.io.yaml.impl;
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public abstract class AbstractBoundMetaschema
* <p>
* Will also load any imported Metaschemas.
*
*
*
* @param clazz
* the Metaschema class
* @param bindingContext
Expand Down Expand Up @@ -118,7 +118,7 @@ private static IMetaschema createInstance(

/**
* Construct a new Metaschema instance.
*
*
* @param importedMetaschema
* Metaschema imports associated with the metaschema
* @param bindingContext
Expand All @@ -133,7 +133,7 @@ protected AbstractBoundMetaschema(

/**
* Get the Metaschema binding context.
*
*
* @return the context
*/
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ public void writeRoot(Object instance, IXmlWritingContext context) throws XMLStr
@Override
public void writeRoot(Object instance, IJsonWritingContext context) throws IOException {

@SuppressWarnings("resource")
JsonGenerator writer = context.getWriter(); // NOPMD - intentional
@SuppressWarnings("resource") JsonGenerator writer = context.getWriter(); // NOPMD - intentional

// first read the initial START_OBJECT
writer.writeStartObject();
Expand Down
7 changes: 4 additions & 3 deletions metaschema-java-binding/src/site/markdown/index.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ The following code illustrates how to load data into a bound object using this [

```java
// get the binding context instance, which manages Metaschema-to-object binding information
BindingContext bindingContext = BindingContext.newInstance();
IBindingContext bindingContext = IBindingContext.instance();

// create a loader which is used to parse the content
IBoundLoader loader = bindingContext.newBoundLoader();

// specify the bound class to load data into and the file to load it from
// the loader figures out the format to load from (i.e. JSON)
BoundClass object = loader.load(BoundClass.class, new File("src/test/resources/test-content/bound-class-simple.json"));
BoundClass object
= loader.load(BoundClass.class, new File("src/test/resources/test-content/bound-class-simple.json"));
System.out.println(object.getId());
```

Expand All @@ -40,7 +41,7 @@ Next you can save the data in a specific format. In the following example, we sa

```java
// create a serializer to write the object
Serializer<BoundClass> serializer = bindingContext.newSerializer(Format.XML, BoundClass.class);
ISerializer<BoundClass> serializer = bindingContext.newSerializer(Format.XML, BoundClass.class);
serializer.serialize(object, new File("target/bound-class-simple.xml"));
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@

package gov.nist.secauto.metaschema.binding;

//import gov.nist.secauto.metaschema.binding.io.Format;
//import gov.nist.secauto.metaschema.binding.io.IBoundLoader;
//import gov.nist.secauto.metaschema.binding.io.ISerializer;
//
//import org.junit.jupiter.api.Test;
//
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.io.IOException;

class Examples { // NOPMD

// @SuppressWarnings("PMD")
// @Test
// void testLoadJson() throws FileNotFoundException, IOException {
// // get the binding context instance, which manages Metaschema-to-object binding information
// IBindingContext bindingContext = IBindingContext.newInstance();
// IBindingContext bindingContext = IBindingContext.instance();
//
// // create a loader which is used to parse the content
// IBoundLoader loader = bindingContext.newBoundLoader();
Expand All @@ -48,7 +58,7 @@ class Examples { // NOPMD
// @Test
// void test() throws FileNotFoundException, IOException {
// // get the binding context instance, which manages Metaschema-to-object binding information
// IBindingContext bindingContext = IBindingContext.newInstance();
// IBindingContext bindingContext = IBindingContext.instance();
//
// // create a loader which is used to parse the content
// IBoundLoader loader = bindingContext.newBoundLoader();
Expand All @@ -60,8 +70,7 @@ class Examples { // NOPMD
// File("src/test/resources/test-content/bound-class-simple.json"));
//
// // create a serializer to write the object
// ISerializer<@NonNull BoundClass> serializer = bindingContext.newSerializer(Format.XML,
// BoundClass.class);
// ISerializer<Object> serializer = bindingContext.newSerializer(Format.XML, Object.class);
// serializer.serialize(object, new File("target/bound-class-simple.xml"));
// }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ protected boolean addClassName(@NonNull String packageOrTypeName, @NonNull Strin
}

private String generateClassName(@NonNull String packageOrTypeName, @NonNull IFlagContainer definition) {
@NonNull
String className = getBindingConfiguration().getClassName(definition);
@NonNull String className = getBindingConfiguration().getClassName(definition);

Set<String> classNames = getClassNamesFor(packageOrTypeName);
synchronized (classNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ private static Object read(@NonNull Format format, @NonNull Path file, @NonNull

private static <CLASS> void write(@NonNull Format format, @NonNull Path file, CLASS rootObject) throws IOException {
IBindingContext context = IBindingContext.instance();
@SuppressWarnings("unchecked")
Class<CLASS> clazz = (Class<CLASS>) rootObject.getClass();
@SuppressWarnings("unchecked") Class<CLASS> clazz = (Class<CLASS>) rootObject.getClass();

try (Writer writer = Files.newBufferedWriter(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
Expand Down Expand Up @@ -215,8 +214,7 @@ void testFieldsWithFlagMetaschema()
Assertions.assertEquals("complex-field1", reflectMethod(field1, "getId"));
Assertions.assertEquals("test-string", reflectMethod(field1, "getValue"));

@SuppressWarnings("unchecked")
List<Object> field2s
@SuppressWarnings("unchecked") List<Object> field2s
= (List<Object>) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields2"),
obj);
Assertions.assertNotNull(field2s);
Expand All @@ -225,8 +223,7 @@ void testFieldsWithFlagMetaschema()
Assertions.assertEquals("complex-field2-1", reflectMethod(field2, "getId"));
Assertions.assertEquals("test-string2", reflectMethod(field2, "getValue"));

@SuppressWarnings("unchecked")
List<Object> field3s
@SuppressWarnings("unchecked") List<Object> field3s
= (List<Object>) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields3"),
obj);
Assertions.assertEquals(2, field3s.size());
Expand All @@ -242,8 +239,7 @@ void testFieldsWithFlagMetaschema()
});

Assertions.assertAll("ComplexFields4", () -> {
@SuppressWarnings("unchecked")
Map<String, Object> collection
@SuppressWarnings("unchecked") Map<String, Object> collection
= (Map<String, Object>) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields4"),
obj);
Assertions.assertNotNull(collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ public void setIdReference(@NonNull BasedSequence value) {
@Override
@NonNull
public BasedSequence[] getSegments() {
@NonNull
BasedSequence[] retval = { getType(), getIdReference() };
@NonNull BasedSequence[] retval = { getType(), getIdReference() };
return retval;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ private static <RESULT_TYPE> Class<? extends RESULT_TYPE> findCommonBase(
baseType,
first,
ObjectUtils.notNull(expressionClasses.subList(1, expressionClasses.size())));
@SuppressWarnings("unchecked")
Class<? extends RESULT_TYPE> newBase = (Class<? extends RESULT_TYPE>) leastCommon;
@SuppressWarnings("unchecked") Class<? extends RESULT_TYPE> newBase
= (Class<? extends RESULT_TYPE>) leastCommon;
if (newBase != null) {
retval = newBase;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static <ITEM_TYPE extends IItem> ISequence<ITEM_TYPE> empty() {
* Construct a new sequence containing the provided {@code item}.
* <p>
* If the item is {@code null} and empty sequence will be created.
*
*
* @param <ITEM_TYPE>
* the type of items contained in the sequence.
* @param item
Expand All @@ -77,7 +77,7 @@ public static <ITEM_TYPE extends IItem> ISequence<ITEM_TYPE> of( // NOPMD - inte

/**
* Construct a new sequence containing the provided {@code items}.
*
*
* @param <ITEM_TYPE>
* the type of items contained in the sequence.
* @param items
Expand All @@ -98,7 +98,7 @@ public static <ITEM_TYPE extends IItem> ISequence<ITEM_TYPE> of( // NOPMD - inte

/**
* Construct a new sequence containing the provided {@code items}.
*
*
* @param <ITEM_TYPE>
* the type of items contained in the sequence.
* @param items
Expand All @@ -113,15 +113,15 @@ public static <ITEM_TYPE extends IItem> ISequence<ITEM_TYPE> of( // NOPMD - inte

/**
* Get the items in this sequence as a {@link List}.
*
*
* @return a list containing all the items of the sequence
*/
@NonNull
List<ITEM_TYPE> asList();

/**
* Get the items in this sequence as a {@link Stream}.
*
*
* @return a stream containing all the items of the sequence
*/
// TODO: rename to "stream"
Expand All @@ -130,21 +130,21 @@ public static <ITEM_TYPE extends IItem> ISequence<ITEM_TYPE> of( // NOPMD - inte

/**
* Determine if this sequence is empty.
*
*
* @return {@code true} if the sequence contains no items, or {@code false} otherwise
*/
boolean isEmpty();

/**
* Get the count of items in this sequence.
*
*
* @return the count of items
*/
int size();

/**
* Iterate over each item in the sequence using the provided {@code action}.
*
*
* @param action
* code to execute for each item
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@

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

public abstract class AbstractAssemblyInstanceNodeItem<
F extends IFlagNodeItem,
M extends IModelNodeItem,
P extends INodeItem, L extends AbstractModelNodeContext.Model<F, M>>
public abstract class AbstractAssemblyInstanceNodeItem<F extends IFlagNodeItem, M extends IModelNodeItem, P extends INodeItem, L extends AbstractModelNodeContext.Model<
F, M>>
extends AbstractModelNodeContext<F, M, L>
implements IAssemblyNodeItem {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
* @param <P>
* the parent node item type
*/
abstract class AbstractFieldInstanceNodeItem<
F extends IFlagNodeItem,
P extends IAssemblyNodeItem,
L extends AbstractNodeContext.Flags<F>>
abstract class AbstractFieldInstanceNodeItem<F extends IFlagNodeItem, P extends IAssemblyNodeItem, L extends AbstractNodeContext.Flags<
F>>
extends AbstractNodeContext<F, L>
implements IFieldNodeItem {
@NonNull
Expand Down
Loading

0 comments on commit 968150f

Please sign in to comment.