1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.model.container;
18
19 import java.util.Locale;
20
21 import org.apache.commons.jxpath.Container;
22 import org.apache.commons.jxpath.ri.QName;
23 import org.apache.commons.jxpath.ri.compiler.NodeTest;
24 import org.apache.commons.jxpath.ri.model.NodeIterator;
25 import org.apache.commons.jxpath.ri.model.NodePointer;
26 import org.apache.commons.jxpath.util.ValueUtils;
27
28
29
30
31
32
33
34
35
36 public class ContainerPointer extends NodePointer {
37 private Container container;
38 private NodePointer valuePointer;
39
40 private static final long serialVersionUID = 6140752946621686118L;
41
42
43
44
45
46
47 public ContainerPointer(Container container, Locale locale) {
48 super(null, locale);
49 this.container = container;
50 }
51
52
53
54
55
56
57 public ContainerPointer(NodePointer parent, Container container) {
58 super(parent);
59 this.container = container;
60 }
61
62
63
64
65
66 public boolean isContainer() {
67 return true;
68 }
69
70 public QName getName() {
71 return null;
72 }
73
74 public Object getBaseValue() {
75 return container;
76 }
77
78 public boolean isCollection() {
79 Object value = getBaseValue();
80 return value != null && ValueUtils.isCollection(value);
81 }
82
83 public int getLength() {
84 Object value = getBaseValue();
85 return value == null ? 1 : ValueUtils.getLength(value);
86 }
87
88 public boolean isLeaf() {
89 return getValuePointer().isLeaf();
90 }
91
92 public Object getImmediateNode() {
93 Object value = getBaseValue();
94 if (index != WHOLE_COLLECTION) {
95 return index >= 0 && index < getLength() ? ValueUtils.getValue(value, index) : null;
96 }
97 return ValueUtils.getValue(value);
98 }
99
100 public void setValue(Object value) {
101
102 container.setValue(value);
103 }
104
105 public NodePointer getImmediateValuePointer() {
106 if (valuePointer == null) {
107 Object value = getImmediateNode();
108 valuePointer = NodePointer.newChildNodePointer(this, getName(), value);
109 }
110 return valuePointer;
111 }
112
113 public int hashCode() {
114 return System.identityHashCode(container) + index;
115 }
116
117 public boolean equals(Object object) {
118 if (object == this) {
119 return true;
120 }
121
122 if (!(object instanceof ContainerPointer)) {
123 return false;
124 }
125
126 ContainerPointer other = (ContainerPointer) object;
127 return container == other.container && index == other.index;
128 }
129
130 public NodeIterator childIterator(
131 NodeTest test,
132 boolean reverse,
133 NodePointer startWith) {
134 return getValuePointer().childIterator(test, reverse, startWith);
135 }
136
137 public NodeIterator attributeIterator(QName name) {
138 return getValuePointer().attributeIterator(name);
139 }
140
141 public NodeIterator namespaceIterator() {
142 return getValuePointer().namespaceIterator();
143 }
144
145 public NodePointer namespacePointer(String namespace) {
146 return getValuePointer().namespacePointer(namespace);
147 }
148
149 public boolean testNode(NodeTest nodeTest) {
150 return getValuePointer().testNode(nodeTest);
151 }
152
153 public int compareChildNodePointers(
154 NodePointer pointer1,
155 NodePointer pointer2) {
156 return pointer1.getIndex() - pointer2.getIndex();
157 }
158
159 public String getNamespaceURI(String prefix) {
160 return getValuePointer().getNamespaceURI(prefix);
161 }
162
163 public String asPath() {
164 return parent == null ? "/" : parent.asPath();
165 }
166 }