package de.tblsoft.solr.pipeline.filter;

import de.tblsoft.solr.pipeline.AbstractFilter;
import de.tblsoft.solr.pipeline.bean.Document;
import de.tblsoft.solr.util.IOUtils;
import de.tblsoft.solr.util.OutputStreamStringBuilder;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by tblsoft on 21.02.16.
 */
public class N3Writer extends AbstractFilter {

    private String absoluteFilename;

    private OutputStream outputStream;

    private OutputStreamStringBuilder outputStreamStringBuilder;


    @Override
    public void init() {




            String relativeFilename = getProperty("filename", null);
            absoluteFilename = IOUtils.getAbsoluteFile(getBaseDir(), relativeFilename);

            verify(absoluteFilename, "For the FileLineWriter a filname must be defined.");

            try {
                outputStream = IOUtils.getOutputStream(absoluteFilename);
                outputStreamStringBuilder = new OutputStreamStringBuilder(outputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            super.init();

        }

    @Override
    public void document(Document document) {
        try {
            String subject = document.getFieldValue("subject");
            String predicate = document.getFieldValue("predicate");
            String object = document.getFieldValue("object");

            outputStreamStringBuilder.append("<").append(subject).append(">");
            outputStreamStringBuilder.append("<").append(predicate).append(">");
            outputStreamStringBuilder.append("<").append(object).append(">");
            outputStreamStringBuilder.append("\n");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        super.document(document);
    }



    @Override
    public void end() {
        try {
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        super.end();
    }

}

@@ -1467,32 +1467,45 @@
 ect).append(%22%3E%22)
+.append(%22%5Ct%22)
 ;%0A            ou
@@ -1561,32 +1561,45 @@
 ate).append(%22%3E%22)
+.append(%22%5Ct%22)
 ;%0A            ou
@@ -1622,33 +1622,34 @@
 Builder.append(%22
-%3C
+%5C%22
 %22).append(object
@@ -1658,17 +1658,18 @@
 append(%22
-%3E
+%5C%22
 %22);%0A    

improve N3Writer