Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/StringLiteral.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10 import org.mozilla.javascript.ScriptRuntime;
11
12 /**
13 * AST node for a single- or double-quoted string literal.
14 * Node type is {@link Token#STRING}.<p>
15 */
16 public class StringLiteral extends AstNode {
17
18 private String value;
19 private char quoteChar;
20
21 {
22 type = Token.STRING;
23 }
24
25 public StringLiteral() {
26 }
27
28 public StringLiteral(int pos) {
29 super(pos);
30 }
31
32 /**
33 * Creates a string literal node at the specified position.
34 * @param len the length <em>including</em> the enclosing quotes
35 */
36 public StringLiteral(int pos, int len) {
37 super(pos, len);
38 }
39
40 /**
41 * Returns the node's value: the parsed string without the enclosing quotes
42 * @return the node's value, a {@link String} of unescaped characters
43 * that includes the delimiter quotes.
44 */
45 public String getValue() {
46 return value;
47 }
48
49 /**
50 * Returns the string value, optionally including the enclosing quotes.
51 */
52 public String getValue(boolean includeQuotes) {
53 if (!includeQuotes)
54 return value;
55 return quoteChar + value + quoteChar;
56 }
57
58 /**
59 * Sets the node's value. Do not include the enclosing quotes.
60 * @param value the node's value
61 * @throws IllegalArgumentException} if value is {@code null}
62 */
63 public void setValue(String value) {
64 assertNotNull(value);
65 this.value = value;
66 }
67
68 /**
69 * Returns the character used as the delimiter for this string.
70 */
71 public char getQuoteCharacter() {
72 return quoteChar;
73 }
74
75 public void setQuoteCharacter(char c) {
76 quoteChar = c;
77 }
78
79 @Override
80 public String toSource(int depth) {
81 return new StringBuilder(makeIndent(depth))
82 .append(quoteChar)
83 .append(ScriptRuntime.escapeString(value, quoteChar))
84 .append(quoteChar)
85 .toString();
86 }
87
88 /**
89 * Visits this node. There are no children to visit.
90 */
91 @Override
92 public void visit(NodeVisitor v) {
93 v.visit(this);
94 }
95 }