| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 11 | import net.sf.saxon.expr.parser.RebindingMap; |
| 12 | import net.sf.saxon.om.GroundedValue; |
| 13 | import net.sf.saxon.value.StringValue; |
| 14 | |
| 15 | /** |
| 16 | * Subclass of Literal used specifically for string literals, as this is a common case |
| 17 | */ |
| 18 | public class StringLiteral extends Literal { |
| 19 | |
| 20 | /** |
| 21 | * Create a StringLiteral that wraps a StringValue |
| 22 | * |
| 23 | * @param value the StringValue |
| 24 | */ |
| 25 | |
| 26 | public StringLiteral(StringValue value) { |
| 27 | super((GroundedValue)value); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Create a StringLiteral that wraps any CharSequence (including, of course, a String) |
| 32 | * |
| 33 | * @param value the CharSequence to be wrapped |
| 34 | */ |
| 35 | |
| 36 | public StringLiteral(CharSequence value) { |
| 37 | this(StringValue.makeStringValue(value)); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the value represented by this Literal |
| 42 | * |
| 43 | * @return the constant value |
| 44 | */ |
| 45 | @Override |
| 46 | public StringValue getValue() { |
| 47 | return (StringValue)super.getValue(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the string represented by this StringLiteral |
| 52 | * |
| 53 | * @return the underlying string |
| 54 | */ |
| 55 | |
| 56 | public String getStringValue() { |
| 57 | //noinspection RedundantCast |
| 58 | return ((StringValue) getValue()).getStringValue(); |
| 59 | } |
| 60 | |
| 61 | /*@NotNull*/ |
| 62 | public Expression copy(RebindingMap rebindings) { |
| 63 | StringLiteral stringLiteral = new StringLiteral(getValue()); |
| 64 | ExpressionTool.copyLocationInfo(this, stringLiteral); |
| 65 | return stringLiteral; |
| 66 | } |
| 67 | } |
| 68 |