1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.tree.xpath;
18
19 import java.util.List;
20 import java.util.Locale;
21
22 import org.apache.commons.configuration.tree.ConfigurationNode;
23 import org.apache.commons.jxpath.ri.Compiler;
24 import org.apache.commons.jxpath.ri.QName;
25 import org.apache.commons.jxpath.ri.compiler.NodeTest;
26 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
27 import org.apache.commons.jxpath.ri.model.NodeIterator;
28 import org.apache.commons.jxpath.ri.model.NodePointer;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 class ConfigurationNodePointer extends NodePointer
45 {
46
47
48
49 private static final long serialVersionUID = -1087475639680007713L;
50
51
52 private ConfigurationNode node;
53
54
55
56
57
58
59
60 public ConfigurationNodePointer(ConfigurationNode node, Locale locale)
61 {
62 super(null, locale);
63 this.node = node;
64 }
65
66
67
68
69
70
71
72
73 public ConfigurationNodePointer(NodePointer parent, ConfigurationNode node)
74 {
75 super(parent);
76 this.node = node;
77 }
78
79
80
81
82
83
84
85 @Override
86 public boolean isLeaf()
87 {
88 return node.getChildrenCount() < 1;
89 }
90
91
92
93
94
95
96 @Override
97 public boolean isCollection()
98 {
99 return false;
100 }
101
102
103
104
105
106
107 @Override
108 public int getLength()
109 {
110 return 1;
111 }
112
113
114
115
116
117
118
119 @Override
120 public boolean isAttribute()
121 {
122 return node.isAttribute();
123 }
124
125
126
127
128
129
130 @Override
131 public QName getName()
132 {
133 return new QName(null, node.getName());
134 }
135
136
137
138
139
140
141
142 @Override
143 public Object getBaseValue()
144 {
145 return node;
146 }
147
148
149
150
151
152
153 @Override
154 public Object getImmediateNode()
155 {
156 return node;
157 }
158
159
160
161
162
163
164 @Override
165 public Object getValue()
166 {
167 return node.getValue();
168 }
169
170
171
172
173
174
175 @Override
176 public void setValue(Object value)
177 {
178 node.setValue(value);
179 }
180
181
182
183
184
185
186
187
188 @Override
189 public int compareChildNodePointers(NodePointer pointer1,
190 NodePointer pointer2)
191 {
192 ConfigurationNode node1 = (ConfigurationNode) pointer1.getBaseValue();
193 ConfigurationNode node2 = (ConfigurationNode) pointer2.getBaseValue();
194
195
196 if (node1.isAttribute() && !node2.isAttribute())
197 {
198 return -1;
199 }
200 else if (node2.isAttribute() && !node1.isAttribute())
201 {
202 return 1;
203 }
204
205 else
206 {
207
208 List<ConfigurationNode> subNodes = node1.isAttribute() ? node.getAttributes() : node
209 .getChildren();
210 for (ConfigurationNode child : subNodes)
211 {
212 if (child == node1)
213 {
214 return -1;
215 }
216 else if (child == node2)
217 {
218 return 1;
219 }
220 }
221 return 0;
222 }
223 }
224
225
226
227
228
229
230
231 @Override
232 public NodeIterator attributeIterator(QName name)
233 {
234 return new ConfigurationNodeIteratorAttribute(this, name);
235 }
236
237
238
239
240
241
242
243
244
245 @Override
246 public NodeIterator childIterator(NodeTest test, boolean reverse,
247 NodePointer startWith)
248 {
249 return new ConfigurationNodeIteratorChildren(this, test, reverse,
250 startWith);
251 }
252
253
254
255
256
257
258
259
260 @Override
261 public boolean testNode(NodeTest test)
262 {
263 if (test instanceof NodeTypeTest
264 && ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_TEXT)
265 {
266 return true;
267 }
268 return super.testNode(test);
269 }
270 }