[cvs] Expresso commit by rauld: Added validateURL and isURL methods.
JCorporate Ltd
jcorp at jcorp2.servlets.net
Sun Oct 10 15:01:01 PDT 2004
Log Message:
-----------
Added validateURL and isURL methods.
Thanks to Chris Di Giano
Modified Files:
--------------
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/validation:
ExpressoFieldChecks.java
Revision Data
-------------
Index: ExpressoFieldChecks.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/validation/ExpressoFieldChecks.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/validation/ExpressoFieldChecks.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/validation/ExpressoFieldChecks.java -u -r1.2 -r1.3
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/validation/ExpressoFieldChecks.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/validation/ExpressoFieldChecks.java
@@ -68,6 +68,8 @@
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
+import java.net.URI;
+import java.net.URISyntaxException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -909,6 +911,70 @@
}
/**
+ * Checks if a field is a valid url. Does not currently
+ * honor Struts URL checking options such as allowallschemes
+ * and nofragment.
+ *
+ * @param bean
+ * The bean validation is being performed on.
+ * @param va
+ * The <code>ValidatorAction</code> that is currently being
+ * performed.
+ * @param field
+ * The <code>Field</code> object associated with the current
+ * field being validated.
+ * @param errors
+ * The <code>ActionErrors</code> object to add errors to if any
+ * validation errors occur.
+ * @param request
+ * Current request object.
+ * @return True if valid, false otherwise.
+ *
+ * @author Chris DiGiano
+ */
+ public static boolean validateURL(Object bean, ValidatorAction va,
+ Field field, ActionErrors errors,
+ HttpServletRequest request) {
+
+ String value = null;
+ if (isString(bean)) {
+ value = (String) bean;
+ }
+ else {
+ value = getValueAsString(bean, field.getProperty());
+ }
+
+ if (!GenericValidator.isBlankOrNull(value) && !isURL(value)) {
+ errors.add(field.getKey(), Resources.getActionError(
+ request, va, field));
+ return false;
+ }
+ else {
+ return true;
+ }
+ }
+
+ /**
+ * Determine if the given <code>value</code> is formatted as a proper URL.
+ *
+ * @param value
+ * @return
+ *
+ * @author Chris DiGiano
+ */
+ private static boolean isURL(String value) {
+ try {
+ URI uri = new URI(value);
+ }
+ catch (URISyntaxException e) {
+ return false;
+ }
+ return true;
+ }
+
+//Utility methods
+
+ /**
* Return <code>true</code> if the specified object is a String or a <code>null</code>
* value.
*
@@ -929,13 +995,14 @@
if (bean instanceof DefaultForm) {
abean = (DefaultForm) bean;
try {
- value = abean.getAttribute(property) != null ? abean.getAttribute(property) : abean.getField(property);
+ value = abean.getAttribute(property) != null ?
+ abean.getAttribute(property) : abean.getField(property);
}
catch (ControllerException ex) {
log.error(ex.getMessage(), ex);
}
}
- else{
+ else {
try {
value = PropertyUtils.getProperty(bean, property);
}
More information about the cvs
mailing list