01: package org.geotools.xml;
02:
03: import org.apache.commons.jxpath.JXPathContext;
04: import org.apache.commons.jxpath.JXPathContextFactory;
05: import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
06: import org.geotools.factory.Hints;
07: import org.geotools.feature.Feature;
08: import org.geotools.feature.FeatureType;
09: import org.geotools.feature.IllegalAttributeException;
10: import org.geotools.filter.expression.PropertyAccessor;
11: import org.geotools.filter.expression.PropertyAccessorFactory;
12: import org.geotools.xml.impl.jxpath.FeatureNodeFactory;
13: import org.xml.sax.helpers.NamespaceSupport;
14:
15: /**
16: * PropertyAccessorFactory used to create property accessors which can handle
17: * xpath expressions against instances of {@link Feature}.
18: *
19: * @author Justin Deoliveira, The Open Planning Project
20: *
21: */
22: public class XPathPropertyAccessorFactory implements
23: PropertyAccessorFactory {
24:
25: /**
26: * Namespace support hint
27: */
28: public static Hints.Key NAMESPACE_SUPPORT = new Hints.Key(
29: NamespaceSupport.class);
30:
31: static {
32: //unfortunatley, jxpath only works against concreate classes
33: //JXPathIntrospector.registerDynamicClass(DefaultFeature.class, FeaturePropertyHandler.class);
34: JXPathContextReferenceImpl
35: .addNodePointerFactory(new FeatureNodeFactory());
36: }
37:
38: public PropertyAccessor createPropertyAccessor(Class type,
39: String xpath, Class target, Hints hints) {
40:
41: if (Feature.class.isAssignableFrom(type)) {
42: return new XPathPropertyAcessor();
43: }
44:
45: if (FeatureType.class.isAssignableFrom(type)) {
46: return new XPathPropertyAcessor();
47: }
48:
49: return null;
50: }
51:
52: static class XPathPropertyAcessor implements PropertyAccessor {
53:
54: public boolean canHandle(Object object, String xpath,
55: Class target) {
56: //TODO: some better check for a valid xpath expression
57: return xpath != null && !"".equals(xpath.trim());
58: }
59:
60: public Object get(Object object, String xpath, Class target) {
61: return context(object).getValue(xpath);
62: }
63:
64: public void set(Object object, String xpath, Object value,
65: Class target) throws IllegalAttributeException {
66: context(object).setValue(xpath, value);
67: }
68:
69: JXPathContext context(Object object) {
70: JXPathContext context = JXPathContextFactory.newInstance()
71: .newContext(null, object);
72: context.setLenient(true);
73:
74: return context;
75: }
76:
77: }
78: }
|