1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.cst;
7   
8   import dev.metaschema.core.metapath.DynamicContext;
9   import dev.metaschema.core.metapath.IExpression;
10  import dev.metaschema.core.metapath.item.IItem;
11  import dev.metaschema.core.metapath.item.ISequence;
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  
14  /**
15   * A common base class for Metapath expression implementations, providing common
16   * utility functions.
17   */
18  public abstract class AbstractExpression implements IExpression {
19    @NonNull
20    private final String text;
21  
22    /**
23     * Construct a new expression.
24     *
25     * @param text
26     *          the parsed text of the expression
27     */
28    public AbstractExpression(@NonNull String text) {
29      this.text = text;
30    }
31  
32    @Override
33    public String getPath() {
34      return text;
35    }
36  
37    @Override
38    public String toString() {
39      return CSTPrinter.toString(this);
40    }
41  
42    @Override
43    public ISequence<?> accept(DynamicContext dynamicContext, ISequence<?> focus) {
44      dynamicContext.pushExecutionStack(this);
45      try {
46        return evaluate(dynamicContext, focus);
47      } finally {
48        dynamicContext.popExecutionStack(this);
49      }
50    }
51  
52    /**
53     * Evaluate this expression, producing a sequence result.
54     *
55     * @param dynamicContext
56     *          the dynamic evaluation context
57     * @param focus
58     *          the outer focus of the expression
59     * @return the result of evaluation
60     */
61    @NonNull
62    protected abstract ISequence<? extends IItem> evaluate(
63        @NonNull DynamicContext dynamicContext,
64        @NonNull ISequence<?> focus);
65  }