    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
    018package org.apache.commons.jxpath.ri.model.jdom;
    019
    020import org.apache.commons.jxpath.ri.QName;
    021import org.apache.commons.jxpath.ri.model.NodePointer;
    022
    023/**
    024 * Represents a namespace node.
    025 */
    026public class JDOMNamespacePointer extends NodePointer {
    027
    028    private static final long serialVersionUID = 7935311686545862379L;
    029
    030    /** Namespace prefix. */
    031    private final String prefix;
    032
    033    /** Namespace URI string. */
    034    private String namespaceURI;
    035
    036    /**
    037     * Constructs a new JDOMNamespacePointer.
    038     *
    039     * @param parent parent pointer
    040     * @param prefix ns prefix
    041     */
    042    public JDOMNamespacePointer(final NodePointer parent, final String prefix) {
    043        super(parent);
    044        this.prefix = prefix;
    045    }
    046
    047    /**
    048     * Constructs a new JDOMNamespacePointer.
    049     *
    050     * @param parent       parent pointer
    051     * @param prefix       ns prefix
    052     * @param namespaceURI ns URI
    053     */
    054    public JDOMNamespacePointer(final NodePointer parent, final String prefix, final String namespaceURI) {
    055        super(parent);
    056        this.prefix = prefix;
    057        this.namespaceURI = namespaceURI;
    058    }
    059
    060    @Override
    061    public String asPath() {
    062        final StringBuilder buffer = new StringBuilder();
    063        if (parent != null) {
    064            buffer.append(parent.asPath());
    065            if (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) != '/') {
    066                buffer.append('/');
    067            }
    068        }
    069        buffer.append("namespace::");
    070        buffer.append(prefix);
    071        return buffer.toString();
    072    }
    073
    074    @Override
    075    public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
    076        // Won't happen - namespaces don't have children
    077        return 0;
    078    }
    079
    080    @Override
    081    public boolean equals(final Object object) {
    082        return object == this || object instanceof JDOMNamespacePointer && prefix.equals(((JDOMNamespacePointer) object).prefix);
    083    }
    084
    085    @Override
    086    public Object getBaseValue() {
    087        return null;
    088    }
    089
    090    @Override
    091    public Object getImmediateNode() {
    092        return getNamespaceURI();
    093    }
    094
    095    @Override
    096    public int getLength() {
    097        return 1;
    098    }
    099
    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}