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