comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents f94a95009423
children 8f836fb6f592
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.utils;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.GeometryFactory;
5 import com.vividsolutions.jts.geom.Point;
6
7 import com.vividsolutions.jts.io.ParseException;
8 import com.vividsolutions.jts.io.WKTReader;
9
10 import de.intevation.gnv.geobackend.util.DateUtils;
11
12 import de.intevation.gnv.utils.exception.ValidationException;
13
14 import java.util.Date;
15
16 import org.apache.commons.validator.GenericValidator;
17
18 import org.apache.log4j.Logger;
19
20 /**
21 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
22 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
23 *
24 */
25 public class InputValidator {
26 /**
27 * the logger, used to log exceptions and additonaly information
28 */
29 private static Logger log = Logger.getLogger(InputValidator.class);
30
31
32 public final static String NODATASELECTEDVALUE = "n/n";
33
34 /**
35 * Constructor
36 */
37 public InputValidator() {
38 super();
39 }
40
41 /**
42 * Validates the input of a range of double or date values. The input values
43 * need to be valid double or date values. <i>minInput</i> needs to be
44 * smaller or equal <i>maxInput</i>.
45 *
46 * @param minInput The lower bound.
47 * @param maxInput The upper bound.
48 * @param type One of 'Date' or 'Double'.
49 * @return true, if the input is valid, otherwise false.
50 */
51 public static boolean isInputValid(String minInput, String maxInput, String type) {
52 log.debug("InputValidator.isInputValid " + minInput + " " + maxInput + " " +type);
53 boolean returnValue = false;
54 if ("Date".equalsIgnoreCase(type)) {
55 try {
56 Date min = DateUtils.getDateFromString(minInput,DateUtils.DATE_PATTERN);
57 Date max = DateUtils.getDateFromString(maxInput,DateUtils.DATE_PATTERN);
58 int value = max.compareTo(min);
59 returnValue = value >= 0;
60 } catch (Exception e) {
61 log.error(e,e);
62 }
63 } else if ("Double".equalsIgnoreCase(type)) {
64 try {
65 double min = Double.parseDouble(minInput);
66 double max = Double.parseDouble(maxInput);
67 returnValue = max >= min;
68 } catch (Exception e) {
69 log.error(e,e);
70 }
71 }
72 log.debug("Is valid? " + returnValue);
73 return returnValue;
74 }
75
76 /**
77 * Validates an input.
78 *
79 * @param input The input value.
80 * @param type The input value type.
81 * @return true if the input is valid, otherwise false.
82 */
83 public static boolean isInputValid(String input, String type) {
84 if (input.length() == 0 || input.equals("")) {
85 return false;
86 }
87
88 log.debug("InputValidator.isInputValid " + input + " " + type);
89
90 // Let's check polygons and linestrings first, because they might
91 // contain comma. A splitting at comma characters wouldn't be good here.
92 if ("Polygon".equalsIgnoreCase(type) || "Linestring".equalsIgnoreCase(type))
93 {
94 try {
95 WKTReader reader = new WKTReader();
96 reader.read(input);
97
98 return true;
99 }
100 catch (ParseException pe) {
101 log.warn(pe, pe);
102 return false;
103 }
104 }
105
106 // Check all the other input here
107 boolean returnValue = false;
108 String[] values = input.split(",");
109
110 for (int i = 0; i < values.length; i++) {
111 boolean valid;
112
113 if (NODATASELECTEDVALUE.equals(values[i].trim())){
114 valid = true;
115 } else if ("Integer".equalsIgnoreCase(type)) {
116 valid = GenericValidator.isInt(values[i].trim());
117 } else if ("Double".equalsIgnoreCase(type)) {
118 valid = GenericValidator.isDouble(values[i].trim());
119 } else if ("String".equalsIgnoreCase(type)) {
120 valid = GenericValidator.matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO:
121 // FIXME:
122 // VALIDATE
123 // REGEXP
124 } else if ("Date".equalsIgnoreCase(type)) {
125 valid = GenericValidator.isDate(values[i].trim(),
126 DateUtils.DATE_PATTERN, true);
127 } else if ("Point".equalsIgnoreCase(type) || "Geometry".equals(type)) {
128 valid = GenericValidator.matchRegexp(values[i], "[0-9]"); // TODO:
129 // FIXME:
130 // VALIDATE
131 // REGEXP
132 } else if ("AttributeName".equalsIgnoreCase(type)) {
133 valid = org.apache.commons.validator.GenericValidator
134 .matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO: FIXME:
135 // VALIDATE
136 // REGEXP
137 } else if ("Coordinate".equalsIgnoreCase(type)) {
138 try {
139 valid = getPointValue(values[i]) != null;
140 } catch (ValidationException e) {
141 log.debug(e.getMessage());
142 valid = false;
143 }
144 } else {
145 valid = false;
146 }
147 if (!valid) {
148 returnValue = false;
149 break;
150 } else {
151 returnValue = true;
152 }
153 }
154 log.debug("Is valid? " + returnValue);
155 return returnValue;
156 }
157
158
159 /**
160 * Returns a point from wkt string.
161 *
162 * @param value The wkt string.
163 * @return a point.
164 * @throws ValidationException if <i>value</i> is not valid.
165 */
166 public static Point getPointValue(String value) throws ValidationException{
167 log.debug("InputValidator.getPointValue " + value);
168
169 if (value.toLowerCase().startsWith("point")){
170 try {
171 return (Point)new WKTReader().read(value);
172 } catch (ParseException e) {
173 log.error(e,e);
174 throw new ValidationException(e);
175 }
176 }else{
177 String[] s, p;
178
179 double x=0,y=0;
180 log.info("Position :"+value);
181 s = value.split(" ");
182 if (s.length != 2) {
183 throw new ValidationException("Kein Blank separiert Breite und Länge");
184 }
185 p = s[0].split("[nNsS]");
186 try {
187 if (p.length == 1)
188 y = new Double(p[0]);
189 else
190 y = new Double(p[0]) + new Double(p[1]) / new Double(60.);
191 if (s[0].toLowerCase().contains("s"))
192 y = -y;
193 }
194 catch (Exception e) {
195 throw new ValidationException("Kein N|S oder nicht im ersten Substring, zB 56n42");
196
197 }
198 p = s[1].split("[eEwW]");
199 try {
200 if (p.length ==1)
201 x = new Double(p[0]);
202 else
203 x = new Double(p[0]) + new Double(p[1]) / new Double(60.) ;
204 if (s[1].toLowerCase().contains("w"))
205 x = -x;
206 }
207 catch (Exception e) {
208 throw new ValidationException("Kein E|W oder nicht im zweiten Substring");
209 }
210 return new GeometryFactory().createPoint(new Coordinate(x,y));
211 }
212 }
213
214
215 /**
216 * Makes sure that <i>tmp</i> is between <i>lo</i> and <i>up</i>.
217 *
218 * @param tmp The value to validate.
219 * @param lo The lower range bound.
220 * @param up The upper range bound.
221 * @return true, if tmp is valid, otherwise false.
222 */
223 public static boolean isDateValid(Date tmp, Date lo, Date up) {
224 // we need to transform the given dates into seconds, because
225 // they differ in milliseconds -> that's why we cannot use the
226 // Date.compareTo(Date) method.
227 long tmpTime = tmp.getTime() / 1000;
228 long tmpLow = lo.getTime() / 1000;
229 long tmpUp = up.getTime() / 1000;
230
231 if (tmpTime < tmpLow || tmpTime > tmpUp) {
232 log.warn(
233 "Date [" + tmp.toString() + "] is out of range ["
234 + lo.toString() + " to "+ up.toString() + "].");
235 return false;
236 }
237
238 return true;
239 }
240 }
241 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org