/******************************************************************
 * File:        Main.java
 * Created by:  Dave Reynolds
 * Created on:  3 Dec 2013
 * 
 * (c) Copyright 2013, Epimorphics Limited
 *
 *****************************************************************/

package com.epimorphics.dclib;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.jena.riot.system.StreamRDF;
import org.apache.jena.riot.writer.WriterStreamRDFBlocks;

import com.epimorphics.dclib.framework.ConverterProcess;
import com.epimorphics.dclib.framework.ConverterService;
import com.epimorphics.dclib.framework.DataContext;
import com.epimorphics.dclib.framework.Template;
import com.epimorphics.dclib.templates.TemplateFactory;
import com.epimorphics.tasks.LiveProgressMonitor;
import com.epimorphics.tasks.ProgressMessage;
import com.epimorphics.tasks.SimpleProgressMonitor;
import com.epimorphics.util.NameUtils;
import com.hp.hpl.jena.rdf.model.Model;

public class Main {
    public static final String DEBUG_FLAG = "--debug";
    public static final String STREAMING_FLAG = "--streaming";
    
    public static void main(String[] argsIn) throws IOException {
        boolean debug = false;
        boolean streaming = false;
        
        // TODO proper command line parsing
        
        List<String> args = new ArrayList<>(argsIn.length);
        for (String arg : argsIn) args.add(arg);
        
        if (args.contains(DEBUG_FLAG)) {
            debug = true;
            args.remove(DEBUG_FLAG);
        }
        if (args.contains(STREAMING_FLAG)) {
            streaming = true;
            args.remove(STREAMING_FLAG);
        }

        if (args.size() < 2) {
            System.err.println("Usage:  java -jar dclib.jar [--debug] [--streaming] template.json ... data.csv");
            System.exit(1);
        }
        String templateName = args.get(0);
        String dataFile = args.get( args.size() -1 );
        
        ConverterService service = new ConverterService();
        DataContext dc = service.getDataContext();
        for (int i = 1; i < args.size() - 1; i++) {
            Template aux = TemplateFactory.templateFrom(args.get(i), dc);
            dc.registerTemplate(aux);
        }
        
        SimpleProgressMonitor reporter = new LiveProgressMonitor();

        boolean succeeded = false;
        if (streaming) {
            Template template = TemplateFactory.templateFrom(templateName, dc);
            
            File dataFileF = new File(dataFile);
            String filename = dataFileF.getName();
            String filebasename = NameUtils.removeExtension(filename);
            dc.getGlobalEnv().put(ConverterProcess.FILE_NAME, filename);
            dc.getGlobalEnv().put(ConverterProcess.FILE_BASE_NAME, filebasename);
            InputStream is = new FileInputStream(dataFileF);
            
            ConverterProcess process = new ConverterProcess(dc, is);
            process.setDebug(debug);
            process.setTemplate( template );
            process.setMessageReporter( reporter );
            process.setAllowNullRows(true);
            
            StreamRDF stream = new WriterStreamRDFBlocks( System.out );
            process.setOutputStream( stream );
            
            succeeded = process.process();
            stream.finish();
            
        } else {
            Model m = service.simpleConvert(templateName, dataFile, reporter, debug);
            if (m != null) {
                m.write(System.out, "Turtle");
            } else {
                succeeded = false;
            }
        }
        if (!succeeded) {
            System.err.println("Failed to convert data");
            for (ProgressMessage message : reporter.getMessages()) {
                System.err.println(message.toString());
            }
            System.exit(1);
        }
    }
    
    
}

@@ -456,16 +456,92 @@
 .List;%0A%0A
+import org.apache.jena.riot.Lang;%0Aimport org.apache.jena.riot.RDFLanguages;%0A
 import o
@@ -610,21 +610,15 @@
 iot.
-writer.Writer
+system.
 Stre
@@ -622,22 +622,22 @@
 treamRDF
-Blocks
+Writer
 ;%0A%0Aimpor
@@ -1264,24 +1264,84 @@
 streaming%22;%0A
+    public static final String NTRIPLE_FLAG = %22--ntriples%22;%0A
     %0A    pub
@@ -1457,32 +1457,66 @@
 eaming = false;%0A
+        boolean ntriples = false;%0A
         %0A       
@@ -1918,16 +1918,137 @@
        %7D
+%0A        if (args.contains(NTRIPLE_FLAG)) %7B%0A            ntriples = true;%0A            args.remove(NTRIPLE_FLAG);%0A        %7D
 %0A%0A      
@@ -2155,16 +2155,29 @@
 reaming%5D
+ %5B--ntriples%5D
  templat
@@ -3553,46 +3553,91 @@
 m = 
-new WriterStreamRDFBlocks( System.out 
+StreamRDFWriter.getWriterStream(System.out,  ntriples ? Lang.NTRIPLES : Lang.TURTLE
 );%0A 
@@ -3952,16 +3952,76 @@
 ut, 
-%22
+ntriples ? RDFLanguages.strLangNTriples : RDFLanguages.strLang
 Turtle
-%22
 );%0A 

Support --ntriples output