1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.cst.path;
7   
8   import dev.metaschema.core.metapath.DynamicContext;
9   import dev.metaschema.core.metapath.IExpression;
10  import dev.metaschema.core.metapath.cst.IExpressionVisitor;
11  import dev.metaschema.core.metapath.item.ISequence;
12  import dev.metaschema.core.metapath.item.ItemUtils;
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  
15  /**
16   * An expression that finds an ancestor of the document root using the
17   * {@code right} expression.
18   * <p>
19   * Based on the XPath 3.1
20   * <a href= "https://www.w3.org/TR/xpath-31/#id-path-operator">path
21   * operator</a>.
22   */
23  public class RootDoubleSlashPath
24      extends AbstractRootPathExpression {
25  
26    /**
27     * Construct a new expression that finds an ancestor of the document root using
28     * the {@code right} expression.
29     *
30     * @param text
31     *          the parsed text of the expression
32     * @param node
33     *          the path to evaluate relative to the document root
34     */
35    public RootDoubleSlashPath(@NonNull String text, @NonNull IExpression node) {
36      super(text, node);
37    }
38  
39    @Override
40    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
41      return visitor.visitRootDoubleSlashPath(this, context);
42    }
43  
44    @Override
45    protected ISequence<?> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
46      return ISequence.of(search(
47          getExpression(),
48          dynamicContext,
49          ItemUtils.getDocumentNodeItems(dynamicContext, focus)));
50    }
51  }