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