package eu.dnetlib.enabling.aas.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * File utilities.
 * @author mhorst
 *
 */
public class FileUtils {

        public static final String CLASSPATH_PREFIX = "classpath:";

        /**
         * Resolves relative paths and classpaths into absolute paths.
         * @param filePaths
         * @return resolved paths
         */
        public static List<String> resolveAbsolutePaths(
        		List<String> filePaths) {
        	if (filePaths!=null) {
        		List<String> result = new ArrayList<String>(filePaths.size());
        		for (String sourcePath : filePaths) {
        			result.add(resolveAbsolutePath(sourcePath));
        		}
        		return result;
        	} else {
        		return null;
        	}
        }
        
        /**
         * Resolves relative paths and classpaths into absolute paths.
         * @param filePath
         * @return resolved path
         */
        public static String resolveAbsolutePath(String filePath) {
                File file = getFile(filePath);
                return file!=null?file.getAbsolutePath():null;
        }

        /**
         * Returns {@link File} object for given filePath
         * @param filePath
         * @return File object
         */
        public static File getFile(String filePath) {
                if (filePath==null)
                        return null;
                File file = null;
//              support for classpaths
                if (filePath.startsWith(CLASSPATH_PREFIX)) {
                        filePath = filePath.substring(CLASSPATH_PREFIX.length());
                }
//              added support for reading file content by web application
                URL fileURL = FileUtils.class.getResource("/"+filePath);
                if (fileURL!=null) {
                        file = new File(fileURL.getPath());
                } else {
                        file = new File(filePath);
                }
                return file;
        }

        /**
         * Returns file content for given path.
         * Path might be absolute, relative file path or classpath.
         * @param filePath
         * @return
         */
        public static String getFileContent(String filePath) {
                try {
                        File file = getFile(filePath);
                        if (file==null) {
                                return null;
                        }
                        BufferedReader reader = new BufferedReader(new FileReader(file));
                        StringBuffer strBuff = new StringBuffer();
                        String line;
                        while ((line = reader.readLine())!=null) {
                                strBuff.append(line);
                        }
                        return strBuff.toString();
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        return null;
                } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                }
        }

}