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.apache.maven.api.xml.XmlNode;
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 XmlNode node;
38  
39      public Xpp3DomNodePointer(XmlNode node) {
40          super(null);
41          this.node = node;
42      }
43  
44      public Xpp3DomNodePointer(NodePointer parent, XmlNode node) {
45          super(parent);
46          this.node = node;
47      }
48  
49      @Override
50      public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2) {
51          XmlNode node1 = (XmlNode) pointer1.getBaseValue();
52          XmlNode node2 = (XmlNode) pointer2.getBaseValue();
53          if (node1 == node2) {
54              return 0;
55          }
56          for (XmlNode child : node.getChildren()) {
57              if (child == node1) {
58                  return -1;
59              }
60              if (child == node2) {
61                  return 1;
62              }
63          }
64          return 0;
65      }
66  
67      @Override
68      public Object getValue() {
69          return getValue(node);
70      }
71  
72      private static Object getValue(XmlNode node) {
73          if (node.getValue() != null) {
74              return node.getValue();
75          } else {
76              List<Object> children = new ArrayList<>();
77              for (XmlNode child : node.getChildren()) {
78                  children.add(getValue(child));
79              }
80              return children;
81          }
82      }
83  
84      @Override
85      public Object getBaseValue() {
86          return node;
87      }
88  
89      @Override
90      public Object getImmediateNode() {
91          return node;
92      }
93  
94      @Override
95      public int getLength() {
96          return 1;
97      }
98  
99      @Override
100     public QName getName() {
101         return new QName(null, node.getName());
102     }
103 
104     @Override
105     public boolean isCollection() {
106         return false;
107     }
108 
109     @Override
110     public boolean isLeaf() {
111         return node.getChildren().isEmpty();
112     }
113 
114     @Override
115     public void setValue(Object value) {
116         throw new UnsupportedOperationException();
117     }
118 
119     @Override
120     public NodeIterator childIterator(NodeTest test, boolean reverse, NodePointer startWith) {
121         return new Xpp3DomNodeIterator(this, test, reverse, startWith);
122     }
123 
124     @Override
125     public NodeIterator attributeIterator(QName qname) {
126         return new Xpp3DomAttributeIterator(this, qname);
127     }
128 }