001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.model.dom;
017:
018: import org.apache.commons.jxpath.ri.Compiler;
019: import org.apache.commons.jxpath.ri.QName;
020: import org.apache.commons.jxpath.ri.compiler.NodeTest;
021: import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
022: import org.apache.commons.jxpath.ri.model.NodePointer;
023:
024: /**
025: * Represents a namespace node.
026: *
027: * @author Dmitri Plotnikov
028: * @version $Revision: 1.13 $ $Date: 2004/04/01 02:55:32 $
029: */
030: public class NamespacePointer extends NodePointer {
031: private String prefix;
032: private String namespaceURI;
033:
034: public NamespacePointer(NodePointer parent, String prefix) {
035: super (parent);
036: this .prefix = prefix;
037: }
038:
039: public NamespacePointer(NodePointer parent, String prefix,
040: String namespaceURI) {
041: super (parent);
042: this .prefix = prefix;
043: this .namespaceURI = namespaceURI;
044: }
045:
046: public QName getName() {
047: return new QName(prefix);
048: }
049:
050: public Object getBaseValue() {
051: return null;
052: }
053:
054: public boolean isCollection() {
055: return false;
056: }
057:
058: public int getLength() {
059: return 1;
060: }
061:
062: public Object getImmediateNode() {
063: return getNamespaceURI();
064: }
065:
066: public String getNamespaceURI() {
067: if (namespaceURI == null) {
068: namespaceURI = parent.getNamespaceURI(prefix);
069: }
070: return namespaceURI;
071: }
072:
073: public boolean isLeaf() {
074: return true;
075: }
076:
077: /**
078: * Throws UnsupportedOperationException.
079: */
080: public void setValue(Object value) {
081: throw new UnsupportedOperationException(
082: "Cannot modify DOM trees");
083: }
084:
085: public boolean testNode(NodeTest nodeTest) {
086: return nodeTest == null
087: || ((nodeTest instanceof NodeTypeTest) && ((NodeTypeTest) nodeTest)
088: .getNodeType() == Compiler.NODE_TYPE_NODE);
089: }
090:
091: public String asPath() {
092: StringBuffer buffer = new StringBuffer();
093: if (parent != null) {
094: buffer.append(parent.asPath());
095: if (buffer.length() == 0
096: || buffer.charAt(buffer.length() - 1) != '/') {
097: buffer.append('/');
098: }
099: }
100: buffer.append("namespace::");
101: buffer.append(prefix);
102: return buffer.toString();
103: }
104:
105: public int hashCode() {
106: return prefix.hashCode();
107: }
108:
109: public boolean equals(Object object) {
110: if (object == this ) {
111: return true;
112: }
113:
114: if (!(object instanceof NamespacePointer)) {
115: return false;
116: }
117:
118: NamespacePointer other = (NamespacePointer) object;
119: return prefix.equals(other.prefix);
120: }
121:
122: public int compareChildNodePointers(NodePointer pointer1,
123: NodePointer pointer2) {
124: // Won't happen - namespaces don't have children
125: return 0;
126: }
127: }
|