0
# PMD JSP Language Module
1
2
PMD JSP Language Module provides static code analysis capabilities for JavaServer Pages (JSP) files. This module implements a complete JSP language parser, generates Abstract Syntax Trees (AST) for JSP documents, and includes comprehensive rule-based code quality checks specifically designed for JSP syntax and best practices.
3
4
## Package Information
5
6
- **Package Name**: pmd-jsp
7
- **Package Type**: Maven
8
- **Group ID**: net.sourceforge.pmd
9
- **Artifact ID**: pmd-jsp
10
- **Language**: Java
11
- **Installation**: Add to `pom.xml`:
12
13
```xml
14
<dependency>
15
<groupId>net.sourceforge.pmd</groupId>
16
<artifactId>pmd-jsp</artifactId>
17
<version>7.13.0</version>
18
</dependency>
19
```
20
21
## Core Imports
22
23
```java
24
import net.sourceforge.pmd.lang.jsp.JspLanguageModule;
25
import net.sourceforge.pmd.lang.jsp.ast.*;
26
import net.sourceforge.pmd.lang.jsp.rule.AbstractJspRule;
27
```
28
29
## Basic Usage
30
31
```java
32
import net.sourceforge.pmd.lang.jsp.JspLanguageModule;
33
import net.sourceforge.pmd.lang.jsp.ast.ASTCompilationUnit;
34
import net.sourceforge.pmd.lang.jsp.ast.JspParser;
35
import net.sourceforge.pmd.lang.ast.Parser;
36
37
// Get the JSP language module instance
38
JspLanguageModule jspModule = JspLanguageModule.getInstance();
39
40
// Create parser for JSP files
41
Parser parser = new JspParser();
42
43
// Parse JSP content into AST (requires PMD framework setup)
44
// ASTCompilationUnit ast = parser.parse(parserTask);
45
```
46
47
## Architecture
48
49
The PMD JSP module is built on several key components:
50
51
- **Language Registration**: `JspLanguageModule` registers JSP as a supported language in PMD
52
- **Parser**: `JspParser` converts JSP source into Abstract Syntax Trees using generated JavaCC parser
53
- **AST Nodes**: Comprehensive set of AST node classes representing all JSP/HTML elements
54
- **Visitor Pattern**: `JspVisitor` interface and `JspVisitorBase` for AST traversal
55
- **Rule Framework**: `AbstractJspRule` base class for implementing custom JSP analysis rules
56
- **CPD Support**: `JspCpdLexer` for copy-paste detection in JSP files
57
58
## Capabilities
59
60
### Language Module Registration
61
62
Register JSP language with PMD framework and configure file extensions.
63
64
```java { .api }
65
public class JspLanguageModule extends SimpleLanguageModuleBase implements CpdCapableLanguage {
66
public JspLanguageModule();
67
public static JspLanguageModule getInstance();
68
public CpdLexer createCpdLexer(LanguagePropertyBundle bundle);
69
}
70
```
71
72
[Language Module](./language-module.md)
73
74
### JSP Parsing and AST
75
76
Parse JSP files into Abstract Syntax Trees with comprehensive node types for all JSP constructs.
77
78
```java { .api }
79
public final class JspParser extends JjtreeParserAdapter<ASTCompilationUnit> {
80
public JspParser();
81
protected ASTCompilationUnit parseImpl(CharStream cs, ParserTask task) throws ParseException;
82
}
83
84
public interface JspNode extends JjtreeNode<JspNode> {
85
}
86
87
public final class ASTCompilationUnit extends AbstractJspNode implements RootNode {
88
public AstInfo<ASTCompilationUnit> getAstInfo();
89
}
90
```
91
92
[JSP Parser and AST](./jsp-parser-ast.md)
93
94
### AST Node Types
95
96
Comprehensive set of AST node classes for JSP elements, HTML structures, and embedded expressions.
97
98
```java { .api }
99
public final class ASTElement extends AbstractJspNode {
100
public String getName();
101
public boolean isHasNamespacePrefix();
102
public String getNamespacePrefix();
103
public String getLocalName();
104
public boolean isEmpty();
105
public boolean isUnclosed();
106
}
107
108
public final class ASTJspExpression extends AbstractExpression {
109
public String getContent();
110
}
111
112
public final class ASTElExpression extends AbstractExpression {
113
public String getContent();
114
}
115
```
116
117
[AST Node Types](./ast-node-types.md)
118
119
### Visitor Pattern
120
121
Traverse and analyze JSP AST using the visitor pattern for custom analysis logic.
122
123
```java { .api }
124
public class JspVisitorBase<P, R> extends AstVisitorBase<P, R> implements JspVisitor<P, R> {
125
}
126
127
public abstract class AbstractJspNode extends AbstractJjtreeNode<AbstractJspNode, JspNode> implements JspNode {
128
public final <P, R> R acceptVisitor(AstVisitor<? super P, ? extends R> visitor, P data);
129
protected abstract <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
130
}
131
```
132
133
[Visitor Pattern](./visitor-pattern.md)
134
135
### Rule Development
136
137
Create custom JSP analysis rules using the rule framework for code quality checks.
138
139
```java { .api }
140
public abstract class AbstractJspRule extends AbstractRule implements JspVisitor<Object, Object> {
141
public void apply(Node target, RuleContext ctx);
142
public Object visitNode(Node node, Object param);
143
}
144
```
145
146
[Rule Development](./rule-development.md)
147
148
### Copy-Paste Detection
149
150
Tokenize JSP files for duplicate code detection with PMD's CPD (Copy-Paste Detector).
151
152
```java { .api }
153
public class JspCpdLexer extends JavaccCpdLexer {
154
protected TokenManager<JavaccToken> makeLexerImpl(TextDocument doc);
155
}
156
```
157
158
[Copy-Paste Detection](./copy-paste-detection.md)
159
160
## Built-in Rules
161
162
The module includes pre-built rules organized into categories:
163
164
- **Best Practices**: JSF nesting, class attributes, HTML comments, JSP forwards
165
- **Security**: Unsanitized JSP expressions, iframe attributes
166
- **Design**: Inline styles, scriptlets, long scripts
167
- **Code Style**: Duplicate imports
168
- **Error Prone**: Encoding issues
169
170
All rules are defined in XML rulesets and can be configured through PMD's standard rule configuration mechanisms.
171
172
## Types
173
174
### Core Interfaces
175
176
```java { .api }
177
public interface JspNode extends JjtreeNode<JspNode> {
178
// Marker interface for all JSP AST nodes
179
// Extends JjtreeNode to provide standard AST node functionality
180
}
181
182
public interface JspVisitor<P, R> {
183
// Generated interface for visiting JSP AST nodes
184
// Contains visit methods for all concrete AST node types
185
}
186
```
187
188
### Base Classes
189
190
```java { .api }
191
public abstract class AbstractJspNode extends AbstractJjtreeNode<AbstractJspNode, JspNode> implements JspNode {
192
protected AbstractJspNode(int id);
193
public final <P, R> R acceptVisitor(AstVisitor<? super P, ? extends R> visitor, P data);
194
protected abstract <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
195
public String getXPathNodeName();
196
}
197
198
public abstract class AbstractContentNode extends AbstractJspNode {
199
public String getContent();
200
}
201
202
public abstract class AbstractExpression extends AbstractContentNode {
203
}
204
```
205
206
### Language Registration
207
208
```java { .api }
209
public class JspHandler extends AbstractPmdLanguageVersionHandler {
210
public Parser getParser();
211
}
212
```
213
214
### Exception Types
215
216
```java { .api }
217
// Standard PMD parsing exceptions apply
218
import net.sourceforge.pmd.lang.ast.ParseException;
219
```