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.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.apache.commons.configuration.tree.ConfigurationNode;
24 import org.apache.commons.jxpath.ri.Compiler;
25 import org.apache.commons.jxpath.ri.QName;
26 import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
27 import org.apache.commons.jxpath.ri.compiler.NodeTest;
28 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
29 import org.apache.commons.jxpath.ri.model.NodePointer;
30 import org.apache.commons.lang.StringUtils;
31
32
33
34
35
36
37
38
39
40
41
42 class ConfigurationNodeIteratorChildren extends ConfigurationNodeIteratorBase
43 {
44
45
46
47
48
49
50
51
52
53 public ConfigurationNodeIteratorChildren(NodePointer parent,
54 NodeTest nodeTest, boolean reverse, NodePointer startsWith)
55 {
56 super(parent, reverse);
57 ConfigurationNode root = (ConfigurationNode) parent.getNode();
58 List<ConfigurationNode> childNodes = createSubNodeList(root, nodeTest);
59 initSubNodeList(childNodes);
60 if (startsWith != null)
61 {
62 setStartOffset(findStartIndex(root,
63 (ConfigurationNode) startsWith.getNode()));
64 }
65 }
66
67
68
69
70
71
72
73
74
75
76 protected List<ConfigurationNode> createSubNodeList(ConfigurationNode node, NodeTest test)
77 {
78 List<ConfigurationNode> children = node.getChildren();
79
80 if (test == null)
81 {
82 return children;
83 }
84 else
85 {
86 if (test instanceof NodeNameTest)
87 {
88 NodeNameTest nameTest = (NodeNameTest) test;
89 QName name = nameTest.getNodeName();
90 if (name.getPrefix() == null)
91 {
92 if (nameTest.isWildcard())
93 {
94 return children;
95 }
96
97 List<ConfigurationNode> result = new ArrayList<ConfigurationNode>();
98 for (ConfigurationNode child : children)
99 {
100 if (StringUtils.equals(name.getName(), child.getName()))
101 {
102 result.add(child);
103 }
104 }
105 return result;
106 }
107 }
108
109 else if (test instanceof NodeTypeTest)
110 {
111 NodeTypeTest typeTest = (NodeTypeTest) test;
112 if (typeTest.getNodeType() == Compiler.NODE_TYPE_NODE
113 || typeTest.getNodeType() == Compiler.NODE_TYPE_TEXT)
114 {
115 return children;
116 }
117 }
118 }
119
120 return Collections.emptyList();
121 }
122
123
124
125
126
127
128
129
130
131 protected int findStartIndex(ConfigurationNode node,
132 ConfigurationNode startNode)
133 {
134 for (int index = 0; index < node.getChildrenCount(); index++)
135 {
136 if (node.getChild(index) == startNode)
137 {
138 return index;
139 }
140 }
141
142 return -1;
143 }
144 }