Example usage for java.lang StringBuffer setCharAt

List of usage examples for java.lang StringBuffer setCharAt

Introduction

In this page you can find the example usage for java.lang StringBuffer setCharAt.

Prototype

@Override
public synchronized void setCharAt(int index, char ch) 

Source Link

Usage

From source file:org.milyn.javabean.BeanPopulator.java

private String toBeanId(String beanClassName) {
    String[] beanClassNameTokens = beanClassName.split("\\.");
    StringBuffer simpleClassName = new StringBuffer(beanClassNameTokens[beanClassNameTokens.length - 1]);

    // Lowercase the first char...
    simpleClassName.setCharAt(0, Character.toLowerCase(simpleClassName.charAt(0)));

    return simpleClassName.toString();
}

From source file:bboss.org.apache.velocity.runtime.parser.node.PropertyExecutor.java

/**
 * @param clazz/*from   w w  w.  ja v  a  2  s  .  c  o m*/
 * @param property
 */
protected void discover(final Class clazz, final String property) {
    /*
     *  this is gross and linear, but it keeps it straightforward.
     */

    try {
        Object[] params = {};

        StringBuffer sb = new StringBuffer("get");
        sb.append(property);

        setMethod(introspector.getMethod(clazz, sb.toString(), params));

        if (!isAlive()) {
            /*
             *  now the convenience, flip the 1st character
             */

            char c = sb.charAt(3);

            if (Character.isLowerCase(c)) {
                sb.setCharAt(3, Character.toUpperCase(c));
            } else {
                sb.setCharAt(3, Character.toLowerCase(c));
            }

            setMethod(introspector.getMethod(clazz, sb.toString(), params));
        }
    }
    /**
     * pass through application level runtime exceptions
     */
    catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        String msg = "Exception while looking for property getter for '" + property;
        log.error(msg, e);
        throw new VelocityException(msg, e);
    }
}

From source file:org.apache.struts.config.ActionConfigMatcher.java

/**
 * <p> Inserts into a value wildcard-matched strings where specified.
 * </p>//  www. jav a2 s  .  c  o  m
 *
 * @param val  The value to convert
 * @param vars A Map of wildcard-matched strings
 * @return The new value
 */
protected String convertParam(String val, Map vars) {
    if (val == null) {
        return null;
    } else if (val.indexOf("{") == -1) {
        return val;
    }

    Map.Entry entry;
    StringBuffer key = new StringBuffer("{0}");
    StringBuffer ret = new StringBuffer(val);
    String keyTmp;
    int x;

    for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
        entry = (Map.Entry) i.next();
        key.setCharAt(1, ((String) entry.getKey()).charAt(0));
        keyTmp = key.toString();

        // Replace all instances of the placeholder
        while ((x = ret.toString().indexOf(keyTmp)) > -1) {
            ret.replace(x, x + 3, (String) entry.getValue());
        }
    }

    return ret.toString();
}

From source file:org.zeroturnaround.jenkins.LiveRebelProxy.java

public String sanitize(String name) {
    StringBuffer sb = new StringBuffer(name);
    for (int i = 0; i < sb.length(); i++)
        if (!Character.isLetterOrDigit(sb.charAt(i)))
            sb.setCharAt(i, '-');
    sb.append("-");
    sb.append(DigestUtils.md5Hex(name));
    return sb.toString();
}

From source file:org.apache.crunch.types.writable.TupleWritable.java

/**
 * Convert Tuple to String as in the following.
 * <tt>[<child1>,<child2>,...,<childn>]</tt>
 *//*from ww w  . j  ava 2  s  .  co m*/
public String toString() {
    StringBuffer buf = new StringBuffer("[");
    for (int i = 0; i < values.length; ++i) {
        if (has(i)) {
            buf.append(values[i].toString());
        }
        buf.append(",");
    }
    if (values.length != 0)
        buf.setCharAt(buf.length() - 1, ']');
    else
        buf.append(']');
    return buf.toString();
}

From source file:com.commander4j.util.JUtility.java

public static String removeChar(String s, char c) {
    StringBuffer r = new StringBuffer(s.length());
    r.setLength(s.length());/*  w  w w .  j  a  v  a  2s .com*/
    int current = 0;
    for (int i = 0; i < s.length(); i++) {
        char cur = s.charAt(i);
        if (cur != c)
            r.setCharAt(current++, cur);
    }
    return r.toString();
}

From source file:com.ms.commons.log.MsSyslogAppender.java

/**
 * Gets HEADER portion of packet.//from www  .j  ava  2  s .c om
 * 
 * @param timeStamp number of milliseconds after the standard base time.
 * @return HEADER portion of packet, will be zero-length string if header is false.
 * @since 1.2.15
 */
