Search in sources :

Example 16 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project thunder by RohanNagar.

the class SecretSourceProvider method open.

/**
 * Open the configuration, substituting secrets with the value retrieved from the configured
 * {@link SecretProvider}.
 *
 * @param path the path to the configuration file
 * @return an {@link InputStream} representing the final configuration
 * @throws IOException if the file cannot be read
 */
@Override
public InputStream open(String path) throws IOException {
    // Read the configuration with the base ConfigurationSourceProvider
    // in order to get the correct secret provider.
    SecretProvider secretProvider;
    try {
        secretProvider = FACTORY.build(baseProvider, path).getSecretProvider();
    } catch (ConfigurationException e) {
        // we need to open the configuration as normal to show any errors.
        return baseProvider.open(path);
    }
    // Now, build the SubstitutingSourceProvider to substitute all ${...} secrets with the value
    // provided through the SecretProvider.
    var stringSubstitutor = new StringSubstitutor(secretProvider).setEnableUndefinedVariableException(true);
    var substitutingSourceProvider = new SubstitutingSourceProvider(baseProvider, stringSubstitutor);
    return substitutingSourceProvider.open(path);
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) ConfigurationException(io.dropwizard.configuration.ConfigurationException) StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Example 17 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project PharmCAT by PharmGKB.

the class ResultSerializer method toHtml.

public ResultSerializer toHtml(Result result, Path htmlFile) throws IOException {
    Preconditions.checkNotNull(result);
    Preconditions.checkNotNull(htmlFile);
    Preconditions.checkArgument(htmlFile.toString().endsWith(".html"));
    StringBuilder builder = new StringBuilder();
    for (GeneCall call : result.getGeneCalls()) {
        MatchData matchData = call.getMatchData();
        builder.append("<h3>").append(call.getGene()).append("</h3>");
        builder.append("<ul>");
        for (DiplotypeMatch diplotype : call.getDiplotypes()) {
            builder.append("<li>").append(diplotype.getName()).append(" (").append(diplotype.getScore()).append(")</li>");
        }
        builder.append("</ul>");
        builder.append("<table class=\"table table-striped table-hover table-condensed\">");
        // position
        builder.append("<tr>");
        builder.append("<th>Definition Position</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getPosition()).append("</th>");
        }
        builder.append("</tr>");
        // rsid
        builder.append("<tr>");
        builder.append("<th></th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>");
            if (v.getRsid() != null) {
                builder.append(v.getRsid());
            }
            builder.append("</th>");
        }
        builder.append("</tr>");
        // VCF position
        builder.append("<tr>");
        builder.append("<th>VCF Position</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getPosition()).append("</th>");
        }
        builder.append("</tr>");
        // sample
        builder.append("<tr>");
        builder.append("<th>VCF REF,ALTs</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getVcfAlleles()).append("</th>");
        }
        builder.append("</tr>");
        builder.append("<tr class=\"success\">");
        builder.append("<th>VCF Call</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getVcfCall()).append("</th>");
        }
        builder.append("</tr>");
        Set<String> matchedHaplotypeNames = new HashSet<>();
        if (call.getHaplotypes().size() > 0) {
            for (HaplotypeMatch hm : call.getHaplotypes()) {
                matchedHaplotypeNames.add(hm.getHaplotype().getName());
                printAllele(builder, hm.getHaplotype().getName(), hm.getHaplotype().getPermutations().pattern(), "info");
                for (String seq : hm.getSequences()) {
                    printAllele(builder, null, seq, null);
                }
            }
        }
        if (m_alwaysShowUnmatchedHaplotypes || matchedHaplotypeNames.size() == 0) {
            for (NamedAllele haplotype : matchData.getHaplotypes()) {
                if (!matchedHaplotypeNames.contains(haplotype.getName())) {
                    printAllele(builder, haplotype.getName(), haplotype.getPermutations().pattern(), "danger");
                }
            }
        }
        builder.append("</table>");
        if (matchData.getMissingPositions().size() > 0) {
            builder.append("<p>There ");
            if (matchData.getMissingPositions().size() > 1) {
                builder.append("were ");
            } else {
                builder.append("was ");
            }
            builder.append(matchData.getMissingPositions().size()).append(" missing positions from the VCF file:</p>").append("<ul>");
            for (VariantLocus variant : matchData.getMissingPositions()) {
                builder.append("<li>").append(variant.getPosition()).append(" (").append(variant.getChromosomeHgvsName()).append(")</li>");
            }
            builder.append("</ul>");
            if (call.getUncallableHaplotypes().size() > 0) {
                builder.append("<p>The following haplotype(s) were eliminated from consideration:</p>").append("<ul>");
                for (String name : call.getUncallableHaplotypes()) {
                    builder.append("<li>").append(name).append("</li>");
                }
                builder.append("</ul>");
            }
            if (call.getHaplotypes().size() > 0) {
                builder.append("<p>The following haplotypes were called even though tag positions were missing:</p>").append("<ul>");
                for (HaplotypeMatch hm : call.getHaplotypes()) {
                    if (hm.getHaplotype().getMissingPositions().size() > 0) {
                        builder.append("<li>Called ").append(hm.getName()).append(" without ").append(hm.getHaplotype().getMissingPositions().stream().map(VariantLocus::getChromosomeHgvsName).collect(Collectors.joining(", "))).append("</li>");
                    }
                }
                builder.append("</ul>");
            }
        }
    }
    try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(htmlFile, StandardCharsets.UTF_8))) {
        Map<String, String> varMap = new HashMap<>();
        varMap.put("title", "PharmCAT Allele Call Report for " + result.getMetadata().getInputFilename());
        varMap.put("content", builder.toString());
        varMap.put("timestamp", m_dateFormat.format(new Date()));
        StringSubstitutor sub = new StringSubstitutor(varMap);
        writer.println(sub.replace(getHtmlTemplate()));
    }
    return this;
}
Also used : HashMap(java.util.HashMap) NamedAllele(org.pharmgkb.pharmcat.definition.model.NamedAllele) Date(java.util.Date) Variant(org.pharmgkb.pharmcat.haplotype.model.Variant) HaplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch) StringSubstitutor(org.apache.commons.text.StringSubstitutor) VariantLocus(org.pharmgkb.pharmcat.definition.model.VariantLocus) DiplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.DiplotypeMatch) GeneCall(org.pharmgkb.pharmcat.haplotype.model.GeneCall) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter)

