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.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 name the name of the selected attribute
48       */
49      public ConfigurationNodeIteratorAttribute(final ConfigurationNodePointer<T> parent, final QName name) {
50          super(parent, false);
51          parentPointer = parent;
52          attributeNames = createAttributeDataList(parent, name);
53      }
54  
55      /**
56       * Creates a pointer for the node at the specified position.
57       *
58       * @param position the desired position
59       * @return a pointer for the attribute at this position
60       */
61      @Override
62      protected NodePointer createNodePointer(final int position) {
63          return new ConfigurationAttributePointer<>(parentPointer, attributeNames.get(position));
64      }
65  
66      /**
67       * Returns the size of the managed iteration.
68       *
69       * @return the iteration size
70       */
71      @Override
72      protected int size() {
73          return attributeNames.size();
74      }
75  
76      /**
77       * Determines which attributes are selected based on the passed in node name.
78       *
79       * @param parent the parent node pointer
80       * @param name the name of the selected attribute
81       * @return a list with the selected attributes
82       */
83      private List<String> createAttributeDataList(final ConfigurationNodePointer<T> parent, final QName name) {
84          final List<String> result = new ArrayList<>();
85          if (!WILDCARD.equals(name.getName())) {
86              addAttributeData(parent, result, qualifiedName(name));
87          } else {
88              final Set<String> names = new LinkedHashSet<>(parent.getNodeHandler().getAttributes(parent.getConfigurationNode()));
89              final String prefix = name.getPrefix() != null ? prefixName(name.getPrefix(), null) : null;
90              names.forEach(n -> {
91                  if (prefix == null || StringUtils.startsWith(n, prefix)) {
92                      addAttributeData(parent, result, n);
93                  }
94              });
95          }
96  
97          return result;
98      }
99  
100     /**
101      * Helper method for checking whether an attribute is defined and adding it to the list of attributes to iterate over.
102      *
103      * @param parent the parent node pointer
104      * @param result the result list
105      * @param name the name of the current attribute
106      */
107     private void addAttributeData(final ConfigurationNodePointer<T> parent, final List<String> result, final String name) {
108         if (parent.getNodeHandler().getAttributeValue(parent.getConfigurationNode(), name) != null) {
109             result.add(name);
110         }
111     }
112 }