Example usage for java.lang StringBuilder replace

List of usage examples for java.lang StringBuilder replace

Introduction

In this page you can find the example usage for java.lang StringBuilder replace.

Prototype

@Override
public StringBuilder replace(int start, int end, String str) 

Source Link

Usage

From source file:org.jumpmind.symmetric.io.PostgresBulkDatabaseWriter.java

private String createCopyMgrSql() {
    StringBuilder sql = new StringBuilder("COPY ");
    DatabaseInfo dbInfo = platform.getDatabaseInfo();
    String quote = dbInfo.getDelimiterToken();
    String catalogSeparator = dbInfo.getCatalogSeparator();
    String schemaSeparator = dbInfo.getSchemaSeparator();
    sql.append(targetTable.getQualifiedTableName(quote, catalogSeparator, schemaSeparator));
    sql.append("(");
    Column[] columns = targetTable.getColumns();

    for (Column column : columns) {
        String columnName = column.getName();
        if (StringUtils.isNotBlank(columnName)) {
            sql.append(quote);/*from  w  ww . j  av  a  2 s.c  o  m*/
            sql.append(columnName);
            sql.append(quote);
            sql.append(",");
        }
    }
    sql.replace(sql.length() - 1, sql.length(), ")");
    sql.append("FROM STDIN with delimiter ',' csv quote ''''");
    return sql.toString();
}

From source file:fr.landel.utils.scripts.ScriptsReplacer.java

private boolean checkExpression(final String expression, final Set<String> keys, final List<String> variables) {
    Pair<Integer, Integer> bounds;
    StringBuilder sb = new StringBuilder(expression);
    String condition;//w  w w. ja  v  a 2  s .c o  m
    boolean isOk = false;

    for (bounds = this.findDeepestCondition(sb, this.template.getBlockOpen(),
            this.template.getBlockClose()); bounds != null; bounds = this.findDeepestCondition(sb,
                    this.template.getBlockOpen(), this.template.getBlockClose())) {

        condition = sb.substring(bounds.getLeft(), bounds.getRight());

        isOk = this.checkVariables(keys, variables, condition, isOk);

        sb.replace(bounds.getLeft() - 1, bounds.getRight() + 1, Boolean.toString(isOk));
    }

    return this.checkVariables(keys, variables, sb.toString(), isOk);
}

From source file:fr.landel.utils.commons.StringUtils.java

/**
 * Injects all arguments in the specified char sequence. The arguments are
 * injected by replacement of keys between braces. To exclude keys, just
 * double braces (like {{key}} will return {key}). If some keys aren't
 * found, they are ignored./* ww w  . j av a2  s. c o m*/
 * 
 * <p>
 * precondition: {@code charSequence} cannot be {@code null}
 * </p>
 * 
 * <pre>
 * StringUtils.injectKeys(Pair.of("${", "}"), Pair.of("${{", "}}"), "", Pair.of("key", "test"));
 * // =&gt; ""
 * 
 * StringUtils.injectKeys(Pair.of("${", "}"), Pair.of("${{", "}}"), "I'll go to the {where} this {when}", Pair.of("where", "beach"),
 *         Pair.of("when", "afternoon"));
 * // =&gt; "I'll go to the beach this afternoon"
 * 
 * StringUtils.injectKeys(Pair.of("${", "}"), Pair.of("${{", "}}"), "I'll go to {key}{{key}}{key}", Pair.of("key", "beach"));
 * // =&gt; "I'll go to beach{key}beach"
 * </pre>
 * 
 * @param include
 *            the characters that surround the property key to replace
 * @param exclude
 *            the characters that surround the property key to exclude of
 *            replacement
 * @param charSequence
 *            the input char sequence
 * @param arguments
 *            the pairs to inject
 * @param <T>
 *            the type arguments
 * @return the result with replacements
 */
@SafeVarargs
public static <T extends Map.Entry<String, Object>> String injectKeys(final Pair<String, String> include,
        final Pair<String, String> exclude, final CharSequence charSequence, final T... arguments) {

    checkParamsInjectKeys(include, exclude, charSequence);

    if (isEmpty(charSequence) || arguments == null || arguments.length == 0) {
        return charSequence.toString();
    }

    final StringBuilder output = new StringBuilder(charSequence);

    // if no brace, just returns the string
    if (output.indexOf(include.getLeft()) < 0) {
        return output.toString();
    }

    for (T argument : arguments) {
        if (argument != null) {
            int index = 0;

            final String key = new StringBuilder(include.getLeft()).append(argument.getKey())
                    .append(include.getRight()).toString();
            final String keyExclude = new StringBuilder(exclude.getLeft()).append(argument.getKey())
                    .append(exclude.getRight()).toString();

            // replace the excluded braces by a temporary string
            while ((index = output.indexOf(keyExclude, index)) > -1) {
                output.replace(index, index + keyExclude.length(), TEMP_REPLACEMENT);
            }

            // replace the key by the argument
            index = 0;
            while ((index = output.indexOf(key, index)) > -1) {
                output.replace(index, index + key.length(), String.valueOf(argument.getValue()));
            }

            // replace the temporary string by the excluded braces
            index = 0;
            while ((index = output.indexOf(TEMP_REPLACEMENT, index)) > -1) {
                output.replace(index, index + TEMP_REPLACEMENT.length(), key);
            }
        }
    }

    return output.toString();
}

