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