view gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 174:dbf68950b4d9

Added real Coordinate-Query to HorizontalProfile- and VerticalProfile-Mesh gnv-artifacts/trunk@213 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 13 Oct 2009 10:17:46 +0000
parents 1b2fc94766c9
children 3e82b4f1c455
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.utils;

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);

    /**
     * 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 = 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