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 */
017package org.apache.commons.jxpath.ri.model.jdom;
018
019import org.apache.commons.jxpath.ri.QName;
020import org.apache.commons.jxpath.ri.model.NodePointer;
021
022/**
023 * Represents a namespace node.
024 *
025 * @author Dmitri Plotnikov
026 * @version $Revision: 652884 $ $Date: 2008-05-02 16:02:00 -0400 (Fri, 02 May 2008) $
027 */
028public class JDOMNamespacePointer extends NodePointer {
029    private String prefix;
030    private String namespaceURI;
031
032    private static final long serialVersionUID = 7935311686545862379L;
033
034    /**
035     * Create a new JDOMNamespacePointer.
036     * @param parent parent pointer
037     * @param prefix ns prefix
038     */
039    public JDOMNamespacePointer(NodePointer parent, String prefix) {
040        super(parent);
041        this.prefix = prefix;
042    }
043
044    /**
045     * Create a new JDOMNamespacePointer.
046     * @param parent parent pointer
047     * @param prefix ns prefix
048     * @param namespaceURI ns URI
049     */
050    public JDOMNamespacePointer(
051            NodePointer parent,
052            String prefix,
053            String namespaceURI) {
054        super(parent);
055        this.prefix = prefix;
056        this.namespaceURI = namespaceURI;
057    }
058
059    public QName getName() {
060        return new QName(prefix);
061    }
062
063    public Object getBaseValue() {
064        return null;
065    }
066
067    public boolean isCollection() {
068        return false;
069    }
070
071    public int getLength() {
072        return 1;
073    }
074
075    public Object getImmediateNode() {
076        return getNamespaceURI();
077    }
078
079    public String getNamespaceURI() {
080        if (namespaceURI == null) {
081            namespaceURI = parent.getNamespaceURI(prefix);
082        }
083        return namespaceURI;
084    }
085
086    public boolean isLeaf() {
087        return true;
088    }
089
090    /**
091     * Throws UnsupportedOperationException.
092     * @param value Object value to set
093     */
094    public void setValue(Object value) {
095        throw new UnsupportedOperationException("Cannot modify a namespace");
096    }
097
098    public String asPath() {
099        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}