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    *     https://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.LinkedHashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.commons.jxpath.ri.QName;
25  import org.apache.commons.jxpath.ri.model.NodePointer;
26  import org.apache.commons.lang3.StringUtils;
27  
28  /**
29   * A specialized node iterator implementation that deals with attribute nodes.
30   *
31   * @param <T> the type of the nodes this iterator deals with
32   */
33  final class ConfigurationNodeIteratorAttribute<T> extends AbstractConfigurationNodeIterator<T> {
34      /** Constant for the wildcard node name. */
35      private static final String WILDCARD = "*";
36  
37      /** Stores the parent node pointer. */
38      private final ConfigurationNodePointer<T> parentPointer;
39  
40      /** A list with the names of the managed attributes. */
41      private final List<String> attributeNames;
42  
43      /**
44       * Creates a new instance of {@code ConfigurationNodeIteratorAttribute}.
45       *
46       * @param parent the parent node pointer
47       * @param qName the name of the selected attribute
48       */
49      public ConfigurationNodeIteratorAttribute(final ConfigurationNodePointer<T> parent, final QName qName) {
50          super(parent, false);
51          parentPointer = parent;
52          attributeNames = createAttributeDataList(parent, qName);
53      }
54  
55      /**
56       * Helper method for checking whether an attribute is defined and adding it to the list of attributes to iterate over.
57       *
58       * @param parent the parent node pointer
59       * @param result the result list
60       * @param name the name of the current attribute
61       */
62      private void addAttributeData(final ConfigurationNodePointer<T> parent, final List<String> result, final String name) {
63          if (parent.getNodeHandler().getAttributeValue(parent.getConfigurationNode(), name) != null) {
64              result.add(name);
65          }
66      }
67  
68      /**
69       * Determines which attributes are selected based on the passed in node name.
70       *
71       * @param parent the parent node pointer
72       * @param qName the name of the selected attribute
73       * @return a list with the selected attributes
74       */
75      private List<String> createAttributeDataList(final ConfigurationNodePointer<T> parent, final QName qName) {
76          final List<String> result = new ArrayList<>();
77          if (!WILDCARD.equals(qName.getName())) {
78              addAttributeData(parent, result, qualifiedName(qName));
79          } else {
80              final Set<String> names = new LinkedHashSet<>(parent.getNodeHandler().getAttributes(parent.getConfigurationNode()));
81              final String prefix = qName.getPrefix() != null ? prefixName(qName.getPrefix(), null) : null;
82              names.forEach(n -> {
83                  if (prefix == null || StringUtils.startsWith(n, prefix)) {
84                      addAttributeData(parent, result, n);
85                  }
86              });
87          }
88  
89          return result;
90      }
91  
92      /**
93       * Creates a pointer for the node at the specified position.
94       *
95       * @param position the desired position
96       * @return a pointer for the attribute at this position
97       */
98      @Override
99      protected NodePointer createNodePointer(final int position) {
100         return new ConfigurationAttributePointer<>(parentPointer, attributeNames.get(position));
101     }
102 
103     /**
104      * Returns the size of the managed iteration.
105      *
106      * @return the iteration size
107      */
108     @Override
109     protected int size() {
110         return attributeNames.size();
111     }
112 }