1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.model.dom;
18
19 import org.apache.commons.jxpath.ri.Compiler;
20 import org.apache.commons.jxpath.ri.QName;
21 import org.apache.commons.jxpath.ri.compiler.NodeTest;
22 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
23 import org.apache.commons.jxpath.ri.model.NodePointer;
24 import org.apache.commons.jxpath.util.TypeUtils;
25 import org.w3c.dom.Attr;
26
27
28
29
30
31
32
33
34 public class DOMAttributePointer extends NodePointer {
35 private static final long serialVersionUID = 1115085175427555951L;
36
37 private Attr attr;
38
39
40
41
42
43
44 public DOMAttributePointer(NodePointer parent, Attr attr) {
45 super(parent);
46 this.attr = attr;
47 }
48
49 public QName getName() {
50 return new QName(
51 DOMNodePointer.getPrefix(attr),
52 DOMNodePointer.getLocalName(attr));
53 }
54
55 public String getNamespaceURI() {
56 String prefix = DOMNodePointer.getPrefix(attr);
57 return prefix == null ? null : parent.getNamespaceURI(prefix);
58 }
59
60 public Object getValue() {
61 String value = attr.getValue();
62 if (value == null || (value.equals("") && !attr.getSpecified())) {
63 return null;
64 }
65 return value;
66 }
67
68 public Object getBaseValue() {
69 return attr;
70 }
71
72 public boolean isCollection() {
73 return false;
74 }
75
76 public int getLength() {
77 return 1;
78 }
79
80 public Object getImmediateNode() {
81 return attr;
82 }
83
84 public boolean isActual() {
85 return true;
86 }
87
88 public boolean isLeaf() {
89 return true;
90 }
91
92 public boolean testNode(NodeTest nodeTest) {
93 return nodeTest == null
94 || ((nodeTest instanceof NodeTypeTest)
95 && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE);
96 }
97
98
99
100
101
102 public void setValue(Object value) {
103 attr.setValue((String) TypeUtils.convert(value, String.class));
104 }
105
106 public void remove() {
107 attr.getOwnerElement().removeAttributeNode(attr);
108 }
109
110 public String asPath() {
111 StringBuffer buffer = new StringBuffer();
112 if (parent != null) {
113 buffer.append(parent.asPath());
114 if (buffer.length() == 0
115 || buffer.charAt(buffer.length() - 1) != '/') {
116 buffer.append('/');
117 }
118 }
119 buffer.append('@');
120 buffer.append(getName());
121 return buffer.toString();
122 }
123
124 public int hashCode() {
125 return System.identityHashCode(attr);
126 }
127
128 public boolean equals(Object object) {
129 return object == this || object instanceof DOMAttributePointer
130 && attr == ((DOMAttributePointer) object).attr;
131 }
132
133 public int compareChildNodePointers(NodePointer pointer1,
134 NodePointer pointer2) {
135
136 return 0;
137 }
138 }