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 tagList.add(elm);
31 }
32
33
34
35
36
37
38
39 public boolean closeTag(String closingTagName) {
40 if (StringUtil.isEmpty(closingTagName))
41 throw new IllegalStateException(
42 "Tried to close a tag with empty name");
43
44 int lastRegisteredTagIdx = tagList.size() - 1;
45
46
47
48
49 boolean matchingTagFound = false;
50 List<ASTElement> processedElmnts = new ArrayList<ASTElement>();
51 for (int i = lastRegisteredTagIdx; i >= 0; i--) {
52 ASTElement parent = tagList.get(i);
53 String parentName = parent.getName();
54
55 processedElmnts.add(parent);
56 if (parentName.equals(closingTagName)) {
57
58 parent.setUnclosed(false);
59
60 parent.setEmpty(false);
61 matchingTagFound = true;
62 break;
63 } else {
64
65
66 if ( !parent.isEmpty()) {
67 parent.setUnclosed(true);
68 }
69
70 parent.setEmpty(true);
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83 if (matchingTagFound) {
84 tagList.removeAll(processedElmnts);
85 }
86
87 return matchingTagFound;
88 }
89
90 public void closeTag(ASTElement z) {
91 closeTag(z.getName());
92 }
93 }