ConfigurationNodeIteratorChildren.java

  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. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.apache.commons.jxpath.ri.Compiler;
  22. import org.apache.commons.jxpath.ri.QName;
  23. import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
  24. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  25. import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
  26. import org.apache.commons.jxpath.ri.model.NodePointer;
  27. import org.apache.commons.lang3.StringUtils;

  28. /**
  29.  * A specialized iterator implementation for the child nodes of a configuration node.
  30.  *
  31.  * @param <T> the type of the nodes this iterator deals with
  32.  * @since 1.3
  33.  */
  34. final class ConfigurationNodeIteratorChildren<T> extends AbstractConfigurationNodeIterator<T> {

  35.     /** The list with the sub nodes to iterate over. */
  36.     private final List<T> subNodes;

  37.     /**
  38.      * Creates a new instance of {@code ConfigurationNodeIteratorChildren} and initializes it.
  39.      *
  40.      * @param parent the parent pointer
  41.      * @param nodeTest the test selecting the sub nodes
  42.      * @param reverse the reverse flag
  43.      * @param startsWith the first element of the iteration
  44.      */
  45.     public ConfigurationNodeIteratorChildren(final ConfigurationNodePointer<T> parent, final NodeTest nodeTest, final boolean reverse,
  46.         final ConfigurationNodePointer<T> startsWith) {
  47.         super(parent, reverse);
  48.         final T root = parent.getConfigurationNode();
  49.         subNodes = createSubNodeList(root, nodeTest);

  50.         if (startsWith != null) {
  51.             setStartOffset(findStartIndex(subNodes, startsWith.getConfigurationNode()));
  52.         } else if (reverse) {
  53.             setStartOffset(size());
  54.         }
  55.     }

  56.     /**
  57.      * Creates the configuration node pointer for the current position.
  58.      *
  59.      * @param position the current position in the iteration
  60.      * @return the node pointer
  61.      */
  62.     @Override
  63.     protected NodePointer createNodePointer(final int position) {
  64.         return new ConfigurationNodePointer<>(getParent(), subNodes.get(position), getNodeHandler());
  65.     }

  66.     /**
  67.      * Creates the list with sub nodes. This method gets called during initialization phase. It finds out, based on the
  68.      * given test, which nodes must be iterated over.
  69.      *
  70.      * @param node the current node
  71.      * @param test the test object
  72.      * @return a list with the matching nodes
  73.      */
  74.     private List<T> createSubNodeList(final T node, final NodeTest test) {
  75.         if (test == null) {
  76.             return getNodeHandler().getChildren(node);
  77.         }
  78.         if (test instanceof NodeNameTest) {
  79.             final NodeNameTest nameTest = (NodeNameTest) test;
  80.             final QName qName = nameTest.getNodeName();
  81.             return nameTest.isWildcard() ? createSubNodeListForWildcardName(node, qName) : createSubNodeListForName(node, qName);
  82.         }
  83.         if (test instanceof NodeTypeTest) {
  84.             final NodeTypeTest typeTest = (NodeTypeTest) test;
  85.             if (typeTest.getNodeType() == Compiler.NODE_TYPE_NODE || typeTest.getNodeType() == Compiler.NODE_TYPE_TEXT) {
  86.                 return getNodeHandler().getChildren(node);
  87.             }
  88.         }

  89.         return Collections.emptyList();
  90.     }

  91.     /**
  92.      * Obtains the list of selected nodes for a {@code NodeNameTest} with either a simple or a qualified name.
  93.      *
  94.      * @param node the current node
  95.      * @param qName the name to be selected
  96.      * @return the list with selected sub nodes
  97.      */
  98.     private List<T> createSubNodeListForName(final T node, final QName qName) {
  99.         final String compareName = qualifiedName(qName);
  100.         final List<T> result = new ArrayList<>();
  101.         getNodeHandler().getChildren(node).forEach(child -> {
  102.             if (StringUtils.equals(compareName, getNodeHandler().nodeName(child))) {
  103.                 result.add(child);
  104.             }
  105.         });
  106.         return result;
  107.     }

  108.     /**
  109.      * Obtains the list of selected sub nodes for a {@code NodeNameTest} with a wildcard name.
  110.      *
  111.      * @param node the current node
  112.      * @param qName the name to be selected
  113.      * @return the list with selected sub nodes
  114.      */
  115.     private List<T> createSubNodeListForWildcardName(final T node, final QName qName) {
  116.         final List<T> children = getNodeHandler().getChildren(node);
  117.         if (qName.getPrefix() == null) {
  118.             return children;
  119.         }
  120.         final List<T> prefixChildren = new ArrayList<>(children.size());
  121.         final String prefix = prefixName(qName.getPrefix(), null);
  122.         children.forEach(child -> {
  123.             if (StringUtils.startsWith(getNodeHandler().nodeName(child), prefix)) {
  124.                 prefixChildren.add(child);
  125.             }
  126.         });
  127.         return prefixChildren;
  128.     }

  129.     /**
  130.      * Determines the start position of the iteration. Finds the index of the given start node in the children of the root
  131.      * node.
  132.      *
  133.      * @param children the children of the root node
  134.      * @param startNode the start node
  135.      * @return the start node's index
  136.      */
  137.     private int findStartIndex(final List<T> children, final T startNode) {
  138.         int index = 0;
  139.         for (final T child : children) {
  140.             if (child == startNode) {
  141.                 return index;
  142.             }
  143.             index++;
  144.         }

  145.         return -1;
  146.     }

  147.     /**
  148.      * Returns the number of elements in this iteration. This is the number of elements in the children list.
  149.      *
  150.      * @return the number of elements
  151.      */
  152.     @Override
  153.     protected int size() {
  154.         return subNodes.size();
  155.     }

  156. }