01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.filter.expression;
17:
18: import org.geotools.feature.Feature;
19: import org.opengis.filter.expression.Expression;
20: import org.opengis.filter.expression.ExpressionVisitor;
21:
22: //
23: /**
24: * Abstract superclass of these Expression implementations.
25: * <p>
26: * Contains additional support for "Expression chaining". This allows
27: * Expressions to be constructed as a chain of Java commands similar to the use
28: * of the java collections api.
29: * </p>
30: * <p>
31: * Note: Expression chaining is a simple developer convience, it has no effect
32: * on the data model exposed by the GeoAPI interfaces.
33: * </p>
34: * <p>
35: * Idea: We may also be able to teach this implementation to make use of
36: * JXPath to extract "attribute values" from Java Beans, DOM, JDOM in addition
37: * to the geotools & geoapi FeatureType models. It is a cunning plan - any
38: * implementation will make use of this abstract base class.
39: * </p>
40: *
41: * @author Jody Garnett
42: *
43: */
44: public abstract class ExpressionAbstract implements Expression {
45:
46: /** Subclass should overide, default implementation returns null */
47: public Object evaluate(Object object) {
48: return null;
49: }
50:
51: /**
52: * Default implementation delegates handling of context
53: * conversion to Value utility class.
54: * <p>
55: * Subclasses are expected to make use of the Value utility class
56: * (as the easiest way to provide value morphing in confirmance with
57: * the Filter specification).
58: *
59: */
60: public Object evaluate(Object object, Class context) {
61: Value value = new Value(evaluate(object));
62: return value.value(context);
63: }
64:
65: /** Subclass should override, default implementation just returns extraData */
66: public Object accept(ExpressionVisitor visitor, Object extraData) {
67: return extraData;
68: }
69:
70: /**
71: * Helper method for subclasses to reduce null checks
72: * @param expression
73: * @param feature
74: * @return value or null
75: */
76: protected Object eval(Expression expression, Feature feature) {
77: if (expression == null || feature == null)
78: return null;
79: return expression.evaluate(feature);
80: }
81:
82: /**
83: * Helper method for subclasses to reduce null checks
84: *
85: * @param expression
86: * @param feature
87: * @return value or null
88: */
89: protected Object eval(org.geotools.filter.Expression expression,
90: Feature feature) {
91: if (expression == null || feature == null)
92: return null;
93: return expression.evaluate(feature);
94: }
95: }
|