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 java.util.List;
9   
10  import dev.metaschema.core.metapath.DynamicContext;
11  import dev.metaschema.core.metapath.IExpression;
12  import dev.metaschema.core.metapath.cst.IExpressionVisitor;
13  import dev.metaschema.core.metapath.item.ISequence;
14  import dev.metaschema.core.metapath.item.ItemUtils;
15  import dev.metaschema.core.metapath.item.node.INodeItem;
16  import dev.metaschema.core.util.CollectionUtil;
17  import edu.umd.cs.findbugs.annotations.NonNull;
18  
19  /**
20   * An expression that gets the document root.
21   * <p>
22   * Based on the XPath 3.1
23   * <a href= "https://www.w3.org/TR/xpath-31/#id-path-operator">path
24   * operator</a>.
25   * <p>
26   * This class handles the root path expression "/", which selects the document
27   * root node when evaluated. The evaluation follows the XPath specification for
28   * absolute paths.
29   */
30  public class RootSlashOnlyPath
31      extends AbstractPathExpression<INodeItem> {
32  
33    /**
34     * Construct a new path expression.
35     *
36     * @param text
37     *          the parsed text of the expression
38     */
39    public RootSlashOnlyPath(@NonNull String text) {
40      super(text);
41    }
42  
43    @Override
44    public List<? extends IExpression> getChildren() {
45      return CollectionUtil.emptyList();
46    }
47  
48    @Override
49    public Class<INodeItem> getBaseResultType() {
50      return INodeItem.class;
51    }
52  
53    @Override
54    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
55      return visitor.visitRootSlashOnlyPath(this, context);
56    }
57  
58    @Override
59    protected ISequence<? extends INodeItem> evaluate(DynamicContext dynamicContext, ISequence<?> focus) {
60      return ItemUtils.getDocumentNodeItems(dynamicContext, focus);
61    }
62  }