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  package org.apache.commons.jxpath.ri.model.dom;
18  
19  import org.apache.commons.jxpath.ri.Compiler;
20  import org.apache.commons.jxpath.ri.QName;
21  import org.apache.commons.jxpath.ri.compiler.NodeTest;
22  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
23  import org.apache.commons.jxpath.ri.model.NodePointer;
24  
25  /**
26   * Represents a namespace node.
27   *
28   * @author Dmitri Plotnikov
29   * @version $Revision: 652884 $ $Date: 2008-05-02 16:02:00 -0400 (Fri, 02 May 2008) $
30   */
31  public class NamespacePointer extends NodePointer {
32      private String prefix;
33      private String namespaceURI;
34  
35      private static final long serialVersionUID = -7622456151550131709L;
36  
37      /**
38       * Create a new NamespacePointer.
39       * @param parent parent pointer
40       * @param prefix associated ns prefix.
41       */
42      public NamespacePointer(NodePointer parent, String prefix) {
43          super(parent);
44          this.prefix = prefix;
45      }
46  
47      /**
48       * Create a new NamespacePointer.
49       * @param parent parent pointer
50       * @param prefix associated ns prefix.
51       * @param namespaceURI associated ns URI.
52       */
53      public NamespacePointer(
54          NodePointer parent,
55          String prefix,
56          String namespaceURI) {
57          super(parent);
58          this.prefix = prefix;
59          this.namespaceURI = namespaceURI;
60      }
61  
62      public QName getName() {
63          return new QName(prefix);
64      }
65  
66      public Object getBaseValue() {
67          return null;
68      }
69  
70      public boolean isCollection() {
71          return false;
72      }
73  
74      public int getLength() {
75          return 1;
76      }
77  
78      public Object getImmediateNode() {
79          return getNamespaceURI();
80      }
81  
82      public String getNamespaceURI() {
83          if (namespaceURI == null) {
84              namespaceURI = parent.getNamespaceURI(prefix);
85          }
86          return namespaceURI;
87      }
88  
89      public boolean isLeaf() {
90          return true;
91      }
92  
93      /**
94       * Throws UnsupportedOperationException.
95       * @param value Object
96       */
97      public void setValue(Object value) {
98          throw new UnsupportedOperationException("Cannot modify DOM trees");
99      }
100 
101     public boolean testNode(NodeTest nodeTest) {
102         return nodeTest == null
103             || ((nodeTest instanceof NodeTypeTest)
104                 && ((NodeTypeTest) nodeTest).getNodeType()
105                     == Compiler.NODE_TYPE_NODE);
106     }
107 
108     public String asPath() {
109         StringBuffer buffer = new StringBuffer();
110         if (parent != null) {
111             buffer.append(parent.asPath());
112             if (buffer.length() == 0
113                 || buffer.charAt(buffer.length() - 1) != '/') {
114                 buffer.append('/');
115             }
116         }
117         buffer.append("namespace::");
118         buffer.append(prefix);
119         return buffer.toString();
120     }
121 
122     public int hashCode() {
123         return prefix.hashCode();
124     }
125 
126     public boolean equals(Object object) {
127         if (object == this) {
128             return true;
129         }
130 
131         if (!(object instanceof NamespacePointer)) {
132             return false;
133         }
134 
135         NamespacePointer other = (NamespacePointer) object;
136         return prefix.equals(other.prefix);
137     }
138 
139     public int compareChildNodePointers(
140         NodePointer pointer1,
141         NodePointer pointer2) {
142         // Won't happen - namespaces don't have children
143         return 0;
144     }
145 }