struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [09/13] struts git commit: WW-4578 Cleans up code
Date Wed, 19 Apr 2017 07:42:23 GMT
WW-4578 Cleans up code


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/1ad6d2c3
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/1ad6d2c3
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/1ad6d2c3

Branch: refs/heads/master
Commit: 1ad6d2c31726864275458c02f9cbbe22936f7833
Parents: 569e1f6
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Fri Apr 14 11:25:22 2017 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Fri Apr 14 11:25:22 2017 +0200

----------------------------------------------------------------------
 .../validators/RegexFieldValidator.java         | 24 ++++++++--------
 .../validators/RequiredStringValidator.java     | 29 ++++++++++++--------
 .../validators/StringLengthFieldValidator.java  | 13 ++++++---
 .../validator/validators/URLValidator.java      |  6 ++--
 4 files changed, 39 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/1ad6d2c3/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
index b1ca900..eff835c 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
@@ -95,9 +95,9 @@ public class RegexFieldValidator extends FieldValidatorSupport {
     private String regex;
     private String regexExpression;
     private Boolean caseSensitive = true;
-    private String caseSensitiveExpression = "";
+    private String caseSensitiveExpression = EMPTY_STRING;
     private Boolean trim = true;
-    private String trimExpression = "";
+    private String trimExpression = EMPTY_STRING;
 
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
@@ -108,32 +108,30 @@ public class RegexFieldValidator extends FieldValidatorSupport {
         LOG.debug("Defined regexp as [{}]", regexToUse);
 
         if (value == null || regexToUse == null) {
+            LOG.debug("Either value is empty (please use a required validator) or regex is
empty");
             return;
         }
 
-        if (value instanceof String) {
-            validateFieldValue(object, fieldName, (String) value, regexToUse);
-        }
-
         if (value.getClass().isArray()) {
             Object[] values = (Object[]) value;
             for (Object objValue: values) {
-                validateFieldValue(object, fieldName, Objects.toString(objValue, EMPTY_STRING),
regexToUse);
+                validateFieldValue(object, Objects.toString(objValue, EMPTY_STRING), regexToUse);
             }
-        }
-
-        if (Collection.class.isAssignableFrom(value.getClass())) {
-            Collection<Object> values = (Collection<Object>) value;
+        } else if (Collection.class.isAssignableFrom(value.getClass())) {
+            Collection values = (Collection) value;
             for (Object objValue : values) {
-                validateFieldValue(object, fieldName, Objects.toString(objValue, EMPTY_STRING),
regexToUse);
+                validateFieldValue(object, Objects.toString(objValue, EMPTY_STRING), regexToUse);
             }
+        } else {
+           validateFieldValue(object, Objects.toString(value, EMPTY_STRING), regexToUse);
         }
     }
 
-    protected void validateFieldValue(Object object, String fieldName, String value, String
regexToUse) {
+    protected void validateFieldValue(Object object, String value, String regexToUse) {
         // string must not be empty
         String str = value.trim();
         if (str.length() == 0) {
+            LOG.debug("Value is empty, please use a required validator");
             return;
         }
 

http://git-wip-us.apache.org/repos/asf/struts/blob/1ad6d2c3/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
index 433fd60..d11ff4e 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RequiredStringValidator.java
@@ -109,23 +109,28 @@ public class RequiredStringValidator extends FieldValidatorSupport {
     }
 
     protected void validateValue(Object object, Object fieldValue) {
-        if (fieldValue == null) {
-            addFieldError(getFieldName(), object);
-            return;
-        }
+        try {
+            setCurrentValue(fieldValue);
+            if (fieldValue == null) {
+                addFieldError(getFieldName(), object);
+                return;
+            }
 
-        if (fieldValue instanceof String) {
-            String stingValue = (String) fieldValue;
+            if (fieldValue instanceof String) {
+                String stingValue = (String) fieldValue;
 
-            if (trim) {
-                stingValue = stingValue.trim();
-            }
+                if (trim) {
+                    stingValue = stingValue.trim();
+                }
 
-            if (stingValue.length() == 0) {
+                if (stingValue.length() == 0) {
+                    addFieldError(getFieldName(), object);
+                }
+            } else {
                 addFieldError(getFieldName(), object);
             }
-        } else {
-            addFieldError(getFieldName(), object);
+        } finally {
+            setCurrentValue(null);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/struts/blob/1ad6d2c3/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
index 0c4f0fc..2e1957d 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
@@ -183,10 +183,15 @@ public class StringLengthFieldValidator extends FieldValidatorSupport
{
         int minLengthToUse = getMinLength();
         int maxLengthToUse = getMaxLength();
 
-        if ((minLengthToUse > -1) && (stringValue.length() < minLengthToUse))
{
-            addFieldError(fieldName, object);
-        } else if ((maxLengthToUse > -1) && (stringValue.length() > maxLengthToUse))
{
-            addFieldError(fieldName, object);
+        try {
+            setCurrentValue(stringValue);
+            if ((minLengthToUse > -1) && (stringValue.length() < minLengthToUse))
{
+                addFieldError(fieldName, object);
+            } else if ((maxLengthToUse > -1) && (stringValue.length() > maxLengthToUse))
{
+                addFieldError(fieldName, object);
+            }
+        } finally {
+            setCurrentValue(null);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/struts/blob/1ad6d2c3/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
index 6dd29f7..eab0fa8 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
@@ -69,9 +69,7 @@ public class URLValidator extends FieldValidatorSupport {
     public void validate(Object object) throws ValidationException {
         Object value = getFieldValue(fieldName, object);
 
-        // if there is no value - don't do comparison
-        // if a value is required, a required validator should be added to the field
-        String stringValue = Objects.toString(value, "").trim();
+        String stringValue = Objects.toString(value, EMPTY_STRING).trim();
         if (stringValue.length() == 0) {
             LOG.debug("Value for field {} is empty, won't ba validated, please use a required
validator", fieldName);
             return;
@@ -96,7 +94,7 @@ public class URLValidator extends FieldValidatorSupport {
     }
 
     protected void validateValue(Object object, Object value) {
-        String stringValue = Objects.toString(value, "").trim();
+        String stringValue = Objects.toString(value, EMPTY_STRING).trim();
         if (stringValue.length() == 0) {
             LOG.debug("Value for field {} is empty, won't ba validated, please use a required
validator", fieldName);
             return;


Mime
View raw message