1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2019, 2021 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.util.ArrayList; |
18 | import java.util.List; |
19 | |
20 | import org.eclipse.jdt.core.compiler.CharOperation; |
21 | import org.eclipse.jdt.core.compiler.InvalidInputException; |
22 | import org.eclipse.jdt.internal.compiler.parser.Scanner; |
23 | import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; |
24 | import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; |
25 | |
26 | /** |
27 | * TextBolck AST node type. |
28 | * |
29 | * These are block of String literal nodes. |
30 | * |
31 | * @since 3.22 |
32 | */ |
33 | @SuppressWarnings("rawtypes") |
34 | public class TextBlock extends Expression { |
35 | |
36 | /** |
37 | * The "escapedValue" structural property of this node type (type: {@link String}). |
38 | * @since 3.0 |
39 | */ |
40 | public static final SimplePropertyDescriptor ESCAPED_VALUE_PROPERTY = |
41 | new SimplePropertyDescriptor(TextBlock.class, "escapedValue", String.class, MANDATORY); //$NON-NLS-1$ |
42 | |
43 | /** |
44 | * A list of property descriptors (element type: |
45 | * {@link StructuralPropertyDescriptor}), |
46 | * or null if uninitialized. |
47 | */ |
48 | private static final List PROPERTY_DESCRIPTORS; |
49 | |
50 | static { |
51 | List propertyList = new ArrayList(2); |
52 | createPropertyList(TextBlock.class, propertyList); |
53 | addProperty(ESCAPED_VALUE_PROPERTY, propertyList); |
54 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
55 | } |
56 | |
57 | /** |
58 | * Returns a list of structural property descriptors for this node type. |
59 | * Clients must not modify the result. |
60 | * |
61 | * @param apiLevel the API level; one of the |
62 | * <code>AST.JLS*</code> constants |
63 | |
64 | * @return a list of property descriptors (element type: |
65 | * {@link StructuralPropertyDescriptor}) |
66 | * @since 3.24 |
67 | */ |
68 | public static List propertyDescriptors(int apiLevel) { |
69 | return PROPERTY_DESCRIPTORS; |
70 | } |
71 | |
72 | /** |
73 | * The literal string, including quotes and escapes; defaults to the |
74 | * literal for the empty string. |
75 | */ |
76 | private String escapedValue = "\"\"";//$NON-NLS-1$ |
77 | |
78 | /** |
79 | * The literal string without the quotes and the applicable preceding and |
80 | * trailing whitespace if any ; defaults to empty string. |
81 | */ |
82 | private String literalValue = "";//$NON-NLS-1$ |
83 | |
84 | /** |
85 | * Creates a new unparented TextBlock node owned by the given AST. |
86 | * By default, the TextBlock denotes the empty string. |
87 | * <p> |
88 | * N.B. This constructor is package-private. |
89 | * </p> |
90 | * |
91 | * @param ast the AST that is to own this node |
92 | * @exception UnsupportedOperationException if this operation is used beloe JLS15 |
93 | */ |
94 | TextBlock(AST ast) { |
95 | super(ast); |
96 | unsupportedBelow15(); |
97 | } |
98 | |
99 | @Override |
100 | final List internalStructuralPropertiesForType(int apiLevel) { |
101 | return propertyDescriptors(apiLevel); |
102 | } |
103 | |
104 | @Override |
105 | final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { |
106 | if (property == ESCAPED_VALUE_PROPERTY) { |
107 | if (get) { |
108 | return getEscapedValue(); |
109 | } else { |
110 | setEscapedValue((String) value); |
111 | return null; |
112 | } |
113 | } |
114 | // allow default implementation to flag the error |
115 | return super.internalGetSetObjectProperty(property, get, value); |
116 | } |
117 | |
118 | @Override |
119 | final int getNodeType0() { |
120 | return TEXT_BLOCK; |
121 | } |
122 | |
123 | @Override |
124 | ASTNode clone0(AST target) { |
125 | TextBlock result = new TextBlock(target); |
126 | result.setSourceRange(getStartPosition(), getLength()); |
127 | result.setEscapedValue(getEscapedValue()); |
128 | return result; |
129 | } |
130 | |
131 | @Override |
132 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
133 | // dispatch to correct overloaded match method |
134 | return matcher.match(this, other); |
135 | } |
136 | |
137 | @Override |
138 | void accept0(ASTVisitor visitor) { |
139 | visitor.visit(this); |
140 | visitor.endVisit(this); |
141 | } |
142 | |
143 | /** |
144 | * Returns the string value of this literal node to the given string |
145 | * literal token. The token is the sequence of characters that would appear |
146 | * in the source program, including enclosing double quotes and embedded |
147 | * escapes. |
148 | * |
149 | * @return the string literal token, including enclosing double |
150 | * quotes and embedded escapes |
151 | * @since 3.24 |
152 | */ |
153 | public String getEscapedValue() { |
154 | return this.escapedValue; |
155 | } |
156 | |
157 | /** |
158 | * Sets the string value of this literal node to the given string literal |
159 | * token. The token is the sequence of characters that would appear in the |
160 | * source program, including enclosing double quotes and embedded escapes. |
161 | * For example, |
162 | * <ul> |
163 | * <li><code>""</code> <code>setLiteral("\"\"")</code></li> |
164 | * <li><code>"hello world"</code> <code>setLiteral("\"hello world\"")</code></li> |
165 | * <li><code>"boo\nhoo"</code> <code>setLiteral("\"boo\\nhoo\"")</code></li> |
166 | * </ul> |
167 | * |
168 | * @param token the string literal token, including enclosing double |
169 | * quotes and embedded escapes |
170 | * @exception IllegalArgumentException if the argument is incorrect |
171 | * @since 3.24 |
172 | */ |
173 | public void setEscapedValue(String token) { |
174 | // update internalSetEscapedValue(String) if this is changed |
175 | if (token == null) { |
176 | throw new IllegalArgumentException("Token cannot be null"); //$NON-NLS-1$ |
177 | } |
178 | Scanner scanner = this.ast.scanner; |
179 | char[] source = token.toCharArray(); |
180 | scanner.setSource(source); |
181 | scanner.resetTo(0, source.length); |
182 | try { |
183 | int tokenType = scanner.getNextToken(); |
184 | switch(tokenType) { |
185 | case TerminalTokens.TokenNameTextBlock: |
186 | break; |
187 | default: |
188 | throw new IllegalArgumentException("Invalid Text Block : >" + token + "<"); //$NON-NLS-1$//$NON-NLS-2$ |
189 | } |
190 | } catch(InvalidInputException e) { |
191 | throw new IllegalArgumentException("Invalid Text Block : >" + token + "<");//$NON-NLS-1$//$NON-NLS-2$ |
192 | } |
193 | preValueChange(ESCAPED_VALUE_PROPERTY); |
194 | this.escapedValue = token; |
195 | postValueChange(ESCAPED_VALUE_PROPERTY); |
196 | } |
197 | |
198 | /* (omit javadoc for this method) |
199 | * This method is does what setEscapedValue(String) does but without any validation. |
200 | * In addition, it also sets the literalValue property. |
201 | */ |
202 | void internalSetEscapedValue(String token, String literal) { |
203 | preValueChange(ESCAPED_VALUE_PROPERTY); |
204 | this.escapedValue = token; |
205 | this.literalValue = literal; |
206 | postValueChange(ESCAPED_VALUE_PROPERTY); |
207 | } |
208 | /** |
209 | * Returns the value of this literal node. |
210 | * <p> |
211 | * For example, |
212 | * <pre> |
213 | * TextBlock s; |
214 | * s.setEscapedValue("\"\"\" \n hello\\n world\""); |
215 | * assert s.getLiteralValue().equals("hello\n world"); |
216 | * </pre> |
217 | * <p> |
218 | * Note that this is a convenience method that converts from the stored |
219 | * TextBlock token returned by <code>getEscapedLiteral</code>. |
220 | * </p> |
221 | * |
222 | * @return the string value without enclosing triple quotes |
223 | * @exception IllegalArgumentException if the literal value cannot be converted |
224 | * @since 3.24 |
225 | */ |
226 | public String getLiteralValue() { |
227 | if (!this.literalValue.isEmpty()) { |
228 | return this.literalValue; |
229 | } |
230 | char[] escaped = getEscapedValue().toCharArray(); |
231 | int len = escaped.length; |
232 | if (len < 7) { |
233 | throw new IllegalArgumentException(); |
234 | } |
235 | |
236 | int start = -1; |
237 | loop: for (int i = 3; i < len; i++) { |
238 | char c = escaped[i]; |
239 | if (ScannerHelper.isWhitespace(c)) { |
240 | switch (c) { |
241 | case 10 : /* \ u000a: LINE FEED */ |
242 | case 13 : /* \ u000d: CARRIAGE RETURN */ |
243 | start = i + 1; |
244 | break loop; |
245 | default: |
246 | break; |
247 | } |
248 | } else { |
249 | break loop; |
250 | } |
251 | } |
252 | if (start == -1) { |
253 | throw new IllegalArgumentException(); |
254 | } |
255 | return new String( |
256 | CharOperation.subarray(escaped, start, len - 3) |
257 | ); |
258 | } |
259 | |
260 | |
261 | @Override |
262 | int memSize() { |
263 | int size = BASE_NODE_SIZE + 1 * 4 + stringSize(this.escapedValue); |
264 | return size; |
265 | } |
266 | |
267 | @Override |
268 | int treeSize() { |
269 | return memSize(); |
270 | } |
271 | } |
272 | |
273 |
Members