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.List;
22  import java.util.Map;
23  import java.util.stream.Collectors;
24  
25  import org.apache.commons.jxpath.ri.QName;
26  import org.apache.commons.jxpath.ri.model.NodeIterator;
27  import org.apache.commons.jxpath.ri.model.NodePointer;
28  import org.apache.maven.internal.xml.XmlNodeImpl;
29  
30  /**
31   * An attribute iterator for JXPath to support <code>Xpp3Dom</code>.
32   *
33   */
34  class Xpp3DomAttributeIterator implements NodeIterator {
35  
36      private NodePointer parent;
37  
38      private XmlNodeImpl node;
39  
40      private List<Map.Entry<String, String>> attributes;
41  
42      private Map.Entry<String, String> attribute;
43  
44      private int position;
45  
46      public Xpp3DomAttributeIterator(NodePointer parent, QName qname) {
47          this.parent = parent;
48          this.node = (XmlNodeImpl) parent.getNode();
49  
50          this.attributes = this.node.getAttributes().entrySet().stream()
51                  .filter(a -> a.getKey().equals(qname.getName()) || "*".equals(qname.getName()))
52                  .collect(Collectors.toList());
53      }
54  
55      public NodePointer getNodePointer() {
56          if (position == 0) {
57              setPosition(1);
58          }
59          return (attribute == null) ? null : new Xpp3DomAttributePointer(parent, attribute);
60      }
61  
62      public int getPosition() {
63          return position;
64      }
65  
66      public boolean setPosition(int position) {
67          this.position = position;
68          attribute = (position > 0 && position <= attributes.size()) ? attributes.get(position - 1) : null;
69          return attribute != null;
70      }
71  }