All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tunyk.mvn.plugins.htmlcompressor.FileTool Maven / Gradle / Ivy

package com.tunyk.mvn.plugins.htmlcompressor;

import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class FileTool {

    private String rootDirPath;
    private String[] fileExt;
    private boolean recursive;
    private String fileEncoding;

    public FileTool(String rootDir, String[] fileExt, boolean recursive) throws IOException {
        this.setRootDirPath(rootDir);
        this.fileExt = fileExt;
        this.recursive = recursive;
    }

    public Map getFiles() throws IOException {
        Map map = new HashMap();
        File rootDir = new File(rootDirPath);
        // TODO: fix unchecked warning
        Collection files = FileUtils.listFiles(rootDir, fileExt, recursive);
        int truncationIndex = 0;
        for (File file : files) {
            String normalizedFilePath = file.getCanonicalPath().replaceAll("\\\\", "/");
            if (truncationIndex == 0) {
                truncationIndex = normalizedFilePath.indexOf(rootDirPath) + rootDirPath.length() + 1;
            }
            String key = normalizedFilePath.substring(truncationIndex);
            String value = FileUtils.readFileToString(file, fileEncoding);
            map.put(key, value);
        }
        return map;
    }

    public void writeFiles(Map map, String targetDir) throws IOException {
        for(String key : map.keySet()) {
            File file = new File(targetDir + "/" + key);
            FileUtils.writeStringToFile(file, map.get(key), fileEncoding);
        }
    }

    public void writeToJsonFile(Map map, String targetFile, String integrationCode) throws IOException, JSONException {
        String replacePattern = "%s";
        File file = new File(targetFile);
        JSONObject json = new JSONObject();
        for(String key : map.keySet()) {
            json.put(key, map.get(key));
        }
        if (integrationCode == null) {
            integrationCode = replacePattern;
        }
        if (integrationCode.indexOf(replacePattern) == -1) {
            integrationCode += replacePattern;
        }
        String contents = integrationCode.replaceFirst(replacePattern, json.toString().replace("\\", "\\\\"));
        FileUtils.writeStringToFile(file, contents, fileEncoding);
    }

    public static String humanReadableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

    public static String getElapsedHMSTime(long elapsedTime) {
        String format = String.format("%%0%dd", 2);
        elapsedTime = elapsedTime / 1000;
        String seconds = String.format(format, elapsedTime % 60);
        String minutes = String.format(format, (elapsedTime % 3600) / 60);
        String hours = String.format(format, elapsedTime / 3600);
        return hours + ":" + minutes + ":" + seconds;
    }

    public String getRootDirPath() {
        return rootDirPath;
    }

    public void setRootDirPath(String rootDirPath) throws IOException {
        File file = new File(rootDirPath);
        this.rootDirPath = file.getCanonicalPath().replaceAll("\\\\", "/").replaceAll("/$", "");
    }

    public String[] getFileExt() {
        return fileExt;
    }

    public void setFileExt(String[] fileExt) {
        this.fileExt = fileExt;
    }

    public boolean isRecursive() {
        return recursive;
    }

    public void setRecursive(boolean recursive) {
        this.recursive = recursive;
    }

    public String getFileEncoding() {
        return fileEncoding;
    }

    public void setFileEncoding(String fileEncoding) {
        this.fileEncoding = fileEncoding;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy