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