ConfigurationNodeIteratorAttribute.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.LinkedHashSet;
  20. import java.util.List;
  21. import java.util.Set;

  22. import org.apache.commons.jxpath.ri.QName;
  23. import org.apache.commons.jxpath.ri.model.NodePointer;
  24. import org.apache.commons.lang3.StringUtils;

  25. /**
  26.  * A specialized node iterator implementation that deals with attribute nodes.
  27.  *
  28.  * @param <T> the type of the nodes this iterator deals with
  29.  */
  30. final class ConfigurationNodeIteratorAttribute<T> extends AbstractConfigurationNodeIterator<T> {
  31.     /** Constant for the wildcard node name. */
  32.     private static final String WILDCARD = "*";

  33.     /** Stores the parent node pointer. */
  34.     private final ConfigurationNodePointer<T> parentPointer;

  35.     /** A list with the names of the managed attributes. */
  36.     private final List<String> attributeNames;

  37.     /**
  38.      * Creates a new instance of {@code ConfigurationNodeIteratorAttribute}.
  39.      *
  40.      * @param parent the parent node pointer
  41.      * @param qName the name of the selected attribute
  42.      */
  43.     public ConfigurationNodeIteratorAttribute(final ConfigurationNodePointer<T> parent, final QName qName) {
  44.         super(parent, false);
  45.         parentPointer = parent;
  46.         attributeNames = createAttributeDataList(parent, qName);
  47.     }

  48.     /**
  49.      * Helper method for checking whether an attribute is defined and adding it to the list of attributes to iterate over.
  50.      *
  51.      * @param parent the parent node pointer
  52.      * @param result the result list
  53.      * @param name the name of the current attribute
  54.      */
  55.     private void addAttributeData(final ConfigurationNodePointer<T> parent, final List<String> result, final String name) {
  56.         if (parent.getNodeHandler().getAttributeValue(parent.getConfigurationNode(), name) != null) {
  57.             result.add(name);
  58.         }
  59.     }

  60.     /**
  61.      * Determines which attributes are selected based on the passed in node name.
  62.      *
  63.      * @param parent the parent node pointer
  64.      * @param qName the name of the selected attribute
  65.      * @return a list with the selected attributes
  66.      */
  67.     private List<String> createAttributeDataList(final ConfigurationNodePointer<T> parent, final QName qName) {
  68.         final List<String> result = new ArrayList<>();
  69.         if (!WILDCARD.equals(qName.getName())) {
  70.             addAttributeData(parent, result, qualifiedName(qName));
  71.         } else {
  72.             final Set<String> names = new LinkedHashSet<>(parent.getNodeHandler().getAttributes(parent.getConfigurationNode()));
  73.             final String prefix = qName.getPrefix() != null ? prefixName(qName.getPrefix(), null) : null;
  74.             names.forEach(n -> {
  75.                 if (prefix == null || StringUtils.startsWith(n, prefix)) {
  76.                     addAttributeData(parent, result, n);
  77.                 }
  78.             });
  79.         }

  80.         return result;
  81.     }

  82.     /**
  83.      * Creates a pointer for the node at the specified position.
  84.      *
  85.      * @param position the desired position
  86.      * @return a pointer for the attribute at this position
  87.      */
  88.     @Override
  89.     protected NodePointer createNodePointer(final int position) {
  90.         return new ConfigurationAttributePointer<>(parentPointer, attributeNames.get(position));
  91.     }

  92.     /**
  93.      * Returns the size of the managed iteration.
  94.      *
  95.      * @return the iteration size
  96.      */
  97.     @Override
  98.     protected int size() {
  99.         return attributeNames.size();
  100.     }
  101. }