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:tds.itemrenderer.processing.ITSUrlBase64.java

private String replaceHtmlMatch(Matcher matcher) {
    String basePath = _filePath.replace(Path.getFileName(_filePath), "");
    String imageFile = matcher.group("src");
    String imagePath = Path.combine(basePath, imageFile);

    byte[] binaryData = null;
    try {/*from www  .ja v a  2  s .c  om*/
        binaryData = FileUtils.readFileToByteArray(new File(imagePath));
    } catch (IOException e) {
        throw new ITSDocumentProcessingException(e);
    }

    final String base64Format = "data:image/%s;base64,%s";
    String extension = Path.getExtension(imageFile).replace(".", "");
    String base64 = String.format(base64Format, extension, DatatypeConverter.printBase64Binary(binaryData));

    String imageTag = matcher.group("img");

    Matcher matcher2 = matcher.pattern().matcher(imageTag);
    StringBuilder sb = new StringBuilder(imageTag);
    matcher2.find();
    return sb.replace(matcher2.start(2), matcher2.end(2), base64).toString();

}

From source file:org.beangle.ems.database.service.SqlService.java

@SuppressWarnings("deprecation")
public String getLimitString(String sql, PageLimit limit) {
    try {//from  ww  w.  j av  a2  s. c om
        int offset = (limit.getPageNo() - 1) * limit.getPageSize();
        String newSql = dialect.getLimitString(sql, offset, limit.getPageSize());
        StringBuilder sb = new StringBuilder(newSql);
        int index = sb.lastIndexOf("?");
        if (-1 != index)
            sb.replace(index, index + 1, String.valueOf(limit.getPageSize()));

        index = sb.lastIndexOf("?");
        if (-1 != index)
            sb.replace(index, index + 1, String.valueOf(offset));

        return sb.toString();
    } catch (Exception e) {
        throw new RuntimeException("cannot limit sql:" + sql, e);
    }
}

From source file:com.stratio.deep.cassandra.util.CassandraUtils.java

private static String getLuceneWhereClause(Filter filter) {
    String result;/*from   w  w w . j  a v  a  2s.c om*/

    StringBuilder sb = new StringBuilder("{filter:{type:\"boolean\",must:[");

    String column = filter.getField();
    String value = (String) filter.getValue();

    // Generate query for column
    String[] processedQuery = processLuceneQueryType(value);
    sb.append("{type:\"");
    sb.append(processedQuery[0]);
    sb.append("\",field:\"");
    sb.append(column);
    sb.append("\",value:\"");
    sb.append(processedQuery[1]);
    sb.append("\"},");

    sb.replace(sb.length() - 1, sb.length(), "");
    sb.append("]}}");

    result = sb.toString();

    return result;

}

From source file:org.eclipse.jubula.client.ui.rcp.widgets.ParamValueValidator.java

/**
 * @param e event/* w  ww  .ja  v  a 2s . c  om*/
 * @return value to validate
 */
String getNewValue(VerifyEvent e) {
    Text txt = (Text) e.widget;
    final String oldValue = txt.getText();
    StringBuilder workValue = new StringBuilder(oldValue);
    workValue.replace(e.start, e.end, e.text);
    String newValue = workValue.toString();
    return newValue;
}

From source file:stg.utils.StringUtils.java

/**
 * A whole word is identified as a word that does not have a valid {@link Character#isJavaIdentifierPart(char)} before or after the searchStr.
 * /*from  w ww  .j  a v  a 2s . co m*/
 * @param text in which the search and replace is needed. 
 * @param word to be searched for
 * @param newWord to be replaced with
 * @return replaced text.
 */
public static String replaceAllWholeWord(String text, String word, String newWord) {
    StringBuilder sb = new StringBuilder(text);
    int index = 0;
    while (sb.indexOf(word, index) > -1) {
        index = sb.indexOf(word, index);
        boolean notAWholeWord = false;
        if (index > 0) {
            char c = sb.charAt(index - 1);
            if (Character.isJavaIdentifierPart(c)) {
                notAWholeWord = true;
            }
            if (index + word.length() < sb.length()) {
                c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                sb.replace(index, index + word.length(), newWord);
            }
        } else if (index == 0) {
            if (index + word.length() < sb.length()) {
                char c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                sb.replace(0, word.length(), newWord);
            }
        }
        index++;
    }
    return sb.toString();
}

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

public static String normalizeFragment(StringBuilder sb, int startIndx, int endIndx)
        throws URLMalformedInputException {
    if (sb.length() == 0 || sb.codePointAt(startIndx) != '#') {
        throw new URLMalformedInputException("Fragment should start with '#'");
    }//from  ww w.j a va 2s .co  m
    StringBuilder ret = new StringBuilder(sb.substring(startIndx, endIndx));

    sb.replace(startIndx, endIndx, encodeFragment(ret));
    return sb.toString();
}

From source file:edu.uci.ics.jung.visualization.util.LabelWrapper.java

/**
 * line-wrap the passed String as an html string with
 * break Strings inserted.// www  . j av a  2  s  .  c o m
 * 
 * @param str
 * @return
 */
private String wrap(String str) {
    StringBuilder buf = new StringBuilder(str);
    int len = lineLength;
    while (len < buf.length()) {
        int idx = buf.lastIndexOf(" ", len);
        if (idx != -1) {
            buf.replace(idx, idx + 1, breaker);
            len = idx + breaker.length() + lineLength;
        } else {
            buf.insert(len, breaker);
            len += breaker.length() + lineLength;
        }
    }
    buf.insert(0, "<html>");
    return buf.toString();
}

From source file:org.apache.commons.digester3.examples.plugins.pipeline.SubstituteTransform.java

public String transform(String s) {
    StringBuilder buf = new StringBuilder(s);
    while (true) {
        int idx = buf.indexOf(from);
        if (idx == -1) {
            break;
        }// www.j a v  a2s.  co m

        buf.replace(idx, idx + from.length(), to);
    }
    return buf.toString();
}

From source file:de.uni_potsdam.hpi.asg.logictool.helper.BDDHelper.java

@SuppressWarnings("unchecked")
private static String formatArbitraryFunction(BDD bdd, Netlist netlist) {
    StringBuilder str = new StringBuilder();
    Object x = bdd.allsat();/*w w  w . j  a  v a 2 s.c  om*/
    if (x instanceof LinkedList<?>) {
        for (Object o : (LinkedList<Object>) x) {
            int id = 0;
            byte[] a = (byte[]) o;
            str.append("(");
            for (byte b : a) {
                switch (b) {
                case -1:
                    break;
                case 0:
                    str.append("!" + netlist.getNetlistVariableByBddId(id).getName() + "*");
                    break;
                case 1:
                    str.append(netlist.getNetlistVariableByBddId(id).getName() + "*");
                    break;
                default:
                    logger.error("BBD allsat entry unknown");
                }
                id++;
            }
            str.replace(str.length() - 1, str.length(), ")+");
        }
        str.replace(str.length() - 1, str.length(), "");
    }
    return str.toString();
}

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

static String encodeFragment(StringBuilder sb) {
    int i = -1;/*  w ww.j  ava2 s.  c  om*/
    URLNormalizerUtils.removeObfuscatedEncoding(sb, new EncodingType[] { EncodingType.FRAGMENT });
    //TODO discuss whether escape '%' char
    while ((i = findFirstMatch(sb, "%", i + 1)) >= 0) {
        if (i + 3 > sb.length() || !percentEnc.matcher(sb.substring(i, i + 3)).matches()) {
            sb.replace(i, i + 1, "%25");
        }
    }
    return sb.toString();
}