1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.model.dynamic;
18
19 import java.util.Arrays;
20 import java.util.Map;
21
22 import org.apache.commons.jxpath.AbstractFactory;
23 import org.apache.commons.jxpath.DynamicPropertyHandler;
24 import org.apache.commons.jxpath.JXPathAbstractFactoryException;
25 import org.apache.commons.jxpath.JXPathContext;
26 import org.apache.commons.jxpath.JXPathInvalidAccessException;
27 import org.apache.commons.jxpath.ri.model.NodePointer;
28 import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
29 import org.apache.commons.jxpath.util.ValueUtils;
30
31
32
33
34
35
36
37 public class DynamicPropertyPointer extends PropertyPointer {
38
39 private static final long serialVersionUID = -5720585681149150822L;
40
41 private DynamicPropertyHandler handler;
42 private String name;
43 private String[] names;
44 private String requiredPropertyName;
45
46
47
48
49
50
51 public DynamicPropertyPointer(NodePointer parent,
52 DynamicPropertyHandler handler) {
53 super(parent);
54 this.handler = handler;
55 }
56
57
58
59
60
61 public boolean isContainer() {
62 return true;
63 }
64
65
66
67
68
69 public int getPropertyCount() {
70 return getPropertyNames().length;
71 }
72
73
74
75
76
77 public String[] getPropertyNames() {
78 if (names == null) {
79 String[] allNames = handler.getPropertyNames(getBean());
80 names = new String[allNames.length];
81 for (int i = 0; i < names.length; i++) {
82 names[i] = allNames[i];
83 }
84 Arrays.sort(names);
85 if (requiredPropertyName != null) {
86 int inx = Arrays.binarySearch(names, requiredPropertyName);
87 if (inx < 0) {
88 allNames = names;
89 names = new String[allNames.length + 1];
90 names[0] = requiredPropertyName;
91 System.arraycopy(allNames, 0, names, 1, allNames.length);
92 Arrays.sort(names);
93 }
94 }
95 }
96 return names;
97 }
98
99
100
101
102
103
104 public String getPropertyName() {
105 if (name == null) {
106 String[] names = getPropertyNames();
107 name = propertyIndex >= 0 && propertyIndex < names.length ? names[propertyIndex] : "*";
108 }
109 return name;
110 }
111
112
113
114
115
116
117
118
119
120 public void setPropertyName(String propertyName) {
121 setPropertyIndex(UNSPECIFIED_PROPERTY);
122 this.name = propertyName;
123 requiredPropertyName = propertyName;
124 if (names != null && Arrays.binarySearch(names, propertyName) < 0) {
125 names = null;
126 }
127 }
128
129
130
131
132
133
134 public int getPropertyIndex() {
135 if (propertyIndex == UNSPECIFIED_PROPERTY) {
136 String[] names = getPropertyNames();
137 for (int i = 0; i < names.length; i++) {
138 if (names[i].equals(name)) {
139 setPropertyIndex(i);
140 break;
141 }
142 }
143 }
144 return super.getPropertyIndex();
145 }
146
147
148
149
150
151
152 public void setPropertyIndex(int index) {
153 if (propertyIndex != index) {
154 super.setPropertyIndex(index);
155 name = null;
156 }
157 }
158
159
160
161
162
163
164 public Object getBaseValue() {
165 return handler.getProperty(getBean(), getPropertyName());
166 }
167
168
169
170
171
172
173
174
175 public Object getImmediateNode() {
176 Object value;
177 if (index == WHOLE_COLLECTION) {
178 value = ValueUtils.getValue(handler.getProperty(
179 getBean(),
180 getPropertyName()));
181 }
182 else {
183 value = ValueUtils.getValue(handler.getProperty(
184 getBean(),
185 getPropertyName()), index);
186 }
187 return value;
188 }
189
190
191
192
193
194
195 protected boolean isActualProperty() {
196 return true;
197 }
198
199
200
201
202
203
204
205 public void setValue(Object value) {
206 if (index == WHOLE_COLLECTION) {
207 handler.setProperty(getBean(), getPropertyName(), value);
208 }
209 else {
210 ValueUtils.setValue(
211 handler.getProperty(getBean(), getPropertyName()),
212 index,
213 value);
214 }
215 }
216
217 public NodePointer createPath(JXPathContext context) {
218
219 Object collection = getBaseValue();
220 if (collection == null) {
221 AbstractFactory factory = getAbstractFactory(context);
222 boolean success =
223 factory.createObject(
224 context,
225 this,
226 getBean(),
227 getPropertyName(),
228 0);
229 if (!success) {
230 throw new JXPathAbstractFactoryException(
231 "Factory could not create an object for path: " + asPath());
232 }
233 collection = getBaseValue();
234 }
235
236 if (index != WHOLE_COLLECTION) {
237 if (index < 0) {
238 throw new JXPathInvalidAccessException("Index is less than 1: "
239 + asPath());
240 }
241
242 if (index >= getLength()) {
243 collection = ValueUtils.expandCollection(collection, index + 1);
244 handler.setProperty(getBean(), getPropertyName(), collection);
245 }
246 }
247
248 return this;
249 }
250
251 public NodePointer createPath(JXPathContext context, Object value) {
252 if (index == WHOLE_COLLECTION) {
253 handler.setProperty(getBean(), getPropertyName(), value);
254 }
255 else {
256 createPath(context);
257 ValueUtils.setValue(getBaseValue(), index, value);
258 }
259 return this;
260 }
261
262 public void remove() {
263 if (index == WHOLE_COLLECTION) {
264 removeKey();
265 }
266 else if (isCollection()) {
267 Object collection = ValueUtils.remove(getBaseValue(), index);
268 handler.setProperty(getBean(), getPropertyName(), collection);
269 }
270 else if (index == 0) {
271 removeKey();
272 }
273 }
274
275
276
277
278 private void removeKey() {
279 Object bean = getBean();
280 if (bean instanceof Map) {
281 ((Map) bean).remove(getPropertyName());
282 }
283 else {
284 handler.setProperty(bean, getPropertyName(), null);
285 }
286 }
287
288 public String asPath() {
289 StringBuffer buffer = new StringBuffer();
290 buffer.append(getImmediateParentPointer().asPath());
291 if (buffer.length() == 0) {
292 buffer.append("/.");
293 }
294 else if (buffer.charAt(buffer.length() - 1) == '/') {
295 buffer.append('.');
296 }
297 buffer.append("[@name='");
298 buffer.append(escape(getPropertyName()));
299 buffer.append("']");
300 if (index != WHOLE_COLLECTION && isCollection()) {
301 buffer.append('[').append(index + 1).append(']');
302 }
303 return buffer.toString();
304 }
305
306 }