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:org.agiso.core.lang.util.ObjectUtils.java

/**
 * Metoda generujca reprezentacj acuchow obiektu. Przeglda wszystkie
 * pola obiektu i pobiera ich reprezentacj acuchow. Na tej podstawie
 * generuje acuch wynikowy./*from   ww  w .j a  va  2 s. c  o m*/
 * 
 * @param clazz
 * @param object
 */
private static void toStringObject(Class<?> clazz, Object object) {
    StringBuffer buffer = (StringBuffer) ThreadUtils.getAttribute(TOSTRING_TCBUFF);

    String hexHash = Integer.toHexString(System.identityHashCode(object));
    if (0 == buffer.length()) {
        buffer.append(clazz.getSimpleName()).append('@').append(hexHash).append(TOSTRING_PREFIX);
    }

    @SuppressWarnings("unchecked")
    Set<String> converted = (Set<String>) ThreadUtils.getAttribute(TOSTRING_TCATTR);
    converted.add(object.getClass().getCanonicalName() + "@" + hexHash);

    // Rekurencyjne przegldanie wszystkich klas nadrzdnych:
    Class<?> superClass = clazz.getSuperclass();
    if (superClass != null) {
        toStringObject(superClass, object);
    }

    String hyphen = "";
    if (TOSTRING_PREFIX.charAt(0) != buffer.charAt(buffer.length() - 1)) {
        hyphen = TOSTRING_HYPHEN;
    }

    for (Field field : clazz.getDeclaredFields()) {
        try {
            field.setAccessible(true);
            if (field.isAnnotationPresent(InToString.class)) {
                InToString inToString = field.getAnnotation(InToString.class);
                if (!inToString.ignore()) {
                    String name = inToString.name();
                    buffer.append(hyphen).append((name.length() > 0) ? name : field.getName())
                            .append(TOSTRING_COLON);
                    toStringField(field.get(object));
                    hyphen = TOSTRING_HYPHEN;
                }
            } else if ((field.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) == 0) {
                buffer.append(hyphen).append(field.getName()).append(TOSTRING_COLON);
                toStringField(field.get(object));
                hyphen = TOSTRING_HYPHEN;
            }
        } catch (Exception e) {
            // TODO: Zaimplementowa logowanie wyjtku
            e.printStackTrace();
        }
    }
}

From source file:org.sakaiproject.imagegallery.springutil.SqlScriptParser.java

/**
 * Parse the SQL script to produce an array of database statements.
 * @param sqlScriptReader A reader that reads the SQL script text.
 * @return An array of strings, each containing a single statement.
 * @throws RuntimeException-wrapped IOException if the script cannot be read.
 * @throws RuntimeException-wrapped ParseException if the script cannot be parsed.
 *///from  www . j  a  v a  2  s  .  com
