comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents 3ddc22aab764
children 1efb6a66c7d9
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 /**
2 *
3 */
4 package de.intevation.gnv.utils;
5
6 import java.util.Date;
7
8 import org.apache.commons.validator.GenericValidator;
9 import org.apache.log4j.Logger;
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 de.intevation.gnv.geobackend.util.DateUtils;
16 import de.intevation.gnv.utils.exception.ValidationException;
17
18 /**
19 * @author Tim Englich <tim.englich@intevation.de>
20 *
21 */
22 public class InputValidator {
23 /**
24 * the logger, used to log exceptions and additonaly information
25 */
26 private static Logger log = Logger.getLogger(InputValidator.class);
27
28
29 public final static String NODATASELECTEDVALUE = "n/n";
30
31 /**
32 * Constructor
33 */
34 public InputValidator() {
35 super();
36 }
37
38 public boolean isInputValid(String minInput, String maxInput, String type) {
39 log.debug("InputValidator.isInputValid " + minInput + " " + maxInput + " " +type);
40 boolean returnValue = false;
41 if ("Date".equalsIgnoreCase(type)) {
42 try {
43 Date min = DateUtils.getDateFromString(minInput,DateUtils.DATE_PATTERN);
44 Date max = DateUtils.getDateFromString(maxInput,DateUtils.DATE_PATTERN);
45 int value = max.compareTo(min);
46 returnValue = value >= 0;
47 } catch (Exception e) {
48 log.error(e,e);
49 }
50 } else if ("Double".equalsIgnoreCase(type)) {
51 try {
52 double min = Double.parseDouble(minInput);
53 double max = Double.parseDouble(maxInput);
54 returnValue = max >= min;
55 } catch (Exception e) {
56 log.error(e,e);
57 }
58 }
59 log.debug("Is valid? " + returnValue);
60 return returnValue;
61 }
62
63 public boolean isInputValid(String input, String type) {
64 log.debug("InputValidator.isInputValid " + input + " " + type);
65 boolean returnValue = false;
66 String[] values = input.split(",");
67 for (int i = 0; i < values.length; i++) {
68 boolean valid;
69
70 if (NODATASELECTEDVALUE.equals(values[i].trim())){
71 valid = true;
72 } else if ("Integer".equalsIgnoreCase(type)) {
73 valid = GenericValidator.isInt(values[i].trim());
74 } else if ("Double".equalsIgnoreCase(type)) {
75 valid = GenericValidator.isDouble(values[i].trim());
76 } else if ("String".equalsIgnoreCase(type)) {
77 valid = GenericValidator.matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO:
78 // FIXME:
79 // VALIDATE
80 // REGEXP
81 } else if ("Date".equalsIgnoreCase(type)) {
82 valid = GenericValidator.isDate(values[i].trim(),
83 DateUtils.DATE_PATTERN, true);
84 } else if ("Point".equalsIgnoreCase(type)) {
85 valid = GenericValidator.matchRegexp(values[i], "[0-9]"); // TODO:
86 // FIXME:
87 // VALIDATE
88 // REGEXP
89 } else if ("AttributeName".equalsIgnoreCase(type)) {
90 valid = org.apache.commons.validator.GenericValidator
91 .matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO: FIXME:
92 // VALIDATE
93 // REGEXP
94 } else if ("Coordinate".equalsIgnoreCase(type)) {
95 try {
96 valid = this.getPointValue(values[i]) != null;
97 } catch (ValidationException e) {
98 log.debug(e.getMessage());
99 valid = false;
100 }
101 } else {
102 valid = false;
103 }
104 if (!valid) {
105 returnValue = false;
106 break;
107 } else {
108 returnValue = true;
109 }
110 }
111 log.debug("Is valid? " + returnValue);
112 return returnValue;
113 }
114
115
116 public Point getPointValue(String value) throws ValidationException{
117 log.debug("InputValidator.getPointValue " + value);
118 String[] s, p;
119
120 double x=0,y=0;
121 log.info("Position :"+value);
122 s = value.split(" ");
123 if (s.length != 2) {
124 throw new ValidationException("Kein Blank separiert Breite und Länge");
125 }
126 p = s[0].split("[nNsS]");
127 try {
128 if (p.length == 1)
129 y = new Double(p[0]);
130 else
131 y = new Double(p[0]) + new Double(p[1]) / new Double(60.);
132 if (s[0].toLowerCase().contains("s"))
133 y = -y;
134 }
135 catch (Exception e) {
136 throw new ValidationException("Kein N|S oder nicht im ersten Substring, zB 56n42");
137
138 }
139 p = s[1].split("[eEwW]");
140 try {
141 if (p.length ==1)
142 x = new Double(p[0]);
143 else
144 x = new Double(p[0]) + new Double(p[1]) / new Double(60.) ;
145 if (s[1].toLowerCase().contains("w"))
146 x = -x;
147 }
148 catch (Exception e) {
149 throw new ValidationException("Kein E|W oder nicht im zweiten Substring");
150 }
151 return new GeometryFactory().createPoint(new Coordinate(x,y));
152 }
153
154 }

http://dive4elements.wald.intevation.org