1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.model.beans;
18
19 import org.apache.commons.jxpath.AbstractFactory;
20 import org.apache.commons.jxpath.JXPathAbstractFactoryException;
21 import org.apache.commons.jxpath.JXPathContext;
22 import org.apache.commons.jxpath.JXPathInvalidAccessException;
23 import org.apache.commons.jxpath.ri.QName;
24 import org.apache.commons.jxpath.ri.model.NodePointer;
25
26
27
28
29
30 public class NullPropertyPointer extends PropertyPointer {
31
32 private String propertyName = "*";
33 private boolean byNameAttribute = false;
34
35 private static final long serialVersionUID = 5296593071854982754L;
36
37
38
39
40
41 public NullPropertyPointer(NodePointer parent) {
42 super(parent);
43 }
44
45 public QName getName() {
46 return new QName(propertyName);
47 }
48
49 public void setPropertyIndex(int index) {
50 }
51
52 public int getLength() {
53 return 0;
54 }
55
56 public Object getBaseValue() {
57 return null;
58 }
59
60 public Object getImmediateNode() {
61 return null;
62 }
63
64 public boolean isLeaf() {
65 return true;
66 }
67
68 public NodePointer getValuePointer() {
69 return new NullPointer(this, new QName(getPropertyName()));
70 }
71
72 protected boolean isActualProperty() {
73 return false;
74 }
75
76 public boolean isActual() {
77 return false;
78 }
79
80 public boolean isContainer() {
81 return true;
82 }
83
84 public void setValue(Object value) {
85 if (parent == null || parent.isContainer()) {
86 throw new JXPathInvalidAccessException(
87 "Cannot set property "
88 + asPath()
89 + ", the target object is null");
90 }
91 if (parent instanceof PropertyOwnerPointer
92 && ((PropertyOwnerPointer) parent)
93 .isDynamicPropertyDeclarationSupported()) {
94
95
96 PropertyPointer propertyPointer =
97 ((PropertyOwnerPointer) parent).getPropertyPointer();
98 propertyPointer.setPropertyName(propertyName);
99 propertyPointer.setValue(value);
100 }
101 else {
102 throw new JXPathInvalidAccessException(
103 "Cannot set property "
104 + asPath()
105 + ", path does not match a changeable location");
106 }
107 }
108
109 public NodePointer createPath(JXPathContext context) {
110 NodePointer newParent = parent.createPath(context);
111 if (isAttribute()) {
112 return newParent.createAttribute(context, getName());
113 }
114 if (parent instanceof NullPointer && parent.equals(newParent)) {
115 throw createBadFactoryException(context.getFactory());
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130 if (newParent instanceof PropertyOwnerPointer) {
131 PropertyOwnerPointer pop = (PropertyOwnerPointer) newParent;
132 newParent = pop.getPropertyPointer();
133 }
134 return newParent.createChild(context, getName(), getIndex());
135 }
136
137 public NodePointer createPath(JXPathContext context, Object value) {
138 NodePointer newParent = parent.createPath(context);
139 if (isAttribute()) {
140 NodePointer pointer = newParent.createAttribute(context, getName());
141 pointer.setValue(value);
142 return pointer;
143 }
144 if (parent instanceof NullPointer && parent.equals(newParent)) {
145 throw createBadFactoryException(context.getFactory());
146 }
147 if (newParent instanceof PropertyOwnerPointer) {
148 PropertyOwnerPointer pop = (PropertyOwnerPointer) newParent;
149 newParent = pop.getPropertyPointer();
150 }
151 return newParent.createChild(context, getName(), index, value);
152 }
153
154 public NodePointer createChild(JXPathContext context, QName name, int index) {
155 return createPath(context).createChild(context, name, index);
156 }
157
158 public NodePointer createChild(JXPathContext context, QName name,
159 int index, Object value) {
160 return createPath(context).createChild(context, name, index, value);
161 }
162
163 public String getPropertyName() {
164 return propertyName;
165 }
166
167 public void setPropertyName(String propertyName) {
168 this.propertyName = propertyName;
169 }
170
171
172
173
174
175 public void setNameAttributeValue(String attributeValue) {
176 this.propertyName = attributeValue;
177 byNameAttribute = true;
178 }
179
180 public boolean isCollection() {
181 return getIndex() != WHOLE_COLLECTION;
182 }
183
184 public int getPropertyCount() {
185 return 0;
186 }
187
188 public String[] getPropertyNames() {
189 return new String[0];
190 }
191
192 public String asPath() {
193 if (!byNameAttribute) {
194 return super.asPath();
195 }
196 StringBuffer buffer = new StringBuffer();
197 buffer.append(getImmediateParentPointer().asPath());
198 buffer.append("[@name='");
199 buffer.append(escape(getPropertyName()));
200 buffer.append("']");
201 if (index != WHOLE_COLLECTION) {
202 buffer.append('[').append(index + 1).append(']');
203 }
204 return buffer.toString();
205 }
206
207
208
209
210
211
212 private JXPathAbstractFactoryException createBadFactoryException(AbstractFactory factory) {
213 return new JXPathAbstractFactoryException("Factory " + factory
214 + " reported success creating object for path: " + asPath()
215 + " but object was null. Terminating to avoid stack recursion.");
216 }
217 }