public static String[] parse(Reader sqlScriptReader) {
    char statementDelimiter = ';';
    List<String> statements = new ArrayList<String>();

    StringBuffer sql = new StringBuffer(1024);
    String line = "";
    BufferedReader in = new BufferedReader(sqlScriptReader);
    int lineNumber = 0;

    // Read each line and build up statements.
    try {
        while ((line = in.readLine()) != null) {
            lineNumber++;
            // Trim
            line = cleanLine(line);

            // Check for statement delimiter change.
            Character newDelimiter = parseForNewStatementDelimiter(line);
            if (newDelimiter != null) {
                statementDelimiter = newDelimiter.charValue();
                continue;
            }

            // Validate and strip comments
            parseLine(line, sql, lineNumber, statementDelimiter);
            if (sql.length() > 0) {
                if (sql.charAt(sql.length() - 1) == statementDelimiter) {
                    // This line terminates the statement.
                    // Lose the delimiter.
                    String statement = sql.toString().substring(0, sql.length() - 1).trim();
                    if (statement.length() > 0) {
                        statements.add(statement);
                        s_log.debug("Found statement: " + statement);
                    }
                    // Clear buffer for the next statement.
                    sql.replace(0, sql.length(), "");
                } else {
                    // This line does not terminate the statement. Add a
                    // space and go on to the next one.
                    sql.append(" ");
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // Catch any statements not followed by delimiter.
    String orphanStatement = sql.toString().trim();
    if (orphanStatement.length() > 0) {
        statements.add(orphanStatement);
        s_log.debug("Found statement: " + orphanStatement);
    }

    String[] result = new String[statements.size()];
    statements.toArray(result);
    return result;
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading whitespace from the given String.
 *
 * @param str the String to check/*w  ww.ja  va  2  s .  c om*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim trailing whitespace from the given String.
 *
 * @param str the String to check/*from   w w  w  .j  a v a 2s .c  o m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading and trailing whitespace from the given String.
 *
 * @param str the String to check//from   w w w  .  j  av  a2 s . c  o m
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:net.lightbody.bmp.proxy.jetty.util.URI.java

/** Convert a path to a cananonical form.
 * All instances of "." and ".." are factored out.  Null is returned
 * if the path tries to .. above it's root.
 * @param path /*from w  w w. ja v  a2  s  .com*/
 * @return path or null.
 */
public static String canonicalPath(String path) {
    if (path == null || path.length() == 0)
        return path;

    int end = path.length();
    int queryIdx = path.indexOf('?');
    int start = path.lastIndexOf('/', (queryIdx > 0 ? queryIdx : end));

    search: while (end > 0) {
        switch (end - start) {
        case 2: // possible single dot
            if (path.charAt(start + 1) != '.')
                break;
            break search;
        case 3: // possible double dot
            if (path.charAt(start + 1) != '.' || path.charAt(start + 2) != '.')
                break;
            break search;
        }

        end = start;
        start = path.lastIndexOf('/', end - 1);
    }

    // If we have checked the entire string
    if (start >= end)
        return path;

    StringBuffer buf = new StringBuffer(path);
    int delStart = -1;
    int delEnd = -1;
    int skip = 0;

    while (end > 0) {
        switch (end - start) {
        case 2: // possible single dot
            if (buf.charAt(start + 1) != '.') {
                if (skip > 0 && --skip == 0) {
                    delStart = start >= 0 ? start : 0;
                    if (delStart > 0 && delEnd == buf.length() && buf.charAt(delEnd - 1) == '.')
                        delStart++;
                }
                break;
            }

            if (start < 0 && buf.length() > 2 && buf.charAt(1) == '/' && buf.charAt(2) == '/')
                break;

            if (delEnd < 0)
                delEnd = end;
            delStart = start;
            if (delStart < 0 || delStart == 0 && buf.charAt(delStart) == '/') {
                delStart++;
                if (delEnd < buf.length() && buf.charAt(delEnd) == '/')
                    delEnd++;
                break;
            }
            if (end == buf.length())
                delStart++;

            end = start--;
            while (start >= 0 && buf.charAt(start) != '/')
                start--;
            continue;

        case 3: // possible double dot
            if (buf.charAt(start + 1) != '.' || buf.charAt(start + 2) != '.') {
                if (skip > 0 && --skip == 0) {
                    delStart = start >= 0 ? start : 0;
                    if (delStart > 0 && delEnd == buf.length() && buf.charAt(delEnd - 1) == '.')
                        delStart++;
                }
                break;
            }

            delStart = start;
            if (delEnd < 0)
                delEnd = end;

            skip++;
            end = start--;
            while (start >= 0 && buf.charAt(start) != '/')
                start--;
            continue;

        default:
            if (skip > 0 && --skip == 0) {
                delStart = start >= 0 ? start : 0;
                if (delEnd == buf.length() && buf.charAt(delEnd - 1) == '.')
                    delStart++;
            }
        }

        // Do the delete
        if (skip <= 0 && delStart >= 0 && delStart >= 0) {
            buf.delete(delStart, delEnd);
            delStart = delEnd = -1;
            if (skip > 0)
                delEnd = end;
        }

        end = start--;
        while (start >= 0 && buf.charAt(start) != '/')
            start--;
    }

    // Too many ..
    if (skip > 0)
        return null;

    // Do the delete
    if (delEnd >= 0)
        buf.delete(delStart, delEnd);

    return buf.toString();
}

From source file:org.apache.myfaces.custom.inputAjax.HtmlCommandButtonAjaxRenderer.java

protected StringBuffer buildOnClick(UIComponent uiComponent, FacesContext facesContext, ResponseWriter writer)
        throws IOException {
    String clientId = uiComponent.getClientId(facesContext);
    String submitFunctionStart = AjaxRendererUtils.JS_MYFACES_NAMESPACE + "ajaxSubmit3('" + clientId + "');";

    StringBuffer buf = super.buildOnClick(uiComponent, facesContext, writer);

    if (buf.length() != 0 && !(buf.charAt(buf.length() - 1) == ';')) {
        buf.append(";");
    }//  www.j ava 2 s.  c om
    buf.append(submitFunctionStart);

    return buf;
}

From source file:cn.remex.core.util.StringUtils.java

/**
 * /*from  ww w .  j  a v a2 s.c o m*/
 * Trim all occurences of the supplied leading character from the given String.
 * @param str the String to check 
 * @param leadingCharacter the leading character to be trimmed ?
 * @return the trimmed String ??
 */
public static String trimLeadingCharacter(final String str, final char leadingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(0) == leadingCharacter) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:cn.remex.core.util.StringUtils.java

/**
 * ?//from w w w  .  ja  v  a 2 s  .  c o  m
 * Trim all occurences of the supplied trailing character from the given String.
 * @param str the String to check 
 * @param trailingCharacter the trailing character to be trimmed ?
 * @return the trimmed String ??
 */
public static String trimTrailingCharacter(final String str, final char trailingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(buf.length() - 1) == trailingCharacter) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim <i>all</i> whitespace from the given String:
 * leading, trailing, and inbetween characters.
 *
 * @param str the String to check/*  w w w  .  j a v  a2 s  .  c  o  m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimAllWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    int index = 0;
    while (buf.length() > index) {
        if (Character.isWhitespace(buf.charAt(index))) {
            buf.deleteCharAt(index);
        } else {
            index++;
        }
    }
    return buf.toString();
}