View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project.harness;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.jxpath.ri.QName;
25  import org.apache.commons.jxpath.ri.compiler.NodeTest;
26  import org.apache.commons.jxpath.ri.model.NodeIterator;
27  import org.apache.commons.jxpath.ri.model.NodePointer;
28  import org.codehaus.plexus.util.xml.Xpp3Dom;
29  
30  /**
31   * A node pointer for JXPath to support <code>Xpp3Dom</code>.
32   *
33   * @author Benjamin Bentmann
34   */
35  class Xpp3DomNodePointer extends NodePointer {
36  
37      private Xpp3Dom node;
38  
39      public Xpp3DomNodePointer(Xpp3Dom node) {
40          super(null);
41          this.node = node;
42      }
43  
44      public Xpp3DomNodePointer(NodePointer parent, Xpp3Dom node) {
45          super(parent);
46          this.node = node;
47      }
48  
49      @Override
50      public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2) {
51          Xpp3Dom node1 = (Xpp3Dom) pointer1.getBaseValue();
52          Xpp3Dom node2 = (Xpp3Dom) pointer2.getBaseValue();
53          if (node1 == node2) {
54              return 0;
55          }
56          for (int i = 0; i < node.getChildCount(); i++) {
57              Xpp3Dom child = node.getChild(i);
58              if (child == node1) {
59                  return -1;
60              }
61              if (child == node2) {
62                  return 1;
63              }
64          }
65          return 0;
66      }
67  
68      @Override
69      public Object getValue() {
70          return getValue(node);
71      }
72  
73      private static Object getValue(Xpp3Dom node) {
74          if (node.getValue() != null) {
75              return node.getValue();
76          } else {
77              List<Object> children = new ArrayList<>();
78              for (int i = 0; i < node.getChildCount(); i++) {
79                  children.add(getValue(node.getChild(i)));
80              }
81              return children;
82          }
83      }
84  
85      @Override
86      public Object getBaseValue() {
87          return node;
88      }
89  
90      @Override
91      public Object getImmediateNode() {
92          return node;
93      }
94  
95      @Override
96      public int getLength() {
97          return 1;
98      }
99  
100     @Override
101     public QName getName() {
102         return new QName(null, node.getName());
103     }
104 
105     @Override
106     public boolean isCollection() {
107         return false;
108     }
109 
110     @Override
111     public boolean isLeaf() {
112         return node.getChildCount() <= 0;
113     }
114 
115     @Override
116     public void setValue(Object value) {
117         throw new UnsupportedOperationException();
118     }
119 
120     @Override
121     public NodeIterator childIterator(NodeTest test, boolean reverse, NodePointer startWith) {
122         return new Xpp3DomNodeIterator(this, test, reverse, startWith);
123     }
124 
125     @Override
126     public NodeIterator attributeIterator(QName qname) {
127         return new Xpp3DomAttributeIterator(this, qname);
128     }
129 }