private String getPacketHeader(final long timeStamp) {
    if (header) {
        StringBuffer buf = new StringBuffer(dateFormat.format(new Date(timeStamp)));
        // RFC 3164 says leading space, not leading zero on days 1-9
        if (buf.charAt(4) == '0') {
            buf.setCharAt(4, ' ');
        }
        buf.append(getLocalHostname());
        buf.append(' ');
        return buf.toString();
    }
    return "";
}

From source file:com.niki.normalizer.Metaphone.java

/**
 * Find the metaphone value of a String. This is similar to the
 * soundex algorithm, but better at finding similar sounding words.
 * All input is converted to upper case.
 * Limitations: Input format is expected to be a single ASCII word
 * with only characters in the A - Z range, no punctuation or numbers.
 *
 * @param txt String to find the metaphone code for
 * @return A metaphone code corresponding to the String supplied
 *///from   w  w  w  .  j a va 2 s. c  om
public static String metaphone(String txt) {
    boolean hard = false;
    if ((txt == null) || (txt.length() == 0)) {
        return "";
    }
    // single character is itself
    if (txt.length() == 1) {
        return txt.toUpperCase(java.util.Locale.ENGLISH);
    }

    char[] inwd = txt.toUpperCase(java.util.Locale.ENGLISH).toCharArray();

    StringBuffer local = new StringBuffer(40); // manipulate
    StringBuffer code = new StringBuffer(10); //   output
    // handle initial 2 characters exceptions
    switch (inwd[0]) {
    case 'K':
    case 'G':
    case 'P': /* looking for KN, etc*/
        if (inwd[1] == 'N') {
            local.append(inwd, 1, inwd.length - 1);
        } else {
            local.append(inwd);
        }
        break;
    case 'A': /* looking for AE */
        if (inwd[1] == 'E') {
            local.append(inwd, 1, inwd.length - 1);
        } else {
            local.append(inwd);
        }
        break;
    case 'W': /* looking for WR or WH */
        if (inwd[1] == 'R') { // WR -> R
            local.append(inwd, 1, inwd.length - 1);
            break;
        }
        if (inwd[1] == 'H') {
            local.append(inwd, 1, inwd.length - 1);
            local.setCharAt(0, 'W'); // WH -> W
        } else {
            local.append(inwd);
        }
        break;
    case 'X': /* initial X becomes S */
        inwd[0] = 'S';
        local.append(inwd);
        break;
    default:
        local.append(inwd);
    } // now local has working string with initials fixed

    int wdsz = local.length();
    int n = 0;

    while ((code.length() < 4) && (n < wdsz)) { // max code size of 4 works well
        char symb = local.charAt(n);
        // remove duplicate letters except C
        if ((symb != 'C') && (isPreviousChar(local, n, symb))) {
            n++;
        } else { // not dup
            switch (symb) {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                if (n == 0) {
                    code.append(symb);
                }
                break; // only use vowel if leading char
            case 'B':
                if (isPreviousChar(local, n, 'M') && isLastChar(wdsz, n)) { // B is silent if word ends in MB
                    break;
                }
                code.append(symb);
                break;
            case 'C': // lots of C special cases
                /* discard if SCI, SCE or SCY */
                if (isPreviousChar(local, n, 'S') && !isLastChar(wdsz, n)
                        && (FRONTV.indexOf(local.charAt(n + 1)) >= 0)) {
                    break;
                }
                if (regionMatch(local, n, "CIA")) { // "CIA" -> X
                    code.append('X');
                    break;
                }
                if (!isLastChar(wdsz, n) && (FRONTV.indexOf(local.charAt(n + 1)) >= 0)) {
                    code.append('S');
                    break; // CI,CE,CY -> S
                }
                if (isPreviousChar(local, n, 'S') && isNextChar(local, n, 'H')) { // SCH->sk
                    code.append('K');
                    break;
                }
                if (isNextChar(local, n, 'H')) { // detect CH
                    if ((n == 0) && (wdsz >= 3) && isVowel(local, 2)) { // CH consonant -> K consonant
                        code.append('K');
                    } else {
                        code.append('X'); // CHvowel -> X
                    }
                } else {
                    code.append('K');
                }
                break;
            case 'D':
                if (!isLastChar(wdsz, n + 1) && isNextChar(local, n, 'G')
                        && (FRONTV.indexOf(local.charAt(n + 2)) >= 0)) { // DGE DGI DGY -> J 
                    code.append('J');
                    n += 2;
                } else {
                    code.append('T');
                }
                break;
            case 'G': // GH silent at end or before consonant
                if (isLastChar(wdsz, n + 1) && isNextChar(local, n, 'H')) {
                    break;
                }
                if (!isLastChar(wdsz, n + 1) && isNextChar(local, n, 'H') && !isVowel(local, n + 2)) {
                    break;
                }
                if ((n > 0) && (regionMatch(local, n, "GN") || regionMatch(local, n, "GNED"))) {
                    break; // silent G
                }
                if (isPreviousChar(local, n, 'G')) {
                    // NOTE: Given that duplicated chars are removed, I don't see how this can ever be true
                    hard = true;
                } else {
                    hard = false;
                }
                if (!isLastChar(wdsz, n) && (FRONTV.indexOf(local.charAt(n + 1)) >= 0) && (!hard)) {
                    code.append('J');
                } else {
                    code.append('K');
                }
                break;
            case 'H':
                if (isLastChar(wdsz, n)) {
                    break; // terminal H
                }
                if ((n > 0) && (VARSON.indexOf(local.charAt(n - 1)) >= 0)) {
                    break;
                }
                if (isVowel(local, n + 1)) {
                    code.append('H'); // Hvowel
                }
                break;
            case 'F':
            case 'J':
            case 'L':
            case 'M':
            case 'N':
            case 'R':
                code.append(symb);
                break;
            case 'K':
                if (n > 0) { // not initial
                    if (!isPreviousChar(local, n, 'C')) {
                        code.append(symb);
                    }
                } else {
                    code.append(symb); // initial K
                }
                break;
            case 'P':
                if (isNextChar(local, n, 'H')) {
                    // PH -> F
                    code.append('F');
                } else {
                    code.append(symb);
                }
                break;
            case 'Q':
                code.append('K');
                break;
            case 'S':
                if (regionMatch(local, n, "SH") || regionMatch(local, n, "SIO")
                        || regionMatch(local, n, "SIA")) {
                    code.append('X');
                } else {
                    code.append('S');
                }
                break;
            case 'T':
                if (regionMatch(local, n, "TIA") || regionMatch(local, n, "TIO")) {
                    code.append('X');
                    break;
                }
                if (regionMatch(local, n, "TCH")) {
                    // Silent if in "TCH"
                    break;
                }
                // substitute numeral 0 for TH (resembles theta after all)
                if (regionMatch(local, n, "TH")) {
                    code.append('0');
                } else {
                    code.append('T');
                }
                break;
            case 'V':
                code.append('F');
                break;
            case 'W':
            case 'Y': // silent if not followed by vowel
                if (!isLastChar(wdsz, n) && isVowel(local, n + 1)) {
                    code.append(symb);
                }
                break;
            case 'X':
                code.append('K');
                code.append('S');
                break;
            case 'Z':
                code.append('S');
                break;
            } // end switch
            n++;
        } // end else from symb != 'C'
        if (code.length() > 4) {
            code.setLength(4);
        }
    }
    return code.toString();
}

