View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model.dom;
19  
20  import org.apache.commons.jxpath.ri.Compiler;
21  import org.apache.commons.jxpath.ri.QName;
22  import org.apache.commons.jxpath.ri.compiler.NodeTest;
23  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
24  import org.apache.commons.jxpath.ri.model.NodePointer;
25  
26  /**
27   * Represents a namespace node.
28   */
29  public class NamespacePointer extends NodePointer {
30  
31      private static final long serialVersionUID = -7622456151550131709L;
32  
33      /**
34       * Namespace prefix.
35       */
36      private final String prefix;
37  
38      /**
39       * Namespace URI String.
40       */
41      private String namespaceURI;
42  
43      /**
44       * Constructs a new NamespacePointer.
45       *
46       * @param parent parent pointer
47       * @param prefix namespace prefix.
48       */
49      public NamespacePointer(final NodePointer parent, final String prefix) {
50          super(parent);
51          this.prefix = prefix;
52      }
53  
54      /**
55       * Constructs a new NamespacePointer.
56       *
57       * @param parent       parent pointer
58       * @param prefix       namespace prefix.
59       * @param namespaceURI namespace URI.
60       */
61      public NamespacePointer(final NodePointer parent, final String prefix, final String namespaceURI) {
62          super(parent);
63          this.prefix = prefix;
64          this.namespaceURI = namespaceURI;
65      }
66  
67      @Override
68      public String asPath() {
69          final StringBuilder buffer = new StringBuilder();
70          if (parent != null) {
71              buffer.append(parent.asPath());
72              if (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) != '/') {
73                  buffer.append('/');
74              }
75          }
76          buffer.append("namespace::");
77          buffer.append(prefix);
78          return buffer.toString();
79      }
80  
81      @Override
82      public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
83          // Won't happen - namespaces don't have children
84          return 0;
85      }
86  
87      @Override
88      public boolean equals(final Object object) {
89          if (object == this) {
90              return true;
91          }
92          if (!(object instanceof NamespacePointer)) {
93              return false;
94          }
95          final NamespacePointer other = (NamespacePointer) object;
96          return prefix.equals(other.prefix);
97      }
98  
99      @Override
100     public Object getBaseValue() {
101         return null;
102     }
103 
104     @Override
105     public Object getImmediateNode() {
106         return getNamespaceURI();
107     }
108 
109     @Override
110     public int getLength() {
111         return 1;
112     }
113 
114     @Override
115     public QName getName() {
116         return new QName(prefix);
117     }
118 
119     @Override
120     public String getNamespaceURI() {
121         if (namespaceURI == null) {
122             namespaceURI = parent.getNamespaceURI(prefix);
123         }
124         return namespaceURI;
125     }
126 
127     @Override
128     public int hashCode() {
129         return prefix.hashCode();
130     }
131 
132     @Override
133     public boolean isCollection() {
134         return false;
135     }
136 
137     @Override
138     public boolean isLeaf() {
139         return true;
140     }
141 
142     /**
143      * Throws UnsupportedOperationException.
144      *
145      * @param value Object
146      */
147     @Override
148     public void setValue(final Object value) {
149         throw new UnsupportedOperationException("Cannot modify DOM trees");
150     }
151 
152     @Override
153     public boolean testNode(final NodeTest nodeTest) {
154         return nodeTest == null || nodeTest instanceof NodeTypeTest && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE;
155     }
156 }