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.eclipse.winery.common.Util.java

public static String namespaceToJavaPackage(String namespace) {
    URI uri;//  w w w.  j  a  v a2  s  .com
    try {
        uri = new URI(namespace);
    } catch (URISyntaxException e) {
        Util.logger.debug(e.getMessage(), e);
        return "uri.invalid";
    }
    StringBuilder sb = new StringBuilder();

    String host = uri.getHost();
    if (host != null) {
        Util.addReversed(sb, host, "\\.");
    }

    String path = uri.getPath();
    if (!path.equals("")) {
        if (path.startsWith("/")) {
            // remove first slash
            path = path.substring(1);
        }

        // and then handle the string
        Util.addAsIs(sb, path, "/");
    }

    // remove the final dot
    sb.replace(sb.length() - 1, sb.length(), "");

    return Util.cleanName(sb.toString());
}

From source file:hu.qgears.xtextdoc.util.EscapeString.java

public static String unEscapeHtml(String str) {
    StringBuilder w = new StringBuilder();
    String mem = "";
    int lastAmpIndex = -1;
    for (int i = 0; i < str.length(); ++i) {
        char ch = str.charAt(i);
        w.append(ch);//from w  w w  . ja  va  2 s  .  com
        switch (ch) {
        case '&':
            lastAmpIndex = w.length() - 1;
            break;
        case ';': {
            if (lastAmpIndex >= 0) {
                mem = w.substring(lastAmpIndex, w.length());
                if (htmlEscapeChars.containsKey(mem)) {
                    w.replace(lastAmpIndex, w.length(), "" + htmlEscapeChars.get(mem));
                    lastAmpIndex = -1;
                }
            }
            break;
        }

        }
    }
    return w.toString();
}

From source file:com.conwet.xjsp.json.JSONUtil.java

/**
 * Extracts from the buffer a whole stanza or an stream finalizer "]}".
 * The recognized text is removed from the buffer.
 * // w ww.j av  a2 s.c o  m
 * Stanza recognition is implemented as a FSM. States:
 * <ul>
 *      <li>0. starting</li>
 *      <li>1. accepting text</li>
 *      <li>2. within an string</li>
 *      <li>3. finalizer</li>
 * </ul>
 * 
 * <img src="../../../../../resources/fsm.png"/>
 * 
 * @return Recognized text or <code>null</code>
 */
public static String extractStanza(StringBuilder buffer) throws ParseException {
    discardSpaces(buffer);

    int state = 0;
    int pos = 0;
    int level = 1;

    while (pos < buffer.length()) {
        char c = buffer.charAt(pos);
        switch (state) {
        case 0:
            switch (c) {
            case '{':
                state = 1;
                break;
            case ']':
                state = 3;
                break;
            default:
                throw new ParseException(ParseException.ERROR_UNEXPECTED_CHAR);
            }
            break;

        case 1:
            switch (c) {
            case '{':
                level++;
                break;
            case '}':
                level--;
                if (level == 0) {
                    String stanza = buffer.substring(0, pos + 1);
                    buffer.replace(0, pos + 1, "");
                    return stanza;
                }
                break;

            case '"':
                state = 2;
                break;

            default:
                // nothing
            }
            break;

        case 2:
            switch (c) {
            case '\\':
                pos++;
                break;

            case '"':
                state = 1;
                break;

            default:
                // nothing
            }
            break;

        case 3:
            if (isSpace(c)) {
                pos++;
            } else if (c == '}') {
                buffer.replace(0, pos + 1, "");
                return "]}";
            }

        default:
            throw new IllegalStateException();
        }
        pos++;
    }

    return null;
}

From source file:com.webbfontaine.valuewebb.model.util.Utils.java

public static String toWords(CharSequence str) {
    StringBuilder res = new StringBuilder(str.length());
    for (int i = 0; i < str.length(); i++) {
        Character ch = str.charAt(i);
        if (Character.isUpperCase(ch)) {
            res.append(' ').append(ch);
        } else {/*w w w  .j  a va 2s.c o m*/
            res.append(ch);
        }
    }
    char c = Character.toUpperCase(res.charAt(0));
    res.replace(0, 1, new String(new char[] { c }));
    return res.toString();
}

From source file:org.eclipse.jubula.client.ui.rcp.validator.TestDataCubeReferenceValidator.java

/**
 * {@inheritDoc}/*from ww w .ja  v  a 2 s. com*/
 */
public ValidationState validateInput(VerifyEvent e) {
    Text txt = (Text) e.widget;
    StringBuilder workValue = new StringBuilder(txt.getText());
    workValue.replace(e.start, e.end, e.text);
    String newValue = workValue.toString();
    boolean mightMatch = false;
    for (IParameterInterfacePO dataCube : TestDataCubeBP.getAllTestDataCubesFor(m_project)) {
        if (StringUtils.equals(e.text, dataCube.getName())) {
            return ValidationState.OK;
        }
        if (!mightMatch && StringUtils.startsWith(dataCube.getName(), newValue)) {
            mightMatch = true;
        }
    }

    if (mightMatch) {
        return ValidationState.MightMatchAccept;
    }

    return ValidationState.MightMatchReject;
}

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

