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 28, 2023
1 parent a316ea2 commit 4905565
Show file tree
Hide file tree
Showing 43 changed files with 495 additions and 396 deletions.
15 changes: 2 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>gov.nist.secauto</groupId>
<artifactId>oss-parent</artifactId>
<version>26</version>
<version>27-SNAPSHOT</version>
</parent>

<groupId>gov.nist.secauto.oscal</groupId>
Expand Down Expand Up @@ -243,18 +243,7 @@

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
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
68 changes: 0 additions & 68 deletions src/main/java/gov/nist/secauto/oscal/lib/OscalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,19 @@

package gov.nist.secauto.oscal.lib;

import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream;

import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.model.BackMatter.Resource;
import gov.nist.secauto.oscal.lib.model.BackMatter.Resource.Base64;
import gov.nist.secauto.oscal.lib.model.BackMatter.Resource.Rlink;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.umd.cs.findbugs.annotations.NonNull;
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 Expand Up @@ -103,37 +92,6 @@ public static String internalReferenceFragmentToId(@NonNull String fragment) {
return retval;
}

public static boolean hasBase64Data(@NonNull Resource resource) {
return resource.getBase64() != null;
}

@Nullable
public static ByteBuffer getBase64Data(@NonNull Resource resource) {
Base64 base64 = resource.getBase64();

ByteBuffer retval = null;
if (base64 != null) {
retval = base64.getValue();
}
return retval;
}

@Nullable
public static URI getResourceURI(@NonNull Resource resource, @Nullable String preferredMediaType) {
URI retval;
if (hasBase64Data(resource)) {
UUID uuid = resource.getUuid();
if (uuid == null) {
throw new IllegalArgumentException("resource has a null UUID");
}
retval = ObjectUtils.notNull(URI.create("#" + uuid));
} else {
Rlink rlink = findMatchingRLink(resource, preferredMediaType);
retval = rlink == null ? null : rlink.getHref();
}
return retval;
}

@Nullable
public static Rlink findMatchingRLink(@NonNull Resource resource, @Nullable String preferredMediaType) {
// find a suitable rlink reference
Expand All @@ -153,30 +111,4 @@ public static Rlink findMatchingRLink(@NonNull Resource resource, @Nullable Stri
}
return retval;
}

@Nullable
public static InputSource newInputSource(@NonNull Resource resource, @NonNull EntityResolver resolver,
@Nullable String preferredMediaType) throws IOException {
URI uri = getResourceURI(resource, preferredMediaType);
if (uri == null) {
throw new IOException(String.format("unable to determine URI for resource '%s'", resource.getUuid()));
}

InputSource retval;
try {
retval = resolver.resolveEntity(null, uri.toASCIIString());
} catch (SAXException ex) {
throw new IOException(ex);
}

if (hasBase64Data(resource)) {
// handle base64 encoded data
ByteBuffer buffer = getBase64Data(resource);
if (buffer == null) {
throw new IOException(String.format("null base64 value for resource '%s'", resource.getUuid()));
}
retval.setByteStream(new ByteBufferBackedInputStream(buffer));
}
return retval;
}
}
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 @@ -48,7 +48,8 @@ public interface IControlContainer {
boolean addControl(@NonNull Control item);

/**
* Remove the first matching {@link Control} item from the underlying collection.
* Remove the first matching {@link Control} item from the underlying
* collection.
*
* @param item
* the item to remove
Expand All @@ -68,7 +69,8 @@ public interface IControlContainer {
boolean addParam(@NonNull Parameter item);

/**
* Remove the first matching {@link Parameter} item from the underlying collection.
* Remove the first matching {@link Parameter} item from the underlying
* collection.
*
* @param item
* the item to remove
Expand All @@ -77,7 +79,8 @@ public interface IControlContainer {
boolean removeParam(@NonNull Parameter item);

/**
* Get the parameter identifiers referenced in the object's context, but not by their child objects.
* Get the parameter identifiers referenced in the object's context, but not by
* their child objects.
*
* @return a stream of identifiers
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public interface IGroupContainer extends IControlContainer {
boolean addGroup(@NonNull CatalogGroup item);

/**
* Remove the first matching {@link CatalogGroup} item from the underlying collection.
* Remove the first matching {@link CatalogGroup} item from the underlying
* collection.
*
* @param item
* the item to remove
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public Builder uuid(@NonNull UUID uuid) {
return this;
}

@SuppressWarnings("PMD.NullAssignment") // needed
@NonNull
public Builder namespace(@NonNull URI namespace) {
if (IProperty.OSCAL_NAMESPACE.equals(namespace)) {
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 @@ -47,8 +47,8 @@ public ProfileResolutionException(String message) {
}

/**
* Create a new profile resolution exception with the provided {@code message} based on the provided
* {@code cause}.
* Create a new profile resolution exception with the provided {@code message}
* based on the provided {@code cause}.
*
* @param message
* a description of the error that occurred
Expand Down
Loading

0 comments on commit 4905565

Please sign in to comment.