001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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;
017:
018: import java.util.logging.Logger;
019:
020: import org.geotools.feature.AttributeType;
021: import org.geotools.feature.Feature;
022: import org.geotools.feature.FeatureType;
023: import org.opengis.filter.expression.ExpressionVisitor;
024:
025: /**
026: * Defines a complex filter (could also be called logical filter). This filter
027: * holds one or more filters together and relates them logically in an
028: * internally defined manner.
029: *
030: * @author Rob Hranac, TOPP
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/filter/AttributeExpressionImpl2.java $
032: * @version $Id: AttributeExpressionImpl2.java 27862 2007-11-12 19:51:19Z desruisseaux $
033: */
034: public class AttributeExpressionImpl2 extends DefaultExpression
035: implements AttributeExpression {
036: /** The logger for the default core module. */
037: private static final Logger LOGGER = org.geotools.util.logging.Logging
038: .getLogger("org.geotools.core");
039:
040: /** Holds all sub filters of this filter. */
041: protected String attPath;
042:
043: /** Holds all sub filters of this filter. */
044: protected AttributeType at = null;
045:
046: /**
047: * Constructor with the schema for this attribute.
048: *
049: * @param at the AttributeType schema for this attribute.
050: */
051: protected AttributeExpressionImpl2(AttributeType at) {
052: this .at = at;
053: this .expressionType = ATTRIBUTE;
054: }
055:
056: /**
057: * Constructor with minimum dataset for a valid expression.
058: *
059: * @param attPath The initial (required) sub filter.
060: *
061: * @throws IllegalFilterException If the attribute path is not in the
062: * schema.
063: *
064: * @deprecated use {@link #setPropertyName(String)}.
065: */
066: public final void setAttributePath(String attPath)
067: throws IllegalFilterException {
068: setPropertyName(attPath);
069: }
070:
071: /**
072: * This method calls {@link #getPropertyName()}.
073: *
074: * @deprecated use {@link #getPropertyName()}
075: */
076: public final String getAttributePath() {
077: return getPropertyName();
078: }
079:
080: /**
081: * Gets the path to the attribute to be evaluated by this expression.
082: *
083: * {@link org.opengis.filter.expression.PropertyName#getPropertyName()}.
084: */
085: public String getPropertyName() {
086: return attPath;
087: }
088:
089: /**
090: * Unsupported operation
091: *
092: * @throws IllegalFilterException.
093: */
094: public void setPropertyName(String name) {
095:
096: throw new IllegalFilterException("Attribute: " + attPath
097: + " is not in stated schema " + ".");
098:
099: }
100:
101: /**
102: * Gets the value of this attribute from the passed feature.
103: *
104: * @param feature Feature from which to extract attribute value.
105: */
106: public Object evaluate(Feature feature) {
107: return feature.getAttribute(attPath);
108: }
109:
110: /* shouldn't this class dissapear as AttributeExpressionImpl does the job pretty well now?*/
111: public Object evaluate(Object object) {
112: if (object instanceof Feature) {
113: return evaluate((Feature) object);
114: }
115: return null;//just to respect old behavoir
116: }
117:
118: /**
119: * Return this expression as a string.
120: *
121: * @return String representation of this attribute expression.
122: */
123: public String toString() {
124: return attPath;
125: }
126:
127: /**
128: * Compares this filter to the specified object. Returns true if the
129: * passed in object is the same as this expression. Checks to make sure
130: * the expression types are the same as well as the attribute paths and
131: * schemas.
132: *
133: * @param obj - the object to compare this ExpressionAttribute against.
134: *
135: * @return true if specified object is equal to this filter; else false
136: */
137: public boolean equals(Object obj) {
138: if (obj.getClass() == this .getClass()) {
139: AttributeExpressionImpl2 expAttr = (AttributeExpressionImpl2) obj;
140:
141: boolean isEqual = (expAttr.getType() == this .expressionType);
142: isEqual = (expAttr.attPath != null) ? (isEqual && expAttr.attPath
143: .equals(this .attPath))
144: : (isEqual && (this .attPath == null));
145:
146: return isEqual;
147: } else {
148: return false;
149: }
150: }
151:
152: /**
153: * Override of hashCode method.
154: *
155: * @return a code to hash this object by.
156: */
157: public int hashCode() {
158: int result = 17;
159: result = (37 * result)
160: + (attPath == null ? 0 : attPath.hashCode());
161: return result;
162: }
163:
164: /**
165: * Used by FilterVisitors to perform some action on this filter instance.
166: * Typicaly used by Filter decoders, but may also be used by any thing
167: * which needs infomration from filter structure. Implementations should
168: * always call: visitor.visit(this); It is importatant that this is not
169: * left to a parent class unless the parents API is identical.
170: *
171: * @param visitor The visitor which requires access to this filter, the
172: * method must call visitor.visit(this);
173: */
174: public Object accept(ExpressionVisitor visitor, Object extraData) {
175: return visitor.visit(this, extraData);
176: }
177:
178: }
|