package de.tblsoft.solr.pipeline;

import de.tblsoft.solr.util.IOUtils;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tblsoft on 11.02.16.
 */
public class XmlReader extends AbstractReader {

    @Override
    public void read() {
        try {
            String filename = getProperty("filename", null);
            String absoluteFilename = IOUtils.getAbsoluteFile(getBaseDir(),filename);
            InputStream in = IOUtils.getInputStream(absoluteFilename);

            List<String> transformationFiles = getPropertyAsList("transformations", new ArrayList<String>());

            XMLReader myReader = XMLReaderFactory.createXMLReader();
            ContentHandler mySerializer = new PipelineSaxContentHandler(executer);
            boolean firstFile = true;
            TransformerHandler lastHandler = null;
            for(String file: transformationFiles) {
                TransformerHandler currentHandler = getTransformerHandler(file);
                if(firstFile) {
                    firstFile = false;
                    myReader.setContentHandler(currentHandler);
                    lastHandler = currentHandler;
                    continue;
                }
                lastHandler.setResult(new SAXResult(currentHandler));
                lastHandler = currentHandler;

            }
            if(lastHandler == null) {
                myReader.setContentHandler(mySerializer);
            } else {
                lastHandler.setResult(new SAXResult(mySerializer));
            }

            myReader.parse(new InputSource(in));



            in.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        //executer.document(document);
    }


    TransformerHandler getTransformerHandler(String fileName) throws TransformerConfigurationException {
        fileName = IOUtils.getAbsoluteFile(getBaseDir(),fileName);
        if(fileName.endsWith(".stx")) {
            SAXTransformerFactory stxFactory = new net.sf.joost.trax.TransformerFactoryImpl();
            return stxFactory.newTransformerHandler(new StreamSource(fileName));

        }
        if(fileName.endsWith(".xsl")) {
            SAXTransformerFactory xslFactory = (SAXTransformerFactory) TransformerFactory.newInstance();;
            return xslFactory.newTransformerHandler(new StreamSource(fileName));
        }
        throw new IllegalArgumentException("Only the filetypes xsl and stx are supported.");
    }

}

@@ -568,16 +568,39 @@
 ayList;%0A
+import java.util.Date;%0A
 import j
@@ -742,24 +742,25 @@
 id read() %7B%0A
+%0A
         try 
@@ -822,16 +822,18 @@
  null);%0A
+%0A%0A
         
@@ -895,24 +895,25 @@
 etBaseDir(),
+ 
 filename);%0A 
@@ -927,22 +927,29 @@
     
-InputStream in
+List%3CString%3E fileList
  = I
@@ -958,27 +958,21 @@
 tils.get
-InputStream
+Files
 (absolut
@@ -988,119 +988,8 @@
 );%0A%0A
-            List%3CString%3E transformationFiles = getPropertyAsList(%22transformations%22, new ArrayList%3CString%3E());%0A%0A
     
@@ -1237,19 +1237,124 @@
         
-for
+List%3CString%3E transformationFiles = getPropertyAsList(%22transformations%22, new ArrayList%3C%3E());%0A            for 
 (String 
@@ -1357,16 +1357,17 @@
 ing file
+ 
 : transf
@@ -1482,16 +1482,17 @@
       if
+ 
 (firstFi
@@ -1843,16 +1843,17 @@
       if
+ 
 (lastHan
@@ -2030,16 +2030,138 @@
      %7D%0A%0A
+            for (String sourceFile : fileList) %7B%0A                InputStream in = IOUtils.getInputStream(sourceFile);%0A    
         
@@ -2201,19 +2201,20 @@
 e(in));%0A
-%0A%0A%0A
+    
         
@@ -2228,16 +2228,31 @@
 close();
+%0A%0A            %7D
 %0A       
@@ -2259,17 +2259,16 @@
  %7D catch
- 
 (Excepti
@@ -2272,17 +2272,16 @@
 ption e)
- 
 %7B%0A      
@@ -2331,49 +2331,8 @@
   %7D%0A
-%0A%0A        //executer.document(document);%0A
     

allow the reading of multiple files in the xml reader