View Javadoc

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