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