1
2
3
4 package net.sourceforge.pmd.lang.jsp.ast;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import net.sourceforge.pmd.util.StringUtil;
10
11
12
13
14
15
16
17
18
19
20
21 public class OpenTagRegister {
22
23 private List<ASTElement> tagList = new ArrayList<ASTElement>();
24
25 public void openTag(ASTElement elm) {
26 if (elm == null || StringUtil.isEmpty(elm.getName())) {
27 throw new IllegalStateException(
28 "Tried to open a tag with empty name");
29 }
30
31 tagList.add(elm);
32 }
33
34
35
36
37
38
39
40 public boolean closeTag(String closingTagName) {
41 if (StringUtil.isEmpty(closingTagName)) {
42 throw new IllegalStateException(
43 "Tried to close a tag with empty name");
44 }
45
46 int lastRegisteredTagIdx = tagList.size() - 1;
47
48
49
50
51 boolean matchingTagFound = false;
52 List<ASTElement> processedElmnts = new ArrayList<ASTElement>();
53 for (int i = lastRegisteredTagIdx; i >= 0; i--) {
54 ASTElement parent = tagList.get(i);
55 String parentName = parent.getName();
56
57 processedElmnts.add(parent);
58 if (parentName.equals(closingTagName)) {
59
60 parent.setUnclosed(false);
61
62 parent.setEmpty(false);
63 matchingTagFound = true;
64 break;
65 } else {
66
67
68 if ( !parent.isEmpty()) {
69 parent.setUnclosed(true);
70 }
71
72 parent.setEmpty(true);
73 }
74 }
75
76
77
78
79
80
81
82
83
84
85 if (matchingTagFound) {
86 tagList.removeAll(processedElmnts);
87 }
88
89 return matchingTagFound;
90 }
91
92 public void closeTag(ASTElement z) {
93 closeTag(z.getName());
94 }
95 }