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:edu.harvard.iq.dvn.core.web.admin.EditLockssConfigPage.java

private static boolean doValidate(Object value) {
    boolean valid = false;
    String address = value.toString();
    // first, assume it's a domain name
    if (address.startsWith("*.")) {
        StringBuffer sb = new StringBuffer(address);
        sb.setCharAt(0, 'a');
        address = sb.toString();//from  w  w  w  . j  a v a 2s .  c  om
    }
    valid = validateDomainName(address);

    if (!valid) {
        // Try to validate it as an ip address
        String ipAddress = value.toString();

        // for the purposes of validation, if the string ends in ".*",
        // replace it with dummy data for the validator.
        if (ipAddress.endsWith(".*")) {
            StringBuffer sb = new StringBuffer(ipAddress);
            sb.setCharAt(ipAddress.length() - 1, '1');
            ipAddress = sb.toString();
            // if necessary, add dummy values to the end of the string,
            // so it will pass validation.
            String[] splitStrings = ipAddress.split("\\.");
            if (splitStrings.length == 2) {
                ipAddress += ".1.1";
            } else if (splitStrings.length == 3) {
                ipAddress += ".1";
            }
        }
        InetAddressValidator val = InetAddressValidator.getInstance();

        valid = val.isValid(ipAddress);
    }
    return valid;
}

From source file:ubic.basecode.util.StringUtil.java

/**
 * Made by Nicolas// w  ww .  ja v  a 2 s. com
 * 
 * @param a line in a file cvs format
 * @return the same line but in tsv format
 */
public static String cvs2tsv(String line) {

    StringBuffer newLine = new StringBuffer(line);

    boolean change = true;

    for (int position = 0; position < newLine.length(); position++) {

        if (newLine.charAt(position) == ',' && change) {
            newLine.setCharAt(position, '\t');
        } else if (newLine.charAt(position) == '"') {

            if (change) {
                change = false;
            } else {
                change = true;
            }
        }
    }
    return newLine.toString().replaceAll("\"", "");
}

From source file:ExecuteSQL.java

/** This utility method is used when printing the table of results */
static void overwrite(StringBuffer b, int pos, String s) {
    int slen = s.length(); // string length
    int blen = b.length(); // buffer length
    if (pos + slen > blen)
        slen = blen - pos; // does it fit?
    for (int i = 0; i < slen; i++)
        // copy string into buffer
        b.setCharAt(pos + i, s.charAt(i));
}

From source file:SystemIDResolver.java

/**
 * Replace spaces with "%20" and backslashes with forward slashes in 
 * the input string to generate a well-formed URI string.
 *
 * @param str The input string//from  w  ww  .  j  a  v  a  2 s.co m
 * @return The string after conversion
 */
private static String replaceChars(String str) {
    StringBuffer buf = new StringBuffer(str);
    int length = buf.length();
    for (int i = 0; i < length; i++) {
        char currentChar = buf.charAt(i);
        // Replace space with "%20"
        if (currentChar == ' ') {
            buf.setCharAt(i, '%');
            buf.insert(i + 1, "20");
            length = length + 2;
            i = i + 2;
        }
        // Replace backslash with forward slash
        else if (currentChar == '\\') {
            buf.setCharAt(i, '/');
        }
    }

    return buf.toString();
}

From source file:org.squale.squalecommon.util.mapping.Mapping.java

/**
 * Obtention du getter d'une mtrique La mthode est recherche dans la classe correspondante, la mthode est du
 * type getXxxx//from  w  w w.j ava 2 s  .  co m
 * 
 * @param pMetric mtrique (par exemple mccabe.class.dit)
 * @return mthode d'accs  cette mtrique (par exemple McCabeQAClassMetricsBO.getDit)
 * @throws IllegalArgumentException si pMetric mal form
 */
public static Method getMetricGetter(String pMetric) throws IllegalArgumentException {
    Method result = null;
    int index = pMetric.lastIndexOf('.');
    if (index < 0) {
        throw new IllegalArgumentException("Metric name should be named tool.component.name");
    }
    String measure = pMetric.substring(0, index);
    String metric = pMetric.substring(index + 1);
    // Obtention de la classe
    Class cl = getMeasureClass(measure);
    // Un log d'erreur aura t fait par la mthode
    // auparavant, inutile d'engorger le log
    if (cl != null) {
        try {
            // On forme le nom du getter
            StringBuffer methodName = new StringBuffer(metric);
            // Respect de la norme javabean pour le caractre en majuscules
            methodName.setCharAt(0, Character.toTitleCase(methodName.charAt(0)));
            methodName.insert(0, "get");
            result = cl.getMethod(methodName.toString(), null);
        } catch (SecurityException e) {
            LOG.error("Getter not found " + pMetric, e);
        } catch (NoSuchMethodException e) {
            LOG.error("Getter not found " + pMetric, e);
        }
    }
    return result;
}

