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