Example usage for org.apache.commons.lang3.text StrSubstitutor replace

List of usage examples for org.apache.commons.lang3.text StrSubstitutor replace

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrSubstitutor replace.

Prototype

public String replace(final Object source) 

Source Link

Document

Replaces all the occurrences of variables in the given source object with their matching values from the resolver.

Usage

From source file:de.britter.beyondstringutils.StrSubstitutorExample.java

public static void main(String[] args) {
    Map<String, String> values = singletonMap("key", "a value");
    String tmpl = "Replacing ${key} & fallbacks ${undefined:-1234}";
    StrSubstitutor sub = new StrSubstitutor(values);
    String replaced = sub.replace(tmpl);

    System.out.println(replaced);
}

From source file:com.rest.samples.getReportFromJasperServerWithSeparateAuthFormatURL.java

public static void main(String[] args) {
    // TODO code application logic here
    Map<String, String> params = new HashMap<String, String>();
    params.put("host", "10.49.28.3");
    params.put("port", "8081");
    params.put("reportName", "vencimientos");
    params.put("parametros", "feini=2016-09-30&fefin=2016-09-30");
    StrSubstitutor sub = new StrSubstitutor(params, "{", "}");
    String urlTemplate = "http://{host}:{port}/jasperserver/rest_v2/reports/Reportes/{reportName}.pdf?{parametros}";
    String url = sub.replace(urlTemplate);

    try {/*from ww w .  j  a v a2  s .  co m*/
        CredentialsProvider cp = new BasicCredentialsProvider();
        cp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("jasperadmin", "jasperadmin"));
        CloseableHttpClient hc = HttpClientBuilder.create().setDefaultCredentialsProvider(cp).build();

        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/pdf");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();
        OutputStream os = new FileOutputStream(new File("vencimientos.pdf"));
        int read = 0;
        byte[] bytes = new byte[2048];

        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        is.close();
        os.close();

        if (Desktop.isDesktopSupported()) {
            File pdfFile = new File("vencimientos.pdf");
            Desktop.getDesktop().open(pdfFile);
        }

    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.qcadoo.mes.deviationCausesReporting.dataProvider.DeviationWithOccurrencesDataProvider.java

private static String prepareTotalOccurrencesForProblemProjection() {
    Collection<String> subQueries = Collections2.transform(Arrays.asList(DeviationType.values()),
            new Function<DeviationType, String>() {

                @Override//from  w ww  .  j a  v  a2 s .  c  o  m
                public String apply(final DeviationType deviationType) {
                    DeviationModelDescriber modelDescriber = deviationType.getModelDescriber();
                    HashMap<String, String> placeholderValues = Maps.newHashMap();
                    placeholderValues.put("MODEL_PLUGIN", modelDescriber.getModelPlugin());
                    placeholderValues.put("MODEL_NAME", modelDescriber.getModelName());
                    placeholderValues.put("REASON_TYPE_FIELD_NAME", modelDescriber.getReasonTypeFieldName());
                    placeholderValues.put("ORDER_PATH", deviationType.getPathToOrder());
                    placeholderValues.put("ORDER_STATE", OrderFields.STATE);
                    StrSubstitutor substitutor = new StrSubstitutor(placeholderValues, "${", "}");
                    return substitutor.replace(PARTIAL_COUNT_SUB_QUERY_TPL).toString();
                }
            });
    return StringUtils.join(subQueries, " + ");
}

From source file:edu.sdsc.scigraph.internal.CypherUtil.java

static String substituteRelationships(String query, final Multimap<String, Object> valueMap) {
    StrSubstitutor substitutor = new StrSubstitutor(new StrLookup<String>() {
        @Override/*from  ww  w. j ava 2 s . co  m*/
        public String lookup(String key) {
            return on('|').join(valueMap.get(key));
        }
    });
    return substitutor.replace(query);
}

From source file:com.cybernostics.jsp2thymeleaf.api.util.AlternateFormatStrings.java

private static Set<String> getRequiredValuesForFormat(String candidateFormat) {
    Set<String> requiredVariables = new HashSet<>();
    StrSubstitutor ss = SimpleStringTemplateProcessor.getSubstitutor(new StrLookup<String>() {
        @Override// w w  w  . j  a  v a2  s.  com
        public String lookup(String key) {
            final ValueDefaultAndFilter keyDefaultFilter = ValueDefaultAndFilter.parse(key);
            if (keyDefaultFilter.isRequired()) {
                requiredVariables.add(keyDefaultFilter.getName());
            }
            return "";
        }
    });
    ss.replace(candidateFormat); // do a dummy replace to find out what's needed
    return requiredVariables;
}

From source file:com.sunchenbin.store.feilong.core.lang.StringUtil.java

/**
 * The following example demonstrates this:
 * /*from  w w w.  j  a v a 2  s . c o  m*/
 * <pre>
 * Map valuesMap = HashMap();
 * valuesMap.put(&quot;animal&quot;, &quot;quick brown fox&quot;);
 * valuesMap.put(&quot;target&quot;, &quot;lazy dog&quot;);
 * 
 * StrSubstitutor sub = new StrSubstitutor(valuesMap);
 * String templateString = &quot;The ${animal} jumped over the ${target}.&quot;;
 * String resolvedString = sub.replace(templateString);
 * </pre>
 * 
 * yielding:
 * 
 * <pre>
 *      The quick brown fox jumped over the lazy dog.
 * </pre>
 *
 * @param <V>
 *            the value type
 * @param templateString
 *            the template string
 * @param valuesMap
 *            the values map
 * @return the string
 * @see org.apache.commons.lang3.text.StrSubstitutor#replace(String)
 * @since 1.1.1
 */
public static <V> String replace(String templateString, Map<String, V> valuesMap) {
    StrSubstitutor strSubstitutor = new StrSubstitutor(valuesMap);
    return strSubstitutor.replace(templateString);
}

From source file:com.discovery.darchrow.lang.StringUtil.java

/**
 * * The following example demonstrates this:
 * /*from   ww  w .  j  a  v  a2  s. c  o  m*/
 * <pre>
 * Map valuesMap = HashMap();
 * valuesMap.put(&quot;animal&quot;, &quot;quick brown fox&quot;);
 * valuesMap.put(&quot;target&quot;, &quot;lazy dog&quot;);
 * String templateString = &quot;The ${animal} jumped over the ${target}.&quot;;
 * StrSubstitutor sub = new StrSubstitutor(valuesMap);
 * String resolvedString = sub.replace(templateString);
 * </pre>
 * 
 * yielding:
 * 
 * <pre>
 *      The quick brown fox jumped over the lazy dog.
 * </pre>
 *
 * @param <V>
 *            the value type
 * @param templateString
 *            the template string
 * @param valuesMap
 *            the values map
 * @return the string
 * @see org.apache.commons.lang3.text.StrSubstitutor
 * @since 1.1.1
 */
public static final <V> String replace(String templateString, Map<String, V> valuesMap) {
    StrSubstitutor strSubstitutor = new StrSubstitutor(valuesMap);
    String resolvedString = strSubstitutor.replace(templateString);
    return resolvedString;
}

From source file:com.astamuse.asta4d.util.i18n.formatter.ApacheStrSubstitutorFormatter.java

@Override
public String format(String pattern, Map<String, Object> paramMap) {
    StrSubstitutor sub = new StrSubstitutor(paramMap, prefix, suffix, escape);
    return sub.replace(pattern);
}

From source file:com.hotelbeds.distribution.hotel_api_sdk.types.HotelContentPaths.java

public String getUrl(HotelApiService service, HotelApiVersion version, Map<String, String> params,
        String alternativeHotelContentPath) {
    if (params == null) {
        params = new HashMap<>();
    }//from www .  j a va 2 s.c  om
    params.put("path", service.getHotelContentPath(alternativeHotelContentPath));
    params.put("version", version.getVersion());
    StrSubstitutor strSubstitutor = new StrSubstitutor(params);
    return strSubstitutor.replace(urlTemplate);
}

From source file:com.hotelbeds.distribution.hotel_api_sdk.types.HotelApiPaths.java

public String getUrl(HotelApiService service, HotelApiVersion version, Map<String, String> params,
        String alternativeHotelApiPath) {
    if (params == null) {
        params = new HashMap<>();
    }/*from  w  w w .ja v  a  2  s.c  o  m*/
    params.put("path", service.getHotelApiPath(alternativeHotelApiPath));
    if (!params.containsKey("version")) {
        params.put("version", version.getVersion());
    }
    StrSubstitutor strSubstitutor = new StrSubstitutor(params);
    return strSubstitutor.replace(urlTemplate);
}