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.Date;
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);
            List<String> fileList = IOUtils.getFiles(absoluteFilename);

            XMLReader myReader = XMLReaderFactory.createXMLReader();
            ContentHandler mySerializer = new PipelineSaxContentHandler(executer);
            boolean firstFile = true;
            TransformerHandler lastHandler = null;
            List<String> transformationFiles = getPropertyAsList("transformations", new ArrayList<>());
            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));
            }

            for (String sourceFile : fileList) {
                InputStream in = IOUtils.getInputStream(sourceFile);
                myReader.parse(new InputSource(in));
                in.close();

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


    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.");
    }

}

@@ -65,16 +65,73 @@
 OUtils;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -629,31 +629,8 @@
 st;%0A
-import java.util.Date;%0A
 impo
@@ -749,85 +749,278 @@
     
-@Override%0A    public void read() %7B%0A%0A        try %7B%0A            String filename
+private static Logger LOG = LoggerFactory.getLogger(XmlReader.class);%0A%0A    @Override%0A    public void read() %7B%0A%0A        String currentFileName = null;%0A%0A        try %7B%0A            String deprecatedFilename = getProperty(%22filename%22, null);%0A            List%3CString%3E filenames
  = g
@@ -1021,32 +1021,38 @@
 es = getProperty
+AsList
 (%22filename%22, nul
@@ -1049,20 +1049,251 @@
 name
+s
 %22, n
-ull);%0A%0A%0A
+ew ArrayList%3C%3E());%0A%0A            if(deprecatedFilename != null) %7B%0A                filenames.add(deprecatedFilename);%0A%0A            %7D%0A%0A            List%3CString%3E fileList = new ArrayList%3C%3E();%0A            for(String filename : filenames) %7B%0A    
     
@@ -1383,36 +1383,27 @@
             
-List%3CString%3E
+   
  fileList = 
@@ -1399,19 +1399,24 @@
 fileList
- = 
+.addAll(
 IOUtils.
@@ -1433,33 +1433,48 @@
 bsoluteFilename)
+)
 ;
+%0A            %7D
 %0A%0A            XM
@@ -2497,32 +2497,33 @@
 %0A            %7D%0A%0A
+%0A
             for 
@@ -2551,24 +2551,70 @@
 fileList) %7B%0A
+                currentFileName = sourceFile;%0A
             
@@ -2796,16 +2796,124 @@
 ion e)%7B%0A
+            LOG.error(%22Could not process file: %22 + currentFileName + %22 because of: %22 + e.getMessage(), e );%0A
         

allow the processing of multiple filenames in xml reader