From source file:org.apache.lucene.analysis.de.GermanStemmer.java

/**
  * Does some optimizations on the term. This optimisations are contextual.
  *///w w  w.j  av a2 s .  c o m
 private void optimize(StringBuffer buffer) {
     // Additional step for female plurals of professions and inhabitants.
     if (buffer.length() > 5 && buffer.substring(buffer.length() - 5, buffer.length()).equals("erin*")) {
         buffer.deleteCharAt(buffer.length() - 1);
         strip(buffer);
     }
     // Additional step for irregular plural nouns like "Matrizen -> Matrix".
     if (buffer.charAt(buffer.length() - 1) == ('z')) {
         buffer.setCharAt(buffer.length() - 1, 'x');
     }
 }

From source file:org.apache.axis.utils.JavaUtils.java

/**
 * Makes the value passed in <code>initValue</code> unique among the
 * {@link String} values contained in <code>values</code> by suffixing
 * it with a decimal digit suffix.//from www .ja  v a 2s .  c o  m
 */
public static String getUniqueValue(Collection values, String initValue) {

    if (!values.contains(initValue)) {
        return initValue;
    } else {

        StringBuffer unqVal = new StringBuffer(initValue);
        int beg = unqVal.length(), cur, end;
        while (Character.isDigit(unqVal.charAt(beg - 1))) {
            beg--;
        }
        if (beg == unqVal.length()) {
            unqVal.append('1');
        }
        cur = end = unqVal.length() - 1;

        while (values.contains(unqVal.toString())) {

            if (unqVal.charAt(cur) < '9') {
                unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1));
            }

            else {

                while (cur-- > beg) {
                    if (unqVal.charAt(cur) < '9') {
                        unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1));
                        break;
                    }
                }

                // See if there's a need to insert a new digit.
                if (cur < beg) {
                    unqVal.insert(++cur, '1');
                    end++;
                }
                while (cur < end) {
                    unqVal.setCharAt(++cur, '0');
                }

            }

        }

        return unqVal.toString();

    } /*  For  else  clause  of  selection-statement   If(!values ...   */

}