/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.csu.sdmx;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author themi
 */
public class Utils {
    //transforms MultipartFile into List of lines
    public static List<String> readLinesList(MultipartFile file){
        try{
            return readLinesList(file.getInputStream());
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }
    
    //transforms InputStream into List of lines
    public static List<String> readLinesList(InputStream is){
        List<String> fileLines = new ArrayList<>();
        if(is != null){
            Scanner scnr = new Scanner(is);
            while (scnr.hasNextLine()){ 
                fileLines.add(scnr.nextLine());
            }
            scnr.close(); 
        }
        
        return fileLines;
    }
    
    
    //transforms MultipartFile into single String
    public static String readLinesString(MultipartFile file){
        try{
            return readString(file.getInputStream());
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }
    
    //transforms InputStream into single String
    public static String readString(InputStream is){
        String fileString = "";
        if(is != null){
            Scanner scnr = new Scanner(is);
            while (scnr.hasNextLine()){ 
                fileString += (scnr.nextLine());
            } 
            scnr.close(); 
        }
        return fileString;
    }
    
    
    //transforms String to InputStream
    public static InputStream toInputStream(String text){
        try {
            return new ByteArrayInputStream(text.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    
    //name..
    public static void copyInputStreamToOutputStream(InputStream in, OutputStream out) {
    try {
        byte[] buf = new byte[1024];
        int len;
        while((len=in.read(buf))>0){
            out.write(buf,0,len);
        }
//        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    //joins String array using delimiter
    public static String join(String[] strings, String delimiter){
        if(strings != null)
            return join(Arrays.asList(strings), delimiter);
        else
            return "";
    }
    
    //joins String list using delimiter
    public static String join(List<String> strings, String delimiter){
        if(strings.size() > 0){
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < strings.size()-1; i++){
                builder.append(strings.get(i)).append(delimiter);
            }
            builder.append(strings.get(strings.size()-1));

            return builder.toString();
        }
        
        return "";
    }
    
    
    //Finds first found node determined by Xpath
    public static Element getByXpath(Document doc, String xpathExp){
        try {
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expr = xpath.compile(xpathExp);
            
            return (Element)expr.evaluate(doc, XPathConstants.NODE);
        } catch (XPathExpressionException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    //Transforms Document to InputStream
    public static InputStream docToIS(Document doc){
        try {
            DOMSource domSource = new DOMSource(doc);
            StringWriter writer = new StringWriter();
            StreamResult streamResult = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, streamResult);
//            System.out.println(writer.toString());
            return toInputStream(writer.toString());
        } catch (TransformerException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
