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 org.geotools.feature.Feature;
020: import org.geotools.feature.FeatureType;
021: import org.geotools.validation.DefaultFeatureValidation;
022: import org.geotools.validation.ValidationResults;
023:
024: /**
025: * RangeFeatureValidation validates that a number is within a given range.
026: *
027: * <p>
028: * RangeFeatureValidation is a quick and simple class the checks that the given
029: * number resides within a given range.
030: * </p>
031: *
032: * <p>
033: * Capabilities:
034: *
035: * <ul>
036: * <li>
037: * Default max value is Integer.MAX_VALUE;
038: * </li>
039: * <li>
040: * Default min value is Integer.MIN_VALUE;
041: * </li>
042: * <li>
043: * If only one boundary of the range is set, only that boundary is checked.
044: * </li>
045: * <li>
046: * The value of the integer is contained in the field specified by path.
047: * </li>
048: * </ul>
049: *
050: * Example Use:
051: * <pre><code>
052: * RangeFeatureValidation x = new RangeFeatureValidation();
053: *
054: * x.setMin(3);
055: * x.setMax(5);
056: * x.setName("id");
057: *
058: * boolean result = x.validate(feature, featureType, results);
059: * </code></pre>
060: * </p>
061: *
062: * @author rgould, Refractions Research, Inc.
063: * @author $Author: cholmesny $ (last modification)
064: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/attributes/RangeValidation.java $
065: * @version $Id: RangeValidation.java 22266 2006-10-19 11:30:55Z acuster $
066: */
067: public class RangeValidation extends DefaultFeatureValidation {
068: private int max = Integer.MAX_VALUE;
069: private int min = Integer.MIN_VALUE;
070: /** XPath expression used to specify attribute */
071: private String attribute;
072:
073: /**
074: * RangeFeatureValidation constructor.
075: *
076: * <p>
077: * Description
078: * </p>
079: */
080: public RangeValidation() {
081: super ();
082: }
083:
084: /**
085: * Override validate.
086: *
087: * <p>
088: * Description ...
089: * </p>
090: *
091: * @param feature
092: * @param type
093: * @param results
094: *
095: *
096: * @see org.geotools.validation.FeatureValidation#validate(org.geotools.feature.Feature,
097: * org.geotools.feature.FeatureType,
098: * org.geotools.validation.ValidationResults)
099: */
100: public boolean validate(Feature feature, FeatureType type,
101: ValidationResults results) {
102: Object obj = feature.getAttribute(attribute);
103:
104: if (obj == null) {
105: return true; // user can separately issue a nullzero test
106: }
107:
108: if (obj instanceof Number) {
109: Number number = (Number) obj;
110:
111: if (number.intValue() < min) {
112: results.error(feature, attribute + " is less than "
113: + min);
114: return false;
115: }
116:
117: if (number.intValue() > max) {
118: results.error(feature, attribute + " is greater than "
119: + max);
120: return false;
121: }
122: }
123: return true;
124: }
125:
126: /**
127: * Override getPriority.
128: *
129: * <p>
130: * Description ...
131: * </p>
132: *
133: *
134: * @see org.geotools.validation.Validation#getPriority()
135: */
136: public int getPriority() {
137: return 0;
138: }
139:
140: /**
141: * getMax purpose.
142: *
143: * <p>
144: * Description ...
145: * </p>
146: *
147: */
148: public int getMax() {
149: return max;
150: }
151:
152: /**
153: * getMin purpose.
154: *
155: * <p>
156: * Description ...
157: * </p>
158: *
159: */
160: public int getMin() {
161: return min;
162: }
163:
164: /**
165: * getPath purpose.
166: *
167: * <p>
168: * Description ...
169: * </p>
170: *
171: * @task REVISIT: This wasn't compiling for me, as its parent sets
172: * this as final. If needed for some reason then fix it. But it
173: * looks like it should inherit ok... ch
174: */
175: //public String getName() {
176: // return name;
177: //}
178: /**
179: * setMax purpose.
180: *
181: * <p>
182: * Description ...
183: * </p>
184: *
185: * @param i
186: */
187: public void setMax(int i) {
188: max = i;
189: }
190:
191: /**
192: * setMin purpose.
193: *
194: * <p>
195: * Description ...
196: * </p>
197: *
198: * @param i
199: */
200: public void setMin(int i) {
201: min = i;
202: }
203:
204: /**
205: * setPath purpose.
206: *
207: * <p>
208: * Description ...
209: * </p>
210: *
211: * @param string
212: * see note on getName - ch.
213: */
214: //public void setName(String string) {
215: // name = string;
216: //}
217: /**
218: * XPATH expression used to locate attribute
219: * @return xpath
220: */
221: public String getAttribute() {
222: return attribute;
223: }
224:
225: /**
226: * XPATH expression used to locate attribute
227: * @param xpath
228: */
229: public void setAttribute(String xpath) {
230: attribute = xpath;
231: }
232:
233: }
|