-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLStructureBaseVisitorExtended.java
More file actions
86 lines (75 loc) · 3.07 KB
/
Copy pathHTMLStructureBaseVisitorExtended.java
File metadata and controls
86 lines (75 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.Token;
public class HTMLStructureBaseVisitorExtended extends HTMLStructureBaseVisitor<Void> {
private int indentLevel = 0;
private int totalNodes = 0;
private int tagElementCount = 0;
private int textElementCount = 0;
private String getIndent() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indentLevel; i++) {
sb.append(" ");
}
return sb.toString();
}
@Override
public Void visitHtmlDoc(HTMLStructureParser.HtmlDocContext ctx) {
System.out.println("--- Starting Parse Tree Walk ---");
Void result = super.visitHtmlDoc(ctx);
System.out.println("--- Parse Tree Walk Completed ---");
System.out.println("\nStatistics Summary:");
System.out.println(" Total Nodes Processed: " + totalNodes);
System.out.println(" Tag Elements Count: " + tagElementCount);
System.out.println(" Text Elements Count: " + textElementCount);
return result;
}
@Override
public Void visitTagElement(HTMLStructureParser.TagElementContext ctx) {
totalNodes++;
tagElementCount++;
String tagName = ctx.IDENTIFIER(0).getText();
String idName = ctx.id != null ? "#" + ctx.id.getText() : "";
// Collect class names
String classStr = "";
if (ctx.className != null && !ctx.className.isEmpty()) {
classStr = ctx.className.stream()
.map(token -> "." + token.getText())
.collect(Collectors.joining());
}
// Collect attributes
String attrStr = "";
if (ctx.attributeList() != null && ctx.attributeList().attribute() != null) {
List<String> attrs = new ArrayList<>();
for (HTMLStructureParser.AttributeContext attrCtx : ctx.attributeList().attribute()) {
attrs.add(attrCtx.name.getText() + "=" + attrCtx.value.getText());
}
attrStr = "[" + String.join(", ", attrs) + "]";
}
System.out.println(getIndent() + "[Tag] " + tagName + idName + classStr + (attrStr.isEmpty() ? "" : " " + attrStr));
indentLevel++;
// Visit children
if (ctx.element() != null) {
for (HTMLStructureParser.ElementContext child : ctx.element()) {
visit(child);
}
}
indentLevel--;
return null;
}
@Override
public Void visitTextElement(HTMLStructureParser.TextElementContext ctx) {
totalNodes++;
textElementCount++;
String text = ctx.value.getText();
// Remove surrounding quotes
if (text.startsWith("\"") && text.endsWith("\"") && text.length() >= 2) {
text = text.substring(1, text.length() - 1);
} else if (text.startsWith("'") && text.endsWith("'") && text.length() >= 2) {
text = text.substring(1, text.length() - 1);
}
System.out.println(getIndent() + "[Text] \"" + text + "\"");
return null;
}
}