package transform;

import org.codehaus.jackson.map.ObjectMapper;
import whelk.Document;
import whelk.JsonLd;

import java.io.*;
import java.util.Map;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        if (args[0].equals("syntax"))
            generateAndPrintSyntax(args);
        else if (args[0].equals("transform"))
            generateAndPrintTransform(args);
        else
            System.err.println(
                    "Usage:\n" +
                            "java -jar transform.jar syntax [file1]\n" +
                            "  Generate a syntax that convers all json in [file1], one json document per line.\n" +
                            "\n" +
                            "java -jar transform.jar transform [syntax1file] [syntax2file] [file1] [file2]\n" +
                            "  Generate a diff script from syntax1 to syntax2 using values found in the streams\n" +
                            "  each element in order in file1 and file2 is expected to represent the \"the same\"" +
                            "  record, in the various format forms.");
    }

    private static void generateAndPrintSyntax(String[] args) throws IOException
    {
        ObjectMapper mapper = new ObjectMapper();
        BufferedReader in = new BufferedReader(new FileReader(args[1]));

        String jsonString;
        Syntax syntax = new Syntax();
        while ( (jsonString = in.readLine()) != null)
        {
            Map data = mapper.readValue(jsonString, Map.class);
            Document doc = new Document(data);
            data = JsonLd.frame(doc.getCompleteId(), data);
            syntax.expandSyntaxToCover(data);
        }
        in.close();
        System.out.println(syntax);
    }

    private static void generateAndPrintTransform(String[] args) throws IOException
    {
        BufferedReader syntax1Reader = new BufferedReader(new FileReader(args[1]));
        BufferedReader syntax2Reader = new BufferedReader(new FileReader(args[2]));
        Syntax syntax1 = new Syntax(syntax1Reader);
        Syntax syntax2 = new Syntax(syntax2Reader);
        syntax1Reader.close();
        syntax2Reader.close();

        BufferedReader json1Reader = new BufferedReader(new FileReader(args[3]));
        BufferedReader json2Reader = new BufferedReader(new FileReader(args[4]));

        Script script = SyntaxDiffReduce.generateScript(syntax1, syntax2, json1Reader, json2Reader);

        json1Reader.close();
        json2Reader.close();

        System.out.println(script);
    }
}

@@ -102,16 +102,51 @@
 .JsonLd;
+%0Aimport whelk.util.TransformScript;
 %0A%0Aimport
@@ -253,32 +253,74 @@
 rows IOException
+, TransformScript.TransformSyntaxException
 %0A    %7B%0A        i
@@ -473,32 +473,109 @@
 ransform(args);%0A
+        else if (args%5B0%5D.equals(%22execute%22))%0A            executeScript(args);%0A
         else%0A   
@@ -1273,16 +1273,219 @@
 t forms.
+%5Cn%22 +%0A                            %22java -jar transform.jar execute %5Bscriptfile%5D %5Bfile%5D%5Cn%22 +%0A                            %22  Execute the given script on each json document in %5Bfile%5D, one document per line.
 %22);%0A    
@@ -2860,32 +2860,32 @@
 eader.close();%0A%0A
-
         System.o
@@ -2909,11 +2909,767 @@
 );%0A    %7D
+%0A%0A    private static void executeScript(String%5B%5D args) throws IOException, TransformScript.TransformSyntaxException%0A    %7B%0A        BufferedReader scriptReader = new BufferedReader(new FileReader(args%5B1%5D));%0A        StringBuilder scriptText = new StringBuilder();%0A        for(String line = scriptReader.readLine(); line != null; line = scriptReader.readLine())%0A        %7B%0A            scriptText.append(line + %22%5Cn%22);%0A        %7D%0A%0A        TransformScript script = new TransformScript(scriptText.toString());%0A%0A        BufferedReader jsonReader = new BufferedReader(new FileReader(args%5B2%5D));%0A        for(String line = jsonReader.readLine(); line != null; line = jsonReader.readLine())%0A        %7B%0A            System.out.println(script.executeOn(line));%0A        %7D%0A    %7D
 %0A%7D%0A

Add script execution.