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

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

Introduction

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

Prototype

public String replace(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:com.sixdimensions.wcm.cq.component.bindings.impl.ComponentBindingsProviderWebConsole.java

/**
 * Loads the template with the specified name from the classloader and uses
 * it to templatize the properties using Apache Commons Lang's
 * StrSubstitutor and writes it to the response.
 * //from   www. j  a  va 2  s  .co m
 * @param res
 *            the response to write to
 * @param template
 *            the template file to load from the classpath
 * @param properties
 *            the properties to templatize
 * @throws IOException
 */
private void renderBlock(HttpServletResponse res, String templateName, Map<String, String> properties)
        throws IOException {
    InputStream is = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String template = null;
    try {
        is = getClass().getClassLoader().getResourceAsStream(templateName);
        if (is != null) {
            IOUtils.copy(is, baos);
            template = baos.toString();
        } else {
            throw new IOException("Unable to load template " + templateName);
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(baos);
    }
    StrSubstitutor sub = new StrSubstitutor(properties);
    res.getWriter().write(sub.replace(template));
}

From source file:eu.eexcess.opensearch.recommender.PartnerConnector.java

/**
 * Replace the "{searchTerm}" in {@code searchEndpointTemplate} with
 * {@code searchQuery}./*from  w  w  w . ja  v a 2 s. com*/
 * 
 * @param searchEndpointTemplate
 *            the search end point link containing searchTerm placeholder
 * @param searchQuery
 *            the search term
 * @return the substituted link or null on error
 */
private String injectSearchQuery(String searchEndpointTemplate, String searchQuery) {

    try {
        Map<String, String> valuesMap = new HashMap<String, String>();
        valuesMap.put(searchTermsVariableName, searchQuery);
        StrSubstitutor substitutor = new StrSubstitutor(valuesMap, substitutorPrefix, substitutorSuffix);
        return substitutor.replace(partnerConfig.searchEndpoint);
    } catch (Exception e) {
    }

    logger.log(Level.WARNING, "failed to prepare search request url [" + searchEndpointTemplate
            + "] with query [" + searchQuery + "]");
    return null;
}

From source file:com.redhat.lightblue.rest.crud.AbstractCrudResource.java

private String buildProjectionTemplate(String s1) {
    String sp;/*from w w  w. jav  a2  s .c o m*/
    String[] split = s1.split(":");

    Map<String, String> map = new HashMap<>();
    map.put("field", split[0]);
    map.put("include", split[1].charAt(0) == '1' ? "true" : "false");
    map.put("recursive", split[1].length() < 2 ? "false" : (split[1].charAt(1) == 'r' ? "true" : "false"));

    StrSubstitutor sub = new StrSubstitutor(map);

    sp = sub.replace(PROJECTION_TMPL);
    return sp;
}

From source file:com.redhat.lightblue.rest.crud.AbstractCrudResource.java

private String buildSortTemplate(String s1) {
    String ss;/* w w w.  j  av a2  s .c o  m*/

    String[] split = s1.split(":");

    Map<String, String> map = new HashMap<>();
    map.put("field", split[0]);
    map.put("order", split[1].charAt(0) == 'd' ? "$desc" : "$asc");

    StrSubstitutor sub = new StrSubstitutor(map);

    ss = sub.replace(SORT_TMPL);
    return ss;
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnReplicationCodeFileWriter.java

public String generateHeaderBlock() {
    StrSubstitutor sub = new StrSubstitutor(valueMap);
    return sub.replace(headerTemplate);
}

From source file:com.hiperium.bo.manager.exception.ExceptionManager.java

/**
 * Verifies if the error exception ID corresponds to a message that need a
 * parameter exchange./*from   w  ww  .j  av a 2  s  .  co m*/
 *
 * @param infoExcepcion
 *            the information exception
 * @param locale
 *            the locale used to format the message
 * @return the information exception object with the internationalized
 *         message.
 * @throws javax.persistence.NoResultException
 *             if no result exception in the searching
 */
private InformationException createMessageParameters(InformationException infoExcepcion, Locale locale)
        throws NoResultException, InformationException {
    this.log.debug("createMessageParameters - START");
    Map<String, String> var = new HashMap<String, String>();
    // POSTGRES INTEGRITY CODES: http://www.postgresql.org/docs/9.1/static/errcodes-appendix.html
    switch (infoExcepcion.getErrorCode()) {
    case 23502: // Insert null in a required field
        var.put(ENTITY_FIELD, infoExcepcion.getMessage().split("\"")[1]);
        break;
    case 23503: // Foreign key constraint exception
        var.put(ENTITY_CHILD, this.findEntityTableName(infoExcepcion, locale));
        break;
    case 23514: // Check constraint exception
        var.put(ENTITY_FIELD, infoExcepcion.getMessage().split("chk_")[1].split("\"")[0]);
        break;
    default:
        break;
    }
    if (!var.isEmpty()) {
        StrSubstitutor sub = new StrSubstitutor(var);
        infoExcepcion = new InformationException(sub.replace(infoExcepcion.getMessage()));
    }
    this.log.debug("createMessageParameters - END");
    return infoExcepcion;
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnReplicationCodeFileWriter.java

public String generateLibraryLine() {
    String template = "library(${library_1})";
    StrSubstitutor sub = new StrSubstitutor(valueMap);
    return sub.replace(template);
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnReplicationCodeFileWriter.java

public String generateDvnRHelperFileLine() {
    String template = "source('./${dvn_R_helper_file}')";
    StrSubstitutor sub = new StrSubstitutor(valueMap);
    return sub.replace(template);
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnReplicationCodeFileWriter.java

public String generateLoadDataFileLine() {
    String template = "load('./${dvn_RData_FileName}')";
    StrSubstitutor sub = new StrSubstitutor(valueMap);
    return sub.replace(template);
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnReplicationCodeFileWriter.java

public String generateSubsetLine() {
    String template = "${dvn_dataframe}<-subsetVariables(${dvn_dataframe})";
    StrSubstitutor sub = new StrSubstitutor(valueMap);
    return sub.replace(template);
}