1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri.model.beans;
19
20 import java.util.Locale;
21
22 import org.apache.commons.jxpath.JXPathInvalidAccessException;
23 import org.apache.commons.jxpath.ri.Compiler;
24 import org.apache.commons.jxpath.ri.QName;
25 import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
26 import org.apache.commons.jxpath.ri.compiler.NodeTest;
27 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
28 import org.apache.commons.jxpath.ri.model.NodeIterator;
29 import org.apache.commons.jxpath.ri.model.NodePointer;
30 import org.apache.commons.jxpath.util.ValueUtils;
31
32
33
34
35 public abstract class PropertyOwnerPointer extends NodePointer {
36
37 private static final long serialVersionUID = 1L;
38 private static final Object UNINITIALIZED = new Object();
39
40
41
42
43 private Object value = UNINITIALIZED;
44
45
46
47
48
49
50 protected PropertyOwnerPointer(final NodePointer parent) {
51 super(parent);
52 }
53
54
55
56
57
58
59
60 protected PropertyOwnerPointer(final NodePointer parent, final Locale locale) {
61 super(parent, locale);
62 }
63
64 @Override
65 public NodeIterator attributeIterator(final QName qName) {
66 return new BeanAttributeIterator(this, qName);
67 }
68
69 @Override
70 public NodeIterator childIterator(final NodeTest test, final boolean reverse, final NodePointer startWith) {
71 if (test == null) {
72 return createNodeIterator(null, reverse, startWith);
73 }
74 if (test instanceof NodeNameTest) {
75 final NodeNameTest nodeNameTest = (NodeNameTest) test;
76 final QName testName = nodeNameTest.getNodeName();
77 if (isValidProperty(testName)) {
78 return createNodeIterator(nodeNameTest.isWildcard() ? null : testName.toString(), reverse, startWith);
79 }
80 return null;
81 }
82 return test instanceof NodeTypeTest && ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_NODE ? createNodeIterator(null, reverse, startWith)
83 : null;
84 }
85
86 @Override
87 public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
88 final int r = pointer1.getName().toString().compareTo(pointer2.getName().toString());
89 return r == 0 ? pointer1.getIndex() - pointer2.getIndex() : r;
90 }
91
92
93
94
95
96
97
98
99
100 public NodeIterator createNodeIterator(final String property, final boolean reverse, final NodePointer startWith) {
101 return new PropertyIterator(this, property, reverse, startWith);
102 }
103
104 @Override
105 public Object getImmediateNode() {
106 if (value == UNINITIALIZED) {
107 value = index == WHOLE_COLLECTION ? ValueUtils.getValue(getBaseValue()) : ValueUtils.getValue(getBaseValue(), index);
108 }
109 return value;
110 }
111
112 @Override
113 public abstract QName getName();
114
115
116
117
118
119
120 public abstract PropertyPointer getPropertyPointer();
121
122
123
124
125
126
127
128 public boolean isDynamicPropertyDeclarationSupported() {
129 return false;
130 }
131
132
133
134
135
136
137
138
139 public boolean isValidProperty(final QName qName) {
140 return isDefaultNamespace(qName.getPrefix());
141 }
142
143
144
145
146 @Override
147 public void remove() {
148 this.value = null;
149 if (parent == null) {
150 throw new UnsupportedOperationException("Cannot remove an object that is not " + "some other object's property or a collection element");
151 }
152 parent.remove();
153 }
154
155 @Override
156 public void setIndex(final int index) {
157 if (this.index != index) {
158 super.setIndex(index);
159 value = UNINITIALIZED;
160 }
161 }
162
163
164
165
166
167
168 @Override
169 public void setValue(final Object value) {
170 this.value = value;
171 if (parent == null) {
172 throw new UnsupportedOperationException("Cannot replace the root object");
173 }
174 if (!parent.isContainer()) {
175 if (index == WHOLE_COLLECTION) {
176 throw new UnsupportedOperationException("Cannot setValue of an object that is not " + "some other object's property");
177 }
178 throw new JXPathInvalidAccessException("The specified collection element does not exist: " + this);
179 }
180 parent.setValue(value);
181 }
182 }