Example usage for java.lang StringBuffer charAt

List of usage examples for java.lang StringBuffer charAt

Introduction

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

Prototype

@Override
public synchronized char charAt(int index) 

Source Link

Usage

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

private static boolean isNextChar(StringBuffer string, int index, char c) {
    boolean matches = false;
    if (index >= 0 && index < string.length() - 1) {
        matches = string.charAt(index + 1) == c;
    }//from ww w  .  j a  va  2s . co  m
    return matches;
}

From source file:org.nuxeo.apidoc.introspection.XMLWriter.java

/**
 * <p>/* w  ww .  j  a  va2s .  com*/
 * Escape the <code>toString</code> of the given object. For use as body
 * text.
 * </p>
 *
 * @param value
 *            escape <code>value.toString()</code>
 * @return text with escaped delimiters
 */
public static final String escapeBodyValue(Object value) {
    StringBuffer buffer = new StringBuffer(value.toString());
    for (int i = 0, size = buffer.length(); i < size; i++) {
        switch (buffer.charAt(i)) {
        case '<':
            buffer.replace(i, i + 1, LESS_THAN_ENTITY);
            size += 3;
            i += 3;
            break;
        case '>':
            buffer.replace(i, i + 1, GREATER_THAN_ENTITY);
            size += 3;
            i += 3;
            break;
        case '&':
            buffer.replace(i, i + 1, AMPERSAND_ENTITY);
            size += 4;
            i += 4;
            break;
        }
    }
    return buffer.toString();
}

From source file:org.nuxeo.apidoc.introspection.XMLWriter.java

/**
 * <p>//from   w ww  . ja  v  a  2s.c  om
 * Escape the <code>toString</code> of the given object. For use in an
 * attribute value.
 * </p>
 *
 * @param value
 *            escape <code>value.toString()</code>
 * @return text with characters restricted (for use in attributes) escaped
 */
public static final String escapeAttributeValue(Object value) {
    StringBuffer buffer = new StringBuffer(value.toString());
    for (int i = 0, size = buffer.length(); i < size; i++) {
        switch (buffer.charAt(i)) {
        case '<':
            buffer.replace(i, i + 1, LESS_THAN_ENTITY);
            size += 3;
            i += 3;
            break;
        case '>':
            buffer.replace(i, i + 1, GREATER_THAN_ENTITY);
            size += 3;
            i += 3;
            break;
        case '&':
            buffer.replace(i, i + 1, AMPERSAND_ENTITY);
            size += 4;
            i += 4;
            break;
        case '\'':
            buffer.replace(i, i + 1, APOSTROPHE_ENTITY);
            size += 5;
            i += 5;
            break;
        case '\"':
            buffer.replace(i, i + 1, QUOTE_ENTITY);
            size += 5;
            i += 5;
            break;
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static String normalize(final String s) {
    if (s == null) {
        return "";
    }//from   w w w  .j  ava  2  s .  c o  m

    final StringBuffer str = new StringBuffer();
    final int len = s.length();

    for (int i = 0; i < len; i++) {
        final char ch = s.charAt(i);

        switch (ch) {
        case '<': {
            str.append("&lt;");
            break;
        }
        case '>': {
            str.append("&gt;");
            break;
        }
        case '&': {
            str.append("&amp;");
            break;
        }
        case '"': {
            str.append("&quot;");
            break;
        }
        case '\n': {
            if (i > 0) {
                str.charAt(str.length() - 1);
                str.append("\n");
            } else {
                str.append("\n");
            }
            break;
        }
        default: {
            str.append(ch);
        }
        }
    }

    return (str.toString());
}

From source file:Unsigned.java

/**
 * Return the unsigned value of the number.
 * /*from w  w  w .j  av a  2s  .c  om*/
 * @param number
 *            a byte value
 * @return the unsigned value
 */
public static long unsigned(byte number) {
    long result = 0;
    BinaryFormat format = new BinaryFormat();
    String binary = format.format(number);

    StringBuffer buffer = new StringBuffer(binary);
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }

    return (result);
}

From source file:Unsigned.java

/**
 * Return the unsigned value of the number.
 * //  ww w  .  ja v  a  2 s .  c om
 * @param number
 *            a short value
 * @return the unsigned value
 */
public static long unsigned(short number) {
    long result = 0;
    BinaryFormat format = new BinaryFormat();
    String binary = format.format(number);

    StringBuffer buffer = new StringBuffer(binary);
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }

    return (result);
}

From source file:Unsigned.java

/**
 * Return the unsigned value of the number.
 * /*from   ww  w.ja  v a 2 s .  c om*/
 * @param number
 *            an int value
 * @return the unsigned value
 */
public static long unsigned(int number) {
    long result = 0;
    BinaryFormat format = new BinaryFormat();
    String binary = format.format(number);

    StringBuffer buffer = new StringBuffer(binary);
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }

    return (result);
}

