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.
Added support for the Metapath zero-or-one, one-or-more, and exactly-…
…one functions. Resolves metaschema-framework#280 (metaschema-framework#281)
- Loading branch information
1 parent
0d676b4
commit deb73be
Showing
9 changed files
with
491 additions
and
6 deletions.
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
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
83 changes: 83 additions & 0 deletions
83
...rc/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnExactlyOne.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,83 @@ | ||
/* | ||
* 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.IArgument; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IFunction; | ||
import gov.nist.secauto.metaschema.core.metapath.function.InvalidArgumentFunctionException; | ||
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 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-exactly-one">fn:exactly-one</a> | ||
* function. | ||
*/ | ||
public final class FnExactlyOne { | ||
private static final String NAME = "exactly-one"; | ||
@NonNull | ||
static final IFunction SIGNATURE = IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextIndependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("arg") | ||
.type(IItem.type()) | ||
.zeroOrMore() | ||
.build()) | ||
.returnType(IItem.type()) | ||
.returnOne() | ||
.functionHandler(FnExactlyOne::execute) | ||
.build(); | ||
|
||
private FnExactlyOne() { | ||
// disable construction | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<?> execute(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
return fnExactlyOne(ObjectUtils.requireNonNull(arguments.get(0))); | ||
} | ||
|
||
/** | ||
* Check that the provided sequence has exactly one item. | ||
* <p> | ||
* Based on the XPath 3.1 <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-exactly-one">fn:exactly-one</a> | ||
* function. | ||
* | ||
* @param sequence | ||
* the sequence to evaluate | ||
* @return the sequence if it has zero or one items | ||
* @throws InvalidArgumentFunctionException | ||
* with the code | ||
* {@link InvalidArgumentFunctionException#INVALID_ARGUMENT_EXACTLY_ONE} | ||
* if the sequence contains less or more than one item | ||
*/ | ||
@NonNull | ||
public static ISequence<?> fnExactlyOne(@NonNull ISequence<?> sequence) { | ||
if (sequence.size() != 1) { | ||
throw new InvalidArgumentFunctionException( | ||
InvalidArgumentFunctionException.INVALID_ARGUMENT_EXACTLY_ONE, | ||
String.format("fn:exactly-one called with the sequence '%s' containing a number of items other than one.", | ||
sequence.toSignature())); | ||
} | ||
return sequence; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnOneOrMore.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,83 @@ | ||
/* | ||
* 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.IArgument; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IFunction; | ||
import gov.nist.secauto.metaschema.core.metapath.function.InvalidArgumentFunctionException; | ||
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 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-one-or-more">fn:one-or-more</a> | ||
* function. | ||
*/ | ||
public final class FnOneOrMore { | ||
private static final String NAME = "one-or-more"; | ||
@NonNull | ||
static final IFunction SIGNATURE = IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextIndependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("arg") | ||
.type(IItem.type()) | ||
.zeroOrMore() | ||
.build()) | ||
.returnType(IItem.type()) | ||
.returnOneOrMore() | ||
.functionHandler(FnOneOrMore::execute) | ||
.build(); | ||
|
||
private FnOneOrMore() { | ||
// disable construction | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<?> execute(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
return fnOneOrMore(ObjectUtils.requireNonNull(arguments.get(0))); | ||
} | ||
|
||
/** | ||
* Check that the provided sequence has one or more items. | ||
* <p> | ||
* Based on the XPath 3.1 <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-one-or-more">fn:one-or-more</a> | ||
* function. | ||
* | ||
* @param sequence | ||
* the sequence to evaluate | ||
* @return the sequence if it has zero or one items | ||
* @throws InvalidArgumentFunctionException | ||
* with the code | ||
* {@link InvalidArgumentFunctionException#INVALID_ARGUMENT_ONE_OR_MORE} | ||
* if the sequence contains no items | ||
*/ | ||
@NonNull | ||
public static ISequence<?> fnOneOrMore(@NonNull ISequence<?> sequence) { | ||
if (sequence.size() < 1) { | ||
throw new InvalidArgumentFunctionException( | ||
InvalidArgumentFunctionException.INVALID_ARGUMENT_ONE_OR_MORE, | ||
String.format("fn:one-or-more called with the sequence '%s' containing less than one item.", | ||
sequence.toSignature())); | ||
} | ||
return sequence; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnZeroOrOne.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,83 @@ | ||
/* | ||
* 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.IArgument; | ||
import gov.nist.secauto.metaschema.core.metapath.function.IFunction; | ||
import gov.nist.secauto.metaschema.core.metapath.function.InvalidArgumentFunctionException; | ||
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 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-zero-or-one">fn:zero-or-one</a> | ||
* function. | ||
*/ | ||
public final class FnZeroOrOne { | ||
private static final String NAME = "zero-or-one"; | ||
@NonNull | ||
static final IFunction SIGNATURE = IFunction.builder() | ||
.name(NAME) | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextIndependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("arg") | ||
.type(IItem.type()) | ||
.zeroOrMore() | ||
.build()) | ||
.returnType(IItem.type()) | ||
.returnZeroOrOne() | ||
.functionHandler(FnZeroOrOne::execute) | ||
.build(); | ||
|
||
private FnZeroOrOne() { | ||
// disable construction | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<?> execute(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
return fnZeroOrOne(ObjectUtils.requireNonNull(arguments.get(0))); | ||
} | ||
|
||
/** | ||
* Check that the provided sequence has zero or one items. | ||
* <p> | ||
* Based on the XPath 3.1 <a href= | ||
* "https://www.w3.org/TR/xpath-functions-31/#func-zero-or-one">fn:zero-or-one</a> | ||
* function. | ||
* | ||
* @param sequence | ||
* the sequence to evaluate | ||
* @return the sequence if it has zero or one items | ||
* @throws InvalidArgumentFunctionException | ||
* with the code | ||
* {@link InvalidArgumentFunctionException#INVALID_ARGUMENT_ZERO_OR_ONE} | ||
* if the sequence contains more than one item | ||
*/ | ||
@NonNull | ||
public static ISequence<?> fnZeroOrOne(@NonNull ISequence<?> sequence) { | ||
if (sequence.size() > 1) { | ||
throw new InvalidArgumentFunctionException( | ||
InvalidArgumentFunctionException.INVALID_ARGUMENT_ZERO_OR_ONE, | ||
String.format("fn:zero-or-one called with the sequence '%s' containing more than one item.", | ||
sequence.toSignature())); | ||
} | ||
return sequence; | ||
} | ||
} |
Oops, something went wrong.