Skip to content

Commit

Permalink
Apply suggestions from code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Jan 7, 2025
1 parent 8be41b0 commit 7c524da
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down Expand Up @@ -326,7 +327,9 @@ public void pushExecutionStack(@NonNull IExpression expression) {
*/
public void popExecutionStack(@NonNull IExpression expression) {
IExpression popped = this.sharedState.executionStack.pop();
assert expression.equals(popped);
if (!expression.equals(popped)) {
throw new IllegalStateException("Popped expression does not match expected expression");
}
}

/**
Expand All @@ -339,6 +342,18 @@ public List<IExpression> getExecutionStack() {
return CollectionUtil.unmodifiableList(new ArrayList<>(this.sharedState.executionStack));
}

/**
* Provides a formatted stack trace.
*
* @return the formatted stack trace
*/
@NonNull
public String formatExecutionStackTrace() {
return ObjectUtils.notNull(getExecutionStack().stream()
.map(IExpression::toCSTString)
.collect(Collectors.joining("\n-> ")));
}

private class CachingLoader implements IDocumentLoader {
@NonNull
private final IDocumentLoader proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* A base class for all Metapath
* <a href="https://www.w3.org/TR/xpath-31/#id-literals">literal value
* A base class for all Metapath <a href="https://www.w3.org/TR/xpath-31/#id-literals">literal value
* expressions</a>.
*
* @param <RESULT_TYPE>
Expand Down Expand Up @@ -45,7 +44,7 @@ public RESULT_TYPE getValue() {
}

@Override
protected ISequence<?> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
protected ISequence<RESULT_TYPE> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
return ISequence.of(getValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@
import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

/**
* The CST node for a Metapath
* <a href="https://www.w3.org/TR/xpath-31/#combining_seq">except
* The CST node for a Metapath <a href="https://www.w3.org/TR/xpath-31/#combining_seq">except
* expression</a>.
*/
public class Except
extends AbstractFilterExpression {

/**
* Construct a except filter expression, which removes the items resulting from
* the filter expression from the items expression.
* Construct a except filter expression, which removes the items resulting from the filter
* expression from the items expression.
*
* @param text
* the parsed text of the expression
Expand All @@ -43,8 +44,9 @@ public Except(

@Override
protected ISequence<?> applyFilterTo(@NonNull List<? extends IItem> source, @NonNull List<? extends IItem> items) {
Set<IItem> filterSet = new HashSet<>(items);
return ISequence.of(ObjectUtils.notNull(source.stream()
.filter(item -> !items.contains(item))));
.filter(item -> !filterSet.contains(item))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* The CST node for a Metapath
* <a href="https://www.w3.org/TR/xpath-31/#combining_seq">intersect
* The CST node for a Metapath <a href="https://www.w3.org/TR/xpath-31/#combining_seq">intersect
* expression</a>.
*/
public class Intersect
extends AbstractFilterExpression {

/**
* Construct a new Metapath except expression CST node.
* Construct a new Metapath intersect expression CST node.
*
* @param text
* the parsed text of the expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* An XPath 3.1 <a href=
* "https://www.w3.org/TR/xpath-31/#id-quantified-expressions">quantified
* An XPath 3.1 <a href= "https://www.w3.org/TR/xpath-31/#id-quantified-expressions">quantified
* expression</a>.
*/
public class Quantified
Expand All @@ -41,23 +40,21 @@ public class Quantified
*/
public enum Quantifier {
/**
* The quantified expression is {@code true} if at least one evaluation of the
* test expression has the
* <a href="https://www.w3.org/TR/xpath-31/#dt-ebv">effective boolean value</a>
* {@code true}; otherwise the quantified expression is {@code false}.
* The quantified expression is {@code true} if at least one evaluation of the test expression has
* the <a href="https://www.w3.org/TR/xpath-31/#dt-ebv">effective boolean value</a> {@code true};
* otherwise the quantified expression is {@code false}.
* <p>
* This rule implies that, if the in-clauses generate zero binding tuples, the
* value of the quantified expression is {@code false}.
* This rule implies that, if the in-clauses generate zero binding tuples, the value of the
* quantified expression is {@code false}.
*/
SOME,
/**
* the quantified expression is {@code true} if every evaluation of the test
* expression has the <a href="https://www.w3.org/TR/xpath-31/#dt-ebv">effective
* boolean value</a> {@code true}; otherwise the quantified expression is
* {@code false}.
* the quantified expression is {@code true} if every evaluation of the test expression has the
* <a href="https://www.w3.org/TR/xpath-31/#dt-ebv">effective boolean value</a> {@code true};
* otherwise the quantified expression is {@code false}.
* <p>
* This rule implies that, if the in-clauses generate zero binding tuples, the
* value of the quantified expression is {@code true}.
* This rule implies that, if the in-clauses generate zero binding tuples, the value of the
* quantified expression is {@code true}.
*/
EVERY;
}
Expand All @@ -77,11 +74,10 @@ public enum Quantifier {
* @param quantifier
* the quantifier operation
* @param inClauses
* the set of expressions that define the variables to use for
* determining the Cartesian product for evaluation
* the set of expressions that define the variables to use for determining the Cartesian
* product for evaluation
* @param satisfies
* the expression used for evaluation using the Cartesian product of
* the variables
* the expression used for evaluation using the Cartesian product of the variables
*/
public Quantified(
@NonNull String text,
Expand All @@ -105,8 +101,8 @@ public Quantifier getQuantifier() {
}

/**
* Get the set of expressions that define the variables to use for determining
* the Cartesian product for evaluation.
* Get the set of expressions that define the variables to use for determining the Cartesian product
* for evaluation.
*
* @return the variable names mapped to the associated Metapath expression
*/
Expand All @@ -116,8 +112,7 @@ public Map<IEnhancedQName, IExpression> getInClauses() {
}

/**
* Get the expression used for evaluation using the Cartesian product of the
* variables.
* Get the expression used for evaluation using the Cartesian product of the variables.
*
* @return the evaluation expression
*/
Expand All @@ -133,7 +128,7 @@ public List<? extends IExpression> getChildren() {
}

@Override
protected ISequence<?> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
protected ISequence<IBooleanItem> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
Map<IEnhancedQName, ISequence<? extends IItem>> clauses = getInClauses().entrySet().stream()
.map(entry -> Map.entry(
entry.getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
/**
* An expression that gets the document root.
* <p>
* Based on the XPath 3.1
* <a href= "https://www.w3.org/TR/xpath-31/#id-path-operator">path
* Based on the XPath 3.1 <a href= "https://www.w3.org/TR/xpath-31/#id-path-operator">path
* operator</a>.
* <p>
* This class handles the root path expression "/", which selects the document
* root node when evaluated. The evaluation follows the XPath specification for
* absolute paths.
* This class handles the root path expression "/", which selects the document root node when
* evaluated. The evaluation follows the XPath specification for absolute paths.
*/
public class RootSlashOnlyPath
extends AbstractPathExpression<INodeItem> {
Expand Down Expand Up @@ -63,15 +61,21 @@ public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visit
protected ISequence<? extends INodeItem> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
return ISequence.of(ObjectUtils.notNull(focus.stream()
.map(ItemUtils::checkItemIsNodeItemForStep)
.map(item -> Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item))
.findFirst()
.orElseThrow(() -> new DynamicMetapathException(DynamicMetapathException.TREAT_DOES_NOT_MATCH_TYPE,
"Root node not found")))
.peek(item -> {
if (!(item instanceof IDocumentNodeItem)) {
throw new DynamicMetapathException(DynamicMetapathException.TREAT_DOES_NOT_MATCH_TYPE,
"The head of the tree is not a document node.");
}
})));
.map(RootSlashOnlyPath::findAndValidateDocumentRoot)));
}

private static INodeItem findAndValidateDocumentRoot(INodeItem item) {
INodeItem root = Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item))
.findFirst()
.orElseThrow(() -> new DynamicMetapathException(
DynamicMetapathException.TREAT_DOES_NOT_MATCH_TYPE,
"Root node not found"));

if (!(root instanceof IDocumentNodeItem)) {
throw new DynamicMetapathException(
DynamicMetapathException.TREAT_DOES_NOT_MATCH_TYPE,
"The head of the tree is not a document node.");
}
return root;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
*/
/**
* A compact syntax tree node that supports the Metapath
* <a href="https://www.w3.org/TR/xpath-31/#id-instance-of">"instance of"
* operator</a>.
* <a href="https://www.w3.org/TR/xpath-31/#id-instance-of">"instance of" operator</a>.
*/
public class InstanceOf
extends AbstractExpression {
Expand Down

0 comments on commit 7c524da

Please sign in to comment.