diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java index 419cb21aa..6501c8426 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java @@ -50,7 +50,8 @@ public DefaultFunctionLibrary() { // NOPMD - intentional registerFunction(FnContains.SIGNATURE); // https://www.w3.org/TR/xpath-functions-31/#func-count registerFunction(FnCount.SIGNATURE); - // P2: https://www.w3.org/TR/xpath-functions-31/#func-current-date + // https://www.w3.org/TR/xpath-functions-31/#func-current-date + registerFunction(FnCurrentDate.SIGNATURE); // https://www.w3.org/TR/xpath-functions-31/#func-current-dateTime registerFunction(FnCurrentDateTime.SIGNATURE); // https://www.w3.org/TR/xpath-functions-31/#func-current-time diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentDate.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentDate.java new file mode 100644 index 000000000..bedcf0d6c --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentDate.java @@ -0,0 +1,63 @@ +/* + * SPDX-FileCopyrightText: none + * SPDX-License-Identifier: CC0-1.0 + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +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.IDateItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateWithTimeZoneItem; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Implements the XPath 3.1 fn:current-date + * function. + */ +public final class FnCurrentDate { + @NonNull + static final IFunction SIGNATURE = IFunction.builder() + .name("current-date") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) + .deterministic() + .contextDependent() + .focusIndependent() + .returnType(IDateItem.type()) + .returnOne() + .functionHandler(FnCurrentDate::execute) + .build(); + + private FnCurrentDate() { + // disable construction + } + + @SuppressWarnings("unused") + @NonNull + private static ISequence execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + return ISequence.of(fnCurrentDate(dynamicContext)); + } + + /** + * Implements fn:current-date. + * + * @param dynamicContext + * the dynamic evaluation context + * @return the current date + */ + @NonNull + public static IDateItem fnCurrentDate(@NonNull DynamicContext dynamicContext) { + return IDateWithTimeZoneItem.valueOf(dynamicContext.getCurrentDateTime()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentTime.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentTime.java index bc648aa1f..435fe73e3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentTime.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCurrentTime.java @@ -19,7 +19,7 @@ /** * Implements the XPath 3.1 fn:current-dateTime + * "https://www.w3.org/TR/xpath-functions-31/#func-current-time">fn:current-time * function. */ public final class FnCurrentTime { @@ -45,19 +45,19 @@ private static ISequence execute(@NonNull IFunction function, @NonNull List> arguments, @NonNull DynamicContext dynamicContext, IItem focus) { - return ISequence.of(fnCurrentDateTime(dynamicContext)); + return ISequence.of(fnCurrentTime(dynamicContext)); } /** * Implements fn:current-dateTime. + * "https://www.w3.org/TR/xpath-functions-31/#func-current-time">fn:current-time. * * @param dynamicContext * the dynamic evaluation context * @return the current date */ @NonNull - public static ITimeItem fnCurrentDateTime(@NonNull DynamicContext dynamicContext) { + public static ITimeItem fnCurrentTime(@NonNull DynamicContext dynamicContext) { return ITimeWithTimeZoneItem.valueOf(dynamicContext.getCurrentDateTime()); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOf.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOf.java index 71e176121..e92baabe3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOf.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOf.java @@ -15,6 +15,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.ISequence; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.type.InvalidTypeMetapathException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.ArrayList; @@ -84,6 +85,7 @@ private static ISequence executeTwoArg(@NonNull IFunction function * @return a list of index numbers indicating the position of matches in the * sequence */ + @NonNull public static ISequence fnIndexOf(@NonNull List items, @NonNull IAnyAtomicItem search) { int index = 0; @@ -94,9 +96,13 @@ public static ISequence fnIndexOf(@NonNull List it IAnyAtomicItem item = iterator.next(); assert item != null; // use the "eq" operator - if (ComparisonFunctions.valueCompairison(item, ComparisonFunctions.Operator.EQ, search).toBoolean()) { - // Offset for Metapath indices that start from 1 - indices.add(IIntegerItem.valueOf(index)); + try { + if (ComparisonFunctions.valueCompairison(item, ComparisonFunctions.Operator.EQ, search).toBoolean()) { + // Offset for Metapath indices that start from 1 + indices.add(IIntegerItem.valueOf(index)); + } + } catch (InvalidTypeMetapathException ex) { + // this is an effective false on the match } } return ISequence.ofCollection(indices); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOfTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOfTest.java index 14544e77a..c82d8ba06 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOfTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOfTest.java @@ -35,11 +35,9 @@ private static Stream provideValues() { // NOPMD - false positive Arguments.of( sequence(integer(1), integer(4)), "index-of(('a', 'sport', 'and', 'a', 'pasttime'), 'a')"), - // TODO: add current-date() test after metaschema-framework/metaschema-java#162 - // complete - // Arguments.of( - // ISequence.empty(), - // "index-of(current-date(), 23)"), + Arguments.of( + ISequence.empty(), + "index-of(current-date(), 23)"), Arguments.of( sequence(integer(1)), "index-of((true()), 'true')"),