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;
022import org.apache.commons.jxpath.util.TypeUtils;
023import org.jdom.Attribute;
024
025/**
026 * A Pointer that points to a DOM node.
027 */
028public class JDOMAttributePointer extends NodePointer {
029
030    private static final long serialVersionUID = 8896050354479644028L;
031
032    /** JDOM Attribute. */
033    private final Attribute attribute;
034
035    /**
036     * Create a JDOMAttributePointer.
037     *
038     * @param parent NodePointer parent.
039     * @param attribute   JDOM Attribute.
040     */
041    public JDOMAttributePointer(final NodePointer parent, final Attribute attribute) {
042        super(parent);
043        this.attribute = attribute;
044    }
045
046    @Override
047    public String asPath() {
048        final StringBuilder buffer = new StringBuilder();
049        if (parent != null) {
050            buffer.append(parent.asPath());
051            if (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) != '/') {
052                buffer.append('/');
053            }
054        }
055        buffer.append('@');
056        buffer.append(getName());
057        return buffer.toString();
058    }
059
060    @Override
061    public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
062        // Won't happen - attributes don't have children
063        return 0;
064    }
065
066    @Override
067    public boolean equals(final Object object) {
068        return object == this || object instanceof JDOMAttributePointer && ((JDOMAttributePointer) object).attribute == attribute;
069    }
070
071    @Override
072    public Object getBaseValue() {
073        return attribute;
074    }
075
076    @Override
077    public Object getImmediateNode() {
078        return attribute;
079    }
080
081    @Override
082    public int getLength() {
083        return 1;
084    }
085
086    @Override
087    public QName getName() {
088        return new QName(JDOMNodePointer.getPrefix(attribute), JDOMNodePointer.getLocalName(attribute));
089    }
090
091    @Override
092    public String getNamespaceURI() {
093        String uri = attribute.getNamespaceURI();
094        if (uri != null && uri.isEmpty()) {
095            uri = null;
096        }
097        return uri;
098    }
099
100    @Override
101    public Object getValue() {
102        return attribute.getValue();
103    }
104
105    @Override
106    public int hashCode() {
107        return System.identityHashCode(attribute);
108    }
109
110    @Override
111    public boolean isActual() {
112        return true;
113    }
114
115    @Override
116    public boolean isCollection() {
117        return false;
118    }
119
120    @Override
121    public boolean isLeaf() {
122        return true;
123    }
124
125    @Override
126    public void remove() {
127        attribute.getParent().removeAttribute(attribute);
128    }
129
130    @Override
131    public void setValue(final Object value) {
132        attribute.setValue((String) TypeUtils.convert(value, String.class));
133    }
134}