package estivate.converter;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import estivate.core.Converter;

public class RegexExtractorConvertor implements Converter {

    public static final Map<String, Pattern> PATTERNS = new HashMap<String, Pattern>();

    public boolean canConvert(Object value, Class<?> targetType) {
        return targetType.isAssignableFrom(int.class) || targetType.isAssignableFrom(int[].class)
                || targetType.isAssignableFrom(String.class) || targetType.isAssignableFrom(String[].class);
    }

    public Object convert(Object value, Class<?> targetType, String format) {

        Pattern compile = getPattern(format);
        Matcher matcher = compile.matcher(value.toString());
        if (!matcher.find()) {
            throw new RuntimeException("Expression '" + format + "' cant match '" + value.toString() + "'");
        }

        if (targetType.isAssignableFrom(int.class)) {
            return Integer.parseInt(matcher.group(1));
        }
        if (targetType.isAssignableFrom(int[].class)) {

            int[] result = new int[matcher.groupCount()];

            for (int i = 0; i < matcher.groupCount(); i++) {
                result[i] = Integer.parseInt(matcher.group(i + 1));
            }

            return result;
        }
        if (targetType.isAssignableFrom(String[].class)) {

            String[] result = new String[matcher.groupCount()];

            for (int i = 0; i < matcher.groupCount(); i++) {
                result[i] = matcher.group(i + 1);
            }

            return result;
        }

        return matcher.group(1);
    }

    private Pattern getPattern(String format) {
        Pattern pattern = PATTERNS.get(format);
        if (pattern == null) {
            pattern = Pattern.compile(format);
            PATTERNS.put(format, pattern);
        }
        return pattern;
    }

}

@@ -41,23 +41,76 @@
 va.util.
-HashMap
+ArrayList;%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.List
 ;%0D%0Aimpor
@@ -228,16 +228,148 @@
 erter;%0D%0A
+import lombok.extern.slf4j.Slf4j;%0D%0A%0D%0A/**%0D%0A * Format sample: %22picto-(%5C%5Cw)?%5C%5C.png%22%0D%0A * %0D%0A * @author Benoit Theunissen%0D%0A *%0D%0A */%0D%0A@Slf4j
 %0D%0Apublic
@@ -988,32 +988,34 @@
 String());%0D%0A
+%0D%0A
         
 if (!matcher
@@ -1006,25 +1006,398 @@
     
-if (!matcher.find
+List%3CString%3E matches = new ArrayList%3CString%3E();%0D%0A%0D%0A        while (matcher.find()) %7B%0D%0A            int groupCount = matcher.groupCount();%0D%0A            for (int i = 0; i %3C groupCount; i++) %7B%0D%0A                String current = matcher.group(i + 1);%0D%0A                log.debug(%22matching: %7B%7D%22, current);%0D%0A                matches.add(current);%0D%0A            %7D%0D%0A        %7D%0D%0A        if (matches.isEmpty
 ()) 
@@ -1619,25 +1619,23 @@
 t(matche
-r.group(1
+s.get(0
 ));%0D%0A   
@@ -1742,36 +1742,28 @@
 t%5Bmatche
-r.groupCount
+s.size
 ()%5D;%0D%0A
-%0D%0A
         
@@ -1770,53 +1770,61 @@
     
-for (
 int i
+dx
  = 0;
- i %3C matcher.groupCount(); i++
+%0D%0A            for (String match : matches
 ) %7B%0D
@@ -1844,24 +1844,28 @@
     result%5Bi
+dx++
 %5D = Integer.
@@ -1878,31 +1878,16 @@
 nt(match
-er.group(i + 1)
 );%0D%0A    
@@ -1990,34 +1990,32 @@
 ng%5B%5D.class)) %7B%0D%0A
-%0D%0A
             Stri
@@ -2014,218 +2014,47 @@
     
-String%5B%5D result = new String%5Bmatcher.groupCount()%5D;%0D%0A%0D%0A            for (int i = 0; i %3C matcher.groupCount(); i++) %7B%0D%0A                result%5Bi%5D = matcher.group(i + 1);%0D%0A            %7D%0D%0A%0D%0A            return result
+return matches.toArray(new String%5B%5D %7B%7D)
 ;%0D%0A 
@@ -2090,17 +2090,15 @@
 tche
-r.group(1
+s.get(0
 );%0D%0A

use while matcher.find()