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