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