Skip to content

Commit

Permalink
Cleaned up atomize and getAtomicItem method use.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 25, 2024
1 parent c82b4e9 commit 6028a1f
Show file tree
Hide file tree
Showing 34 changed files with 149 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package gov.nist.secauto.metaschema.core.metapath;

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.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
Expand Down Expand Up @@ -74,6 +75,17 @@ static Stream<? extends IItem> normalizeAsItems(@NonNull ICollectionValue value)
: value.toSequence().stream();
}

/**
* Produce a stream of atomic items based on the atomic value of these items.
* <p>
* Supports <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
* atomization</a>.
*
* @return a stream of atomized atomic items.
*/
@NonNull
Stream<IAnyAtomicItem> atomize();

/**
* Get the stream of items for the collection value.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package gov.nist.secauto.metaschema.core.metapath;

import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
import gov.nist.secauto.metaschema.core.metapath.impl.AbstractSequence;
import gov.nist.secauto.metaschema.core.metapath.impl.SequenceN;
import gov.nist.secauto.metaschema.core.metapath.impl.SingletonSequence;
Expand Down Expand Up @@ -158,10 +157,10 @@ default ITEM getFirstItem(boolean requireSingleton) {
* the sequence of items to atomize
* @return the atomized result
*/
@Override
@NonNull
default ISequence<IAnyAtomicItem> atomize() {
return of(ObjectUtils.notNull(stream()
.flatMap(FnData::atomize)));
default Stream<IAnyAtomicItem> atomize() {
return ObjectUtils.notNull(stream().flatMap(IItem::atomize));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public enum ResultType {
* The result is expected to be a {@link String} value.
*/
STRING(String.class, sequence -> {
IAnyAtomicItem item = sequence.atomize().getFirstItem(true);
IAnyAtomicItem item = ISequence.of(sequence.atomize()).getFirstItem(true);
return item == null ? "" : item.asString();
}),
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ private String lookupNamespaceURIForPrefix(@NonNull String prefix) {
* @return the namespace string bound to the prefix, or {@code null} if no
* namespace is bound to the prefix
*/
// FIXME: check for https://www.w3.org/TR/xpath-31/#ERRXPST0081
@Nullable
public String lookupNamespaceForPrefix(@NonNull String prefix) {
String result = lookupNamespaceURIForPrefix(prefix);
Expand Down Expand Up @@ -633,7 +632,6 @@ public Builder baseUri(@NonNull URI uri) {
* @see StaticContext#lookupNamespaceForPrefix(String)
* @see StaticContext#getWellKnownNamespacesMap()
*/
// FIXME: check for https://www.w3.org/TR/xpath-31/#ERRXPST0070 for "meta"
@NonNull
public Builder namespace(@NonNull String prefix, @NonNull URI uri) {
return namespace(prefix, ObjectUtils.notNull(uri.toASCIIString()));
Expand All @@ -648,12 +646,17 @@ public Builder namespace(@NonNull String prefix, @NonNull URI uri) {
* the namespace URI
* @return this builder
* @throws IllegalArgumentException
* if the provided URI is invalid
* if the provided prefix or URI is invalid
* @see StaticContext#lookupNamespaceForPrefix(String)
* @see StaticContext#getWellKnownNamespacesMap()
*/
@NonNull
public Builder namespace(@NonNull String prefix, @NonNull String uri) {
if (MetapathConstants.PREFIX_METAPATH.equals(prefix)) {
// check for https://www.w3.org/TR/xpath-31/#ERRXPST0070 for "meta"
throw new IllegalArgumentException(
"Redefining the prefix '" + MetapathConstants.PREFIX_METAPATH + "' is not allowed.");
}
this.namespaces.put(prefix, uri);
NamespaceCache.instance().indexOf(uri);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public class StaticMetapathException
* an AttributeName in an <a href=
* "https://www.w3.org/TR/xpath-31/#doc-xpath31-AttributeTest">AttributeTest</a>.
*/
// FIXME: Check for use in all calls to the static or dyanmic context to lookup
// a name
public static final int NOT_DEFINED = 8;
/**
* <a href= "https://www.w3.org/TR/xpath-31/#ERRXPST0010">err:MPST0010</a>: An
* implementation that does not support the namespace axis must raise a
* <a href="https://www.w3.org/TR/xpath-31/#dt-static-error">static error</a> if
* it encounters a reference to the namespace axis and XPath 1.0 compatibility
* mode is false.
*/
public static final int AXIS_NAMESPACE_UNSUPPORTED = 10;

/**
* <a href= "https://www.w3.org/TR/xpath-31/#ERRXPST0017">err:MPST0017</a>: It
Expand Down Expand Up @@ -104,6 +110,12 @@ public class StaticMetapathException
*/
public static final int PREFIX_NOT_EXPANDABLE = 81;

/**
* <a href= "https://www.w3.org/TR/xpath-31/#ERRXQST0134">err:MPST0134</a>: The
* namespace axis is not supported.
*/
public static final int AXIS_NAMESPACE_UNSUPPORTED_IN_TEST = 134;

/**
* the serial version UID.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class AbstractExpression implements IExpression {
@Nullable
public static IAnyAtomicItem getFirstDataItem(@NonNull ISequence<?> sequence,
boolean requireSingleton) {
return sequence.atomize().getFirstItem(requireSingleton);
return ISequence.of(sequence.atomize()).getFirstItem(requireSingleton);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,8 @@ protected IExpression handleSimplemapexpr(Metapath10.SimplemapexprContext contex

@Override
protected IExpression handleArrowexpr(Metapath10.ArrowexprContext context) {
// TODO: handle additional syntax for varef and parenthesized
// FIXME: handle additional syntax for varef and parenthesized

return handleGroupedNAiry(context, 0, 3, (ctx, idx, left) -> {
// the next child is "=>"
assert "=>".equals(ctx.getChild(idx).getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public List<? extends IExpression> getChildren() {
public ISequence<? extends IItem> accept(DynamicContext dynamicContext, ISequence<?> focus) {
ISequence<?> target = getBase().accept(dynamicContext, focus);
IItem collection = target.getFirstItem(true);
IAnyAtomicItem key = getArgument().accept(dynamicContext, focus)
.atomize()
IAnyAtomicItem key = ISequence.of(getArgument().accept(dynamicContext, focus).atomize())
.getFirstItem(false);
if (key == null) {
throw new StaticMetapathException(StaticMetapathException.NO_FUNCTION_MATCH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public ISequence<? extends IItem> accept(DynamicContext dynamicContext, ISequenc
ObjectUtils.notNull(getChildren().stream()
.map(item -> {
IExpression keyExpression = item.getKeyExpression();
IAnyAtomicItem key = keyExpression.accept(dynamicContext, focus)
.atomize()
IAnyAtomicItem key = ISequence.of(keyExpression.accept(dynamicContext, focus).atomize())
.getFirstItem(true);
if (key == null) {
throw new InvalidTypeMetapathException(null, String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visit
public ISequence<?> accept(DynamicContext dynamicContext, ISequence<?> focus) {
return ISequence.of(FnConcat.concat(ObjectUtils.notNull(getChildren().stream()
.map(child -> child.accept(dynamicContext, focus))
.flatMap(result -> ObjectUtils.notNull(result).atomize().stream()))));
.flatMap(result -> ObjectUtils.notNull(result).atomize()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visit

@Override
public ISequence<? extends IBooleanItem> accept(DynamicContext dynamicContext, ISequence<?> focus) {
ISequence<? extends IAnyAtomicItem> leftItems = getLeft().accept(dynamicContext, focus).atomize();
ISequence<? extends IAnyAtomicItem> rightItems = getRight().accept(dynamicContext, focus).atomize();
ISequence<? extends IAnyAtomicItem> leftItems = ISequence.of(getLeft().accept(dynamicContext, focus).atomize());
ISequence<? extends IAnyAtomicItem> rightItems = ISequence.of(getRight().accept(dynamicContext, focus).atomize());
return ISequence.of(ComparisonFunctions.generalCompairison(leftItems, getOperator(), rightItems));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.StaticMetapathException;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpression;
import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor;
import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils;
Expand All @@ -20,7 +21,6 @@

import edu.umd.cs.findbugs.annotations.NonNull;

// FIXME: check for https://www.w3.org/TR/xpath-31/#ERRXQST0134 or err:XPST0010
@SuppressWarnings("PMD.ShortClassName") // intentional
public enum Axis implements IExpression {
SELF(Stream::of),
Expand All @@ -34,7 +34,12 @@ public enum Axis implements IExpression {
FOLLOWING_SIBLING(INodeItem::followingSibling),
PRECEDING_SIBLING(INodeItem::precedingSibling),
FOLLOWING(INodeItem::following),
PRECEDING(INodeItem::preceding);
PRECEDING(INodeItem::preceding),
NAMESPACE(focus -> {
throw new StaticMetapathException(
StaticMetapathException.AXIS_NAMESPACE_UNSUPPORTED,
"The 'namespace' axis is not supported");
});

@NonNull
private final Function<INodeItem, Stream<? extends INodeItem>> action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.MetapathException;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.IItemVisitor;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
Expand Down Expand Up @@ -177,7 +176,7 @@ protected static ISequence<?> convertSequence(
Stream<? extends IItem> stream = sequence.safeStream();

if (IAnyAtomicItem.class.isAssignableFrom(requiredSequenceTypeClass)) {
Stream<? extends IAnyAtomicItem> atomicStream = stream.flatMap(FnData::atomize);
Stream<? extends IAnyAtomicItem> atomicStream = stream.flatMap(IItem::atomize);

// if (IUntypedAtomicItem.class.isInstance(item)) { // NOPMD
// // TODO: apply cast to atomic type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gov.nist.secauto.metaschema.core.metapath.function;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
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.IDecimalItem;
Expand Down Expand Up @@ -126,7 +125,7 @@ public static INumericItem toNumeric(@NonNull ISequence<?> sequence, boolean req
@NonNull
public static INumericItem toNumeric(@NonNull IItem item) {
// atomize
IAnyAtomicItem atomicItem = ISequence.getFirstItem(FnData.atomize(item), true);
IAnyAtomicItem atomicItem = ISequence.getFirstItem(item.atomize(), true);
if (atomicItem == null) {
throw new InvalidTypeMetapathException(item, "Unable to cast null item");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.StaticMetapathException;
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.type.IItemType;
import gov.nist.secauto.metaschema.core.metapath.type.ISequenceType;
import gov.nist.secauto.metaschema.core.metapath.type.Occurrence;
Expand Down Expand Up @@ -193,6 +194,11 @@ ISequence<?> execute(
@NonNull DynamicContext dynamicContext,
@NonNull ISequence<?> focus);

@Override
default IAnyAtomicItem toAtomicItem() {
throw new InvalidTypeFunctionException(InvalidTypeFunctionException.DATA_ITEM_IS_FUNCTION, this);
}

/**
* Get the signature of the function as a string.
*
Expand Down Expand Up @@ -235,7 +241,6 @@ static Builder builder(@NonNull StaticContext staticContext) {
/**
* Used to create a function's signature using a builder pattern.
*/
// FIXME: Should return type be ISequenceType?
@SuppressWarnings("PMD.LooseCoupling")
final class Builder {
@NonNull
Expand Down Expand Up @@ -538,6 +543,7 @@ public IFunction build() {
ObjectUtils.requireNonNull(namespace, "the namespace must not be null"),
properties,
new ArrayList<>(arguments),
// FIXME: Should return type be ISequenceType?
ISequenceType.of(returnType, returnOccurrence),
ObjectUtils.requireNonNull(functionHandler, "the function handler must not be null"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
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.function.InvalidTypeFunctionException;
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.IAtomicValuedItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.List;
import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;

Expand Down Expand Up @@ -77,7 +73,7 @@ private static ISequence<IAnyAtomicItem> executeNoArg(@NonNull IFunction functio
if (item == null) {
retval = ISequence.empty();
} else {
IAnyAtomicItem data = fnDataItem(item);
IAnyAtomicItem data = item.toAtomicItem();
retval = ISequence.of(data);
}
return retval;
Expand All @@ -91,83 +87,6 @@ private static ISequence<IAnyAtomicItem> executeOneArg(
@NonNull DynamicContext dynamicContext,
IItem focus) {

return FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))).atomize();
}

/**
* An implementation of
* <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
* atomization</a>.
*
* @param item
* the item to atomize
* @return the atomized result
* @throws InvalidTypeFunctionException
* if the item cannot be cast to an atomic value, most likely because
* it doesn't have a typed value
*/
@NonNull
public static IAnyAtomicItem fnDataItem(@NonNull IItem item) {
IAnyAtomicItem retval = null;
if (item instanceof IAtomicValuedItem) {
retval = ((IAtomicValuedItem) item).toAtomicItem();
}

if (retval != null) {
return retval;
}
throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, item);
}

/**
* An implementation of
* <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
* atomization</a>.
*
* @param item
* the item to atomize
* @return the atomized result
*/
// FIXME: implement atomize on the called methods
@NonNull
public static Stream<IAnyAtomicItem> fnDataItem(@NonNull IArrayItem<?> item) {
return ObjectUtils.notNull(item.stream().flatMap(member -> {
Stream<IAnyAtomicItem> result;
if (member instanceof IItem) {
result = atomize((IItem) member);
} else if (member instanceof ISequence) {
result = ((ISequence<?>) member).stream()
.flatMap(FnData::atomize);
} else {
throw new UnsupportedOperationException("array member not an item or sequence.");
}
return result;
}));
}

/**
* An implementation of
* <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
* atomization</a>.
*
* @param item
* the item to atomize
* @return the atomized result
*/
@NonNull
public static Stream<IAnyAtomicItem> atomize(@NonNull IItem item) {
Stream<IAnyAtomicItem> retval;
if (item instanceof IAnyAtomicItem) {
retval = ObjectUtils.notNull(Stream.of((IAnyAtomicItem) item));
} else if (item instanceof IAtomicValuedItem) {
retval = ObjectUtils.notNull(Stream.of(((IAtomicValuedItem) item).toAtomicItem()));
} else if (item instanceof IArrayItem) {
retval = fnDataItem((IArrayItem<?>) item);
} else if (item instanceof IFunction) {
throw new InvalidTypeFunctionException(InvalidTypeFunctionException.DATA_ITEM_IS_FUNCTION, item);
} else {
throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, item);
}
return retval;
return ISequence.of(FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))).atomize());
}
}
Loading

0 comments on commit 6028a1f

Please sign in to comment.