Skip to content

Commit

Permalink
Fixed a bug causing the UTF8 encoding to not be used when serializing…
Browse files Browse the repository at this point in the history
… to an input stream.
  • Loading branch information
david-waltermire committed Jun 30, 2022
1 parent 6f4575b commit 13e0fee
Show file tree
Hide file tree
Showing 84 changed files with 155 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class AbstractMetaschemaLoaderStrategy implements IMetaschemaLoaderStrategy {
@NotNull
private final Map<@NotNull Class<?>, IClassBinding> classBindingsByClass = new HashMap<>(); // NOPMD - intentional


protected AbstractMetaschemaLoaderStrategy(@NotNull IBindingContext bindingContext) {
this.bindingContext = bindingContext;
}
Expand All @@ -76,7 +75,7 @@ public IMetaschema getMetaschemaInstanceByClass(@NotNull Class<? extends Abstrac
protected IMetaschema newMetaschema(@NotNull Class<? extends AbstractBoundMetaschema> clazz) {
return AbstractBoundMetaschema.createInstance(clazz, getBindingContext());
}

@Override
public IClassBinding getClassBinding(@NotNull Class<?> clazz) {
IClassBinding retval;
Expand Down Expand Up @@ -105,7 +104,7 @@ protected IClassBinding newClassBinding(@NotNull Class<?> clazz) {
}
return retval;
}

@Override
public Map<@NotNull Class<?>, IClassBinding> getClassBindingsByClass() {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
public interface IBindingContext extends IMetaschemaLoaderStrategy {

/**
* Get the singleton {@link IBindingContext} instance, which can be used to load information that binds a model to a
* set of Java classes.
* Get the singleton {@link IBindingContext} instance, which can be used to load information that
* binds a model to a set of Java classes.
*
* @return a new binding context
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ protected IDocumentNodeItem loadAsNodeItemInternal(@NotNull BufferedInputStream
}

IDeserializer<?> deserializer = getDeserializer(clazz, format, getConfiguration());
return (IDocumentNodeItem) deserializer.deserializeToNodeItem(bis, documentUri);
return (IDocumentNodeItem) deserializer.deserializeToNodeItem(bis, documentUri);
}

@NotNull
Expand Down Expand Up @@ -379,7 +379,7 @@ protected <CLASS> CLASS loadInternal(@NotNull Class<CLASS> clazz, @NotNull Buffe

IDeserializer<CLASS> deserializer = getDeserializer(clazz, format, getConfiguration());
INodeItem nodeItem = deserializer.deserializeToNodeItem(bis, documentUri);
return (CLASS)nodeItem.getValue();
return (CLASS) nodeItem.getValue();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface ISerializer<CLASS> extends IMutableConfiguration<SerializationF
* if an error occurred while writing data to the stream
*/
default void serialize(@NotNull CLASS data, @NotNull OutputStream os) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(os);
OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
serialize(data, writer);
writer.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ protected XMLStreamWriter2 newXMLStreamWriter(@NotNull Writer writer) throws IOE
public void serialize(CLASS data, Writer writer) throws IOException {
XMLStreamWriter2 streamWriter = newXMLStreamWriter(writer);
IOException caughtException = null;
IAssemblyClassBinding classBinding = getClassBinding();
IXmlWritingContext writingContext = new DefaultXmlWritingContext(streamWriter);
IAssemblyClassBinding classBinding = getClassBinding();
IXmlWritingContext writingContext = new DefaultXmlWritingContext(streamWriter);

RootAssemblyDefinition root = new RootAssemblyDefinition(classBinding);
RootAssemblyDefinition root = new RootAssemblyDefinition(classBinding);


try {
root.writeRoot(data, writingContext);
streamWriter.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import gov.nist.secauto.metaschema.binding.io.json.DefaultJsonSerializer;
import gov.nist.secauto.metaschema.binding.model.IAssemblyClassBinding;

import org.jetbrains.annotations.NotNull;

public class DefaultYamlSerializer<CLASS>
extends DefaultJsonSerializer<CLASS> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ private YamlFactoryFactory() {
}

public static YAMLFactory newYamlFactoryInstance() {
YAMLFactory retval = new YAMLFactory();
// retval.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
retval.enable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
retval.enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS);
return retval;

return YAMLFactory.builder()
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
.enable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS)
.disable(YAMLGenerator.Feature.SPLIT_LINES)
.build();
}

public static YAMLFactory instance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ protected AbstractNamedModelProperty(@NotNull IAssemblyClassBinding parentClassB
// }

// @Override
// public Stream<? extends INodeItem> getNodeItemsFromParentInstance(IRequiredValueAssemblyNodeItem parentItem,
// public Stream<? extends INodeItem> getNodeItemsFromParentInstance(IRequiredValueAssemblyNodeItem
// parentItem,
// Object parentValue) {
// return newNodeItems(parentItem, getPropertyInfo().getItemsFromParentInstance(parentValue));
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ default Class<?> getItemType() {
return (Class<?>) getType();
}


/**
* Get the current value from the provided {@code parentInstance} object. The provided object must
* be of the type associated with the definition containing this property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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.binding;

import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -53,13 +54,11 @@ void testConstraints() throws MetaschemaException, IOException { // NOPMD - inte
IBindingContext bindingContext = new DefaultBindingContext(CollectionUtil.singleton(constraintSet));
IMetaschema metaschema = bindingContext.getMetaschemaInstanceByClass(TestMetaschema.class);

IAssemblyDefinition root = metaschema.getExportedAssemblyDefinitionByName("root");


IAssemblyDefinition root = metaschema.getExportedAssemblyDefinitionByName("root");

assertNotNull(root, "root not found");
List<? extends IConstraint> constraints = root.getConstraints();
assertFalse(constraints.isEmpty(), "a constraint was expected");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void test() {
assertNotNull(result, "null result");
assertTrue(!result.isEmpty(), "result was empty");
assertEquals(1, result.size(), "unexpected size");
assertEquals(true, ((IBooleanItem)result.asList().iterator().next()).toBoolean(), "unexpected result");
assertEquals(true, ((IBooleanItem) result.asList().iterator().next()).toBoolean(), "unexpected result");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public MarkupMultiline getRemarks() {

@SuppressWarnings("PMD")
@MetaschemaField(
name="simple-field",
name = "simple-field",
metaschema = TestMetaschema.class,
isCollapsible = true)
public static class SimpleField {
Expand Down Expand Up @@ -305,7 +305,7 @@ void testXmlDefaultNameRead()

@SuppressWarnings("PMD")
@MetaschemaField(
name= "simple-field2",
name = "simple-field2",
metaschema = TestMetaschema.class,
isCollapsible = true)
public static class SimpleField2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public MarkupMultiline getRemarks() {
}

@SuppressWarnings("PMD")
@MetaschemaAssembly(name= "simple-assembly", metaschema = TestMetaschema.class, rootName = "test", rootNamespace = "http://example.com/ns")
@MetaschemaAssembly(name = "simple-assembly", metaschema = TestMetaschema.class, rootName = "test",
rootNamespace = "http://example.com/ns")
private static class SimpleAssembly {
@BoundFlag(useName = "id", typeAdapter = StringAdapter.class)
private String _id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import gov.nist.secauto.metaschema.model.common.datatype.adapter.StringAdapter;
import gov.nist.secauto.metaschema.model.common.datatype.adapter.TokenAdapter;

@MetaschemaField(name= "collapsible-flagged-bound-field", isCollapsible = true, metaschema = TestMetaschema.class)
@MetaschemaField(name = "collapsible-flagged-bound-field", isCollapsible = true, metaschema = TestMetaschema.class)
public class CollapsibleFlaggedBoundField {
@JsonKey
@BoundFlag(useName = "field-required-flag", typeAdapter = TokenAdapter.class, required = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import gov.nist.secauto.metaschema.binding.model.annotations.MetaschemaAssembly;

@MetaschemaAssembly(name= "empty-bound-assembly", rootName = "root", metaschema = TestMetaschema.class)
@MetaschemaAssembly(name = "empty-bound-assembly", rootName = "root", metaschema = TestMetaschema.class)
public class EmptyBoundAssembly {

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import gov.nist.secauto.metaschema.binding.model.annotations.MetaschemaAssembly;
import gov.nist.secauto.metaschema.model.common.datatype.adapter.StringAdapter;

@MetaschemaAssembly(name= "flagged-assembly", metaschema = TestMetaschema.class)
@MetaschemaAssembly(name = "flagged-assembly", metaschema = TestMetaschema.class)
public class FlaggedAssembly {
@BoundFlag(useName = "id", typeAdapter = StringAdapter.class)
private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import gov.nist.secauto.metaschema.model.common.datatype.adapter.BooleanAdapter;
import gov.nist.secauto.metaschema.model.common.datatype.adapter.StringAdapter;

@MetaschemaAssembly(name= "flagged-bound-assembly", metaschema = TestMetaschema.class)
@MetaschemaAssembly(name = "flagged-bound-assembly", metaschema = TestMetaschema.class)
public class FlaggedBoundAssembly {
@JsonKey
@BoundFlag(useName = "assembly-required-flag", typeAdapter = StringAdapter.class, required = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import gov.nist.secauto.metaschema.model.common.datatype.adapter.StringAdapter;
import gov.nist.secauto.metaschema.model.common.datatype.adapter.TokenAdapter;

@MetaschemaField(name= "flagged-bound-field", isCollapsible = false, metaschema = TestMetaschema.class)
@MetaschemaField(name = "flagged-bound-field", isCollapsible = false, metaschema = TestMetaschema.class)
public class FlaggedBoundField {
@JsonKey
@BoundFlag(useName = "field-required-flag", typeAdapter = TokenAdapter.class, required = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.Map;

@SuppressWarnings("PMD")
@MetaschemaAssembly(name= "only-model", metaschema = TestMetaschema.class)
@MetaschemaAssembly(name = "only-model", metaschema = TestMetaschema.class)
public class OnlyModelBoundAssembly { // NOPMD - intentional
/*
* ================ = simple field = ================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.Map;
import java.util.UUID;

@MetaschemaAssembly(name= "root", rootName = "root", metaschema = TestMetaschema.class)
@MetaschemaAssembly(name = "root", rootName = "root", metaschema = TestMetaschema.class)
public class RootBoundAssembly {
@BoundFlag(useName = "uuid", defaultValue = "374dd648-b247-483c-afd8-a66ba8876070", typeAdapter = UuidAdapter.class)
private UUID uuid; // NOPMD - intentional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public interface INamedModelInstance extends INamedInstance, IModelInstance {
INamedModelDefinition getDefinition();

/**
* Get the item values for the provided {@code instanceValue}. An instance may be singular or
* many valued.
* Get the item values for the provided {@code instanceValue}. An instance may be singular or many
* valued.
*
* @param instanceValue
* the instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import javax.xml.namespace.QName;

public interface IRootAssemblyDefinition extends IAssemblyDefinition {

@NotNull
static <T extends IAssemblyDefinition> IRootAssemblyDefinition toRootAssemblyDefinition(@NotNull T rootDefinition) {
return new RootAssemblyDefinitionWrapper<IAssemblyDefinition>(rootDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public IMutableConfiguration<T> disableFeature(@NotNull T feature) {
getFeatureSet().remove(feature);
return this;
}

@Override
@SuppressWarnings("null")
public IMutableConfiguration<T> applyConfiguration(@NotNull IConfiguration<T> original) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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.model.common.constraint;

import gov.nist.secauto.metaschema.model.common.metapath.item.IAssemblyNodeItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class DefaultIndexHasKeyConstraint
private final String indexName;

/**
* Create a key reference constraint, which uses a set of key fields to build a key to match against an index.
* Create a key reference constraint, which uses a set of key fields to build a key to match against
* an index.
*
* @param id
* the optional identifier for the constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void newFinding(
@NotNull List<@NotNull ? extends INodeItem> targets,
@NotNull CharSequence message,
Throwable cause) {

ConstraintValidationFinding finding = new ConstraintValidationFinding(constraints, message, cause, node, targets);
findings.add(finding);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.jetbrains.annotations.NotNull;

/**
* Represents an individual enumerated value associated with an {@link IAllowedValuesConstraint}.
* Represents an individual enumerated value associated with an {@link IAllowedValuesConstraint}.
*/
public interface IAllowedValue {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import java.util.List;

/**
* Represents a container of rules constraining the effective model of a Metaschema assembly data instance.
* Represents a container of rules constraining the effective model of a Metaschema assembly data
* instance.
*/
public interface IAssemblyConstraintSupport extends IValueConstraintSupport {
/**
Expand All @@ -58,7 +59,6 @@ public interface IAssemblyConstraintSupport extends IValueConstraintSupport {
@NotNull
List<@NotNull ? extends ICardinalityConstraint> getHasCardinalityConstraints();


void addConstraint(@NotNull IIndexConstraint constraint);

void addConstraint(@NotNull IUniqueConstraint constraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import org.jetbrains.annotations.Nullable;

/**
* Represents a rule requiring a Metaschema assembly data instance to have elements with a minimum and/or maximum occurrence.
* Represents a rule requiring a Metaschema assembly data instance to have elements with a minimum
* and/or maximum occurrence.
*/
public interface ICardinalityConstraint extends IConstraint {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.jetbrains.annotations.NotNull;

/**
* Represents a rule that generates a key-based index containing references to data items found in a Metaschema data instance.
* Represents a rule that generates a key-based index containing references to data items found in a
* Metaschema data instance.
* <p>
* The generated index can be used to check cross-references between Metaschema data objects using the {@link IIndexHasKeyConstraint}.
* The generated index can be used to check cross-references between Metaschema data objects using
* the {@link IIndexHasKeyConstraint}.
*/
public interface IIndexConstraint extends IKeyConstraint {
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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.model.common.constraint;

import org.jetbrains.annotations.NotNull;
Expand All @@ -33,8 +34,10 @@
public interface IScopedContraints {
@NotNull
URI getMetaschemaNamespace();

@NotNull
String getMetaschemaShortName();

@NotNull
List<@NotNull ITargetedConstaints> getTargetedContraints();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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.model.common.constraint;

import gov.nist.secauto.metaschema.model.common.IAssemblyDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
package gov.nist.secauto.metaschema.model.common.constraint;

/**
* Represents a rule that requires all matching data items found in a Metaschema data instance to have a unique key.
* Represents a rule that requires all matching data items found in a Metaschema data instance to
* have a unique key.
* <p>
* This rule is similar to the {@link IIndexConstraint} in how the keys are generated, but this constraint type does not persist a named index.
* This rule is similar to the {@link IIndexConstraint} in how the keys are generated, but this
* constraint type does not persist a named index.
*/
public interface IUniqueConstraint extends IKeyConstraint {

Expand Down
Loading

0 comments on commit 13e0fee

Please sign in to comment.