annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 244:9048bc4b17cd

Added ODV-Export to HorizontalProfiles on InstantaneousPoint gnv-artifacts/trunk@313 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 10 Nov 2009 11:48:41 +0000
parents 3e82b4f1c455
children f1e7ddeef5bc
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);
222
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
25
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
26
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
27 public final static String NODATASELECTEDVALUE = "n/n";
101
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 /**
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
30 * Constructor
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
31 */
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
32 public InputValidator() {
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
33 super();
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
34 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
35
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
36 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
37 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
38 boolean returnValue = false;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
39 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
40 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
41 boolean valid;
222
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
42
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
43 if (NODATASELECTEDVALUE.equals(values[i].trim())){
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
44 valid = true;
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
45 } else if ("Integer".equalsIgnoreCase(type)) {
115
158e89c2263b Removed needless imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 113
diff changeset
46 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
47 } 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
48 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
49 } 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
50 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
51 // FIXME:
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
52 // VALIDATE
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
53 // REGEXP
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
54 } 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
55 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
56 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
57 } 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
58 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
59 // FIXME:
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
60 // VALIDATE
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
61 // REGEXP
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
62 } 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
63 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
64 .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
65 // VALIDATE
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
66 // REGEXP
173
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
67 } else if ("Coordinate".equalsIgnoreCase(type)) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
68 try {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
69 valid = this.getPointValue(values[i]) != null;
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
70 } catch (ValidationException e) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
71 log.debug(e.getMessage());
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
72 valid = false;
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
73 }
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 valid = false;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
76 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
77 if (!valid) {
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
78 returnValue = false;
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
79 break;
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
80 } else {
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
81 returnValue = true;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
82 }
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
83 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
84 log.debug("Is valid? " + returnValue);
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
85 return returnValue;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
86 }
173
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
87
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
88
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
89 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
90 log.debug("InputValidator.getPointValue " + value);
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
91 String[] s, p;
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 double x=0,y=0;
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
94 log.info("Position :"+value);
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
95 s = value.split(" ");
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
96 if (s.length != 2) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
97 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
98 }
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
99 p = s[0].split("[nNsS]");
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
100 try {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
101 if (p.length == 1)
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
102 y = new Double(p[0]);
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
103 else
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
104 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
105 if (s[0].toLowerCase().contains("s"))
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
106 y = -y;
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
107 }
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
108 catch (Exception e) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
109 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
110
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
111 }
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
112 p = s[1].split("[eEwW]");
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
113 try {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
114 if (p.length ==1)
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
115 x = new Double(p[0]);
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
116 else
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
117 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
118 if (s[1].toLowerCase().contains("w"))
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
119 x = -x;
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
120 }
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
121 catch (Exception e) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
122 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
123 }
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
124 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
125 }
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
126
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
127 }

http://dive4elements.wald.intevation.org