From 0b97ec523b7d8ff4cf3db4f427a9e8c42dfa3492 Mon Sep 17 00:00:00 2001 From: bdc34 Date: Wed, 21 Jul 2010 18:44:34 +0000 Subject: [PATCH] merging URL validation in defaultLinkForm.jsp --- .../edit/n3editing/BasicValidation.java | 39 +++++++++++++------ .../edit/n3editing/BasicValidationTest.java | 29 ++++++++++++++ webapp/web/edit/forms/defaultLinkForm.jsp | 2 +- 3 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidationTest.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java index 6048124e0..596ad7f29 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java @@ -2,6 +2,19 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import com.hp.hpl.jena.datatypes.RDFDatatype; import com.hp.hpl.jena.datatypes.TypeMapper; import com.hp.hpl.jena.ontology.OntModelSpec; @@ -12,13 +25,6 @@ import edu.cornell.mannlib.vitro.webapp.beans.Datatype; import edu.cornell.mannlib.vitro.webapp.dao.jena.DatatypeDaoJena; import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena; -import java.util.*; -import java.util.regex.Pattern; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - /** * User: bdc34 * Date: Jan 24, 2008 @@ -146,7 +152,7 @@ public class BasicValidation { return REQUIRED_FIELD_EMPTY_MSG; } // Format validation - if("isDate".equalsIgnoreCase(validationType)){ + else if("isDate".equalsIgnoreCase(validationType)){ if( isDate( value)) return SUCCESS; else @@ -160,10 +166,16 @@ public class BasicValidation { } else { return errorMsg; } - } - + } else if ("httpUrl".equalsIgnoreCase(validationType)){ + //check if it has http or https, we could do more but for now this is all. + if(! value.startsWith("http://") && ! value.startsWith("https://") ){ + return "This URL must start with http:// or https://"; + }else{ + return SUCCESS; + } + } //Date not past validation - if( "dateNotPast".equalsIgnoreCase(validationType)){ + else if( "dateNotPast".equalsIgnoreCase(validationType)){ //if( ! past (value) ) // return "date must not be in the past"; //Current date @@ -239,6 +251,9 @@ public class BasicValidation { return (value == null || value.trim().length() == 0); } + + + private static Pattern urlRX = Pattern.compile("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?"); /** we use null to indicate success */ public final static String SUCCESS = null; @@ -253,7 +268,7 @@ public class BasicValidation { static final List basicValidations; static{ basicValidations = Arrays.asList( - "nonempty","isDate","dateNotPast" ); + "nonempty","isDate","dateNotPast","httpUrl" ); } private Log log = LogFactory.getLog(BasicValidation.class); diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidationTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidationTest.java new file mode 100644 index 000000000..a524b438a --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidationTest.java @@ -0,0 +1,29 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.edit.n3editing; + +import static org.junit.Assert.*; + +import java.util.Collections; + +import junit.framework.Assert; + +import org.junit.Test; + +public class BasicValidationTest { + + @Test + public void testValidate() { + BasicValidation bv = new BasicValidation(Collections.EMPTY_MAP); + String res; + res = bv.validate("httpUrl", "http://example.com/index"); + Assert.assertEquals(res, bv.SUCCESS); + + res = bv.validate("httpUrl", "http://example.com/index?bogus=skjd%20skljd&something=sdkf"); + Assert.assertEquals(res, bv.SUCCESS); + + res = bv.validate("httpUrl", "http://example.com/index#2.23?bogus=skjd%20skljd&something=sdkf"); + Assert.assertEquals(res, bv.SUCCESS); + } + +} diff --git a/webapp/web/edit/forms/defaultLinkForm.jsp b/webapp/web/edit/forms/defaultLinkForm.jsp index a65a311bc..4fdd1b361 100644 --- a/webapp/web/edit/forms/defaultLinkForm.jsp +++ b/webapp/web/edit/forms/defaultLinkForm.jsp @@ -131,7 +131,7 @@ "fields" : { "url" : { "newResource" : "false", - "validators" : [ "nonempty", "datatype:${uriDatatypeUriJson}" ], + "validators" : [ "nonempty", "datatype:${uriDatatypeUriJson}" , "httpUrl" ], "optionsType" : "UNDEFINED", "literalOptions" : [ ], "predicateUri" : "",