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.List;
24  
25  import org.apache.commons.jxpath.ri.Compiler;
26  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
27  import org.apache.commons.jxpath.ri.compiler.NodeTest;
28  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
29  import org.apache.commons.jxpath.ri.model.NodeIterator;
30  import org.apache.commons.jxpath.ri.model.NodePointer;
31  import org.codehaus.plexus.util.StringUtils;
32  import org.codehaus.plexus.util.xml.Xpp3Dom;
33  
34  /**
35   * A node iterator for JXPath to support <code>Xpp3Dom</code>.
36   * 
37   * @author Benjamin Bentmann
38   */
39  class Xpp3DomNodeIterator
40      implements NodeIterator
41  {
42  
43      private NodePointer parent;
44  
45      private NodeTest test;
46  
47      private Xpp3Dom node;
48  
49      private Xpp3Dom[] children;
50  
51      private List<Xpp3Dom> filteredChildren = new ArrayList<Xpp3Dom>();
52  
53      private int filteredIndex;
54  
55      private Xpp3Dom child;
56  
57      private int position;
58  
59      public Xpp3DomNodeIterator( NodePointer parent, NodeTest test, boolean reverse, NodePointer startWith )
60      {
61          this.parent = parent;
62          this.node = (Xpp3Dom) parent.getNode();
63          this.children = this.node.getChildren();
64          if ( startWith != null )
65          {
66              for ( ; filteredIndex < children.length; filteredIndex++ )
67              {
68                  if ( startWith.equals( children[filteredIndex] ) )
69                  {
70                      filteredIndex++;
71                      break;
72                  }
73              }
74          }
75          this.test = test;
76          if ( reverse )
77          {
78              throw new UnsupportedOperationException();
79          }
80      }
81  
82      public NodePointer getNodePointer()
83      {
84          if ( position == 0 )
85          {
86              setPosition( 1 );
87          }
88          return ( child == null ) ? null : new Xpp3DomNodePointer( parent, child );
89      }
90  
91      public int getPosition()
92      {
93          return position;
94      }
95  
96      public boolean setPosition( int position )
97      {
98          this.position = position;
99          filterChildren( position );
100         child = ( position > 0 && position <= filteredChildren.size() ) ? filteredChildren.get( position - 1 ) : null;
101         return child != null;
102     }
103 
104     private void filterChildren( int position )
105     {
106         for ( ; position > filteredChildren.size() && filteredIndex < children.length; filteredIndex++ )
107         {
108             Xpp3Dom child = children[filteredIndex];
109             if ( testNode( child ) )
110             {
111                 filteredChildren.add( child );
112             }
113         }
114     }
115 
116     private boolean testNode( Xpp3Dom node )
117     {
118         if ( test == null )
119         {
120             return true;
121         }
122         if ( test instanceof NodeNameTest )
123         {
124             String nodeName = node.getName();
125             if ( StringUtils.isEmpty( nodeName ) )
126             {
127                 return false;
128             }
129 
130             NodeNameTest nodeNameTest = (NodeNameTest) test;
131             String namespaceURI = nodeNameTest.getNamespaceURI();
132             boolean wildcard = nodeNameTest.isWildcard();
133             String testName = nodeNameTest.getNodeName().getName();
134             String testPrefix = nodeNameTest.getNodeName().getPrefix();
135             if ( wildcard && testPrefix == null )
136             {
137                 return true;
138             }
139             if ( wildcard || testName.equals( nodeName ) )
140             {
141                 return StringUtils.isEmpty( namespaceURI ) || StringUtils.isEmpty( testPrefix );
142             }
143             return false;
144         }
145         if ( test instanceof NodeTypeTest )
146         {
147             switch ( ( (NodeTypeTest) test ).getNodeType() )
148             {
149                 case Compiler.NODE_TYPE_NODE:
150                     return true;
151                 case Compiler.NODE_TYPE_TEXT:
152                     return node.getValue() != null;
153                 default:
154                     return false;
155             }
156         }
157         return false;
158     }
159 
160 }