package fr.univ.nantes.extensiblespud.parser;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

/**
 * @param <T>
 * @author Nina Exposito
 * @author Alexis Giraudet
 * @author Jean-Christophe Guérin
 * @author Jasone Lenormand
 */
public abstract class PropertiesParser<T> implements Parser<T> {
    private Class<T> tClass;

    /**
     * @param tClass
     */
    protected PropertiesParser(Class<T> tClass) {
        this.tClass = tClass;
    }

    /**
     * @param inputStream
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws IOException
     * @throws InvocationTargetException
     */
    public T parse(InputStream inputStream) throws IllegalAccessException, InstantiationException, IOException, InvocationTargetException {
        T t = tClass.newInstance();
        Properties properties = new Properties();

        properties.load(inputStream);
        for (String key : properties.stringPropertyNames()) {
            for (Method method : tClass.getMethods()) {
                StringBuilder methodName = new StringBuilder("set");
                if (key.length() > 0) {
                    methodName.append(Character.toUpperCase(key.charAt(0)));
                    if (key.length() > 1) {
                        methodName.append(key.substring(1));
                    }
                }
                if (method.getName().equals(methodName.toString())) {
                    Class<?> parameterTypes[] = method.getParameterTypes();
                    if (parameterTypes.length == 1) {
                        Class<?> parameterType = parameterTypes[0];

                        if (parameterType.equals(Byte.class)) {
                            method.invoke(t, Byte.parseByte(properties.getProperty(key)));
                        } else if (parameterType.equals(Short.class)) {
                            method.invoke(t, Short.parseShort(properties.getProperty(key)));
                        } else if (parameterType.equals(Integer.class)) {
                            method.invoke(t, Integer.parseInt(properties.getProperty(key)));
                        } else if (parameterType.equals(Long.class)) {
                            method.invoke(t, Long.parseLong(properties.getProperty(key)));
                        } else if (parameterType.equals(Float.class)) {
                            method.invoke(t, Float.parseFloat(properties.getProperty(key)));
                        } else if (parameterType.equals(Double.class)) {
                            method.invoke(t, Double.parseDouble(properties.getProperty(key)));
                        } else if (parameterType.equals(Boolean.class)) {
                            method.invoke(t, Boolean.parseBoolean(properties.getProperty(key)));
                        } else if (parameterType.equals(Character.class)) {
                            method.invoke(t, properties.getProperty(key).charAt(0));
                        } else if (parameterType.equals(String.class)) {
                            method.invoke(t, properties.getProperty(key));
                        } else if (parameterType.equals(Collection.class)) {
                            Collection<String> c = Arrays.asList(properties.getProperty(key).replace("[", "").replace("]", "").replace(" ", "").split(","));
                            for (String s : c) {
                                if (s.isEmpty()) {
                                    c.remove(s);
                                }
                            }
                            method.invoke(t, c);
                        }

                        break;
                    }
                }
            }
        }

        return t;
    }

    /**
     * @return
     */
    public String fileExtension() {
        return "properties";
    }
}

@@ -181,16 +181,44 @@
 Method;%0A
+import java.util.ArrayList;%0A
 import j
@@ -3419,16 +3419,17 @@
 tring%3E c
+0
  = Array
@@ -3525,24 +3525,101 @@
 plit(%22,%22));%0A
+                            Collection%3CString%3E c1 = new ArrayList%3CString%3E();%0A
             
@@ -3651,16 +3651,17 @@
 ng s : c
+0
 ) %7B%0A    
@@ -3692,16 +3692,17 @@
     if (
+!
 s.isEmpt
@@ -3749,15 +3749,13 @@
    c
-.remove
+1.add
 (s);
@@ -3811,32 +3811,32 @@
               %7D%0A
-
                 
@@ -3865,16 +3865,17 @@
 oke(t, c
+1
 );%0A     

fix parser