Skip to content

Commit

Permalink
Integrating further metaschema-java refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Aug 4, 2023
1 parent a316ea2 commit c1ac094
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

package gov.nist.secauto.oscal.lib;

import gov.nist.secauto.metaschema.binding.DefaultBindingContext;
import gov.nist.secauto.metaschema.binding.IBindingMatcher;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
import gov.nist.secauto.metaschema.databind.DefaultBindingContext;
import gov.nist.secauto.metaschema.databind.IBindingMatcher;
import gov.nist.secauto.oscal.lib.model.AssessmentPlan;
import gov.nist.secauto.oscal.lib.model.AssessmentResults;
import gov.nist.secauto.oscal.lib.model.Catalog;
Expand All @@ -51,6 +51,8 @@
public class OscalBindingContext
extends DefaultBindingContext {
@NonNull
public static final String NS_OSCAL = "http://csrc.nist.gov/ns/oscal/1.0";
@NonNull
private static final OscalBindingContext SINGLETON = new OscalBindingContext();

@NonNull
Expand Down Expand Up @@ -186,7 +188,7 @@ private static final class Matcher implements IBindingMatcher {
@Override
public Class<?> getBoundClassForXmlQName(QName startElementQName) {
Class<?> clazz = null;
if ("http://csrc.nist.gov/ns/oscal/1.0".equals(startElementQName.getNamespaceURI())) {
if (NS_OSCAL.equals(startElementQName.getNamespaceURI())) {
switch (startElementQName.getLocalPart()) {
case "catalog":
clazz = Catalog.class;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/gov/nist/secauto/oscal/lib/OscalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import edu.umd.cs.findbugs.annotations.Nullable;

public final class OscalUtils {
public static final String OSCAL_VERSION = "1.0.4";
private static final Pattern INTERNAL_REFERENCE_FRAGMENT_PATTERN = Pattern.compile("^#(.+)$");

private OscalUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.model.AssessmentPart;
import gov.nist.secauto.oscal.lib.model.ControlPart;
import gov.nist.secauto.oscal.lib.model.Property;
Expand All @@ -52,6 +53,7 @@ public final class HasOscalNamespace {
@NonNull
static final IFunction SIGNATURE_ONE_ARG = IFunction.builder()
.name("has-oscal-namespace")
.namespace(OscalBindingContext.NS_OSCAL)
.argument(IArgument.newBuilder()
.name("namespace")
.type(IStringItem.class)
Expand All @@ -69,6 +71,7 @@ public final class HasOscalNamespace {
@NonNull
static final IFunction SIGNATURE_TWO_ARGS = IFunction.builder()
.name("has-oscal-namespace")
.namespace(OscalBindingContext.NS_OSCAL)
.argument(IArgument.newBuilder()
.name("propOrPart")
.type(IAssemblyNodeItem.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.model.Catalog;
import gov.nist.secauto.oscal.lib.profile.resolver.ProfileResolutionException;
import gov.nist.secauto.oscal.lib.profile.resolver.ProfileResolver;
Expand All @@ -50,6 +51,7 @@ public final class ResolveProfile {
@NonNull
static final IFunction SIGNATURE_NO_ARG = IFunction.builder()
.name("resolve-profile")
.namespace(OscalBindingContext.NS_OSCAL)
.returnType(INodeItem.class)
.focusDependent()
.contextDependent()
Expand All @@ -61,6 +63,7 @@ public final class ResolveProfile {
@NonNull
static final IFunction SIGNATURE_ONE_ARG = IFunction.builder()
.name("resolve-profile")
.namespace(OscalBindingContext.NS_OSCAL)
.argument(IArgument.newBuilder()
.name("profile")
.type(INodeItem.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

package gov.nist.secauto.oscal.lib.model.control.catalog;

import gov.nist.secauto.metaschema.binding.io.IDeserializationHandler;
import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.io.IDeserializationHandler;
import gov.nist.secauto.oscal.lib.model.Control;
import gov.nist.secauto.oscal.lib.model.ControlPart;
import gov.nist.secauto.oscal.lib.model.Link;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@

package gov.nist.secauto.oscal.lib.model.metadata;

import gov.nist.secauto.oscal.lib.OscalBindingContext;

import java.net.URI;

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

public interface IProperty {
@SuppressWarnings("null")
@NonNull
URI OSCAL_NAMESPACE = URI.create("http://csrc.nist.gov/ns/oscal");
URI OSCAL_NAMESPACE = URI.create(OscalBindingContext.NS_OSCAL);
@SuppressWarnings("null")
@NonNull
URI RMF_NAMESPACE = URI.create("http://csrc.nist.gov/ns/rmf");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@

package gov.nist.secauto.oscal.lib.profile.resolver;

import gov.nist.secauto.metaschema.binding.io.BindingException;
import gov.nist.secauto.metaschema.binding.io.DeserializationFeature;
import gov.nist.secauto.metaschema.binding.io.IBoundLoader;
import gov.nist.secauto.metaschema.binding.model.IAssemblyClassBinding;
import gov.nist.secauto.metaschema.binding.model.RootAssemblyDefinition;
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
Expand All @@ -45,6 +40,11 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.IRootAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.io.BindingException;
import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
import gov.nist.secauto.metaschema.databind.model.IAssemblyClassBinding;
import gov.nist.secauto.metaschema.databind.model.RootAssemblyDefinition;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.OscalUtils;
import gov.nist.secauto.oscal.lib.model.BackMatter;
Expand Down Expand Up @@ -155,7 +155,7 @@ public void setBoundLoader(@NonNull IBoundLoader loader) {
public DynamicContext getDynamicContext() {
synchronized (this) {
if (dynamicContext == null) {
dynamicContext = new StaticContext().newDynamicContext();
dynamicContext = StaticContext.builder().build().newDynamicContext();
dynamicContext.setDocumentLoader(getBoundLoader());
}
assert dynamicContext != null;
Expand Down Expand Up @@ -377,7 +377,8 @@ protected IIndexer resolveImport(
IDocumentNodeItem document = getDynamicContext().getDocumentLoader().loadAsNodeItem(source);
IDocumentNodeItem importedCatalog = resolve(document, importHistory);

// Create a defensive deep copy of the document and associated values, since we will be making
// Create a defensive deep copy of the document and associated values, since we
// will be making
// changes to the data.
try {
IRootAssemblyNodeItem importedCatalogRoot = ObjectUtils.requireNonNull(getRoot(importedCatalog, CATALOG));
Expand Down Expand Up @@ -507,14 +508,17 @@ protected void structureFlat(@NonNull Catalog resolvedCatalog, @NonNull IRootAss

// {
// // rebuild an index
// IDocumentNodeItem resolvedCatalogItem = DefaultNodeItemFactory.instance().newDocumentNodeItem(
// IDocumentNodeItem resolvedCatalogItem =
// DefaultNodeItemFactory.instance().newDocumentNodeItem(
// new RootAssemblyDefinition(
// ObjectUtils.notNull(
// (IAssemblyClassBinding) OscalBindingContext.instance().getClassBinding(Catalog.class))),
// (IAssemblyClassBinding)
// OscalBindingContext.instance().getClassBinding(Catalog.class))),
// resolvedCatalog,
// profileDocument.getBaseUri());
//
// // FIXME: need to find a better way to create an index that doesn't auto select groups
// // FIXME: need to find a better way to create an index that doesn't auto
// select groups
// IIndexer indexer = new BasicIndexer();
// ControlSelectionVisitor selectionVisitor
// = new ControlSelectionVisitor(IControlFilter.ALWAYS_MATCH, indexer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

package gov.nist.secauto.oscal.lib.profile.resolver.merge;

import gov.nist.secauto.oscal.lib.model.BackMatter.Resource;
import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IRootAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.model.BackMatter.Resource;
import gov.nist.secauto.oscal.lib.model.CatalogGroup;
import gov.nist.secauto.oscal.lib.model.Control;
import gov.nist.secauto.oscal.lib.model.ControlPart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void setInstance(IModelNodeItem<?, ?> item) {
@NonNull
@SuppressWarnings("unchecked")
public <T> T getInstanceValue() {
return (T) getInstance().getValue();
return ObjectUtils.notNull((T) getInstance().getValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

package gov.nist.secauto.oscal.lib.profile.resolver.support;

import gov.nist.secauto.oscal.lib.model.BackMatter.Resource;
import gov.nist.secauto.metaschema.core.datatype.adapter.UuidAdapter;
import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
import gov.nist.secauto.metaschema.core.metapath.MetapathExpression.ResultType;
import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.model.BackMatter.Resource;
import gov.nist.secauto.oscal.lib.model.CatalogGroup;
import gov.nist.secauto.oscal.lib.model.Control;
import gov.nist.secauto.oscal.lib.model.ControlPart;
Expand Down Expand Up @@ -190,11 +190,14 @@ public Collection<IEntityItem> getEntitiesByItemType(@NonNull IEntityItem.ItemTy
return entityGroup == null ? CollectionUtil.emptyList() : ObjectUtils.notNull(entityGroup.values());
}
//
// public EntityItem getEntity(@NonNull ItemType itemType, @NonNull UUID identifier) {
// return getEntity(itemType, ObjectUtils.notNull(identifier.toString()), false);
// public EntityItem getEntity(@NonNull ItemType itemType, @NonNull UUID
// identifier) {
// return getEntity(itemType, ObjectUtils.notNull(identifier.toString()),
// false);
// }
//
// public EntityItem getEntity(@NonNull ItemType itemType, @NonNull String identifier) {
// public EntityItem getEntity(@NonNull ItemType itemType, @NonNull String
// identifier) {
// return getEntity(itemType, identifier, itemType.isUuid());
// }

Expand Down Expand Up @@ -327,10 +330,11 @@ protected final AbstractEntityItem.Builder newBuilder(
/**
* Create a new builder with the provided info.
* <p>
* This method can be overloaded to support applying additional data to the returned builder.
* This method can be overloaded to support applying additional data to the
* returned builder.
* <p>
* When working with identifiers that are case insensitve, it is important to ensure that the
* identifiers are normalized to lower case.
* When working with identifiers that are case insensitve, it is important to
* ensure that the identifiers are normalized to lower case.
*
* @param item
* the Metapath node to associate with the entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.CustomCollectors;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.profile.resolver.support.IEntityItem.ItemType;

import org.apache.logging.log4j.Level;
Expand All @@ -58,7 +59,7 @@ enum SelectionStatus {
}

MetapathExpression HAS_PROP_KEEP_METAPATH = MetapathExpression
.compile("prop[@name='keep' and has-oscal-namespace('http://csrc.nist.gov/ns/oscal')]/@value = 'always'");
.compile("prop[@name='keep' and has-oscal-namespace('" + OscalBindingContext.NS_OSCAL + "')]/@value = 'always'");

Predicate<IEntityItem> KEEP_ENTITY_PREDICATE = new Predicate<>() {

Expand All @@ -76,8 +77,8 @@ static boolean isReferencedEntity(@NonNull IEntityItem entity) {
}

/**
* Keep entities that have a reference count greater than zero or are required to be kept based on
* the "keep"="always property.
* Keep entities that have a reference count greater than zero or are required
* to be kept based on the "keep"="always property.
*
* @param entities
* the entity items to filter
Expand All @@ -88,8 +89,8 @@ static Stream<IEntityItem> getReferencedEntitiesAsStream(@NonNull Collection<IEn
}

/**
* Keep entities that have a reference count of zero or are not required to be kept based on the
* "keep"="always property.
* Keep entities that have a reference count of zero or are not required to be
* kept based on the "keep"="always property.
*
* @param entities
* the entity items to filter
Expand All @@ -100,22 +101,25 @@ static Stream<IEntityItem> getUnreferencedEntitiesAsStream(@NonNull Collection<I
}

/**
* Generates a stream of distinct items that have a reference count greater than zero or are
* required to be kept based on the "keep"="always property.
* Generates a stream of distinct items that have a reference count greater than
* zero or are required to be kept based on the "keep"="always property.
* <p>
* Distinct items are determined based on the item's key using the provided {@code keyMapper}.
* Distinct items are determined based on the item's key using the provided
* {@code keyMapper}.
*
* @param <T>
* the item type
* @param <K>
* the key type
* @param resolvedItems
* a series of previously resolved items to add to prepend to the stream
* a series of previously resolved items to add to prepend to the
* stream
* @param importedEntityItems
* a collection of new items to filter then append to the stream
* @param keyMapper
* the key mapping function to determine the item's key
* @return the resulting series of items with duplicate items with the same key removed
* @return the resulting series of items with duplicate items with the same key
* removed
*/
// TODO: Is this the right name for this method?
static <T, K> Stream<T> filterDistinct(
Expand Down Expand Up @@ -193,7 +197,8 @@ default IEntityItem getEntity(@NonNull IEntityItem.ItemType itemType, @NonNull U
}

/**
* Lookup an item of the given {@code itemType} having the given {@code identifier}.
* Lookup an item of the given {@code itemType} having the given
* {@code identifier}.
* <p>
* Will normalize the case of a UUID-based identifier.
*
Expand All @@ -209,7 +214,8 @@ default IEntityItem getEntity(@NonNull IEntityItem.ItemType itemType, @NonNull S
}

/**
* Lookup an item of the given {@code itemType} having the given {@code identifier}.
* Lookup an item of the given {@code itemType} having the given
* {@code identifier}.
* <p>
* Will normalize the case of a UUID-based the identifier when requested.
*
Expand All @@ -218,7 +224,8 @@ default IEntityItem getEntity(@NonNull IEntityItem.ItemType itemType, @NonNull S
* @param identifier
* the identifier to lookup
* @param normalize
* {@code true} if the identifier case should be normalized or {@code false} otherwise
* {@code true} if the identifier case should be normalized or
* {@code false} otherwise
* @return the matching item or {@code null} if no match was found
*/
@Nullable
Expand Down
15 changes: 7 additions & 8 deletions src/test/java/gov/nist/secauto/oscal/java/ExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import gov.nist.secauto.metaschema.binding.io.DeserializationFeature;
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 gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.model.constraint.DefaultConstraintValidator;
import gov.nist.secauto.metaschema.core.model.constraint.FindingCollectingConstraintValidationHandler;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
import gov.nist.secauto.metaschema.databind.io.Format;
import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
import gov.nist.secauto.metaschema.databind.io.ISerializer;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.model.Catalog;
import gov.nist.secauto.oscal.lib.profile.resolver.ProfileResolutionException;
Expand All @@ -65,9 +65,8 @@ void simpleLoadAndSave() throws IOException {
IBoundLoader loader = bindingContext.newBoundLoader(); // supports loading OSCAL documents

// load an OSCAL catalog
Catalog catalog = loader.load(ObjectUtils.notNull(Paths.get("src/test/resources/content/test-catalog.xml"))); // load
// the
// catalog
Catalog catalog = loader.load(
ObjectUtils.requireNonNull(Paths.get("src/test/resources/content/test-catalog.xml"))); // load the catalog
assertNotNull(catalog);

// Create a serializer which can be used to write multiple catalogs
Expand All @@ -93,7 +92,7 @@ void testConstraintValidation()
IDocumentNodeItem nodeItem = loader.loadAsNodeItem(new URL(
"https://raw.githubusercontent.com/Rene2mt/fedramp-automation/a692b9385d8fbcacbb1d3e3d0b0d7e3c45a205d0/src/content/baselines/rev5/xml/FedRAMP_rev5_HIGH-baseline_profile.xml"));

DynamicContext dynamicContext = new StaticContext().newDynamicContext();
DynamicContext dynamicContext = StaticContext.builder().build().newDynamicContext();
dynamicContext.setDocumentLoader(loader);
FindingCollectingConstraintValidationHandler handler = new FindingCollectingConstraintValidationHandler();
DefaultConstraintValidator validator = new DefaultConstraintValidator(dynamicContext, handler);
Expand Down
Loading

0 comments on commit c1ac094

Please sign in to comment.