1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.cst.items;
7   
8   import dev.metaschema.core.metapath.cst.IExpressionVisitor;
9   import dev.metaschema.core.metapath.item.atomic.IStringItem;
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  
12  /**
13   * An implementation of the
14   * <a href="https://www.w3.org/TR/xpath-31/#id-literals">String Literal
15   * Expression</a> supporting the creation of a Metapath constant literal
16   * {@link IStringItem}.
17   */
18  public class StringLiteral
19      extends AbstractLiteralExpression<IStringItem> {
20    /**
21     * Construct a new expression that always returns the same string value.
22     *
23     * @param text
24     *          the parsed text of the expression
25     * @param value
26     *          the literal value
27     */
28    public StringLiteral(@NonNull String text, @NonNull String value) {
29      super(text, IStringItem.valueOf(removeQuotes(value)));
30    }
31  
32    @Override
33    public Class<IStringItem> getBaseResultType() {
34      return IStringItem.class;
35    }
36  
37    @SuppressWarnings("null")
38    @NonNull
39    private static String removeQuotes(@NonNull String value) {
40      return value.substring(1, value.length() - 1);
41    }
42  
43    @Override
44    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
45      return visitor.visitStringLiteral(this, context);
46    }
47  }