View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2.tree.xpath;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.jxpath.ri.Compiler;
24  import org.apache.commons.jxpath.ri.QName;
25  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
26  import org.apache.commons.jxpath.ri.compiler.NodeTest;
27  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
28  import org.apache.commons.jxpath.ri.model.NodePointer;
29  import org.apache.commons.lang3.StringUtils;
30  
31  /**
32   * A specialized iterator implementation for the child nodes of a configuration node.
33   *
34   * @since 1.3
35   * @param <T> the type of the nodes this iterator deals with
36   */
37  final class ConfigurationNodeIteratorChildren<T> extends AbstractConfigurationNodeIterator<T> {
38  
39      /** The list with the sub nodes to iterate over. */
40      private final List<T> subNodes;
41  
42      /**
43       * Creates a new instance of {@code ConfigurationNodeIteratorChildren} and initializes it.
44       *
45       * @param parent the parent pointer
46       * @param nodeTest the test selecting the sub nodes
47       * @param reverse the reverse flag
48       * @param startsWith the first element of the iteration
49       */
50      public ConfigurationNodeIteratorChildren(final ConfigurationNodePointer<T> parent, final NodeTest nodeTest, final boolean reverse,
51          final ConfigurationNodePointer<T> startsWith) {
52          super(parent, reverse);
53          final T root = parent.getConfigurationNode();
54          subNodes = createSubNodeList(root, nodeTest);
55  
56          if (startsWith != null) {
57              setStartOffset(findStartIndex(subNodes, startsWith.getConfigurationNode()));
58          } else if (reverse) {
59              setStartOffset(size());
60          }
61      }
62  
63      /**
64       * Creates the configuration node pointer for the current position.
65       *
66       * @param position the current position in the iteration
67       * @return the node pointer
68       */
69      @Override
70      protected NodePointer createNodePointer(final int position) {
71          return new ConfigurationNodePointer<>(getParent(), subNodes.get(position), getNodeHandler());
72      }
73  
74      /**
75       * Returns the number of elements in this iteration. This is the number of elements in the children list.
76       *
77       * @return the number of elements
78       */
79      @Override
80      protected int size() {
81          return subNodes.size();
82      }
83  
84      /**
85       * Creates the list with sub nodes. This method gets called during initialization phase. It finds out, based on the
86       * given test, which nodes must be iterated over.
87       *
88       * @param node the current node
89       * @param test the test object
90       * @return a list with the matching nodes
91       */
92      private List<T> createSubNodeList(final T node, final NodeTest test) {
93          if (test == null) {
94              return getNodeHandler().getChildren(node);
95          }
96          if (test instanceof NodeNameTest) {
97              final NodeNameTest nameTest = (NodeNameTest) test;
98              final QName name = nameTest.getNodeName();
99              return nameTest.isWildcard() ? createSubNodeListForWildcardName(node, name) : createSubNodeListForName(node, name);
100         }
101         if (test instanceof NodeTypeTest) {
102             final NodeTypeTest typeTest = (NodeTypeTest) test;
103             if (typeTest.getNodeType() == Compiler.NODE_TYPE_NODE || typeTest.getNodeType() == Compiler.NODE_TYPE_TEXT) {
104                 return getNodeHandler().getChildren(node);
105             }
106         }
107 
108         return Collections.emptyList();
109     }
110 
111     /**
112      * Obtains the list of selected nodes for a {@code NodeNameTest} with either a simple or a qualified name.
113      *
114      * @param node the current node
115      * @param name the name to be selected
116      * @return the list with selected sub nodes
117      */
118     private List<T> createSubNodeListForName(final T node, final QName name) {
119         final String compareName = qualifiedName(name);
120         final List<T> result = new ArrayList<>();
121         getNodeHandler().getChildren(node).forEach(child -> {
122             if (StringUtils.equals(compareName, getNodeHandler().nodeName(child))) {
123                 result.add(child);
124             }
125         });
126         return result;
127     }
128 
129     /**
130      * Obtains the list of selected sub nodes for a {@code NodeNameTest} with a wildcard name.
131      *
132      * @param node the current node
133      * @param name the name to be selected
134      * @return the list with selected sub nodes
135      */
136     private List<T> createSubNodeListForWildcardName(final T node, final QName name) {
137         final List<T> children = getNodeHandler().getChildren(node);
138         if (name.getPrefix() == null) {
139             return children;
140         }
141         final List<T> prefixChildren = new ArrayList<>(children.size());
142         final String prefix = prefixName(name.getPrefix(), null);
143         children.forEach(child -> {
144             if (StringUtils.startsWith(getNodeHandler().nodeName(child), prefix)) {
145                 prefixChildren.add(child);
146             }
147         });
148         return prefixChildren;
149     }
150 
151     /**
152      * Determines the start position of the iteration. Finds the index of the given start node in the children of the root
153      * node.
154      *
155      * @param children the children of the root node
156      * @param startNode the start node
157      * @return the start node's index
158      */
159     private int findStartIndex(final List<T> children, final T startNode) {
160         int index = 0;
161         for (final T child : children) {
162             if (child == startNode) {
163                 return index;
164             }
165             index++;
166         }
167 
168         return -1;
169     }
170 
171 }