1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.internal.utils;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28 import com.puppycrawl.tools.checkstyle.xpath.RootNode;
29 import net.sf.saxon.om.NodeInfo;
30
31
32
33
34
35
36
37 public final class XpathIteratorUtil {
38
39
40
41
42 private static final Map<String, NodeInfo> NODES_BY_LOCAL_PART = new HashMap<>();
43
44 static {
45 fillMap(new RootNode(createTree()));
46 }
47
48 private XpathIteratorUtil() {
49 }
50
51
52
53
54
55
56
57
58 public static NodeInfo findNode(String localPart) {
59 return NODES_BY_LOCAL_PART.get(localPart);
60 }
61
62
63
64
65
66
67 private static void fillMap(NodeInfo cur) {
68 NODES_BY_LOCAL_PART.put(cur.getLocalPart(), cur);
69 for (NodeInfo node : cur.children()) {
70 fillMap(node);
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 private static DetailAST createTree() {
92 final DetailAstImpl rootNode = createNode(TokenTypes.PACKAGE_DEF, 1, 0);
93
94 final DetailAstImpl annotationsNode = createNode(rootNode, TokenTypes.ANNOTATIONS, 1, 8);
95 createNode(annotationsNode, TokenTypes.ANNOTATION_DEF, 1, 8);
96
97 final DetailAstImpl classDefNode = createNode(rootNode, TokenTypes.CLASS_DEF, 3, 0);
98 final DetailAstImpl objblockNode = createNode(classDefNode, TokenTypes.OBJBLOCK, 3, 19);
99 createNode(objblockNode, TokenTypes.LCURLY, 3, 19);
100 final DetailAstImpl methodDefNode = createNode(objblockNode, TokenTypes.METHOD_DEF, 4, 4);
101 createNode(objblockNode, TokenTypes.RCURLY, 8, 0);
102
103 createNode(methodDefNode, TokenTypes.PARAMETERS, 4, 31);
104 createNode(methodDefNode, TokenTypes.SLIST, 4, 33);
105
106 return rootNode;
107 }
108
109
110
111
112
113
114
115
116
117
118
119 private static DetailAstImpl createNode(DetailAstImpl parent, int tokenType, int lineNo,
120 int columnNo) {
121 final DetailAstImpl result = createNode(tokenType, lineNo, columnNo);
122 parent.addChild(result);
123 return result;
124 }
125
126
127
128
129
130
131
132
133
134
135 private static DetailAstImpl createNode(int tokenType, int lineNo, int columnNo) {
136 final DetailAstImpl result = new DetailAstImpl();
137 result.setType(tokenType);
138 result.setLineNo(lineNo);
139 result.setColumnNo(columnNo);
140 return result;
141 }
142 }