Example 18 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project Plan by plan-player-analytics.

the class Message method toString.

public String toString(Serializable... p) {
    if (p == null)
        throw new IllegalArgumentException("null arguments were given!");
    Map<String, Serializable> replaceMap = new HashMap<>();
    for (int i = 0; i < p.length; i++) {
        Serializable value = p[i];
        if (value == null)
            throw new IllegalArgumentException("null is not a valid argument!");
        replaceMap.put(String.valueOf(i), value.toString());
    }
    StringSubstitutor sub = new StringSubstitutor(replaceMap);
    return sub.replace(content);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Example 19 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project Plan by plan-player-analytics.

the class PlaceholderReplacer method apply.

@Override
public String apply(String string) {
    StringSubstitutor sub = new StringSubstitutor(this);
    sub.setEnableSubstitutionInVariables(true);
    return sub.replace(string);
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Example 20 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project inspectit-ocelot by inspectIT.

the class ConfigParser method parseConfig.

/**
 * Parses YAML describing an InspectitConfig into InspectitConfig object.
 *
 * @param configYaml String in YAML format describing an InspectitConfig.
 *
 * @return InspectitConfig described by given YAML.
 */
public InspectitConfig parseConfig(String configYaml) throws JsonProcessingException {
    if (!StringUtils.isEmpty(configYaml)) {
        // config YAMLs contain variables that in the inspectit-ocelot-core project are evaluated by Spring-Boot,
        // however here there is no InspectitEnvironment which is used for that, so instead Jackson is used and
        // the variables are evaluated using a custom version of StringSubstitutor.
        Map<String, Object> yamlMap = mapper.readValue(configYaml, Map.class);
        // remove any invalid keys at the top level to make the String parseable for Jackson
        yamlMap.keySet().removeIf(key -> !key.equals("inspectit"));
        String cleanedConfigYaml = mapper.writeValueAsString(yamlMap);
        StringSubstitutor stringSubstitutor = new NestedMapStringSubstitutor(yamlMap);
        cleanedConfigYaml = stringSubstitutor.replace(cleanedConfigYaml);
        // Parse the InspectitConfig from the created YAML String
        ObjectReader reader = mapper.reader().withRootName("inspectit").forType(InspectitConfig.class);
        return reader.readValue(cleanedConfigYaml);
    } else {
        return new InspectitConfig();
    }
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) InspectitConfig(rocks.inspectit.ocelot.config.model.InspectitConfig)

Aggregations

StringSubstitutor (org.apache.commons.text.StringSubstitutor)71 HashMap (java.util.HashMap)24 Test (org.junit.jupiter.api.Test)19 IOException (java.io.IOException)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 File (java.io.File)8 List (java.util.List)8 InputStream (java.io.InputStream)6 Collectors (java.util.stream.Collectors)6 StringLookup (org.apache.commons.text.lookup.StringLookup)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 JsonObject (com.google.gson.JsonObject)4 URL (java.net.URL)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Scanner (java.util.Scanner)4