CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-net-sourceforge-pmd--pmd-jsp

PMD JSP language module providing static code analysis capabilities for JavaServer Pages files with lexical analysis, AST parsing, and rule-based code quality checks.

Pending
Overview
Eval results
Files

ast-node-types.mddocs/

AST Node Types

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.

HTML/XML Structure Nodes

ASTElement

Represents HTML/XML elements (tags).

public final class ASTElement extends AbstractJspNode {
    public String getName();
    public boolean isHasNamespacePrefix();
    public String getNamespacePrefix();
    public String getLocalName();
    public boolean isEmpty();
    public boolean isUnclosed();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTElement;

// Check element properties
if (element.getName().equals("div")) {
    System.out.println("Found div element");
}

// Handle namespaced elements
if (element.isHasNamespacePrefix()) {
    String prefix = element.getNamespacePrefix(); // e.g., "jsp"
    String localName = element.getLocalName();    // e.g., "forward"
    // Full name would be "jsp:forward"
}

// Check if self-closing tag
if (element.isEmpty()) {
    System.out.println("Self-closing tag: <" + element.getName() + "/>");
}

// Check for unclosed tags
if (element.isUnclosed()) {
    System.out.println("Warning: Unclosed tag detected");
}

Methods:

  • getName(): Returns the full element name (e.g., "div", "jsp:forward")
  • isHasNamespacePrefix(): True if element has namespace prefix (contains ":")
  • getNamespacePrefix(): Returns namespace prefix part (before ":") or empty string
  • getLocalName(): Returns local name part (after ":") or full name if no prefix
  • isEmpty(): True for self-closing tags like <br/>
  • isUnclosed(): True if parser found no proper closing tag

ASTAttribute

Represents element attributes.

public final class ASTAttribute extends AbstractJspNode {
    public String getName();
    public boolean isHasNamespacePrefix();
    public String getNamespacePrefix();
    public String getLocalName();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTAttribute;

// Access attribute name
String attrName = attribute.getName(); // e.g., "class", "xmlns:jsp"

// Handle namespaced attributes
if (attribute.isHasNamespacePrefix()) {
    String prefix = attribute.getNamespacePrefix(); // e.g., "xmlns"
    String localName = attribute.getLocalName();    // e.g., "jsp"
}

ASTAttributeValue

Represents attribute values.

public final class ASTAttributeValue extends AbstractJspNode {
    public String getValue();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTAttributeValue;

// Get attribute value
String value = attributeValue.getValue(); // e.g., "button", "width: 100px"

JSP-Specific Nodes

ASTJspDirective

Represents JSP directives like <%@ page ... %>, <%@ include ... %>.

public final class ASTJspDirective extends AbstractJspNode {
    public String getName();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspDirective;

// Check directive type
String directiveName = directive.getName(); // e.g., "page", "include", "taglib"

if ("page".equals(directiveName)) {
    // Handle page directive
} else if ("include".equals(directiveName)) {
    // Handle include directive
}

ASTJspDirectiveAttribute

Represents attributes within JSP directives.

public final class ASTJspDirectiveAttribute extends AbstractJspNode {
    public String getName();
    public String getValue();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspDirectiveAttribute;

// Access directive attribute
String name = dirAttr.getName();   // e.g., "language", "import"
String value = dirAttr.getValue(); // e.g., "java", "java.util.*"

ASTJspExpression

Represents JSP expressions <%= ... %>.

public final class ASTJspExpression extends AbstractExpression {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspExpression;

// Get expression content
String expression = jspExpr.getContent(); // e.g., "userName", "Math.random()"

ASTJspExpressionInAttribute

Represents JSP expressions used within attribute values.

public final class ASTJspExpressionInAttribute extends AbstractExpression {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspExpressionInAttribute;

// Expression within attribute: <input value="<%= userInput %>">
String expression = jspExprInAttr.getContent(); // e.g., "userInput"

ASTJspScriptlet

Represents JSP scriptlets <% ... %>.

public final class ASTJspScriptlet extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspScriptlet;

// Get scriptlet code
String code = scriptlet.getContent(); // e.g., "if (user != null) { ... }"

ASTJspDeclaration

Represents JSP declarations <%! ... %>.

public final class ASTJspDeclaration extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspDeclaration;

// Get declaration code
String declaration = jspDecl.getContent(); // e.g., "private int counter = 0;"

ASTJspComment

Represents JSP comments <%-- ... --%>.

public final class ASTJspComment extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTJspComment;

// Get comment content
String comment = jspComment.getContent(); // e.g., "TODO: Add validation"

Expression Language Nodes

ASTElExpression

Represents Expression Language expressions ${...}.

public final class ASTElExpression extends AbstractExpression {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTElExpression;

// Get EL expression
String expression = elExpr.getContent(); // e.g., "user.name", "sessionScope.cart"

ASTValueBinding

Represents JSF value binding expressions #{...}.

public final class ASTValueBinding extends AbstractExpression {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTValueBinding;

// Get value binding expression
String binding = valueBinding.getContent(); // e.g., "managedBean.property"

Text and Content Nodes

ASTText

Represents plain text content.

public final class ASTText extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTText;

// Get text content
String text = textNode.getContent(); // e.g., "Hello World", whitespace, etc.

ASTUnparsedText

Represents text that could not be parsed (possibly due to errors).

public final class ASTUnparsedText extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTUnparsedText;

// Get unparsed text - may indicate parsing issues
String unparsedText = unparsedNode.getContent();

ASTCData

Represents CDATA sections.

public final class ASTCData extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTCData;

// Get CDATA content
String cdataContent = cdata.getContent(); // Raw content within <![CDATA[...]]>

ASTHtmlScript

Represents HTML <script> elements with special handling.

public final class ASTHtmlScript extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTHtmlScript;

// Get script content
String scriptCode = htmlScript.getContent(); // JavaScript code within <script> tags

XML/DOCTYPE Nodes

ASTDoctypeDeclaration

Represents DOCTYPE declarations.

public final class ASTDoctypeDeclaration extends AbstractJspNode {
    public String getName();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTDoctypeDeclaration;

// Get DOCTYPE name
String doctypeName = doctype.getName(); // e.g., "html", "HTML"

ASTDoctypeExternalId

Represents external entity references in DOCTYPE.

public final class ASTDoctypeExternalId extends AbstractJspNode {
    public String getUri();
    public boolean isHasPublicId();
    public String getPublicId();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTDoctypeExternalId;

// Get external ID information
String uri = externalId.getUri(); // DTD URI

if (externalId.isHasPublicId()) {
    String publicId = externalId.getPublicId(); // Public identifier
}

ASTDeclaration

Represents XML declarations.

public final class ASTDeclaration extends AbstractJspNode {
    public String getName();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

ASTCommentTag

Represents HTML/XML comment tags.

public final class ASTCommentTag extends AbstractContentNode {
    public String getContent();
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Usage:

import net.sourceforge.pmd.lang.jsp.ast.ASTCommentTag;

// Get HTML comment content
String comment = commentTag.getContent(); // Content of <!-- ... -->

ASTContent

Generic content container.

public final class ASTContent extends AbstractJspNode {
    protected <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);
}

Node Type Checking

Using instanceof

// Check node types
if (node instanceof ASTElement) {
    ASTElement element = (ASTElement) node;
    // Handle element
} else if (node instanceof ASTJspExpression) {
    ASTJspExpression expression = (ASTJspExpression) node;
    // Handle JSP expression
} else if (node instanceof ASTElExpression) {
    ASTElExpression elExpression = (ASTElExpression) node;
    // Handle EL expression
}

Using Stream Filtering

// Find all elements with specific names
List<ASTElement> divElements = root.descendants(ASTElement.class)
    .filter(elem -> "div".equals(elem.getName()))
    .collect(Collectors.toList());

// Find all JSP expressions
List<ASTJspExpression> expressions = root.descendants(ASTJspExpression.class)
    .collect(Collectors.toList());

Content Access Patterns

Getting Text Content

// All content nodes extend AbstractContentNode
if (node instanceof AbstractContentNode) {
    AbstractContentNode contentNode = (AbstractContentNode) node;
    String content = contentNode.getContent();
}

// Specific content node types
String jspExprContent = jspExpression.getContent();    // JSP expression
String elContent = elExpression.getContent();          // EL expression  
String textContent = textNode.getContent();            // Plain text
String scriptContent = scriptletNode.getContent();     // Scriptlet code

Namespace Handling

// Check for namespaced elements and attributes
if (element.isHasNamespacePrefix()) {
    String namespace = element.getNamespacePrefix(); // e.g., "jsp", "c", "fn"
    String localName = element.getLocalName();       // e.g., "forward", "forEach"
    
    // Handle specific namespaces
    if ("jsp".equals(namespace)) {
        // Handle JSP actions
    } else if ("c".equals(namespace)) {
        // Handle JSTL core tags
    }
}

These AST node types provide comprehensive access to all JSP and HTML constructs, enabling detailed analysis and rule development.

Install with Tessl CLI

npx tessl i tessl/maven-net-sourceforge-pmd--pmd-jsp

docs

ast-node-types.md

copy-paste-detection.md

index.md

jsp-parser-ast.md

language-module.md

rule-development.md

visitor-pattern.md

tile.json