annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 1028:41234fcf4c7e

Some Bugfixes which prevent Exceptions executing the Examples in the config-manual gnv-artifacts/trunk@1052 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 04 May 2010 12:40:21 +0000
parents f94a95009423
children 8f836fb6f592
rev   line source
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
1 package de.intevation.gnv.utils;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
2
779
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
3 import com.vividsolutions.jts.geom.Coordinate;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
4 import com.vividsolutions.jts.geom.GeometryFactory;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
5 import com.vividsolutions.jts.geom.Point;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
6
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
7 import com.vividsolutions.jts.io.ParseException;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
8 import com.vividsolutions.jts.io.WKTReader;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
9
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
10 import de.intevation.gnv.geobackend.util.DateUtils;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
11
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
12 import de.intevation.gnv.utils.exception.ValidationException;
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
13
252
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
14 import java.util.Date;
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
15
116
820238357bab Added Horizontal-Profile-Support for Meshes.
Tim Englich <tim.englich@intevation.de>
parents: 115
diff changeset
16 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
17
779
b1f5f2a8840f Ordered imports. Removed needless imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 778
diff changeset
18 import org.apache.log4j.Logger;
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 /**
780
c4156275c1e1 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 779
diff changeset
21 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
785
e764cab8c885 Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 784
diff changeset
22 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
23 *
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
24 */
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
25 public class InputValidator {
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 * 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
28 */
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
29 private static Logger log = Logger.getLogger(InputValidator.class);
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
30
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
31
222
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
32 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
33
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
34 /**
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
35 * Constructor
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
36 */
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
37 public InputValidator() {
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
38 super();
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
39 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
40
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
41 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
42 * Validates the input of a range of double or date values. The input values
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
43 * need to be valid double or date values. <i>minInput</i> needs to be
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
44 * smaller or equal <i>maxInput</i>.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
45 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
46 * @param minInput The lower bound.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
47 * @param maxInput The upper bound.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
48 * @param type One of 'Date' or 'Double'.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
49 * @return true, if the input is valid, otherwise false.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
50 */
785
e764cab8c885 Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 784
diff changeset
51 public static boolean isInputValid(String minInput, String maxInput, String type) {
252
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
52 log.debug("InputValidator.isInputValid " + minInput + " " + maxInput + " " +type);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
53 boolean returnValue = false;
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
54 if ("Date".equalsIgnoreCase(type)) {
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
55 try {
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
56 Date min = DateUtils.getDateFromString(minInput,DateUtils.DATE_PATTERN);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
57 Date max = DateUtils.getDateFromString(maxInput,DateUtils.DATE_PATTERN);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
58 int value = max.compareTo(min);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
59 returnValue = value >= 0;
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
60 } catch (Exception e) {
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
61 log.error(e,e);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
62 }
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
63 } else if ("Double".equalsIgnoreCase(type)) {
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
64 try {
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
65 double min = Double.parseDouble(minInput);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
66 double max = Double.parseDouble(maxInput);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
67 returnValue = max >= min;
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
68 } catch (Exception e) {
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
69 log.error(e,e);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
70 }
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
71 }
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
72 log.debug("Is valid? " + returnValue);
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
73 return returnValue;
f1e7ddeef5bc Added Validation if a given maxvalue is greater than a given minvalue issue19
Tim Englich <tim.englich@intevation.de>
parents: 222
diff changeset
74 }
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
75
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
76 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
77 * Validates an input.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
78 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
79 * @param input The input value.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
80 * @param type The input value type.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
81 * @return true if the input is valid, otherwise false.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
82 */
785
e764cab8c885 Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 784
diff changeset
83 public static boolean isInputValid(String input, String type) {
860
f94a95009423 Improved the input validator: empty strings are invalid (issue251).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
84 if (input.length() == 0 || input.equals("")) {
f94a95009423 Improved the input validator: empty strings are invalid (issue251).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
85 return false;
f94a95009423 Improved the input validator: empty strings are invalid (issue251).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
86 }
f94a95009423 Improved the input validator: empty strings are invalid (issue251).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 836
diff changeset
87
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
88 log.debug("InputValidator.isInputValid " + input + " " + type);
787
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
89
803
feae2f9d6c6f Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 787
diff changeset
90 // Let's check polygons and linestrings first, because they might
787
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
91 // contain comma. A splitting at comma characters wouldn't be good here.
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
92 if ("Polygon".equalsIgnoreCase(type) || "Linestring".equalsIgnoreCase(type))
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
93 {
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
94 try {
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
95 WKTReader reader = new WKTReader();
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
96 reader.read(input);
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
97
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
98 return true;
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
99 }
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
100 catch (ParseException pe) {
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
101 log.warn(pe, pe);
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
102 return false;
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
103 }
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
104 }
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
105
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
106 // Check all the other input here
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
107 boolean returnValue = false;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
108 String[] values = input.split(",");
787
6cd8492019d8 Validate wkt string and display error messages if the validation failed (issue214).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 785
diff changeset
109
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
110 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
111 boolean valid;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
112
222
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
113 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
114 valid = true;
3e82b4f1c455 Removed some Compilation-Errors according to API-Changes in the ArtifactDatabase.
Tim Englich <tim.englich@intevation.de>
parents: 173
diff changeset
115 } else if ("Integer".equalsIgnoreCase(type)) {
115
158e89c2263b Removed needless imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 113
diff changeset
116 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
117 } 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
118 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
119 } 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
120 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
121 // FIXME:
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
122 // VALIDATE
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
123 // REGEXP
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
124 } 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
125 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
126 DateUtils.DATE_PATTERN, true);
731
1efb6a66c7d9 Added Objecttype Geometry for the Validation of Inputdata.
Tim Englich <tim.englich@intevation.de>
parents: 461
diff changeset
127 } else if ("Point".equalsIgnoreCase(type) || "Geometry".equals(type)) {
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
128 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
129 // FIXME:
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
130 // VALIDATE
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
131 // REGEXP
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
132 } 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
133 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
134 .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
135 // VALIDATE
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
136 // REGEXP
173
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
137 } else if ("Coordinate".equalsIgnoreCase(type)) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
138 try {
785
e764cab8c885 Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 784
diff changeset
139 valid = getPointValue(values[i]) != null;
173
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
140 } catch (ValidationException e) {
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
141 log.debug(e.getMessage());
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
142 valid = false;
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
143 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
144 } else {
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
145 valid = false;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
146 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
147 if (!valid) {
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
148 returnValue = false;
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
149 break;
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
150 } else {
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
151 returnValue = true;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
152 }
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
153 }
171
7fb9441dd8af Format Code to max 80 Chars per Row and Cleanup
Tim Englich <tim.englich@intevation.de>
parents: 116
diff changeset
154 log.debug("Is valid? " + returnValue);
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
155 return returnValue;
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
156 }
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
157
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
158
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
159 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
160 * Returns a point from wkt string.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
161 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
162 * @param value The wkt string.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
163 * @return a point.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
164 * @throws ValidationException if <i>value</i> is not valid.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
165 */
785
e764cab8c885 Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 784
diff changeset
166 public static Point getPointValue(String value) throws ValidationException{
173
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
167 log.debug("InputValidator.getPointValue " + value);
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
168
746
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
169 if (value.toLowerCase().startsWith("point")){
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
170 try {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
171 return (Point)new WKTReader().read(value);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
172 } catch (ParseException e) {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
173 log.error(e,e);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
174 throw new ValidationException(e);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
175 }
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
176 }else{
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
177 String[] s, p;
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
178
746
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
179 double x=0,y=0;
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
180 log.info("Position :"+value);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
181 s = value.split(" ");
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
182 if (s.length != 2) {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
183 throw new ValidationException("Kein Blank separiert Breite und Länge");
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
184 }
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
185 p = s[0].split("[nNsS]");
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
186 try {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
187 if (p.length == 1)
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
188 y = new Double(p[0]);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
189 else
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
190 y = new Double(p[0]) + new Double(p[1]) / new Double(60.);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
191 if (s[0].toLowerCase().contains("s"))
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
192 y = -y;
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
193 }
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
194 catch (Exception e) {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
195 throw new ValidationException("Kein N|S oder nicht im ersten Substring, zB 56n42");
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
196
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
197 }
746
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
198 p = s[1].split("[eEwW]");
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
199 try {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
200 if (p.length ==1)
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
201 x = new Double(p[0]);
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
202 else
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
203 x = new Double(p[0]) + new Double(p[1]) / new Double(60.) ;
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
204 if (s[1].toLowerCase().contains("w"))
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
205 x = -x;
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
206 }
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
207 catch (Exception e) {
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
208 throw new ValidationException("Kein E|W oder nicht im zweiten Substring");
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 746
diff changeset
209 }
746
b81f6f896668 Integrated handling of WKT-Strings for Points to extract Points from a given Inputstring.
Tim Englich <tim.englich@intevation.de>
parents: 731
diff changeset
210 return new GeometryFactory().createPoint(new Coordinate(x,y));
173
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
211 }
1b2fc94766c9 Added real Coordinate-Query to TimeSeries-Mesh
Tim Englich <tim.englich@intevation.de>
parents: 171
diff changeset
212 }
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
213
784
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
214
806
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
215 /**
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
216 * Makes sure that <i>tmp</i> is between <i>lo</i> and <i>up</i>.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
217 *
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
218 * @param tmp The value to validate.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
219 * @param lo The lower range bound.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
220 * @param up The upper range bound.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
221 * @return true, if tmp is valid, otherwise false.
2cea76f1112e Added Javadoc in utils package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 803
diff changeset
222 */
785
e764cab8c885 Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 784
diff changeset
223 public static boolean isDateValid(Date tmp, Date lo, Date up) {
784
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
224 // we need to transform the given dates into seconds, because
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
225 // they differ in milliseconds -> that's why we cannot use the
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
226 // Date.compareTo(Date) method.
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
227 long tmpTime = tmp.getTime() / 1000;
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
228 long tmpLow = lo.getTime() / 1000;
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
229 long tmpUp = up.getTime() / 1000;
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
230
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
231 if (tmpTime < tmpLow || tmpTime > tmpUp) {
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
232 log.warn(
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
233 "Date [" + tmp.toString() + "] is out of range ["
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
234 + lo.toString() + " to "+ up.toString() + "].");
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
235 return false;
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
236 }
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
237
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
238 return true;
7b81d563c8c0 Improved date input validation. A given date needs to be in a specific range (issue212).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 780
diff changeset
239 }
101
e01c163ea5b0 Inputvalidation for String, Integer and Date Values added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
240 }
836
05bf8534a35a Using unix line endings only.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 806
diff changeset
241 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org