package org.datacite.mds.validation.constraints.impl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang.StringUtils;
import org.datacite.mds.util.Utils;
import org.datacite.mds.validation.constraints.Doi;

public class DoiValidator implements ConstraintValidator<Doi, String> {

    public void initialize(Doi constraintAnnotation) {
        // nothing to initialize
    }

    public boolean isValid(String doi, ConstraintValidatorContext context) {
        if (doi == null) {
            return true;
        }
        String prefix = Utils.getDoiPrefix(doi);
        String suffix = Utils.getDoiSuffix(doi);
        return !StringUtils.isEmpty(prefix) && !StringUtils.isEmpty(suffix) && prefix.matches("10\\.(\\d)+")
                && suffix.matches("[^\u0000-\u001F\u0080-\u009F]+") && !suffix.matches("./.*");
    }
}

@@ -684,24 +684,25 @@
 x(doi);%0A
+%0A
         
 return !
@@ -697,16 +697,11 @@
     
-return !
+if(
 Stri
@@ -724,20 +724,19 @@
 prefix) 
-&& !
+%7C%7C 
 StringUt
@@ -754,19 +754,81 @@
 (suffix)
- &&
+)%0A            return false;%0A        %0A        boolean isPrefixOK =
  prefix.
@@ -849,16 +849,17 @@
 (%5C%5Cd)+%22)
+;
 %0A       
@@ -863,18 +863,31 @@
     
-        &&
+boolean hasValidChars =
  suf
@@ -935,35 +935,147 @@
 %5D+%22)
- && !suffix.matches(%22./.*%22)
+;%0A        boolean hasValidSuffixStart = !suffix.matches(%22./.*%22);%0A        %0A        return isPrefixOK && hasValidChars && hasValidSuffixStart
 ;%0A  

increase code readabilty