View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project.harness;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.jxpath.ri.QName;
27  import org.apache.commons.jxpath.ri.model.NodeIterator;
28  import org.apache.commons.jxpath.ri.model.NodePointer;
29  import org.codehaus.plexus.util.xml.Xpp3Dom;
30  
31  /**
32   * An attribute iterator for JXPath to support <code>Xpp3Dom</code>.
33   *
34   * @author Benjamin Bentmann
35   */
36  class Xpp3DomAttributeIterator implements NodeIterator {
37  
38      private NodePointer parent;
39  
40      private Xpp3Dom node;
41  
42      private List<Map.Entry<String, String>> attributes;
43  
44      private Map.Entry<String, String> attribute;
45  
46      private int position;
47  
48      public Xpp3DomAttributeIterator(NodePointer parent, QName qname) {
49          this.parent = parent;
50          this.node = (Xpp3Dom) parent.getNode();
51  
52          Map<String, String> map = new LinkedHashMap<>();
53          for (String name : this.node.getAttributeNames()) {
54              if (name.equals(qname.getName()) || "*".equals(qname.getName())) {
55                  String value = this.node.getAttribute(name);
56                  map.put(name, value);
57              }
58          }
59          this.attributes = new ArrayList<>(map.entrySet());
60      }
61  
62      public NodePointer getNodePointer() {
63          if (position == 0) {
64              setPosition(1);
65          }
66          return (attribute == null) ? null : new Xpp3DomAttributePointer(parent, attribute);
67      }
68  
69      public int getPosition() {
70          return position;
71      }
72  
73      public boolean setPosition(int position) {
74          this.position = position;
75          attribute = (position > 0 && position <= attributes.size()) ? attributes.get(position - 1) : null;
76          return attribute != null;
77      }
78  }