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

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

Introduction

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

Prototype

public StrBuilder replace(int startIndex, int endIndex, String replaceStr) 

Source Link

Document

Replaces a portion of the string builder with another string.

Usage

From source file:mitm.djigzo.web.render.impl.AbstractInlineAddClassPattern.java

private void applyPattern(StrBuilder builder) {
    Pattern pattern = getPattern();

    if (pattern != null) {
        Matcher matcher = pattern.matcher(builder.toString());

        StrBuilder copy = null;

        /*/* w  w w .  ja  v  a2 s  .c  o m*/
         * Because we will modify the StrBuilder (replacing matches etc.) we need
         * to keep track of changes with respect to indexes. If there is a match
         * we will create a copy of the line and do the replacing on that line. We
         * however need to correct the indexes because we are adding or removing 
         * characters to the copy.  
         */
        int indexCorrection = 0;

        while (matcher.find()) {
            if (copy == null) {
                copy = new StrBuilder(builder.toString());
            }

            String replaceWith = "<span class=\"" + className + "\">" + matcher.group() + "</span>";

            copy.replace(matcher.start() + indexCorrection, matcher.end() + indexCorrection, replaceWith);

            indexCorrection = indexCorrection + replaceWith.length() - matcher.group().length();
        }

        if (copy != null) {
            /*
             * Content has changed so replace it
             */
            builder.clear();
            builder.append(copy.toString());
        }
    }
}

From source file:org.apache.kylin.query.util.PushDownUtil.java

static String schemaCompletion(String inputSql, String schema) throws SqlParseException {
    if (inputSql == null || inputSql.equals("")) {
        return "";
    }//  w  ww. j  av a 2s .  c  om
    SqlNode node = CalciteParser.parse(inputSql);

    // get all table node that don't have schema by visitor pattern
    FromTablesVisitor ftv = new FromTablesVisitor();
    node.accept(ftv);
    List<SqlNode> tablesWithoutSchema = ftv.getTablesWithoutSchema();
    // sql do not need completion
    if (tablesWithoutSchema.isEmpty()) {
        return inputSql;
    }

    List<Pair<Integer, Integer>> tablesPos = new ArrayList<>();
    for (SqlNode tables : tablesWithoutSchema) {
        tablesPos.add(CalciteParser.getReplacePos(tables, inputSql));
    }

    // make the behind position in the front of the list, so that the front position will not be affected when replaced
    Collections.sort(tablesPos, new Comparator<Pair<Integer, Integer>>() {
        @Override
        public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
            int r = o2.getFirst() - o1.getFirst();
            return r == 0 ? o2.getSecond() - o1.getSecond() : r;
        }
    });

    StrBuilder afterConvert = new StrBuilder(inputSql);
    for (Pair<Integer, Integer> pos : tablesPos) {
        String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond());
        afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema);
    }
    return afterConvert.toString();
}