001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.jxpath.ri.model.dom;
018
019 import org.apache.commons.jxpath.ri.Compiler;
020 import org.apache.commons.jxpath.ri.QName;
021 import org.apache.commons.jxpath.ri.compiler.NodeTest;
022 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
023 import org.apache.commons.jxpath.ri.model.NodePointer;
024
025 /**
026 * Represents a namespace node.
027 *
028 * @author Dmitri Plotnikov
029 * @version $Revision: 652884 $ $Date: 2008-05-02 15:02:00 -0500 (Fri, 02 May 2008) $
030 */
031 public class NamespacePointer extends NodePointer {
032 private String prefix;
033 private String namespaceURI;
034
035 private static final long serialVersionUID = -7622456151550131709L;
036
037 /**
038 * Create a new NamespacePointer.
039 * @param parent parent pointer
040 * @param prefix associated ns prefix.
041 */
042 public NamespacePointer(NodePointer parent, String prefix) {
043 super(parent);
044 this.prefix = prefix;
045 }
046
047 /**
048 * Create a new NamespacePointer.
049 * @param parent parent pointer
050 * @param prefix associated ns prefix.
051 * @param namespaceURI associated ns URI.
052 */
053 public NamespacePointer(
054 NodePointer parent,
055 String prefix,
056 String namespaceURI) {
057 super(parent);
058 this.prefix = prefix;
059 this.namespaceURI = namespaceURI;
060 }
061
062 public QName getName() {
063 return new QName(prefix);
064 }
065
066 public Object getBaseValue() {
067 return null;
068 }
069
070 public boolean isCollection() {
071 return false;
072 }
073
074 public int getLength() {
075 return 1;
076 }
077
078 public Object getImmediateNode() {
079 return getNamespaceURI();
080 }
081
082 public String getNamespaceURI() {
083 if (namespaceURI == null) {
084 namespaceURI = parent.getNamespaceURI(prefix);
085 }
086 return namespaceURI;
087 }
088
089 public boolean isLeaf() {
090 return true;
091 }
092
093 /**
094 * Throws UnsupportedOperationException.
095 * @param value Object
096 */
097 public void setValue(Object value) {
098 throw new UnsupportedOperationException("Cannot modify DOM trees");
099 }
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 }