Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java @ 799:feeaf5aec552
ISSUE213: Wrong Geometrytype used for the generation of an Layer with Multipolygon-Geometries
gnv-artifacts/trunk@881 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 06 Apr 2010 11:56:53 +0000 |
parents | 6cd8492019d8 |
children | feae2f9d6c6f |
line wrap: on
line source
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(); } 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; } public static boolean isInputValid(String input, String type) { 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; } 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)); } } public static boolean isDateValid(Date tmp, Date lo, Date up) { // we need to transform the given dates into seconds, because // they differ in milliseconds -> that's why we cannot use the // Date.compareTo(Date) method. long tmpTime = tmp.getTime() / 1000; long tmpLow = lo.getTime() / 1000; long tmpUp = up.getTime() / 1000; if (tmpTime < tmpLow || tmpTime > tmpUp) { log.warn( "Date [" + tmp.toString() + "] is out of range [" + lo.toString() + " to "+ up.toString() + "]."); return false; } return true; } }