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 normalize-space Metapath function. Had to fix a…
… bug in the string literal handling that inconsistently caused leading and trailing quotes to be removed. Also fixed an issue with the build that was causing surefire to inconsistently handle a JUnit extension. Resolves metaschema-framework#133.
- Loading branch information
1 parent
e565811
commit 8d6343f
Showing
9 changed files
with
169 additions
and
26 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
89 changes: 89 additions & 0 deletions
89
...ain/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnNormalizeSpace.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,89 @@ | ||
/* | ||
* 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.DynamicMetapathException; | ||
import gov.nist.secauto.metaschema.core.metapath.ISequence; | ||
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; | ||
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.atomic.IAnyAtomicItem; | ||
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; | ||
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-normalize-space">fn:normalize-space</a>. | ||
*/ | ||
public final class FnNormalizeSpace { | ||
@NonNull | ||
static final IFunction SIGNATURE_NO_ARG = IFunction.builder() | ||
.name("normalize-space") | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextDependent() | ||
.focusDependent() | ||
.returnType(IStringItem.class) | ||
.returnOne() | ||
.functionHandler(FnNormalizeSpace::executeNoArg) | ||
.build(); | ||
|
||
@NonNull | ||
static final IFunction SIGNATURE_ONE_ARG = IFunction.builder() | ||
.name("normalize-space") | ||
.namespace(MetapathConstants.NS_METAPATH_FUNCTIONS) | ||
.deterministic() | ||
.contextIndependent() | ||
.focusIndependent() | ||
.argument(IArgument.builder() | ||
.name("arg") | ||
.type(IStringItem.class) | ||
.zeroOrOne() | ||
.build()) | ||
.returnType(IStringItem.class) | ||
.returnOne() | ||
.functionHandler(FnNormalizeSpace::executeOneArg) | ||
.build(); | ||
|
||
private FnNormalizeSpace() { | ||
// disable construction | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<IStringItem> executeNoArg(@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
|
||
ISequence<IAnyAtomicItem> retval; | ||
if (focus == null) { | ||
throw new DynamicMetapathException(DynamicMetapathException.DYNAMIC_CONTEXT_ABSENT, "The context is empty"); | ||
} | ||
|
||
return ISequence.of(FnString.fnStringItem(focus).normalizeSpace()); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@NonNull | ||
private static ISequence<IStringItem> executeOneArg( | ||
@NonNull IFunction function, | ||
@NonNull List<ISequence<?>> arguments, | ||
@NonNull DynamicContext dynamicContext, | ||
IItem focus) { | ||
|
||
IStringItem value = FunctionUtils.asTypeOrNull(ObjectUtils.requireNonNull(arguments.get(0)).getFirstItem(true)); | ||
|
||
return value == null ? ISequence.empty() : ISequence.of(value.normalizeSpace()); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...java/gov/nist/secauto/metaschema/core/metapath/function/library/FnNormalizeSpaceTest.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,65 @@ | ||
/* | ||
* 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.array; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer; | ||
import static gov.nist.secauto.metaschema.core.metapath.TestUtils.sequence; | ||
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.ISequence; | ||
import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException; | ||
import gov.nist.secauto.metaschema.core.metapath.MetapathException; | ||
import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; | ||
import gov.nist.secauto.metaschema.core.metapath.function.InvalidTypeFunctionException; | ||
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; | ||
|
||
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 FnNormalizeSpaceTest | ||
extends ExpressionTestBase { | ||
private static Stream<Arguments> provideValues() { // NOPMD - false positive | ||
return Stream.of( | ||
Arguments.of( | ||
string("The wealthy curled darlings of our nation."), | ||
"fn:normalize-space(\" The wealthy curled darlings \n\r \t of our nation. \")")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValues") | ||
void testExpression(@NonNull IStringItem expected, @NonNull String metapath) { | ||
IStringItem result = MetapathExpression.compile(metapath) | ||
.evaluateAs(null, MetapathExpression.ResultType.ITEM, newDynamicContext()); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testNoFocus() { | ||
InvalidTypeMetapathException throwable = assertThrows(InvalidTypeMetapathException.class, | ||
() -> { | ||
try { | ||
ISequence<IStringItem> result = FunctionTestBase.executeFunction( | ||
FnNormalizeSpace.SIGNATURE_NO_ARG, | ||
newDynamicContext(), | ||
null, | ||
List.of(sequence())); | ||
} catch (MetapathException ex) { | ||
throw ex.getCause(); | ||
} | ||
}); | ||
} | ||
} |
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