001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004 TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.validation.attributes;
018:
019: import java.util.logging.Logger;
020:
021: import org.geotools.feature.Feature;
022: import org.geotools.feature.FeatureType;
023: import org.opengis.filter.Filter;
024: import org.geotools.validation.DefaultFeatureValidation;
025: import org.geotools.validation.ValidationResults;
026:
027: /**
028: * Tests to see if an attribute is equal to a provided value.
029: *
030: * <p>
031: * I can only see this test being useful if a Filter is also used. Online
032: * research shows that this test is used in the wild, so we are adding it into
033: * our system.
034: * </p>
035: *
036: * @author Jody Garnett, Refractions Research, Inc.
037: * @author $Author: dmzwiers $ (last modification)
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/attributes/EqualityValidation.java $
039: * @version $Id: EqualityValidation.java 27862 2007-11-12 19:51:19Z desruisseaux $
040: */
041: public class EqualityValidation extends DefaultFeatureValidation {
042: /** The logger for the validation module. */
043: private static final Logger LOGGER = org.geotools.util.logging.Logging
044: .getLogger("org.geotools.validation");
045: private String attributeName;
046:
047: /** Expected value that attribute are supposed to equal */
048: private Object expected;
049:
050: /** Filter used to limit the number of Features we check */
051: private Filter filter = Filter.INCLUDE;
052:
053: /**
054: * No argument constructor, required by the Java Bean Specification.
055: */
056: public EqualityValidation() {
057: }
058:
059: /**
060: * The priority level used to schedule this Validation.
061: *
062: * @return PRORITY_SIMPLE
063: *
064: * @see org.geotools.validation.Validation#getPriority()
065: */
066: public int getPriority() {
067: return PRIORITY_SIMPLE;
068: }
069:
070: /**
071: * Implementation of getTypeNames.
072: *
073: * @return Array of typeNames, or empty array for all, null for disabled
074: *
075: * @see org.geotools.validation.Validation#getTypeRefs()
076: */
077: public String[] getTypeRefs() {
078: if (getTypeRef() == null) {
079: return null;
080: }
081:
082: if (getTypeRef().equals("*")) {
083: return ALL;
084: }
085:
086: return new String[] { getTypeRef(), };
087: }
088:
089: /**
090: * Validation test for feature.
091: *
092: * <p>
093: * Description of test ...
094: * </p>
095: *
096: * @param feature The Feature to be validated
097: * @param type The FeatureType of the feature
098: * @param results The storage for error messages.
099: *
100: * @return <code>true</code> if the feature is a valid geometry.
101: *
102: * @see org.geotools.validation.FeatureValidation#validate
103: */
104: public boolean validate(Feature feature, FeatureType type,
105: ValidationResults results) {
106: if (!filter.evaluate(feature)) {
107: return true;
108: }
109:
110: Object actual = feature.getAttribute(attributeName);
111:
112: if (expected.equals(actual)) {
113: return true;
114: }
115:
116: results.error(feature, attributeName + " did not not equals "
117: + expected);
118:
119: return false;
120: }
121:
122: /**
123: * Access attributeName property.
124: *
125: * @return Returns the attributeName.
126: */
127: public String getAttributeName() {
128: return attributeName;
129: }
130:
131: /**
132: * Set attributeName to attributeName.
133: *
134: * @param attributeName The attributeName to set.
135: */
136: public void setAttributeName(String attributeName) {
137: this .attributeName = attributeName;
138: }
139:
140: /**
141: * Access expected property.
142: *
143: * @return Returns the expected.
144: */
145: public Object getExpected() {
146: return expected;
147: }
148:
149: /**
150: * Set expected to expected.
151: *
152: * @param expected The expected to set.
153: */
154: public void setExpected(Object expected) {
155: this .expected = expected;
156: }
157:
158: /**
159: * Access filter property.
160: *
161: * @return Returns the filter.
162: */
163: public Filter getFilter() {
164: return filter;
165: }
166:
167: /**
168: * Set filter to filter.
169: *
170: * @param filter The filter to set.
171: */
172: public void setFilter(Filter filter) {
173: this.filter = filter;
174: }
175: }
|