Mercurial > dive4elements > gnv-client
annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 199:b66e8a6f3907
Moved Configurationfiles
gnv-artifacts/trunk@253 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Wed, 21 Oct 2009 10:16:52 +0000 |
parents | 1b2fc94766c9 |
children | 3e82b4f1c455 |
rev | line source |
---|---|
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
1 /** |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
2 * |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
3 */ |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
4 package de.intevation.gnv.utils; |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
5 |
116
820238357bab
Added Horizontal-Profile-Support for Meshes.
Tim Englich <tim.englich@intevation.de>
parents:
115
diff
changeset
|
6 import org.apache.commons.validator.GenericValidator; |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
7 import org.apache.log4j.Logger; |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
8 |
173
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
9 import com.vividsolutions.jts.geom.Coordinate; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
10 import com.vividsolutions.jts.geom.GeometryFactory; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
11 import com.vividsolutions.jts.geom.Point; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
12 |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
13 import de.intevation.gnv.geobackend.util.DateUtils; |
173
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
14 import de.intevation.gnv.utils.exception.ValidationException; |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
15 |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
16 /** |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
17 * @author Tim Englich <tim.englich@intevation.de> |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
18 * |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
19 */ |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
20 public class InputValidator { |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
21 /** |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
22 * the logger, used to log exceptions and additonaly information |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
23 */ |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
24 private static Logger log = Logger.getLogger(InputValidator.class); |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
25 |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
26 /** |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
27 * Constructor |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
28 */ |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
29 public InputValidator() { |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
30 super(); |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
31 } |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
32 |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
33 public boolean isInputValid(String input, String type) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
34 log.debug("InputValidator.isInputValid " + input + " " + type); |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
35 boolean returnValue = false; |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
36 String[] values = input.split(","); |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
37 for (int i = 0; i < values.length; i++) { |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
38 boolean valid; |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
39 if ("Integer".equalsIgnoreCase(type)) { |
115
158e89c2263b
Removed needless imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
113
diff
changeset
|
40 valid = GenericValidator.isInt(values[i].trim()); |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
41 } else if ("Double".equalsIgnoreCase(type)) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
42 valid = GenericValidator.isDouble(values[i].trim()); |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
43 } else if ("String".equalsIgnoreCase(type)) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
44 valid = GenericValidator.matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO: |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
45 // FIXME: |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
46 // VALIDATE |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
47 // REGEXP |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
48 } else if ("Date".equalsIgnoreCase(type)) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
49 valid = GenericValidator.isDate(values[i].trim(), |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
50 DateUtils.DATE_PATTERN, true); |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
51 } else if ("Point".equalsIgnoreCase(type)) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
52 valid = GenericValidator.matchRegexp(values[i], "[0-9]"); // TODO: |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
53 // FIXME: |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
54 // VALIDATE |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
55 // REGEXP |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
56 } else if ("AttributeName".equalsIgnoreCase(type)) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
57 valid = org.apache.commons.validator.GenericValidator |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
58 .matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO: FIXME: |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
59 // VALIDATE |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
60 // REGEXP |
173
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
61 } else if ("Coordinate".equalsIgnoreCase(type)) { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
62 try { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
63 valid = this.getPointValue(values[i]) != null; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
64 } catch (ValidationException e) { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
65 log.debug(e.getMessage()); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
66 valid = false; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
67 } |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
68 } else { |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
69 valid = false; |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
70 } |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
71 if (!valid) { |
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
72 returnValue = false; |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
73 break; |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
74 } else { |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
75 returnValue = true; |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
76 } |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
77 } |
171
7fb9441dd8af
Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents:
116
diff
changeset
|
78 log.debug("Is valid? " + returnValue); |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
79 return returnValue; |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
80 } |
173
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
81 |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
82 |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
83 public Point getPointValue(String value) throws ValidationException{ |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
84 log.debug("InputValidator.getPointValue " + value); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
85 String[] s, p; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
86 |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
87 double x=0,y=0; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
88 log.info("Position :"+value); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
89 s = value.split(" "); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
90 if (s.length != 2) { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
91 throw new ValidationException("Kein Blank separiert Breite und L�nge"); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
92 } |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
93 p = s[0].split("[nNsS]"); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
94 try { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
95 if (p.length == 1) |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
96 y = new Double(p[0]); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
97 else |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
98 y = new Double(p[0]) + new Double(p[1]) / new Double(60.); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
99 if (s[0].toLowerCase().contains("s")) |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
100 y = -y; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
101 } |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
102 catch (Exception e) { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
103 throw new ValidationException("Kein N|S oder nicht im ersten Substring, zB 56n42"); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
104 |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
105 } |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
106 p = s[1].split("[eEwW]"); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
107 try { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
108 if (p.length ==1) |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
109 x = new Double(p[0]); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
110 else |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
111 x = new Double(p[0]) + new Double(p[1]) / new Double(60.) ; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
112 if (s[1].toLowerCase().contains("w")) |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
113 x = -x; |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
114 } |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
115 catch (Exception e) { |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
116 throw new ValidationException("Kein E|W oder nicht im zweiten Substring"); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
117 } |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
118 return new GeometryFactory().createPoint(new Coordinate(x,y)); |
1b2fc94766c9
Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents:
171
diff
changeset
|
119 } |
101
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
120 |
e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
121 } |