0
# AST Node Types
1
2
The JSP parser creates specific AST node types for different JSP and HTML constructs. Each node type provides methods to access its specific properties and content.
3
4
## HTML/XML Structure Nodes
5
6
### ASTElement
7
8
Represents HTML/XML elements (tags).
9
10
```java { .api }
11
public final class ASTElement extends AbstractJspNode {
12
public String getName();
13
public boolean isHasNamespacePrefix();
14
public String getNamespacePrefix();
15
public String getLocalName();
16
public boolean isEmpty();
17
public boolean isUnclosed();
18
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
19
}
20
```
21
22
**Usage:**
23
24
```java
25
import net.sourceforge.pmd.lang.jsp.ast.ASTElement;
26
27
// Check element properties
28
if (element.getName().equals("div")) {
29
System.out.println("Found div element");
30
}
31
32
// Handle namespaced elements
33
if (element.isHasNamespacePrefix()) {
34
String prefix = element.getNamespacePrefix(); // e.g., "jsp"
35
String localName = element.getLocalName(); // e.g., "forward"
36
// Full name would be "jsp:forward"
37
}
38
39
// Check if self-closing tag
40
if (element.isEmpty()) {
41
System.out.println("Self-closing tag: <" + element.getName() + "/>");
42
}
43
44
// Check for unclosed tags
45
if (element.isUnclosed()) {
46
System.out.println("Warning: Unclosed tag detected");
47
}
48
```
49
50
**Methods:**
51
52
- `getName()`: Returns the full element name (e.g., "div", "jsp:forward")
53
- `isHasNamespacePrefix()`: True if element has namespace prefix (contains ":")
54
- `getNamespacePrefix()`: Returns namespace prefix part (before ":") or empty string
55
- `getLocalName()`: Returns local name part (after ":") or full name if no prefix
56
- `isEmpty()`: True for self-closing tags like `<br/>`
57
- `isUnclosed()`: True if parser found no proper closing tag
58
59
### ASTAttribute
60
61
Represents element attributes.
62
63
```java { .api }
64
public final class ASTAttribute extends AbstractJspNode {
65
public String getName();
66
public boolean isHasNamespacePrefix();
67
public String getNamespacePrefix();
68
public String getLocalName();
69
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
70
}
71
```
72
73
**Usage:**
74
75
```java
76
import net.sourceforge.pmd.lang.jsp.ast.ASTAttribute;
77
78
// Access attribute name
79
String attrName = attribute.getName(); // e.g., "class", "xmlns:jsp"
80
81
// Handle namespaced attributes
82
if (attribute.isHasNamespacePrefix()) {
83
String prefix = attribute.getNamespacePrefix(); // e.g., "xmlns"
84
String localName = attribute.getLocalName(); // e.g., "jsp"
85
}
86
```
87
88
### ASTAttributeValue
89
90
Represents attribute values.
91
92
```java { .api }
93
public final class ASTAttributeValue extends AbstractJspNode {
94
public String getValue();
95
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
96
}
97
```
98
99
**Usage:**
100
101
```java
102
import net.sourceforge.pmd.lang.jsp.ast.ASTAttributeValue;
103
104
// Get attribute value
105
String value = attributeValue.getValue(); // e.g., "button", "width: 100px"
106
```
107
108
## JSP-Specific Nodes
109
110
### ASTJspDirective
111
112
Represents JSP directives like `<%@ page ... %>`, `<%@ include ... %>`.
113
114
```java { .api }
115
public final class ASTJspDirective extends AbstractJspNode {
116
public String getName();
117
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
118
}
119
```
120
121
**Usage:**
122
123
```java
124
import net.sourceforge.pmd.lang.jsp.ast.ASTJspDirective;
125
126
// Check directive type
127
String directiveName = directive.getName(); // e.g., "page", "include", "taglib"
128
129
if ("page".equals(directiveName)) {
130
// Handle page directive
131
} else if ("include".equals(directiveName)) {
132
// Handle include directive
133
}
134
```
135
136
### ASTJspDirectiveAttribute
137
138
Represents attributes within JSP directives.
139
140
```java { .api }
141
public final class ASTJspDirectiveAttribute extends AbstractJspNode {
142
public String getName();
143
public String getValue();
144
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
145
}
146
```
147
148
**Usage:**
149
150
```java
151
import net.sourceforge.pmd.lang.jsp.ast.ASTJspDirectiveAttribute;
152
153
// Access directive attribute
154
String name = dirAttr.getName(); // e.g., "language", "import"
155
String value = dirAttr.getValue(); // e.g., "java", "java.util.*"
156
```
157
158
### ASTJspExpression
159
160
Represents JSP expressions `<%= ... %>`.
161
162
```java { .api }
163
public final class ASTJspExpression extends AbstractExpression {
164
public String getContent();
165
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
166
}
167
```
168
169
**Usage:**
170
171
```java
172
import net.sourceforge.pmd.lang.jsp.ast.ASTJspExpression;
173
174
// Get expression content
175
String expression = jspExpr.getContent(); // e.g., "userName", "Math.random()"
176
```
177
178
### ASTJspExpressionInAttribute
179
180
Represents JSP expressions used within attribute values.
181
182
```java { .api }
183
public final class ASTJspExpressionInAttribute extends AbstractExpression {
184
public String getContent();
185
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
186
}
187
```
188
189
**Usage:**
190
191
```java
192
import net.sourceforge.pmd.lang.jsp.ast.ASTJspExpressionInAttribute;
193
194
// Expression within attribute: <input value="<%= userInput %>">
195
String expression = jspExprInAttr.getContent(); // e.g., "userInput"
196
```
197
198
### ASTJspScriptlet
199
200
Represents JSP scriptlets `<% ... %>`.
201
202
```java { .api }
203
public final class ASTJspScriptlet extends AbstractContentNode {
204
public String getContent();
205
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
206
}
207
```
208
209
**Usage:**
210
211
```java
212
import net.sourceforge.pmd.lang.jsp.ast.ASTJspScriptlet;
213
214
// Get scriptlet code
215
String code = scriptlet.getContent(); // e.g., "if (user != null) { ... }"
216
```
217
218
### ASTJspDeclaration
219
220
Represents JSP declarations `<%! ... %>`.
221
222
```java { .api }
223
public final class ASTJspDeclaration extends AbstractContentNode {
224
public String getContent();
225
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
226
}
227
```
228
229
**Usage:**
230
231
```java
232
import net.sourceforge.pmd.lang.jsp.ast.ASTJspDeclaration;
233
234
// Get declaration code
235
String declaration = jspDecl.getContent(); // e.g., "private int counter = 0;"
236
```
237
238
### ASTJspComment
239
240
Represents JSP comments `<%-- ... --%>`.
241
242
```java { .api }
243
public final class ASTJspComment extends AbstractContentNode {
244
public String getContent();
245
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
246
}
247
```
248
249
**Usage:**
250
251
```java
252
import net.sourceforge.pmd.lang.jsp.ast.ASTJspComment;
253
254
// Get comment content
255
String comment = jspComment.getContent(); // e.g., "TODO: Add validation"
256
```
257
258
## Expression Language Nodes
259
260
### ASTElExpression
261
262
Represents Expression Language expressions `${...}`.
263
264
```java { .api }
265
public final class ASTElExpression extends AbstractExpression {
266
public String getContent();
267
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
268
}
269
```
270
271
**Usage:**
272
273
```java
274
import net.sourceforge.pmd.lang.jsp.ast.ASTElExpression;
275
276
// Get EL expression
277
String expression = elExpr.getContent(); // e.g., "user.name", "sessionScope.cart"
278
```
279
280
### ASTValueBinding
281
282
Represents JSF value binding expressions `#{...}`.
283
284
```java { .api }
285
public final class ASTValueBinding extends AbstractExpression {
286
public String getContent();
287
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
288
}
289
```
290
291
**Usage:**
292
293
```java
294
import net.sourceforge.pmd.lang.jsp.ast.ASTValueBinding;
295
296
// Get value binding expression
297
String binding = valueBinding.getContent(); // e.g., "managedBean.property"
298
```
299
300
## Text and Content Nodes
301
302
### ASTText
303
304
Represents plain text content.
305
306
```java { .api }
307
public final class ASTText extends AbstractContentNode {
308
public String getContent();
309
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
310
}
311
```
312
313
**Usage:**
314
315
```java
316
import net.sourceforge.pmd.lang.jsp.ast.ASTText;
317
318
// Get text content
319
String text = textNode.getContent(); // e.g., "Hello World", whitespace, etc.
320
```
321
322
### ASTUnparsedText
323
324
Represents text that could not be parsed (possibly due to errors).
325
326
```java { .api }
327
public final class ASTUnparsedText extends AbstractContentNode {
328
public String getContent();
329
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
330
}
331
```
332
333
**Usage:**
334
335
```java
336
import net.sourceforge.pmd.lang.jsp.ast.ASTUnparsedText;
337
338
// Get unparsed text - may indicate parsing issues
339
String unparsedText = unparsedNode.getContent();
340
```
341
342
### ASTCData
343
344
Represents CDATA sections.
345
346
```java { .api }
347
public final class ASTCData extends AbstractContentNode {
348
public String getContent();
349
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
350
}
351
```
352
353
**Usage:**
354
355
```java
356
import net.sourceforge.pmd.lang.jsp.ast.ASTCData;
357
358
// Get CDATA content
359
String cdataContent = cdata.getContent(); // Raw content within <![CDATA[...]]>
360
```
361
362
### ASTHtmlScript
363
364
Represents HTML `<script>` elements with special handling.
365
366
```java { .api }
367
public final class ASTHtmlScript extends AbstractContentNode {
368
public String getContent();
369
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
370
}
371
```
372
373
**Usage:**
374
375
```java
376
import net.sourceforge.pmd.lang.jsp.ast.ASTHtmlScript;
377
378
// Get script content
379
String scriptCode = htmlScript.getContent(); // JavaScript code within <script> tags
380
```
381
382
## XML/DOCTYPE Nodes
383
384
### ASTDoctypeDeclaration
385
386
Represents DOCTYPE declarations.
387
388
```java { .api }
389
public final class ASTDoctypeDeclaration extends AbstractJspNode {
390
public String getName();
391
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
392
}
393
```
394
395
**Usage:**
396
397
```java
398
import net.sourceforge.pmd.lang.jsp.ast.ASTDoctypeDeclaration;
399
400
// Get DOCTYPE name
401
String doctypeName = doctype.getName(); // e.g., "html", "HTML"
402
```
403
404
### ASTDoctypeExternalId
405
406
Represents external entity references in DOCTYPE.
407
408
```java { .api }
409
public final class ASTDoctypeExternalId extends AbstractJspNode {
410
public String getUri();
411
public boolean isHasPublicId();
412
public String getPublicId();
413
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
414
}
415
```
416
417
**Usage:**
418
419
```java
420
import net.sourceforge.pmd.lang.jsp.ast.ASTDoctypeExternalId;
421
422
// Get external ID information
423
String uri = externalId.getUri(); // DTD URI
424
425
if (externalId.isHasPublicId()) {
426
String publicId = externalId.getPublicId(); // Public identifier
427
}
428
```
429
430
### ASTDeclaration
431
432
Represents XML declarations.
433
434
```java { .api }
435
public final class ASTDeclaration extends AbstractJspNode {
436
public String getName();
437
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
438
}
439
```
440
441
### ASTCommentTag
442
443
Represents HTML/XML comment tags.
444
445
```java { .api }
446
public final class ASTCommentTag extends AbstractContentNode {
447
public String getContent();
448
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
449
}
450
```
451
452
**Usage:**
453
454
```java
455
import net.sourceforge.pmd.lang.jsp.ast.ASTCommentTag;
456
457
// Get HTML comment content
458
String comment = commentTag.getContent(); // Content of <!-- ... -->
459
```
460
461
### ASTContent
462
463
Generic content container.
464
465
```java { .api }
466
public final class ASTContent extends AbstractJspNode {
467
protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
468
}
469
```
470
471
## Node Type Checking
472
473
### Using instanceof
474
475
```java
476
// Check node types
477
if (node instanceof ASTElement) {
478
ASTElement element = (ASTElement) node;
479
// Handle element
480
} else if (node instanceof ASTJspExpression) {
481
ASTJspExpression expression = (ASTJspExpression) node;
482
// Handle JSP expression
483
} else if (node instanceof ASTElExpression) {
484
ASTElExpression elExpression = (ASTElExpression) node;
485
// Handle EL expression
486
}
487
```
488
489
### Using Stream Filtering
490
491
```java
492
// Find all elements with specific names
493
List<ASTElement> divElements = root.descendants(ASTElement.class)
494
.filter(elem -> "div".equals(elem.getName()))
495
.collect(Collectors.toList());
496
497
// Find all JSP expressions
498
List<ASTJspExpression> expressions = root.descendants(ASTJspExpression.class)
499
.collect(Collectors.toList());
500
```
501
502
## Content Access Patterns
503
504
### Getting Text Content
505
506
```java
507
// All content nodes extend AbstractContentNode
508
if (node instanceof AbstractContentNode) {
509
AbstractContentNode contentNode = (AbstractContentNode) node;
510
String content = contentNode.getContent();
511
}
512
513
// Specific content node types
514
String jspExprContent = jspExpression.getContent(); // JSP expression
515
String elContent = elExpression.getContent(); // EL expression
516
String textContent = textNode.getContent(); // Plain text
517
String scriptContent = scriptletNode.getContent(); // Scriptlet code
518
```
519
520
### Namespace Handling
521
522
```java
523
// Check for namespaced elements and attributes
524
if (element.isHasNamespacePrefix()) {
525
String namespace = element.getNamespacePrefix(); // e.g., "jsp", "c", "fn"
526
String localName = element.getLocalName(); // e.g., "forward", "forEach"
527
528
// Handle specific namespaces
529
if ("jsp".equals(namespace)) {
530
// Handle JSP actions
531
} else if ("c".equals(namespace)) {
532
// Handle JSTL core tags
533
}
534
}
535
```
536
537
These AST node types provide comprehensive access to all JSP and HTML constructs, enabling detailed analysis and rule development.