From source file:edu.cornell.kfs.coa.document.validation.impl.AccountReversionGlobalRule.java

/**
 * Returns a comma separated String of values
 * /*from  w w  w .  j av  a2 s  .c o m*/
 * @param values
 * @return
 */
public String getParameterValuesForMessage(Collection<String> values) {
    StringBuilder result = new StringBuilder();
    if (ObjectUtils.isNotNull(values) && values.size() > 0) {
        for (String value : values) {
            result.append(value);
            result.append(",");
        }
        result.replace(result.lastIndexOf(","), result.length(), KFSConstants.EMPTY_STRING);
    }

    return result.toString();
}

From source file:ch.ralscha.extdirectspring.controller.RouterControllerSimpleNamedTest.java

@Test
public void testOptionalNoDefaultValue() throws Exception {
    List<String> multiRequests = new ArrayList<String>();

    Map<String, Object> params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Olstead");
    params.put("theAge", "33");
    params.put("active", Boolean.FALSE);
    String edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 2,
            params, null);// w  ww .j ava 2s  .  c  o  m
    multiRequests.add(edRequest);

    MvcResult result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    List<ExtDirectResponse> responses = ControllerUtil
            .readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 2, new ResultObject("Olstead", 33, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("theAge", "33");
    params.put("active", Boolean.FALSE);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 3, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 3, new ResultObject(null, 33, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Olstead");
    params.put("active", Boolean.FALSE);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 4, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 4,
            new ResultObject("Olstead", null, Boolean.FALSE), responses);

    params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Olstead");
    params.put("theAge", 36);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 5, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 5, new ResultObject("Olstead", 36, null),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("active", Boolean.FALSE);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 6, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 6, new ResultObject(null, null, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Miller");
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 7, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 7, new ResultObject("Miller", null, null),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("theAge", 55);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 8, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 8, new ResultObject(null, 55, null), responses);

    params = new LinkedHashMap<String, Object>();
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP3", true, 9, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 9, new ResultObject(null, null, null), responses);

    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (String requests : multiRequests) {
        sb.append(requests);
        sb.append(",");
    }
    sb.replace(sb.length() - 1, sb.length(), "]");

    result = ControllerUtil.performRouterRequest(mockMvc, sb.toString());
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertThat(responses).hasSize(8);
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 2, new ResultObject("Olstead", 33, Boolean.FALSE),
            responses.subList(0, 1));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 3, new ResultObject(null, 33, Boolean.FALSE),
            responses.subList(1, 2));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 4,
            new ResultObject("Olstead", null, Boolean.FALSE), responses.subList(2, 3));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 5, new ResultObject("Olstead", 36, null),
            responses.subList(3, 4));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 6, new ResultObject(null, null, Boolean.FALSE),
            responses.subList(4, 5));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 7, new ResultObject("Miller", null, null),
            responses.subList(5, 6));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 8, new ResultObject(null, 55, null),
            responses.subList(6, 7));
    assertResponse("remoteProviderSimpleNamed", "methodRP3", 9, new ResultObject(null, null, null),
            responses.subList(7, 8));
}

From source file:ch.ralscha.extdirectspring.controller.RouterControllerSimpleNamedTest.java

@Test
public void testDefaultValues() throws Exception {
    List<String> multiRequests = new ArrayList<String>();

    Map<String, Object> params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Olstead");
    params.put("theAge", "33");
    params.put("active", Boolean.FALSE);
    String edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 2,
            params, null);/*from w ww  . java 2  s  .  c om*/
    multiRequests.add(edRequest);
    MvcResult result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    List<ExtDirectResponse> responses = ControllerUtil
            .readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 2, new ResultObject("Olstead", 33, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("theAge", "33");
    params.put("active", Boolean.FALSE);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 3, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 3, new ResultObject("myName", 33, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Olstead");
    params.put("active", Boolean.FALSE);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 4, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 4, new ResultObject("Olstead", 20, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Olstead");
    params.put("theAge", 36);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 5, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 5, new ResultObject("Olstead", 36, Boolean.TRUE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("active", Boolean.FALSE);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 6, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 6, new ResultObject("myName", 20, Boolean.FALSE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("lastName", "Miller");
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 7, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 7, new ResultObject("Miller", 20, Boolean.TRUE),
            responses);

    params = new LinkedHashMap<String, Object>();
    params.put("theAge", 55);
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 8, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 8, new ResultObject("myName", 55, Boolean.TRUE),
            responses);

    params = new LinkedHashMap<String, Object>();
    edRequest = ControllerUtil.createEdsRequest("remoteProviderSimpleNamed", "methodRP2", true, 9, params,
            null);
    multiRequests.add(edRequest);

    result = ControllerUtil.performRouterRequest(mockMvc, edRequest);
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 9, new ResultObject("myName", 20, Boolean.TRUE),
            responses);

    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (String requests : multiRequests) {
        sb.append(requests);
        sb.append(",");
    }
    sb.replace(sb.length() - 1, sb.length(), "]");

    result = ControllerUtil.performRouterRequest(mockMvc, sb.toString());
    responses = ControllerUtil.readDirectResponses(result.getResponse().getContentAsByteArray());
    assertThat(responses).hasSize(8);
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 2, new ResultObject("Olstead", 33, Boolean.FALSE),
            responses.subList(0, 1));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 3, new ResultObject("myName", 33, Boolean.FALSE),
            responses.subList(1, 2));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 4, new ResultObject("Olstead", 20, Boolean.FALSE),
            responses.subList(2, 3));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 5, new ResultObject("Olstead", 36, Boolean.TRUE),
            responses.subList(3, 4));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 6, new ResultObject("myName", 20, Boolean.FALSE),
            responses.subList(4, 5));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 7, new ResultObject("Miller", 20, Boolean.TRUE),
            responses.subList(5, 6));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 8, new ResultObject("myName", 55, Boolean.TRUE),
            responses.subList(6, 7));
    assertResponse("remoteProviderSimpleNamed", "methodRP2", 9, new ResultObject("myName", 20, Boolean.TRUE),
            responses.subList(7, 8));
}

