Mercurial > dive4elements > gnv-client
changeset 101:e01c163ea5b0
Inputvalidation for String, Integer and Date Values added.
gnv-artifacts/trunk@149 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 29 Sep 2009 09:50:25 +0000 (2009-09-29) |
parents | f07637f96312 |
children | 994a39b084d6 |
files | gnv-artifacts/Changelog gnv-artifacts/pom.xml gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java |
diffstat | 4 files changed, 80 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/gnv-artifacts/Changelog Mon Sep 28 14:35:35 2009 +0000 +++ b/gnv-artifacts/Changelog Tue Sep 29 09:50:25 2009 +0000 @@ -1,3 +1,14 @@ +2009-09-29 Tim Englich <tim.englich@intevation.de> + + * pom.xml Edited: + Added the dependency to Apache-Commons-Validator Version 1.3.1 which is + used for the InputValoue-validation. + * src/main/java/de/intevation/gnv/utils/InputValidator.java Added : + An InputValidator for Validating InputValues. + At this Moment only Integer, String and Date is supported. + * src/main/java/de/intevation/gnv/transition/TransitionBase.java (putInputData) Edited: + Add the InputValidator to validate the incomming Values from the Clients. + 2009-09-28 Tim Englich <tim.englich@intevation.de> * src/main/java/de/intevation/gnv/chart/ChartFactory.java (createTimeSeries) Edited:
--- a/gnv-artifacts/pom.xml Mon Sep 28 14:35:35 2009 +0000 +++ b/gnv-artifacts/pom.xml Tue Sep 29 09:50:25 2009 +0000 @@ -67,6 +67,10 @@ <artifactId>ehcache</artifactId> <version>1.6.2</version> </dependency> - + <dependency> + <groupId>commons-validator</groupId> + <artifactId>commons-validator</artifactId> + <version>1.3.1</version> + </dependency> </dependencies> </project>
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java Mon Sep 28 14:35:35 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java Tue Sep 29 09:50:25 2009 +0000 @@ -35,6 +35,7 @@ import de.intevation.gnv.transition.describedata.SingleValueDescribeData; import de.intevation.gnv.transition.exception.TransitionException; import de.intevation.gnv.utils.ArtifactXMLUtilities; +import de.intevation.gnv.utils.InputValidator; /** * @author Tim Englich <tim.englich@intevation.de> @@ -184,6 +185,7 @@ log.debug("TransitionBase.putInputData"); if (inputData != null){ Iterator<InputData> it = inputData.iterator(); + InputValidator iv = new InputValidator(); while(it.hasNext()){ InputData tmpItem = it.next(); InputValue inputValue = this.inputValues.get(tmpItem.getName()); @@ -191,8 +193,15 @@ if (this.inputData == null){ this.inputData = new HashMap<String,InputData>(inputData.size()); } - this.setSelection(tmpItem); - this.inputData.put(tmpItem.getName(),tmpItem); + boolean valid = iv.isInputValid(tmpItem.getValue(), inputValue.getType()); + if (valid){ + this.setSelection(tmpItem); + this.inputData.put(tmpItem.getName(),tmpItem); + }else{ + String errMsg = "Wrong input for "+tmpItem.getValue()+"is not an "+inputValue.getType()+" Value."; + log.warn(errMsg); + throw new TransitionException(errMsg); + } }else{ String errMsg = "No Inputvalue given for Inputdata "+ tmpItem.getName();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java Tue Sep 29 09:50:25 2009 +0000 @@ -0,0 +1,53 @@ +/** + * + */ +package de.intevation.gnv.utils; + +import org.apache.log4j.Logger; + +import de.intevation.gnv.geobackend.util.DateUtils; + +/** + * @author Tim Englich <tim.englich@intevation.de> + * + */ +public class InputValidator { + /** + * the logger, used to log exceptions and additonaly information + */ + private static Logger log = Logger.getLogger(InputValidator.class); + + /** + * Constructor + */ + public InputValidator() { + super(); + } + + public boolean isInputValid(String input, String type){ + log.debug("InputValidator.isInputValid " + input+ " "+type); + boolean returnValue = false; + String[] values = input.split(","); + for (int i = 0; i < values.length; i++){ + boolean valid; + if ("Integer".equalsIgnoreCase(type)){ + valid = org.apache.commons.validator.GenericValidator.isInt(values[i]); + }else if ("String".equalsIgnoreCase(type)){ + valid = org.apache.commons.validator.GenericValidator.matchRegexp(values[i], "[a-zA-Z0-9]"); + }else if ("Date".equalsIgnoreCase(type)){ + valid = org.apache.commons.validator.GenericValidator.isDate(values[i], DateUtils.DATE_PATTERN, true); + }else{ + valid = false; + } + if (!valid){ + returnValue = false; + break; + }else{ + returnValue = true; + } + } + log.debug("Is valid? "+returnValue); + return returnValue; + } + +}