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: * NullZeroFeatureValidation purpose.
026: *
027: * <p>
028: * Description of NullZeroFeatureValidation ...
029: * </p>
030: *
031: * <p>
032: * Capabilities:
033: *
034: * <ul>
035: * <li>
036: * Tests for null/0 atribute values.
037: * </li>
038: * </ul>
039: *
040: * Example Use:
041: * <pre><code>
042: * NullZeroFeatureValidation x = new NullZeroFeatureValidation(...);
043: * </code></pre>
044: * </p>
045: *
046: * @author dzwiers, Refractions Research, Inc.
047: * @author $Author: dmzwiers $ (last modification)
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/attributes/NullZeroValidation.java $
049: * @version $Id: NullZeroValidation.java 22226 2006-10-18 20:06:40Z acuster $
050: */
051: public class NullZeroValidation extends DefaultFeatureValidation {
052: /** XPATH expression for attribtue */
053: private String attribute;
054:
055: public NullZeroValidation() {
056: super ();
057: }
058:
059: /**
060: * Implement validate.
061: *
062: * <p>
063: * Description ...
064: * </p>
065: *
066: * @param feature Provides the attributes to test.
067: * @param type not used.
068: * @param results a reference for returning error codes.
069: *
070: * @return false when null or 0 values are found in the attribute.
071: *
072: * @see org.geotools.validation.FeatureValidation#validate(org.geotools.feature.Feature,
073: * org.geotools.feature.FeatureType,
074: * org.geotools.validation.ValidationResults)
075: */
076: public boolean validate(Feature feature, FeatureType type,
077: ValidationResults results) { // throws Exception {
078:
079: //if attribute not set, just pass
080: if (attribute == null)
081: return true;
082:
083: Object obj = feature.getAttribute(attribute);
084:
085: if (obj == null) {
086: results.error(feature, attribute + " is Empty");
087:
088: return false;
089: }
090:
091: if (obj instanceof Number) {
092: Number number = (Number) obj;
093:
094: if (number.intValue() == 0) {
095: results.error(feature, attribute + " is Zero");
096:
097: return false;
098: }
099: }
100:
101: if (obj instanceof String) {
102: String string = (String) obj;
103:
104: if ("".equals(string.trim())) {
105: results.error(feature, attribute + " is \"\"");
106:
107: return false;
108: }
109: }
110:
111: return true;
112: }
113:
114: /**
115: * Implement getPriority.
116: *
117: * @see org.geotools.validation.Validation#getPriority()
118: */
119: public int getPriority() {
120: return 0;
121: }
122:
123: /**
124: * Implementation of getTypeNames.
125: *
126: * @return Array of typeNames, or empty array for all, null for disabled
127: *
128: * @see org.geotools.validation.Validation#getTypeRefs()
129: */
130: public String[] getTypeRefs() {
131: if (getTypeRef() == null) {
132: return null;
133: }
134:
135: if (getTypeRef().equals("*")) {
136: return ALL;
137: }
138:
139: return new String[] { getTypeRef(), };
140: }
141:
142: /**
143: * Access attributeName property.
144: *
145: * @return the path being stored for validation
146: */
147: public String getAttribute() {
148: return attribute;
149: }
150:
151: /**
152: * set AttributeName to xpath expression.
153: *
154: * @param xpath A String
155: */
156: public void setAttribute(String xpath) {
157: attribute = xpath;
158: }
159: }
|