Skip to content

Commit

Permalink
Removed compile and PMD errors and warnings, mostly related to string…
Browse files Browse the repository at this point in the history
…-join. Adjusted function name resolution, to use the function name resolver, instead of the variable name resolver to ensure that the default function namespace is used.
  • Loading branch information
david-waltermire committed Dec 28, 2024
1 parent ca89ef9 commit fff9a35
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ public ISequence<? extends IItem> accept(DynamicContext dynamicContext, ISequenc
.collect(Collectors.toUnmodifiableList())),
dynamicContext,
focus);
} else if (collection instanceof IFunction) {
return ((IFunction) collection).execute( ObjectUtils.notNull(getArguments().stream()
.map(expr -> expr.accept(dynamicContext, focus))
.collect(Collectors.toUnmodifiableList())), dynamicContext, focus);
}
}
if (collection instanceof IFunction) {
return ((IFunction) collection).execute(ObjectUtils.notNull(getArguments().stream()
.map(expr -> expr.accept(dynamicContext, focus))
.collect(Collectors.toUnmodifiableList())), dynamicContext, focus);
}

// the value to find, which will be the key for a map or the index for an array
IExpression argument = getArguments().stream().findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,32 @@

import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.StaticMetapathException;
import gov.nist.secauto.metaschema.core.metapath.function.FunctionLibrary;
import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
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.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.metapath.type.IItemType;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.List;

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

/**
* /** Implements
* <a href= "https://www.w3.org/TR/xpath-functions-31/#func-function-lookup">fn:function-lookup</a>
* /** Implements <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-function-lookup">fn:function-lookup</a>
* functions.
*/
public final class FnFunctionLookup {
@NonNull
private static final String NAME = "function-lookup";

@NonNull
static final IFunction SIGNATURE= IFunction.builder()
static final IFunction SIGNATURE = IFunction.builder()
.name(NAME)
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS)
.deterministic()
Expand All @@ -52,7 +47,7 @@ public final class FnFunctionLookup {
.name("arity")
.type(IIntegerItem.type())
.one()
.build())
.build())
.returnType(IItemType.function())
.returnZeroOrOne()
.functionHandler(FnFunctionLookup::execute)
Expand All @@ -66,16 +61,17 @@ private static ISequence<IFunction> execute(@NonNull IFunction function,
IItem focus) {
IStringItem name = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true)));
IIntegerItem arity = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true)));
IFunction matchingFunction;
IFunction matchingFunction = null;

try {
matchingFunction = dynamicContext.getStaticContext().lookupFunction(name.asString(), arity.asInteger().intValueExact());
matchingFunction = dynamicContext.getStaticContext().lookupFunction(
name.asString(),
arity.asInteger().intValueExact());
} catch (StaticMetapathException ex) {
if (ex.getCode() != StaticMetapathException.NO_FUNCTION_MATCH) {
throw ex;
}
matchingFunction = null;
}
if (ex.getCode() != StaticMetapathException.NO_FUNCTION_MATCH) {
throw ex;
}
}

return ISequence.of(matchingFunction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

/**
* Implements the XPath 3.1 <a href=
Expand All @@ -47,6 +47,7 @@ public final class FnStringJoin {
.functionHandler(FnStringJoin::execute)
.build();

@NonNull
static final IFunction SIGNATURE_TWO_ARG = IFunction.builder()
.name(NAME)
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS)
Expand All @@ -72,17 +73,17 @@ private FnStringJoin() {
// disable construction
}

@SuppressWarnings("unused")
@SuppressWarnings({ "unused", "PMD.OnlyOneReturn" })
@NonNull
private static ISequence<IStringItem> execute(
@NonNull IFunction function,
@NonNull List<ISequence<?>> arguments,
@NonNull DynamicContext dynamicContext,
IItem focus) {

ISequence<IAnyAtomicItem> arg1 = FunctionUtils.asType(arguments.get(0));
ISequence<IAnyAtomicItem> arg1 = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0)));
IStringItem arg2 = arguments.size() == 1 ? IStringItem.valueOf("")
: FunctionUtils.asType(arguments.get(1).getFirstItem(true));
: FunctionUtils.asTypeOrNull(arguments.get(1).getFirstItem(true));

if (arg1.isEmpty()) {
return ISequence.of(IStringItem.valueOf(""));
Expand All @@ -102,10 +103,12 @@ private static ISequence<IStringItem> execute(
* @return the atomized result
*/
@NonNull
public static IStringItem fnStringJoin(@NonNull List<? extends IAnyAtomicItem> items, IStringItem separator) {
return IStringItem
.valueOf(stringJoin(items.stream().map(item -> item == null ? "" : IStringItem.cast(item).asString()),
separator == null ? "" : separator.asString()));
public static IStringItem fnStringJoin(
@NonNull List<? extends IAnyAtomicItem> items,
@Nullable IStringItem separator) {
return IStringItem.valueOf(stringJoin(
ObjectUtils.notNull(items.stream().map(item -> item == null ? "" : IStringItem.cast(item).asString())),
separator == null ? "" : separator.asString()));
}

/**
Expand All @@ -119,7 +122,9 @@ public static IStringItem fnStringJoin(@NonNull List<? extends IAnyAtomicItem> i
* @return the atomized result
*/
@NonNull
private static String stringJoin(@NonNull Stream<String> items, String separator) {
private static String stringJoin(
@NonNull Stream<String> items,
@NonNull String separator) {
return ObjectUtils.notNull(items.collect(Collectors.joining(separator)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@

import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase;
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression;
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression.ResultType;
import gov.nist.secauto.metaschema.core.metapath.MetapathException;
import gov.nist.secauto.metaschema.core.metapath.function.regex.RegularExpressionMetapathException;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -41,6 +34,7 @@ private static Stream<Arguments> provideValues() { // NOPMD - false positive
@ParameterizedTest
@MethodSource("provideValues")
void test(@NonNull IItem expected, @NonNull String metapath) {
assertEquals(expected, IMetapathExpression.compile(metapath).evaluateAs(null, ResultType.ITEM, newDynamicContext()));
assertEquals(expected,
IMetapathExpression.compile(metapath).evaluateAs(null, ResultType.ITEM, newDynamicContext()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase;
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression;
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;

import org.junit.jupiter.params.ParameterizedTest;
Expand Down

0 comments on commit fff9a35

Please sign in to comment.