001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.filter.function;
017:
018: import org.apache.commons.beanutils.PropertyUtils;
019:
020: /**
021: * Function expression that returns a Boolean indicating if a given property
022: * exists in the structure of the object being evaluated.
023: *
024: * @since 2.4
025: * @author Gabriel Roldan, Axios Engineering
026: * @author Mauricio Pazos, Axios Engineering
027: * @version $Id: PropertyExistsFunction.java 25160 2007-04-13 09:12:38Z vmpazos $
028: * @source $URL:
029: * http://svn.geotools.org/geotools/trunk/gt/modules/library/cql/src/main/java/org/geotools/filter/function/PropertyExistsFunction.java $
030: */
031: import java.lang.reflect.InvocationTargetException;
032: import org.opengis.filter.expression.Expression;
033: import org.opengis.filter.expression.Function;
034: import org.opengis.filter.expression.Literal;
035: import org.opengis.filter.expression.PropertyName;
036: import org.geotools.feature.AttributeType;
037: import org.geotools.feature.Feature;
038: import org.geotools.feature.FeatureType;
039: import org.geotools.filter.FunctionExpressionImpl;
040: import org.geotools.resources.Utilities;
041:
042: public class PropertyExistsFunction extends FunctionExpressionImpl {
043: public PropertyExistsFunction() {
044: super ("PropertyExists");
045: }
046:
047: public int getArgCount() {
048: return 1;
049: }
050:
051: private String getPropertyName() {
052: Expression expr = (Expression) getParameters().get(0);
053:
054: return getPropertyName(expr);
055: }
056:
057: private String getPropertyName(Expression expr) {
058: String propertyName;
059:
060: if (expr instanceof Literal) {
061: propertyName = String.valueOf(((Literal) expr).getValue());
062: } else if (expr instanceof PropertyName) {
063: propertyName = ((PropertyName) expr).getPropertyName();
064: } else {
065: throw new IllegalStateException(
066: "Not a property name expression: " + expr);
067: }
068:
069: return propertyName;
070: }
071:
072: /**
073: * @return {@link Boolean#TRUE} if the <code>feature</code>'s
074: * {@link FeatureType} contains an attribute named as the property
075: * name passed as this function argument, {@link Boolean#FALSE}
076: * otherwise.
077: */
078: public Object evaluate(Feature feature) {
079: String propName = getPropertyName();
080: AttributeType attributeType = feature.getFeatureType()
081: .getAttributeType(propName);
082:
083: return Boolean.valueOf(attributeType != null);
084: }
085:
086: /**
087: * @return {@link Boolean#TRUE} if the Class of the object passed as
088: * argument defines a property names as the property name passed as
089: * this function argument, following the standard Java Beans naming
090: * conventions for getters. {@link Boolean#FALSE} otherwise.
091: */
092: public Object evaluate(Object bean) {
093: if (bean instanceof Feature) {
094: return evaluate((Feature) bean);
095: }
096:
097: final String propName = getPropertyName();
098:
099: Boolean propertyExists = Boolean.TRUE;
100:
101: try {
102: PropertyUtils.getProperty(bean, propName);
103: } catch (NoSuchMethodException e) {
104: propertyExists = Boolean.FALSE;
105: } catch (IllegalAccessException e) {
106: throw new RuntimeException(e);
107: } catch (InvocationTargetException e) {
108: throw new RuntimeException(e);
109: }
110:
111: return propertyExists;
112: }
113:
114: public String toString() {
115: StringBuffer sb = new StringBuffer("PropertyExists('");
116: sb.append(getPropertyName());
117: sb.append("')");
118:
119: String stringVal = sb.toString();
120:
121: return stringVal;
122: }
123:
124: public boolean equals(Object obj) {
125: if (!(obj instanceof Function)) {
126: return false;
127: }
128:
129: Function other = (Function) obj;
130:
131: if (!Utilities.equals(getName(), other.getName())) {
132: return false;
133: }
134:
135: final String propName = getPropertyName();
136:
137: Expression otherPropNameExpr = (Expression) other
138: .getParameters().get(0);
139: final String otherPropName = getPropertyName(otherPropNameExpr);
140:
141: return Utilities.equals(propName, otherPropName);
142: }
143: }
|