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   * @version $Id: Xpp3DomAttributeIterator.java 1035671 2010-11-16 16:04:51Z bentmann $
37   */
38  class Xpp3DomAttributeIterator
39      implements NodeIterator
40  {
41  
42      private NodePointer parent;
43  
44      private Xpp3Dom node;
45  
46      private List<Map.Entry<String, String>> attributes;
47  
48      private Map.Entry<String, String> attribute;
49  
50      private int position;
51  
52      public Xpp3DomAttributeIterator( NodePointer parent, QName qname )
53      {
54          this.parent = parent;
55          this.node = (Xpp3Dom) parent.getNode();
56  
57          Map<String, String> map = new LinkedHashMap<String, String>();
58          for ( String name : this.node.getAttributeNames() )
59          {
60              if ( name.equals( qname.getName() ) || "*".equals( qname.getName() ) )
61              {
62                  String value = this.node.getAttribute( name );
63                  map.put( name, value );
64              }
65          }
66          this.attributes = new ArrayList<Map.Entry<String, String>>( map.entrySet() );
67      }
68  
69      public NodePointer getNodePointer()
70      {
71          if ( position == 0 )
72          {
73              setPosition( 1 );
74          }
75          return ( attribute == null ) ? null : new Xpp3DomAttributePointer( parent, attribute );
76      }
77  
78      public int getPosition()
79      {
80          return position;
81      }
82  
83      public boolean setPosition( int position )
84      {
85          this.position = position;
86          attribute = ( position > 0 && position <= attributes.size() ) ? attributes.get( position - 1 ) : null;
87          return attribute != null;
88      }
89  
90  }