01: package org.geotools.filter.expression;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.geotools.factory.FactoryRegistry;
08: import org.geotools.factory.Hints;
09:
10: /**
11: * Convenience class for looking up a property accessor for a particular object type.
12: *
13: * @author Justin Deoliveira, The Open Planning Project
14: *
15: */
16: public class PropertyAccessors {
17: static final List FACTORY_CACHE;
18:
19: static {
20: FACTORY_CACHE = new ArrayList();
21:
22: // add the simple feature property accessor factory first for performance
23: // reasons
24: FACTORY_CACHE.add(new SimpleFeaturePropertyAccessorFactory());
25: Iterator factories = FactoryRegistry
26: .lookupProviders(PropertyAccessorFactory.class);
27: while (factories.hasNext()) {
28: Object factory = factories.next();
29: if (factory instanceof SimpleFeaturePropertyAccessorFactory)
30: continue;
31:
32: FACTORY_CACHE.add(factory);
33: }
34: }
35:
36: /**
37: * Make sure this class won't be instantianted
38: */
39: private PropertyAccessors() {
40: }
41:
42: /**
43: * Looks up a {@link PropertyAccessor} for a particular object.
44: * <p>
45: * This method will return the first accessor that is capabile of handling the object and xpath
46: * expression provided, no order is guaranteed.
47: * </p>
48: *
49: * @param object
50: * The target object.
51: * @param xpath
52: * An xpath expression denoting a property of the target object.
53: * @param hints
54: * Hints to pass on to factories.
55: *
56: * @return A property accessor, or <code>null</code> if one could not be found.
57: */
58: public static PropertyAccessor findPropertyAccessor(Object object,
59: String xpath, Class target, Hints hints) {
60: if (object == null)
61: return null;
62:
63: for (Iterator it = FACTORY_CACHE.iterator(); it.hasNext();) {
64: PropertyAccessorFactory factory = (PropertyAccessorFactory) it
65: .next();
66: PropertyAccessor accessor = factory.createPropertyAccessor(
67: object.getClass(), xpath, target, hints);
68: if (accessor != null
69: && accessor.canHandle(object, xpath, target)) {
70: return accessor;
71: }
72: }
73: return null;
74: }
75: }
|