From source file:org.dhatim.cdr.annotation.Configurator.java

private static String getPropertyName(Method method) {
    if (!method.getName().startsWith("set")) {
        return null;
    }/*from   w w  w  .j av  a  2  s. c o  m*/

    StringBuffer methodName = new StringBuffer(method.getName());

    if (methodName.length() < 4) {
        return null;
    }

    methodName.delete(0, 3);
    methodName.setCharAt(0, Character.toLowerCase(methodName.charAt(0)));

    return methodName.toString();
}

From source file:org.slage.framework.Tools.java

/**
 * Get a '?' delimited relative file path by starting with a localized
 * absolute path. This function will trim off the path to the working
 * directory and then replace file separators with ? characters (which can
 * then be read back into cross platform paths using GetQMarkDelimitedPath().
 * //from ww w.j  av a 2s  .  c  om
 * @param strPath an absolute file path (including file name if appropriate)
 * @return the '?' delimted relative path
 */
public static String BuildQMarkDelimitedPath(String strPath) {

    if (strPath == null) {
        return null;
    }

    String currentDirectory = System.getProperty("user.dir");
    if (strPath.startsWith(currentDirectory)) {
        strPath = strPath.substring(currentDirectory.length());
    }

    if (strPath.startsWith(GetFileSeparator())) {
        strPath = strPath.substring(GetFileSeparator().length());
    }

    StringBuffer sb = new StringBuffer(strPath);

    // For some reason, replaceAll crashes...
    for (int i = 0; i < strPath.length(); i++) {
        if (sb.charAt(i) == GetFileSeparator().charAt(0)) {
            sb.setCharAt(i, '?');
        }
    }
    return sb.toString();
}

From source file:jdiff.API.java

/**
 * Convert all HTML tags to text by stuffing text into the HTML tag
 * to stop it being an HTML or XML tag. E.g. &quot;<code>foo</code>&quot;
 * becomes &quot;lEsS_tHaNcode>foolEsS_tHaN/code>&quot;. Replace all &lt; 
 * characters/*w ww.ja v  a  2s.  com*/
 * with the string "lEsS_tHaN". Also replace &amp; character with the  
 * string "aNd_cHaR" to avoid text entities. Also replace &quot; 
 * character with the  
 * string "qUoTe_cHaR".
 */
public static String hideHTMLTags(String htmlText) {
    StringBuffer sb = new StringBuffer(htmlText);
    int i = 0;
    while (i < sb.length()) {
        if (sb.charAt(i) == '<') {
            sb.setCharAt(i, 'l');
            sb.insert(i + 1, "EsS_tHaN");
        } else if (sb.charAt(i) == '&') {
            sb.setCharAt(i, 'a');
            sb.insert(i + 1, "Nd_cHaR");
        } else if (sb.charAt(i) == '"') {
            sb.setCharAt(i, 'q');
            sb.insert(i + 1, "uote_cHaR");
        }
        i++;
    }
    return sb.toString();
}