Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 1129:ccfa07b88476
merged geo-backend
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:01 +0200 |
parents | f953c9a559d8 |
children |
line wrap: on
line source
/* * Copyright (c) 2010 by Intevation GmbH * * This program is free software under the LGPL (>=v2.1) * Read the file LGPL.txt coming with the software for details * or visit http://www.gnu.org/licenses/ if it does not exist. */ package de.intevation.gnv.utils; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; import de.intevation.gnv.geobackend.util.DateUtils; import de.intevation.gnv.utils.exception.ValidationException; import java.util.Date; import org.apache.commons.validator.GenericValidator; import org.apache.log4j.Logger; /** * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> * */ 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(); } /** * Validates the input of a range of double or date values. The input values * need to be valid double or date values. <i>minInput</i> needs to be * smaller or equal <i>maxInput</i>. * * @param minInput The lower bound. * @param maxInput The upper bound. * @param type One of 'Date' or 'Double'. * @return true, if the input is valid, otherwise false. */ public static 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; } /** * Validates an input. * * @param input The input value. * @param type The input value type. * @return true if the input is valid, otherwise false. */ public static boolean isInputValid(String input, String type) { if (input.length() == 0 || input.equals("")) { return false; } log.debug("InputValidator.isInputValid " + input + " " + type); // Let's check polygons and linestrings first, because they might // contain comma. A splitting at comma characters wouldn't be good here. if ("Polygon".equalsIgnoreCase(type) || "Linestring".equalsIgnoreCase(type)) { try { WKTReader reader = new WKTReader(); reader.read(input); return true; } catch (ParseException pe) { log.warn(pe, pe); return false; } } // Check all the other input here 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) || "Geometry".equals(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 = 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; } /** * Returns a point from wkt string. * * @param value The wkt string. * @return a point. * @throws ValidationException if <i>value</i> is not valid. */ public static Point getPointValue(String value) throws ValidationException{ log.debug("InputValidator.getPointValue " + value); if (value.toLowerCase().startsWith("point")){ try { return (Point)new WKTReader().read(value); } catch (ParseException e) { log.error(e,e); throw new ValidationException(e); } }else{ 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)); } } /** * Makes sure that <i>tmp</i> is between <i>lo</i> and <i>up</i>. * * @param tmp The value to validate. * @param lo The lower range bound. * @param up The upper range bound. * @return true, if tmp is valid, otherwise false. */ public static boolean isDateValid(Date tmp, Date lo, Date up) { // take the time in seconds to compare long tmpTime = tmp.getTime() / 1000; long tmpLow = lo.getTime() / 1000; long tmpUp = up.getTime() / 1000; if (log.isDebugEnabled()) { log.debug("Date validation..."); log.debug("-> lower bound [sec]: " + tmpLow); log.debug("-> upper bound [sec]: " + tmpUp); log.debug("-> input data [sec]: " + tmpTime); } // XXX There is a buffer of 1 second for the valid range of the time. if (tmpTime < (tmpLow-1) || tmpTime > (tmpUp+1)) { log.warn( "Date [" + tmp.toString() + "] is out of range [" + lo.toString() + " to "+ up.toString() + "]."); return false; } return true; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :