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

org.bdware.ypkdeploy.DeployConfGenerator Maven / Gradle / Ivy

The newest version!
package org.bdware.ypkdeploy;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class DeployConfGenerator {
    final static String TAG = "[DeployConfGenerator] ";

    public static String getFileContent(String path) {
        try {
            File file = new File(path);
            long fileLen = file.length();
            byte[] fileContent = new byte[(int) fileLen];
            FileInputStream in = new FileInputStream(file);
            in.read(fileContent);
            in.close();
            return new String(fileContent, StandardCharsets.UTF_8);
        } catch (IOException e) {
        }
        return null;
    }

    public static void generateDeployConf(String dir, String template, String commonvar,
            String fileName) {
        String templateStr = getFileContent(dir + template);
        try {
            JsonObject commVar = JsonParser.parseReader(new FileReader(new File(dir, commonvar)))
                    .getAsJsonObject();
            String content = templateStr;
            File output = new File(dir, fileName);
            if (!output.getParentFile().exists())
                output.getParentFile().mkdirs();
            FileOutputStream fout = new FileOutputStream(output);
            for (String key : commVar.keySet()) {
                content = content.replaceAll(key, commVar.get(key).getAsString());
            }
            fout.write(content.getBytes(StandardCharsets.UTF_8));
            fout.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void batchGenerateDeployConf(String dir, String template, String commonvar,
            String filesStr) {
        String templateStr = getFileContent(new File(dir, template).getAbsolutePath());
        try {
            JsonObject commVar = JsonParser.parseReader(new FileReader(new File(dir, commonvar)))
                    .getAsJsonObject();
            JsonArray files = JsonParser.parseReader(new FileReader(new File(dir, filesStr)))
                    .getAsJsonArray();
            for (JsonElement je : files) {
                JsonObject file = je.getAsJsonObject();
                File output = new File(dir, file.get("fileName").getAsString());
                if (!output.getParentFile().exists())
                    output.getParentFile().mkdirs();
                FileOutputStream fout = new FileOutputStream(output);
                String content = templateStr;
                for (String key : file.keySet()) {
                    if (!key.startsWith("_"))
                        continue;
                    content = content.replaceAll(key, file.get(key).getAsString());
                }
                for (String key : commVar.keySet()) {
                    if (file.has(key))
                        continue;
                    content = content.replaceAll(key, commVar.get(key).getAsString());
                }
                fout.write(content.getBytes(StandardCharsets.UTF_8));
                fout.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy