1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
package org.jaxen.javabean; |
35 | |
|
36 | |
import java.lang.reflect.Method; |
37 | |
import java.lang.reflect.InvocationTargetException; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.Collection; |
40 | |
|
41 | |
import org.jaxen.DefaultNavigator; |
42 | |
import org.jaxen.FunctionCallException; |
43 | |
import org.jaxen.NamedAccessNavigator; |
44 | |
import org.jaxen.Navigator; |
45 | |
import org.jaxen.XPath; |
46 | |
import org.jaxen.JaxenConstants; |
47 | |
import org.jaxen.util.SingleObjectIterator; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 2 | public class DocumentNavigator |
62 | |
extends DefaultNavigator |
63 | |
implements NamedAccessNavigator |
64 | |
{ |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private static final long serialVersionUID = -1768605107626726499L; |
70 | |
|
71 | |
|
72 | 2 | private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; |
73 | |
|
74 | |
|
75 | 2 | private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; |
76 | |
|
77 | |
|
78 | |
|
79 | 2 | private static final DocumentNavigator instance = new DocumentNavigator(); |
80 | |
|
81 | |
|
82 | |
|
83 | |
public static Navigator getInstance() |
84 | |
{ |
85 | 2 | return instance; |
86 | |
} |
87 | |
|
88 | |
public boolean isElement(Object obj) |
89 | |
{ |
90 | 0 | return (obj instanceof Element); |
91 | |
} |
92 | |
|
93 | |
public boolean isComment(Object obj) |
94 | |
{ |
95 | 0 | return false; |
96 | |
} |
97 | |
|
98 | |
public boolean isText(Object obj) |
99 | |
{ |
100 | 0 | return ( obj instanceof String ); |
101 | |
} |
102 | |
|
103 | |
public boolean isAttribute(Object obj) |
104 | |
{ |
105 | 6 | return false; |
106 | |
} |
107 | |
|
108 | |
public boolean isProcessingInstruction(Object obj) |
109 | |
{ |
110 | 0 | return false; |
111 | |
} |
112 | |
|
113 | |
public boolean isDocument(Object obj) |
114 | |
{ |
115 | 0 | return false; |
116 | |
} |
117 | |
|
118 | |
public boolean isNamespace(Object obj) |
119 | |
{ |
120 | 6 | return false; |
121 | |
} |
122 | |
|
123 | |
public String getElementName(Object obj) |
124 | |
{ |
125 | 0 | return ((Element)obj).getName(); |
126 | |
} |
127 | |
|
128 | |
public String getElementNamespaceUri(Object obj) |
129 | |
{ |
130 | 0 | return ""; |
131 | |
} |
132 | |
|
133 | |
public String getElementQName(Object obj) |
134 | |
{ |
135 | 0 | return ""; |
136 | |
} |
137 | |
|
138 | |
public String getAttributeName(Object obj) |
139 | |
{ |
140 | 0 | return ""; |
141 | |
} |
142 | |
|
143 | |
public String getAttributeNamespaceUri(Object obj) |
144 | |
{ |
145 | 0 | return ""; |
146 | |
} |
147 | |
|
148 | |
public String getAttributeQName(Object obj) |
149 | |
{ |
150 | 0 | return ""; |
151 | |
} |
152 | |
|
153 | |
public Iterator getChildAxisIterator(Object contextNode) |
154 | |
{ |
155 | 6 | return JaxenConstants.EMPTY_ITERATOR; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public Iterator getChildAxisIterator(Object contextNode, |
169 | |
String localName, |
170 | |
String namespacePrefix, |
171 | |
String namespaceURI) |
172 | |
{ |
173 | 8 | Class cls = ((Element)contextNode).getObject().getClass(); |
174 | |
|
175 | 8 | String methodName = javacase( localName ); |
176 | |
|
177 | 8 | Method method = null; |
178 | |
|
179 | |
try |
180 | |
{ |
181 | 8 | method = cls.getMethod( "get" + methodName, EMPTY_CLASS_ARRAY ); |
182 | |
} |
183 | 2 | catch (NoSuchMethodException e) |
184 | |
{ |
185 | |
try |
186 | |
{ |
187 | 2 | method = cls.getMethod( "get" + methodName + "s", EMPTY_CLASS_ARRAY ); |
188 | |
} |
189 | 0 | catch (NoSuchMethodException ee) |
190 | |
{ |
191 | |
try |
192 | |
{ |
193 | 0 | method = cls.getMethod( localName, EMPTY_CLASS_ARRAY ); |
194 | |
} |
195 | 0 | catch (NoSuchMethodException eee) |
196 | |
{ |
197 | 0 | method = null; |
198 | 0 | } |
199 | 2 | } |
200 | 6 | } |
201 | |
|
202 | 8 | if ( method == null ) |
203 | |
{ |
204 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
205 | |
} |
206 | |
|
207 | |
try |
208 | |
{ |
209 | 8 | Object result = method.invoke( ((Element)contextNode).getObject(), EMPTY_OBJECT_ARRAY ); |
210 | |
|
211 | 8 | if ( result == null ) |
212 | |
{ |
213 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
214 | |
} |
215 | |
|
216 | 8 | if ( result instanceof Collection ) |
217 | |
{ |
218 | 2 | return new ElementIterator( (Element) contextNode, localName, ((Collection)result).iterator() ); |
219 | |
} |
220 | |
|
221 | 6 | if ( result.getClass().isArray() ) |
222 | |
{ |
223 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
224 | |
} |
225 | |
|
226 | 6 | return new SingleObjectIterator( new Element( (Element) contextNode, localName, result ) ); |
227 | |
} |
228 | 0 | catch (IllegalAccessException e) |
229 | |
{ |
230 | |
|
231 | |
} |
232 | 0 | catch (InvocationTargetException e) |
233 | |
{ |
234 | |
|
235 | 0 | } |
236 | |
|
237 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
238 | |
} |
239 | |
|
240 | |
public Iterator getParentAxisIterator(Object contextNode) |
241 | |
{ |
242 | 0 | if ( contextNode instanceof Element ) |
243 | |
{ |
244 | 0 | return new SingleObjectIterator( ((Element)contextNode).getParent() ); |
245 | |
} |
246 | |
|
247 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
248 | |
} |
249 | |
|
250 | |
public Iterator getAttributeAxisIterator(Object contextNode) |
251 | |
{ |
252 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public Iterator getAttributeAxisIterator(Object contextNode, |
266 | |
String localName, |
267 | |
String namespacePrefix, |
268 | |
String namespaceURI) { |
269 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
270 | |
} |
271 | |
|
272 | |
public Iterator getNamespaceAxisIterator(Object contextNode) |
273 | |
{ |
274 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
275 | |
} |
276 | |
|
277 | |
public Object getDocumentNode(Object contextNode) |
278 | |
{ |
279 | 0 | return null; |
280 | |
} |
281 | |
|
282 | |
public Object getParentNode(Object contextNode) |
283 | |
{ |
284 | 66 | if ( contextNode instanceof Element ) |
285 | |
{ |
286 | 66 | return ((Element)contextNode).getParent(); |
287 | |
} |
288 | |
|
289 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
290 | |
} |
291 | |
|
292 | |
public String getTextStringValue(Object obj) |
293 | |
{ |
294 | 0 | if ( obj instanceof Element ) |
295 | |
{ |
296 | 0 | return ((Element)obj).getObject().toString(); |
297 | |
} |
298 | 0 | return obj.toString(); |
299 | |
} |
300 | |
|
301 | |
public String getElementStringValue(Object obj) |
302 | |
{ |
303 | 0 | if ( obj instanceof Element ) |
304 | |
{ |
305 | 0 | return ((Element)obj).getObject().toString(); |
306 | |
} |
307 | 0 | return obj.toString(); |
308 | |
} |
309 | |
|
310 | |
public String getAttributeStringValue(Object obj) |
311 | |
{ |
312 | 0 | return obj.toString(); |
313 | |
} |
314 | |
|
315 | |
public String getNamespaceStringValue(Object obj) |
316 | |
{ |
317 | 0 | return obj.toString(); |
318 | |
} |
319 | |
|
320 | |
public String getNamespacePrefix(Object obj) |
321 | |
{ |
322 | 0 | return null; |
323 | |
} |
324 | |
|
325 | |
public String getCommentStringValue(Object obj) |
326 | |
{ |
327 | 0 | return null; |
328 | |
} |
329 | |
|
330 | |
public String translateNamespacePrefixToUri(String prefix, Object context) |
331 | |
{ |
332 | 0 | return null; |
333 | |
} |
334 | |
|
335 | |
public short getNodeType(Object node) |
336 | |
{ |
337 | 0 | return 0; |
338 | |
} |
339 | |
|
340 | |
public Object getDocument(String uri) throws FunctionCallException |
341 | |
{ |
342 | 0 | return null; |
343 | |
} |
344 | |
|
345 | |
public String getProcessingInstructionTarget(Object obj) |
346 | |
{ |
347 | 0 | return null; |
348 | |
} |
349 | |
|
350 | |
public String getProcessingInstructionData(Object obj) |
351 | |
{ |
352 | 0 | return null; |
353 | |
} |
354 | |
|
355 | |
public XPath parseXPath(String xpath) |
356 | |
throws org.jaxen.saxpath.SAXPathException |
357 | |
{ |
358 | 0 | return new JavaBeanXPath( xpath ); |
359 | |
} |
360 | |
|
361 | |
protected String javacase(String name) |
362 | |
{ |
363 | 8 | if ( name.length() == 0 ) |
364 | |
{ |
365 | 0 | return name; |
366 | |
} |
367 | 8 | else if ( name.length() == 1 ) |
368 | |
{ |
369 | 0 | return name.toUpperCase(); |
370 | |
} |
371 | |
|
372 | 8 | return name.substring( 0, 1 ).toUpperCase() + name.substring( 1 ); |
373 | |
} |
374 | |
} |