From source file:org.ambraproject.search.service.SolrSearchService.java

private String createFilterLimitForSubject(String[] subjects) {
    Arrays.sort(subjects); // Consistent order so that each filter will only be cached once.
    StringBuilder fq = new StringBuilder();
    for (String category : subjects) {
        fq.append("subject:\"").append(category).append("\" AND ");
    }//from   w  w w .  j a  va  2  s  .c  o  m
    return fq.replace(fq.length() - 5, fq.length(), "").toString(); // Remove last " OR".
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java

public static String normlizePath(StringBuilder pathIn, int startIndx, int endIndx) throws URLParseException {

    int endSq = findFirstMatch(pathIn, "?#", startIndx);
    if (endSq < 0 || endSq > endIndx) {
        endSq = endIndx;/* www  .j a  v  a2 s .c  o  m*/
    }
    int endReplace = endSq;
    StringBuilder retPath = new StringBuilder(pathIn.substring(startIndx, endSq));

    String[] strPattern = new String[] { "/./" };

    int dotIndex = findFirstMatch(retPath, strPattern, 0);
    while (dotIndex > 0 && dotIndex < endSq) {
        retPath.delete(dotIndex, dotIndex + 2);
        endSq -= 2;
        dotIndex = findFirstMatch(retPath, strPattern, 0);
    }

    strPattern = new String[] { "/.." };
    while (findFirstMatch(retPath, strPattern, 0) == 0) {
        retPath.delete(0, 3);
        endSq -= 3;
    }

    int iDDot = findFirstMatch(retPath, strPattern, 0, endSq);
    if (iDDot < 0) {
        encodePath(retPath, 0, retPath.length());
        if (retPath.length() == 0 || retPath.codePointAt(0) != '/') {
            retPath.insert(0, '/');
        }
        pathIn.replace(startIndx, endReplace, retPath.toString());
        return retPath.toString();
    }
    int iSS = 0;
    iDDot = retPath.indexOf("/..");

    Stack<String> pathStack = new Stack<String>();
    while (iDDot >= 0) {
        int next = retPath.indexOf("/", iSS + 1);
        while (next <= iDDot && next >= 0) {
            pathStack.push(retPath.substring(iSS, next));
            iSS = next;
            next = retPath.indexOf("/", iSS + 1);

        }
        while (iSS == iDDot) {
            if (!pathStack.isEmpty()) {
                pathStack.pop();
            }
            iDDot = retPath.indexOf("/..", iSS + 1);

            if (iDDot < 0) {
                pathStack.push(retPath.substring(iSS + 3));
                break;
            }
            iSS = retPath.indexOf("/", iSS + 1);
        }
    }

    if (pathStack.isEmpty()) {
        encodePath(retPath, 0, retPath.length());
        if (retPath.codePointAt(0) != '/') {
            retPath.insert(0, '/');
        }
        pathIn.replace(startIndx, endReplace, retPath.toString());
        return retPath.toString();
    } else {
        retPath = new StringBuilder();
        for (int i = 0; i < pathStack.size(); i++) {
            retPath.append(pathStack.elementAt(i));
        }
    }
    if (retPath.length() == 0) {
        retPath.append("/");
    }
    encodePath(retPath, 0, retPath.length());
    if (retPath.indexOf("/") != 0) {
        retPath.insert(0, '/');
    }
    pathIn.replace(startIndx, endReplace, retPath.toString());
    return retPath.toString();
}

From source file:org.ambraproject.search.service.SolrSearchService.java

private String createFilterLimitForJournals(String[] journals) {
    Arrays.sort(journals); // Consistent order so that each filter will only be cached once.
    StringBuilder fq = new StringBuilder();
    for (String journal : journals) {
        fq.append("cross_published_journal_key:").append(journal).append(" OR ");
    }//  w ww.  ja va  2 s.c o  m
    return fq.replace(fq.length() - 4, fq.length(), "").toString(); // Remove last " OR".
}