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.xpath;
21
22 import com.puppycrawl.tools.checkstyle.xpath.iterators.DescendantIterator;
23 import net.sf.saxon.Configuration;
24 import net.sf.saxon.om.AxisInfo;
25 import net.sf.saxon.om.GenericTreeInfo;
26 import net.sf.saxon.om.NamespaceUri;
27 import net.sf.saxon.om.NodeInfo;
28 import net.sf.saxon.tree.iter.ArrayIterator;
29 import net.sf.saxon.tree.iter.AxisIterator;
30 import net.sf.saxon.tree.iter.EmptyIterator;
31 import net.sf.saxon.tree.iter.SingleNodeIterator;
32 import net.sf.saxon.type.Type;
33
34
35
36
37 public abstract class AbstractRootNode extends AbstractNode {
38
39
40 private static final String ROOT_NAME = "ROOT";
41
42
43 private static final AbstractNode[] EMPTY_ABSTRACT_NODE_ARRAY = new AbstractNode[0];
44
45
46
47
48 protected AbstractRootNode() {
49 super(new GenericTreeInfo(Configuration.newConfiguration()));
50 }
51
52
53
54
55
56
57
58
59 @Override
60 public int compareOrder(NodeInfo nodeInfo) {
61 throw throwUnsupportedOperationException();
62 }
63
64
65
66
67
68
69
70
71
72 @Override
73 public String getAttributeValue(NamespaceUri namespace, String localPart) {
74 throw throwUnsupportedOperationException();
75 }
76
77
78
79
80
81
82 @Override
83 public String getLocalPart() {
84 return ROOT_NAME;
85 }
86
87
88
89
90
91
92 @Override
93 public int getNodeKind() {
94 return Type.DOCUMENT;
95 }
96
97
98
99
100
101
102 @Override
103 public NodeInfo getParent() {
104 return null;
105 }
106
107
108
109
110
111
112 @Override
113 public NodeInfo getRoot() {
114 return this;
115 }
116
117
118
119
120
121
122
123
124 @Override
125 public AxisIterator iterateAxis(int axisNumber) {
126 return switch (axisNumber) {
127 case AxisInfo.ANCESTOR,
128 AxisInfo.PARENT,
129 AxisInfo.FOLLOWING,
130 AxisInfo.FOLLOWING_SIBLING,
131 AxisInfo.PRECEDING,
132 AxisInfo.PRECEDING_SIBLING -> EmptyIterator.ofNodes();
133
134 case AxisInfo.ANCESTOR_OR_SELF,
135 AxisInfo.SELF -> SingleNodeIterator.makeIterator(this);
136
137 case AxisInfo.CHILD -> {
138 if (hasChildNodes()) {
139 yield new ArrayIterator.OfNodes<>(
140 getChildren().toArray(EMPTY_ABSTRACT_NODE_ARRAY));
141 }
142 yield EmptyIterator.ofNodes();
143 }
144
145 case AxisInfo.DESCENDANT -> {
146 if (hasChildNodes()) {
147 yield new DescendantIterator(this, DescendantIterator.StartWith.CHILDREN);
148 }
149 yield EmptyIterator.ofNodes();
150 }
151
152 case AxisInfo.DESCENDANT_OR_SELF ->
153 new DescendantIterator(this, DescendantIterator.StartWith.CURRENT_NODE);
154
155 default -> throw throwUnsupportedOperationException();
156 };
157 }
158
159
160
161
162
163
164 @Override
165 public int getDepth() {
166 return 0;
167 }
168
169
170
171
172
173
174 private static UnsupportedOperationException throwUnsupportedOperationException() {
175 return new UnsupportedOperationException("Operation is not supported");
176 }
177
178 }