Skip to content

Commit

Permalink
Adjusted HasOscalNamespace to work more generally for all assemblies …
Browse files Browse the repository at this point in the history
…with a "ns" namespace flag. It honors the default value for this flag if set. This will allow models using the generic metaschema validate-content command to work properly and will help defend against future changes.
  • Loading branch information
david-waltermire committed Sep 26, 2023
1 parent 5bd4245 commit 703f85e
Showing 1 changed file with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@
import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
import gov.nist.secauto.metaschema.core.metapath.function.InvalidTypeFunctionException;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
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.metapath.item.node.IFlagNodeItem;
import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
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;
import gov.nist.secauto.oscal.lib.model.metadata.AbstractProperty;

import java.net.URI;
Expand Down Expand Up @@ -152,16 +154,29 @@ public static IBooleanItem hasNamespace(
throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, propOrPart);
}

URI nodeNamespace;
if (propOrPartObject instanceof Property) {
nodeNamespace = ((Property) propOrPartObject).getNs();
} else if (propOrPartObject instanceof ControlPart) {
nodeNamespace = ((ControlPart) propOrPartObject).getNs();
} else if (propOrPartObject instanceof AssessmentPart) {
nodeNamespace = ((AssessmentPart) propOrPartObject).getNs();
URI nodeNamespace = null;
// get the "ns" flag value
IFlagNodeItem ns = propOrPart.getFlagByName("ns");
if (ns == null) {
// check if the node actually has a "ns" flag
IAssemblyDefinition definition = propOrPart.getDefinition();
IFlagInstance flag = definition.getFlagInstanceByName("ns");
if (flag == null) {
throw new MetapathException(
String.format(
"Node at path '%s' bound to '%s' based on the assembly definition '%s' has no OSCAL namespace",
propOrPart.getMetapath(),
propOrPart.getClass().getName(),
propOrPart.getDefinition().getName()));

}

Object defaultValue = flag.getDefinition().getDefaultValue();
if (defaultValue != null) {
nodeNamespace = IAnyUriItem.valueOf(ObjectUtils.notNull(defaultValue.toString())).getValue();
}
} else {
throw new MetapathException(
String.format("Node of definition type '%s' has no OSCAL namespace", propOrPart.getDefinition().getName()));
nodeNamespace = IAnyUriItem.cast(FnData.fnDataItem(ns)).getValue();
}

String nodeNamespaceString = AbstractProperty.normalizeNamespace(nodeNamespace).toString();
Expand Down

0 comments on commit 703f85e

Please sign in to comment.