From source file:org.eclim.plugin.jdt.command.junit.JUnitUtils.java

private static void appendParameterNamesToMethodName(StringBuffer buffer, String[] parameters) {
    for (String parameter : parameters) {
        final StringBuffer buf = new StringBuffer(
                Signature.getSimpleName(Signature.toString(Signature.getElementType(parameter))));
        final char character = buf.charAt(0);
        if (buf.length() > 0 && !Character.isUpperCase(character)) {
            buf.setCharAt(0, Character.toUpperCase(character));
        }/*from  w  w w.ja v a  2 s. c  om*/
        buffer.append(buf.toString());
        for (int j = 0, arrayCount = Signature.getArrayCount(parameter); j < arrayCount; j++) {
            buffer.append("Array");
        }
    }
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

/**
 * Encode a String so that is is a valid JMS header name
 *
 * @param name the String to encode// w w  w  . j a v  a 2 s  .  c om
 * @return a valid JMS header name
 */
public static String encodeHeader(String name) {
    // check against JMS 1.1 spec, sections 3.5.1 (3.8.1.1)
    boolean nonCompliant = false;

    if (StringUtils.isEmpty(name)) {
        throw new IllegalArgumentException("Header name to encode must not be null or empty");
    }

    int i = 0, length = name.length();
    while (i < length && Character.isJavaIdentifierPart(name.charAt(i))) {
        // zip through
        i++;
    }

    if (i == length) {
        // String is already valid
        return name;
    } else {
        // make a copy, fix up remaining characters
        StringBuffer sb = new StringBuffer(name);
        for (int j = i; j < length; j++) {
            if (!Character.isJavaIdentifierPart(sb.charAt(j))) {
                sb.setCharAt(j, REPLACEMENT_CHAR);
                nonCompliant = true;
            }
        }

        if (nonCompliant) {
            logger.warn(MessageFormat.format(
                    "Header: {0} is not compliant with JMS specification (sec. 3.5.1, 3.8.1.1). It will cause "
                            + "problems in your and other applications. Please update your application code to correct this. "
                            + "Mule renamed it to {1}",
                    name, sb.toString()));
        }

        return sb.toString();
    }
}

From source file:ExecuteSQL.java

/**
 * This method attempts to output the contents of a ResultSet in a textual
 * table. It relies on the ResultSetMetaData class, but a fair bit of the
 * code is simple string manipulation.// www .ja  v  a  2s .c  o  m
 */
static void printResultsTable(ResultSet rs, OutputStream output) throws SQLException {
    // Set up the output stream
    PrintWriter out = new PrintWriter(output);

    // Get some "meta data" (column names, etc.) about the results
    ResultSetMetaData metadata = rs.getMetaData();

    // Variables to hold important data about the table to be displayed
    int numcols = metadata.getColumnCount(); // how many columns
    String[] labels = new String[numcols]; // the column labels
    int[] colwidths = new int[numcols]; // the width of each
    int[] colpos = new int[numcols]; // start position of each
    int linewidth; // total width of table

    // Figure out how wide the columns are, where each one begins,
    // how wide each row of the table will be, etc.
    linewidth = 1; // for the initial '|'.
    for (int i = 0; i < numcols; i++) { // for each column
        colpos[i] = linewidth; // save its position
        labels[i] = metadata.getColumnLabel(i + 1); // get its label
        // Get the column width. If the db doesn't report one, guess
        // 30 characters. Then check the length of the label, and use
        // it if it is larger than the column width
        int size = metadata.getColumnDisplaySize(i + 1);
        if (size == -1)
            size = 30; // Some drivers return -1...
        if (size > 500)
            size = 30; // Don't allow unreasonable sizes
        int labelsize = labels[i].length();
        if (labelsize > size)
            size = labelsize;
        colwidths[i] = size + 1; // save the column the size
        linewidth += colwidths[i] + 2; // increment total size
    }

    // Create a horizontal divider line we use in the table.
    // Also create a blank line that is the initial value of each
    // line of the table
    StringBuffer divider = new StringBuffer(linewidth);
    StringBuffer blankline = new StringBuffer(linewidth);
    for (int i = 0; i < linewidth; i++) {
        divider.insert(i, '-');
        blankline.insert(i, " ");
    }
    // Put special marks in the divider line at the column positions
    for (int i = 0; i < numcols; i++)
        divider.setCharAt(colpos[i] - 1, '+');
    divider.setCharAt(linewidth - 1, '+');

    // Begin the table output with a divider line
    out.println(divider);

    // The next line of the table contains the column labels.
    // Begin with a blank line, and put the column names and column
    // divider characters "|" into it. overwrite() is defined below.
    StringBuffer line = new StringBuffer(blankline.toString());
    line.setCharAt(0, '|');
    for (int i = 0; i < numcols; i++) {
        int pos = colpos[i] + 1 + (colwidths[i] - labels[i].length()) / 2;
        overwrite(line, pos, labels[i]);
        overwrite(line, colpos[i] + colwidths[i], " |");
    }

    // Then output the line of column labels and another divider
    out.println(line);
    out.println(divider);

    // Now, output the table data. Loop through the ResultSet, using
    // the next() method to get the rows one at a time. Obtain the
    // value of each column with getObject(), and output it, much as
    // we did for the column labels above.
    while (rs.next()) {
        line = new StringBuffer(blankline.toString());
        line.setCharAt(0, '|');
        for (int i = 0; i < numcols; i++) {
            Object value = rs.getObject(i + 1);
            if (value != null)
                overwrite(line, colpos[i] + 1, value.toString().trim());
            overwrite(line, colpos[i] + colwidths[i], " |");
        }
        out.println(line);
    }

    // Finally, end the table with one last divider line.
    out.println(divider);
    out.flush();
}

From source file:CryptPassword.java

public static final String crypt(String salt, String original) {
    while (salt.length() < 2)
        salt += "A";

    StringBuffer buffer = new StringBuffer("             ");

    char charZero = salt.charAt(0);
    char charOne = salt.charAt(1);

    buffer.setCharAt(0, charZero);
    buffer.setCharAt(1, charOne);/*from w w  w. j  a  va2s.  co m*/

    int Eswap0 = con_salt[(int) charZero];
    int Eswap1 = con_salt[(int) charOne] << 4;

    byte key[] = new byte[8];

    for (int i = 0; i < key.length; i++)
        key[i] = (byte) 0;

    for (int i = 0; i < key.length && i < original.length(); i++) {
        int iChar = (int) original.charAt(i);

        key[i] = (byte) (iChar << 1);
    }

    int schedule[] = des_set_key(key);
    int out[] = body(schedule, Eswap0, Eswap1);

    byte b[] = new byte[9];

    intToFourBytes(out[0], b, 0);
    intToFourBytes(out[1], b, 4);
    b[8] = 0;

    for (int i = 2, y = 0, u = 0x80; i < 13; i++) {
        for (int j = 0, c = 0; j < 6; j++) {
            c <<= 1;

            if (((int) b[y] & u) != 0)
                c |= 1;

            u >>>= 1;

            if (u == 0) {
                y++;
                u = 0x80;
            }
            buffer.setCharAt(i, (char) cov_2char[c]);
        }
    }
    return (buffer.toString());
}

From source file:Main.java

/**
 * Replaces HTML entities in a string buffer
 * @param buffer the string buffer/* w w  w .j  a  v  a 2 s .c  om*/
 */
public static void replaceEntities(StringBuffer buffer) {
    int i = 0;
    while (i < buffer.length()) {
        if (buffer.charAt(i) == '&') {
            int j = i + 1;
            while (j < buffer.length() && buffer.charAt(j) != ';')
                j++;
            if (j < buffer.length()) {
                char[] chars = new char[j - i - 1];
                buffer.getChars(i + 1, j, chars, 0);
                Character repl = (Character) replacements.get(new String(chars));
                if (repl != null) {
                    buffer.delete(i, j);
                    buffer.setCharAt(i, repl.charValue());
                } else
                    i = j;
            } else
                i = j;
        }
        i++;
    }
}