package uk.org.sappho.validation;

import java.util.List;

import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.FieldContext;
import net.sf.oval.context.OValContext;
import net.sf.oval.exception.OValException;

public class StringArrayCheck extends AbstractAnnotationCheck<StringArrayConstraint> {

    private static final long serialVersionUID = 5510376426614007473L;

    @SuppressWarnings("unchecked")
    public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator)
            throws OValException {

        String error = null;
        List<String> array = null;
        try {
            array = (List<String>) valueToValidate;
        } catch (Throwable t) {
            // catch null or wrong type
        }
        if (array == null)
            error = "is missing or invalid";
        else
            for (String item : array) {
                boolean invalid = true;
                try {
                    invalid = item.length() == 0;
                } catch (Throwable t) {
                    // catch null or wrong type
                }
                if (invalid)
                    error = "contains null or blank string";
            }
        if (error != null) {
            if (context instanceof FieldContext) {
                FieldContext fieldContext = (FieldContext) context;
                error = fieldContext.getField().getDeclaringClass().getName() + "." + fieldContext.getField().getName()
                        + " " + error;
            }
            setMessage(error);
        }
        return error == null;
    }
}

@@ -807,35 +807,40 @@
     
-// catch null or wrong type
+error = %22is missing or invalid%22;
 %0A   
@@ -864,17 +864,17 @@
  (array 
-=
+!
 = null)%0A
@@ -889,54 +889,18 @@
     
-error = %22is missing or invalid%22;%0A        else%0A
+try %7B%0A    
     
@@ -955,38 +955,38 @@
     
-boolean invalid = true;%0A      
+    if (item.length() == 0) %7B%0A
     
@@ -983,38 +983,32 @@
  0) %7B%0A          
-try %7B%0A
                 
@@ -1009,131 +1009,106 @@
     
-      invalid = item.length() == 0;%0A                %7D catch (Throwable t) %7B%0A                    // catch null or wrong type
+error = %22contains at least one blank string%22;%0A                        break;%0A                    %7D
 %0A   
@@ -1138,29 +1138,32 @@
     
-    if (invalid)%0A    
+%7D catch (Throwable t) %7B%0A
     
@@ -1200,28 +1200,28 @@
 null or 
-blank string
+invalid data
 %22;%0A     

Validate string arrays.