package org.embulk.parser.apache.log;

import org.apache.commons.lang3.StringUtils;
import org.embulk.spi.time.TimestampParser;

public class TimestampLogElementFactory implements LogElementFactory<TimestampLogElement>, Patterns {

    private TimestampParser.Task task;
    private String name;

    public TimestampLogElementFactory(TimestampParser.Task task, String name) {
        this.task = task;
        this.name = name;
    }

    @Override
    public TimestampLogElement create(String parameter) {
        if(StringUtils.isEmpty(parameter)){
            return new TimestampLogElement(task, name, "\\[([^\\]]+)\\]");
        }else{
            return new TimestampLogElement(task, name, parameter);
        }
    }
}

@@ -34,16 +34,84 @@
 log;%0D%0A%0D%0A
+import java.util.regex.Pattern;%0D%0Aimport java.util.regex.Matcher;%0D%0A%0D%0A
 import o
@@ -148,16 +148,16 @@
 Utils;%0D%0A
-
 import o
@@ -723,16 +723,73 @@
 %7Delse%7B%0D%0A
+            String regex = toTimestampRegex(parameter);%0D%0A
         
@@ -834,16 +834,23 @@
 k, name,
+ regex,
  paramet
@@ -872,12 +872,1116 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    private String toTimestampRegex(String parameter) %7B%0D%0A        String regex = %22(%22 + parameter + %22)%22;%0D%0A        regex = regex.replaceAll(%22%5C%5C%5B%7C%5C%5C%5D%22,%22%5C%5C%5C%5C$0%22);%0D%0A%0D%0A        regex = regex.replaceAll(%22%25%5BabhpABPZ%5D%22,%22%5BA-z%5D+%22);%0D%0A        regex = regex.replaceAll(%22%25c%22,%22%5BA-z%5D%7B3%7D %5BA-z%5D%7B3%7D %5C%5C%5C%5Cd%7B2%7D %5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D %5C%5C%5C%5Cd%7B4%7D%22);%0D%0A        regex = regex.replaceAll(%22%25%5BdgmyCHIMSUVW%5D%22,%22%5C%5C%5C%5Cd%7B2%7D%22);%0D%0A        regex = regex.replaceAll(%22%25%5BDx%5D%22,%22%5C%5C%5C%5Cd%7B2%7D/%5C%5C%5C%5Cd%7B2%7D/%5C%5C%5C%5Cd%7B2%7D%22);%0D%0A        regex = regex.replaceAll(%22%25%5Bekl%5D%22,%22%5B1-9 %5D%5C%5C%5C%5Cd%22);%0D%0A        regex = regex.replaceAll(%22%25F%22,%22%5C%5C%5C%5Cd%7B4%7D-%5C%5C%5C%5Cd%7B2%7D-%5C%5C%5C%5Cd%7B2%7D%22);%0D%0A        regex = regex.replaceAll(%22%25%5BGY%5D%22,%22%5C%5C%5C%5Cd%7B4%7D%22);%0D%0A        regex = regex.replaceAll(%22%25j%22,%22%5C%5C%5C%5Cd%7B3%7D%22);%0D%0A        regex = regex.replaceAll(%22%25r%22,%22%5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D %5BA-z%5D+%22);%0D%0A        regex = regex.replaceAll(%22%25R%22,%22%5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D%22);%0D%0A        regex = regex.replaceAll(%22%25s%22,%22%5C%5C%5C%5Cd+%22);%0D%0A        regex = regex.replaceAll(%22%25%5BTX%5D%22,%22%5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D:%5C%5C%5C%5Cd%7B2%7D%22);%0D%0A        regex = regex.replaceAll(%22%25%5Buw%5D%22,%22%5C%5C%5C%5Cd%22);%0D%0A        regex = regex.replaceAll(%22%25z%22,%22%5C%5C%5C%5C+%5C%5C%5C%5Cd%7B4%7D%22);%0D%0A%0D%0A        return regex;%0D%0A    %7D%0D
 %0A%7D%0D%0A

support %{format}t format