public static String decodeIPv6(StringBuilder sb) throws URLHostParseException, URLMalformedInputException {

    Stack<Long> values = new Stack<Long>();

    int endSq = getIPv6EndIndex(sb);
    int i = findFirstMatch(sb, "[", 0, endSq);
    int begSq = i >= 0 ? i : 0;
    while (true) {
        int prev = i;
        i = findFirstMatch(sb, ":]/.", prev + 1);
        try {/*from w  w  w .j av  a  2  s.c o m*/
            if (i - prev < 2) {
                if (i < 0 && prev + 1 < endSq) {
                    values.push(Long.parseLong(sb.substring(prev + 1, endSq), 16));
                }
                break;
            }
            if (sb.codePointAt(i) == '.') {
                i = prev;
                break;
            }
            values.push(Long.parseLong(sb.substring(prev + 1, i), 16));
        } catch (NumberFormatException e) {
            LOG.warn("cannot parse IPv6 ");
            LOG.warn(e.getMessage());
            throw new URLHostParseException("Cannot parse IPv6:" + sb.substring(begSq), e);
        }
        if (i == sb.indexOf("::")) {
            break;
        }
    }
    final int parsedValues = values.size();
    int ipv64 = sb.indexOf(".");
    i = sb.indexOf("::");
    if ((ipv64 >= 0 && ipv64 < i)) {
        throw new URLHostParseException("Error in parsing ipv4 in ipv6");
    }
    long[] ipv4tab = null;

    i++;
    while (i > 0) {
        int prev = i + 1;
        i = sb.indexOf(":", prev);
        //            if (i < 0 && ipv64 > 0) {
        //                break;
        //            }
        if (i < 0) {
            if (ipv64 > 0) {
                break;
            }
            i = endSq;
            if (i - prev < 1) {
                break;
            }
            try {
                values.push(Long.parseLong(sb.substring(prev, i), 16));
            } catch (NumberFormatException e) {
                LOG.warn("cannot parse IPv6 ");
                LOG.warn(e.getMessage());
                throw new URLHostParseException("Cannot parse IPv6:" + sb.substring(begSq, endSq), e);
            }
            break;
        }
        try {
            if (i <= endSq) {
                values.push(Long.parseLong(sb.substring(prev, i), 16));
            }
        } catch (NumberFormatException e) {
            LOG.warn("Cannot parse IPv6 " + e.getMessage());
            LOG.warn(e.getMessage());
            throw new URLHostParseException("Cannot parse IPv6:" + sb.substring(begSq, endSq), e);
        }
    }
    if (ipv64 > 0) {
        i = findLastMatch(sb, ":", 0, endSq);
        ipv4tab = decodeIPv4(sb.substring(i + 1, endSq));
        ipv64 = 4;
    } else {
        ipv64 = 0;
    }

    long[] ret = validateAndBuildArray(values, parsedValues, ipv64, ipv4tab);

    if (ret[0] > 0xfc00l) {
        LOG.warn("non routable address (ULA) {}", sb.toString());
    }
    StringBuilder retSb = arrayToIPv6(ret, ipv64 > 0);
    i = retSb.indexOf("::");
    if (i > 0) {
        if (retSb.indexOf("ffff") > 0) {
            retSb.replace(i, i + 1, "0:0:0:0:0");
        } else {
            retSb.replace(i, i + 1, "0:0:0:0:0:0");
        }
    }

    if (endSq == URLNormalizerUtils.findFirstMatch(sb, "]/", 0)) {
        endSq++;
    }
    sb.replace(begSq, endSq, retSb.toString().toLowerCase());
    return retSb.toString();
}

From source file:io.github.seleniumquery.by.common.preparser.NotEqualsAttributeSelectorFix.java

String removeStrings(String input) {
    Pattern p = Pattern.compile("\"([^\"\\\\]|\\\\.)*\"|'([^'\\\\]|\\\\.)*'");
    Matcher m = p.matcher(input);
    StringBuilder sb = new StringBuilder(input);
    while (m.find()) {
        sb.replace(m.start(), m.end(), StringUtils.repeat('_', m.end() - m.start()));
    }//  w w  w.  ja  v a2  s  .  c o m
    return sb.toString();
}

From source file:de.hybris.platform.commerceservices.security.impl.DefaultSecureTokenServiceTest.java

@Test(expected = IllegalArgumentException.class)
public void testChecksum() {
    final SecureToken data = new SecureToken(TEST_DATA, TEST_TS);
    final String token = service.encryptData(data);
    final StringBuilder builder = new StringBuilder(token);
    builder.replace(0, 4, "1234");
    service.decryptData(builder.toString());
}

From source file:io.github.seleniumquery.by.common.preparser.NotEqualsAttributeSelectorFix.java

String removeContains(String input) {
    // :contains matches, \:contains dont, \\:contains does
    Pattern p = Pattern.compile(ESCAPED_SLASHES + ":" + "contains\\(.*?" + ESCAPED_SLASHES + "\\)");
    Matcher m = p.matcher(input);
    StringBuilder sb = new StringBuilder(input);
    while (m.find()) {
        sb.replace(m.start(), m.end(), StringUtils.repeat('_', m.end() - m.start()));
    }/*from  w ww.j av  a2s .c  o m*/
    return sb.toString();
}

From source file:com.opendoorlogistics.core.utils.strings.Strings.java

/**
 * See http://stackoverflow.com/questions/5054995/how-to-replace-case-insensitive-literal-substrings-in-java
 * /*from w  w w.  j a v  a2 s  .  co  m*/
 * @param source
 * @param target
 * @param replacement
 * @return
 */
public static String caseInsensitiveReplace(String source, String target, String replacement) {
    StringBuilder sbSource = new StringBuilder(source);
    StringBuilder sbSourceLower = new StringBuilder(source.toLowerCase());
    String searchString = target.toLowerCase();

    int idx = 0;
    while ((idx = sbSourceLower.indexOf(searchString, idx)) != -1) {
        sbSource.replace(idx, idx + searchString.length(), replacement);
        sbSourceLower.replace(idx, idx + searchString.length(), replacement);
        idx += replacement.length();
    }
    sbSourceLower.setLength(0);
    sbSourceLower.trimToSize();
    sbSourceLower = null;

    return sbSource.toString();
}