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