forked from metaschema-framework/metaschema-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add function-lookup impl and tests for metaschema-framework#301
- Loading branch information
1 parent
f457b9f
commit 0541b88
Showing
4 changed files
with
140 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
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
86 changes: 86 additions & 0 deletions
86
...ain/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnFunctionLookup.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,86 @@ | ||
/* | ||
* 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.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> | ||
* functions. | ||
*/ | ||
public final class FnFunctionLookup { | ||
@NonNull | ||
private static final String NAME = "function-lookup"; | ||
|
||
@NonNull | ||
static final IFunction SIGNATURE= IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextIndependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("name") | ||
.type(IStringItem.type()) | ||
.one() | ||
.build()) | ||
.argument(IArgument.builder() | ||
.name("arity") | ||
.type(IIntegerItem.type()) | ||
.one() | ||
.build()) | ||
.returnType(IItemType.function()) | ||
.returnZeroOrOne() | ||
.functionHandler(FnFunctionLookup::execute) | ||
.build(); | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<IFunction> execute(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
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; | ||
|
||
try { | ||
matchingFunction = dynamicContext.getStaticContext().lookupFunction(name.asString(), arity.asInteger().intValueExact()); | ||
} catch (StaticMetapathException ex) { | ||
if (ex.getCode() != StaticMetapathException.NO_FUNCTION_MATCH) { | ||
throw ex; | ||
} | ||
matchingFunction = null; | ||
} | ||
|
||
return ISequence.of(matchingFunction); | ||
} | ||
|
||
private FnFunctionLookup() { | ||
// disable construction | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...java/gov/nist/secauto/metaschema/core/metapath/function/library/FnFunctionLookupTest.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,46 @@ | ||
/* | ||
* 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.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; | ||
|
||
class FnFunctionLookupTest | ||
extends ExpressionTestBase { | ||
|
||
private static Stream<Arguments> provideValues() { // NOPMD - false positive | ||
return Stream.of( | ||
Arguments.of( | ||
string("bcd"), | ||
"function-lookup('substring', 2)('abcd', 2)")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValues") | ||
void test(@NonNull IItem expected, @NonNull String metapath) { | ||
assertEquals(expected, IMetapathExpression.compile(metapath).evaluateAs(null, ResultType.ITEM, newDynamicContext())); | ||
} | ||
} |