Skip to content

Commit

Permalink
Support for fn:current-date (#274)
Browse files Browse the repository at this point in the history
* Added the fn:current-date Metapath function in support of #162.

* Correct URL link to XPath current-date, not current-dateTime docs

---------

Co-authored-by: A.J. Stein <[email protected]>
  • Loading branch information
david-waltermire and aj-stein-gsa authored Dec 4, 2024
1 parent 55ee739 commit 793e73e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-current-date">fn:current-date</a>
* 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<IDateItem> execute(@NonNull IFunction function,
@NonNull List<ISequence<?>> arguments,
@NonNull DynamicContext dynamicContext,
IItem focus) {
return ISequence.of(fnCurrentDate(dynamicContext));
}

/**
* Implements <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-current-date">fn:current-date</a>.
*
* @param dynamicContext
* the dynamic evaluation context
* @return the current date
*/
@NonNull
public static IDateItem fnCurrentDate(@NonNull DynamicContext dynamicContext) {
return IDateWithTimeZoneItem.valueOf(dynamicContext.getCurrentDateTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/**
* Implements the XPath 3.1 <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-current-dateTime">fn:current-dateTime</a>
* "https://www.w3.org/TR/xpath-functions-31/#func-current-time">fn:current-time</a>
* function.
*/
public final class FnCurrentTime {
Expand All @@ -45,19 +45,19 @@ private static ISequence<ITimeItem> execute(@NonNull IFunction function,
@NonNull List<ISequence<?>> arguments,
@NonNull DynamicContext dynamicContext,
IItem focus) {
return ISequence.of(fnCurrentDateTime(dynamicContext));
return ISequence.of(fnCurrentTime(dynamicContext));
}

/**
* Implements <a href=
* "https://www.w3.org/TR/xpath-functions-31/#func-current-dateTime">fn:current-dateTime</a>.
* "https://www.w3.org/TR/xpath-functions-31/#func-current-time">fn:current-time</a>.
*
* @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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +85,7 @@ private static ISequence<IIntegerItem> executeTwoArg(@NonNull IFunction function
* @return a list of index numbers indicating the position of matches in the
* sequence
*/
@NonNull
public static ISequence<IIntegerItem> fnIndexOf(@NonNull List<IAnyAtomicItem> items,
@NonNull IAnyAtomicItem search) {
int index = 0;
Expand All @@ -94,9 +96,13 @@ public static ISequence<IIntegerItem> fnIndexOf(@NonNull List<IAnyAtomicItem> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ private static Stream<Arguments> 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')"),
Expand Down

0 comments on commit 793e73e

Please sign in to comment.