view gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 510:9e5ed3eebf29

Fixed Compiler-Error after adding further Methods to the Interface ArtifactDatabase. gnv-artifacts/trunk@596 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 22 Jan 2010 10:32:49 +0000
parents 3ddc22aab764
children 1efb6a66c7d9
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.utils;

import java.util.Date;

import org.apache.commons.validator.GenericValidator;
import org.apache.log4j.Logger;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

import de.intevation.gnv.geobackend.util.DateUtils;
import de.intevation.gnv.utils.exception.ValidationException;

/**
 * @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);
    
    
    public final static String NODATASELECTEDVALUE = "n/n";

    /**
     * Constructor
     */
    public InputValidator() {
        super();
    }

    public boolean isInputValid(String minInput, String maxInput, String type) {
        log.debug("InputValidator.isInputValid " + minInput + " " + maxInput + " " +type);
        boolean returnValue = false;
        if ("Date".equalsIgnoreCase(type)) {
            try {
                Date min = DateUtils.getDateFromString(minInput,DateUtils.DATE_PATTERN);
                Date max = DateUtils.getDateFromString(maxInput,DateUtils.DATE_PATTERN);
                int value = max.compareTo(min);
                returnValue = value >= 0;
            } catch (Exception e) {
                log.error(e,e);
            }
        } else if ("Double".equalsIgnoreCase(type)) {
            try {
                double min = Double.parseDouble(minInput);
                double max = Double.parseDouble(maxInput);
                returnValue = max >= min;
            } catch (Exception e) {
                log.error(e,e);
            }
        }
        log.debug("Is valid? " + returnValue);
        return returnValue;
    }
    
    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 (NODATASELECTEDVALUE.equals(values[i].trim())){
                valid = true;
            } else if ("Integer".equalsIgnoreCase(type)) {
                valid = GenericValidator.isInt(values[i].trim());
            } else if ("Double".equalsIgnoreCase(type)) {
                valid = GenericValidator.isDouble(values[i].trim());
            } else if ("String".equalsIgnoreCase(type)) {
                valid = GenericValidator.matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO:
                                                                                // FIXME:
                                                                                // VALIDATE
                                                                                // REGEXP
            } else if ("Date".equalsIgnoreCase(type)) {
                valid = GenericValidator.isDate(values[i].trim(),
                        DateUtils.DATE_PATTERN, true);
            } else if ("Point".equalsIgnoreCase(type)) {
                valid = GenericValidator.matchRegexp(values[i], "[0-9]"); // TODO:
                                                                          // FIXME:
                                                                          // VALIDATE
                                                                          // REGEXP
            } else if ("AttributeName".equalsIgnoreCase(type)) {
                valid = org.apache.commons.validator.GenericValidator
                        .matchRegexp(values[i], "[a-zA-Z0-9]"); // TODO: FIXME:
                                                                // VALIDATE
                                                                // REGEXP
            } else if ("Coordinate".equalsIgnoreCase(type)) {
                    try {
                        valid = this.getPointValue(values[i]) != null;
                    } catch (ValidationException e) {
                        log.debug(e.getMessage());
                        valid = false;
                    }
            } else {
                valid = false;
            }
            if (!valid) {
                returnValue = false;
                break;
            } else {
                returnValue = true;
            }
        }
        log.debug("Is valid? " + returnValue);
        return returnValue;
    }
    
    
    public Point getPointValue(String value) throws ValidationException{
        log.debug("InputValidator.getPointValue " + value);
        String[] s, p;
        
        double x=0,y=0;
        log.info("Position :"+value);
        s = value.split(" ");
        if (s.length != 2) {
              throw new ValidationException("Kein Blank separiert Breite und Länge");
        }
        p = s[0].split("[nNsS]");
        try {
            if (p.length == 1)
                y = new Double(p[0]);
            else
                y = new Double(p[0]) +   new Double(p[1]) / new Double(60.);
            if (s[0].toLowerCase().contains("s"))
                y = -y;
        }
        catch (Exception e) {
            throw new ValidationException("Kein N|S oder nicht im ersten Substring, zB 56n42");
        
        } 
        p = s[1].split("[eEwW]");
        try {
            if (p.length ==1)
                x = new Double(p[0]);
            else
                x = new Double(p[0]) +  new Double(p[1]) / new Double(60.) ;
            if (s[1].toLowerCase().contains("w"))
                x = -x;
        }
        catch (Exception e) {
            throw new ValidationException("Kein E|W oder nicht im zweiten Substring");
        } 
        return new GeometryFactory().createPoint(new Coordinate(x,y));
    }

}

http://dive4elements.wald.intevation.org