-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed the fn:index-of implementation, which has been adjusted to …
…use the value comparison. (#207) Co-authored-by: David Waltermire <[email protected]>
- Loading branch information
1 parent
dff71d3
commit 55ee739
Showing
3 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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.ComparisonFunctions; | ||
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.IAnyAtomicItem; | ||
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; | ||
import gov.nist.secauto.metaschema.core.util.ObjectUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* /** Implements <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-index-of">fn:index-of</a> | ||
* functions. This implementation does not implement the three-arg variant with | ||
* collation at this time. | ||
*/ | ||
public final class FnIndexOf { | ||
@NonNull | ||
private static final String NAME = "index-of"; | ||
@NonNull | ||
static final IFunction SIGNATURE_TWO_ARG = IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextDependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("seq") | ||
.type(IAnyAtomicItem.type()) | ||
.zeroOrMore() | ||
.build()) | ||
.argument(IArgument.builder() | ||
.name("search") | ||
.type(IAnyAtomicItem.type()) | ||
.one() | ||
.build()) | ||
.returnType(IIntegerItem.type()) | ||
.returnZeroOrMore() | ||
.functionHandler(FnIndexOf::executeTwoArg) | ||
.build(); | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<IIntegerItem> executeTwoArg(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
ISequence<IAnyAtomicItem> seq = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); | ||
IAnyAtomicItem search = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); | ||
|
||
if (seq.size() == 0) { | ||
return ISequence.empty(); | ||
} | ||
return fnIndexOf(seq, search); | ||
} | ||
|
||
/** | ||
* Determine if the string provided in the first argument contains the string in | ||
* the second argument as a substring. | ||
* <p> | ||
* Based on the XPath 3.1 <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-index-of">fn:index-of</a> | ||
* function. | ||
* | ||
* @param items | ||
* the items to match against | ||
* @param search | ||
* the item to match | ||
* @return a list of index numbers indicating the position of matches in the | ||
* sequence | ||
*/ | ||
public static ISequence<IIntegerItem> fnIndexOf(@NonNull List<IAnyAtomicItem> items, | ||
@NonNull IAnyAtomicItem search) { | ||
int index = 0; | ||
ListIterator<IAnyAtomicItem> iterator = items.listIterator(); | ||
List<IIntegerItem> indices = new ArrayList<>(); | ||
while (iterator.hasNext()) { | ||
++index; | ||
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)); | ||
} | ||
} | ||
return ISequence.ofCollection(indices); | ||
} | ||
|
||
private FnIndexOf() { | ||
// disable construction | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...c/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnIndexOfTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SPDX-FileCopyrightText: none | ||
* SPDX-License-Identifier: CC0-1.0 | ||
*/ | ||
|
||
package gov.nist.secauto.metaschema.core.metapath.function.library; | ||
|
||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.sequence; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
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.IIntegerItem; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
class FnIndexOfTest | ||
extends ExpressionTestBase { | ||
private static Stream<Arguments> provideValues() { // NOPMD - false positive | ||
return Stream.of( | ||
Arguments.of( | ||
sequence(), | ||
"index-of((10, 20, 30, 40), 35)"), | ||
Arguments.of( | ||
sequence(integer(2), integer(5)), | ||
"index-of((10, 20, 30, 30, 20, 10), 20)"), | ||
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( | ||
sequence(integer(1)), | ||
"index-of((true()), 'true')"), | ||
Arguments.of( | ||
sequence(integer(3), integer(4)), | ||
"index-of([1, [5, 6], [6, 7]], 6)")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValues") | ||
void test(@NonNull ISequence<IIntegerItem> expected, @NonNull String metapath) { | ||
assertEquals(expected, IMetapathExpression.compile(metapath) | ||
.evaluate(null, newDynamicContext())); | ||
} | ||
} |