    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.jdom;
    018
    019    import org.apache.commons.jxpath.ri.QName;
    020    import org.apache.commons.jxpath.ri.model.NodePointer;
    021    import org.apache.commons.jxpath.util.TypeUtils;
    022    import org.jdom.Attribute;
    023
    024    /**
    025     * A Pointer that points to a DOM node.
    026     *
    027     * @author Dmitri Plotnikov
    028     * @version $Revision: 652884 $ $Date: 2008-05-02 15:02:00 -0500 (Fri, 02 May 2008) $
    029     */
    030    public class JDOMAttributePointer extends NodePointer {
    031        private Attribute attr;
    032
    033        private static final long serialVersionUID = 8896050354479644028L;
    034
    035        /**
    036         * Create a JDOMAttributePointer.
    037         * @param parent NodePointer parent
    038         * @param attr JDOM Attribute
    039         */
    040        public JDOMAttributePointer(NodePointer parent, Attribute attr) {
    041            super(parent);
    042            this.attr = attr;
    043        }
    044
    045        public QName getName() {
    046            return new QName(
    047                JDOMNodePointer.getPrefix(attr),
    048                JDOMNodePointer.getLocalName(attr));
    049        }
    050
    051        public String getNamespaceURI() {
    052            String uri = attr.getNamespaceURI();
    053            if (uri != null && uri.equals("")) {
    054                uri = null;
    055            }
    056            return uri;
    057        }
    058
    059        public Object getValue() {
    060            return attr.getValue();
    061        }
    062
    063        public Object getBaseValue() {
    064            return attr;
    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 attr;
    077        }
    078
    079        public boolean isActual() {
    080            return true;
    081        }
    082
    083        public boolean isLeaf() {
    084            return true;
    085        }
    086
    087        public void setValue(Object value) {
    088            attr.setValue((String) TypeUtils.convert(value, String.class));
    089        }
    090
    091        public void remove() {
    092            attr.getParent().removeAttribute(attr);
    093        }
    094
    095        public String asPath() {
    096            StringBuffer buffer = new StringBuffer();
    097            if (parent != null) {
    098                buffer.append(parent.asPath());
    099                if (buffer.length() == 0
    100                    || buffer.charAt(buffer.length() - 1) != '/') {
    101                    buffer.append('/');
    102                }
    103            }
    104            buffer.append('@');
    105            buffer.append(getName());
    106            return buffer.toString();
    107        }
    108
    109        public int hashCode() {
    110            return System.identityHashCode(attr);
    111        }
    112
    113        public boolean equals(Object object) {
    114            return object == this || object instanceof JDOMAttributePointer
    115                    && ((JDOMAttributePointer) object).attr == attr;
    116        }
    117
    118        public int compareChildNodePointers(
    119                NodePointer pointer1,
    120                NodePointer pointer2) {
    121            // Won't happen - attributes don't have children
    122            return 0;
    123        }
    124    }