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.jdom;
017:
018: import org.apache.commons.jxpath.ri.QName;
019: import org.apache.commons.jxpath.ri.model.NodePointer;
020:
021: /**
022: * Represents a namespace node.
023: *
024: * @author Dmitri Plotnikov
025: * @version $Revision: 1.9 $ $Date: 2004/04/01 02:55:31 $
026: */
027: public class JDOMNamespacePointer extends NodePointer {
028: private String prefix;
029: private String namespaceURI;
030:
031: public JDOMNamespacePointer(NodePointer parent, String prefix) {
032: super (parent);
033: this .prefix = prefix;
034: }
035:
036: public JDOMNamespacePointer(NodePointer parent, String prefix,
037: String namespaceURI) {
038: super (parent);
039: this .prefix = prefix;
040: this .namespaceURI = namespaceURI;
041: }
042:
043: public QName getName() {
044: return new QName(prefix);
045: }
046:
047: public Object getBaseValue() {
048: return null;
049: }
050:
051: public boolean isCollection() {
052: return false;
053: }
054:
055: public int getLength() {
056: return 1;
057: }
058:
059: public Object getImmediateNode() {
060: return getNamespaceURI();
061: }
062:
063: public String getNamespaceURI() {
064: if (namespaceURI == null) {
065: namespaceURI = parent.getNamespaceURI(prefix);
066: }
067: return namespaceURI;
068: }
069:
070: public boolean isLeaf() {
071: return true;
072: }
073:
074: /**
075: * Throws UnsupportedOperationException.
076: */
077: public void setValue(Object value) {
078: throw new UnsupportedOperationException(
079: "Cannot modify a namespace");
080: }
081:
082: public String asPath() {
083: StringBuffer buffer = new StringBuffer();
084: if (parent != null) {
085: buffer.append(parent.asPath());
086: if (buffer.length() == 0
087: || buffer.charAt(buffer.length() - 1) != '/') {
088: buffer.append('/');
089: }
090: }
091: buffer.append("namespace::");
092: buffer.append(prefix);
093: return buffer.toString();
094: }
095:
096: public int hashCode() {
097: return prefix.hashCode();
098: }
099:
100: public boolean equals(Object object) {
101: if (object == this ) {
102: return true;
103: }
104:
105: if (!(object instanceof JDOMNamespacePointer)) {
106: return false;
107: }
108:
109: JDOMNamespacePointer other = (JDOMNamespacePointer) object;
110: return prefix.equals(other.prefix);
111: }
112:
113: public int compareChildNodePointers(NodePointer pointer1,
114: NodePointer pointer2) {
115: // Won't happen - namespaces don't have children
116: return 0;
117: }
118: }
|