comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org