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 14, 2023
1 parent a316ea2 commit d1c06d8
Show file tree
Hide file tree
Showing 24 changed files with 276 additions and 223 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
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 @@ -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
Loading

0 comments on commit d1c06d8

Please sign in to comment.