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       * Creates the list with sub nodes. This method gets called during initialization phase. It finds out, based on the
76       * given test, which nodes must be iterated over.
77       *
78       * @param node the current node
79       * @param test the test object
80       * @return a list with the matching nodes
81       */
82      private List<T> createSubNodeList(final T node, final NodeTest test) {
83          if (test == null) {
84              return getNodeHandler().getChildren(node);
85          }
86          if (test instanceof NodeNameTest) {
87              final NodeNameTest nameTest = (NodeNameTest) test;
88              final QName name = nameTest.getNodeName();
89              return nameTest.isWildcard() ? createSubNodeListForWildcardName(node, name) : createSubNodeListForName(node, name);
90          }
91          if (test instanceof NodeTypeTest) {
92              final NodeTypeTest typeTest = (NodeTypeTest) test;
93              if (typeTest.getNodeType() == Compiler.NODE_TYPE_NODE || typeTest.getNodeType() == Compiler.NODE_TYPE_TEXT) {
94                  return getNodeHandler().getChildren(node);
95              }
96          }
97  
98          return Collections.emptyList();
99      }
100 
101     /**
102      * Obtains the list of selected nodes for a {@code NodeNameTest} with either a simple or a qualified name.
103      *
104      * @param node the current node
105      * @param name the name to be selected
106      * @return the list with selected sub nodes
107      */
108     private List<T> createSubNodeListForName(final T node, final QName name) {
109         final String compareName = qualifiedName(name);
110         final List<T> result = new ArrayList<>();
111         getNodeHandler().getChildren(node).forEach(child -> {
112             if (StringUtils.equals(compareName, getNodeHandler().nodeName(child))) {
113                 result.add(child);
114             }
115         });
116         return result;
117     }
118 
119     /**
120      * Obtains the list of selected sub nodes for a {@code NodeNameTest} with a wildcard name.
121      *
122      * @param node the current node
123      * @param name the name to be selected
124      * @return the list with selected sub nodes
125      */
126     private List<T> createSubNodeListForWildcardName(final T node, final QName name) {
127         final List<T> children = getNodeHandler().getChildren(node);
128         if (name.getPrefix() == null) {
129             return children;
130         }
131         final List<T> prefixChildren = new ArrayList<>(children.size());
132         final String prefix = prefixName(name.getPrefix(), null);
133         children.forEach(child -> {
134             if (StringUtils.startsWith(getNodeHandler().nodeName(child), prefix)) {
135                 prefixChildren.add(child);
136             }
137         });
138         return prefixChildren;
139     }
140 
141     /**
142      * Determines the start position of the iteration. Finds the index of the given start node in the children of the root
143      * node.
144      *
145      * @param children the children of the root node
146      * @param startNode the start node
147      * @return the start node's index
148      */
149     private int findStartIndex(final List<T> children, final T startNode) {
150         int index = 0;
151         for (final T child : children) {
152             if (child == startNode) {
153                 return index;
154             }
155             index++;
156         }
157 
158         return -1;
159     }
160 
161     /**
162      * Returns the number of elements in this iteration. This is the number of elements in the children list.
163      *
164      * @return the number of elements
165      */
166     @Override
167     protected int size() {
168         return subNodes.size();
169     }
170 
171 }