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.QName;
26  import org.apache.commons.jxpath.ri.compiler.NodeTest;
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   * A node pointer for JXPath to support <code>Xpp3Dom</code>.
33   * 
34   * @author Benjamin Bentmann
35   * @version $Id: Xpp3DomNodePointer.java 1035671 2010-11-16 16:04:51Z bentmann $
36   */
37  class Xpp3DomNodePointer
38      extends NodePointer
39  {
40  
41      private Xpp3Dom node;
42  
43      public Xpp3DomNodePointer( Xpp3Dom node )
44      {
45          super( null );
46          this.node = node;
47      }
48  
49      public Xpp3DomNodePointer( NodePointer parent, Xpp3Dom node )
50      {
51          super( parent );
52          this.node = node;
53      }
54  
55      @Override
56      public int compareChildNodePointers( NodePointer pointer1, NodePointer pointer2 )
57      {
58          Xpp3Dom node1 = (Xpp3Dom) pointer1.getBaseValue();
59          Xpp3Dom node2 = (Xpp3Dom) pointer2.getBaseValue();
60          if ( node1 == node2 )
61          {
62              return 0;
63          }
64          for ( int i = 0; i < node.getChildCount(); i++ )
65          {
66              Xpp3Dom child = node.getChild( i );
67              if ( child == node1 )
68              {
69                  return -1;
70              }
71              if ( child == node2 )
72              {
73                  return 1;
74              }
75          }
76          return 0;
77      }
78  
79      @Override
80      public Object getValue()
81      {
82          return getValue(node);
83      }
84  
85      private static Object getValue( Xpp3Dom node )
86      {
87          if ( node.getValue() != null )
88          {
89              return node.getValue().trim();
90          }
91          else
92          {
93              List<Object> children = new ArrayList<Object>();
94              for ( int i = 0; i < node.getChildCount(); i++ )
95              {
96                  children.add( getValue( node.getChild( i ) ) );
97              }
98              return children;
99          }
100     }
101 
102     @Override
103     public Object getBaseValue()
104     {
105         return node;
106     }
107 
108     @Override
109     public Object getImmediateNode()
110     {
111         return node;
112     }
113 
114     @Override
115     public int getLength()
116     {
117         return 1;
118     }
119 
120     @Override
121     public QName getName()
122     {
123         return new QName( null, node.getName() );
124     }
125 
126     @Override
127     public boolean isCollection()
128     {
129         return false;
130     }
131 
132     @Override
133     public boolean isLeaf()
134     {
135         return node.getChildCount() <= 0;
136     }
137 
138     @Override
139     public void setValue( Object value )
140     {
141         throw new UnsupportedOperationException();
142     }
143 
144     @Override
145     public NodeIterator childIterator( NodeTest test, boolean reverse, NodePointer startWith )
146     {
147         return new Xpp3DomNodeIterator( this, test, reverse, startWith );
148     }
149 
150     @Override
151     public NodeIterator attributeIterator( QName qname )
152     {
153         return new Xpp3DomAttributeIterator( this, qname );
154     }
155 
156 }