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

com.checkmarx.configprovider.resource.AbstractFileResource Maven / Gradle / Ivy

package com.checkmarx.configprovider.resource;

import com.checkmarx.configprovider.dto.ResourceType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import lombok.Getter;
import org.apache.commons.io.IOUtils;

import javax.naming.ConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;


@Getter
public abstract class AbstractFileResource extends ParsableResource {

    protected ResourceType type;
    protected Config config;

    protected AbstractFileResource(){}

    protected static boolean isYml(String name) {
        return ResourceType.YAML == ResourceType.getTypeByNameOrExtention(name);
    }

    
    Config jsonToConfig(String fileContent) {
        return ConfigFactory.parseString(fileContent);
    }

    Config yamlToConfig(String yamlContent, String path) throws ConfigurationException {
        try{

            ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());

            Object obj = yamlReader.readValue(yamlContent, Object.class);
            ObjectMapper jsonWriter = new ObjectMapper();
            String jsonAsStr = jsonWriter.writeValueAsString(obj)
                /*replace a single " with space if it is not escaped (\")*/
                .replaceAll("(?




© 2015 - 2025 Weber Informatics LLC | Privacy Policy