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:jdiff.API.java

/** 
 * <b>NOT USED</b>. /*from   w  ww .  j ava 2  s  .c  om*/
 *
 * Replace all instances of <p> with <p/>. Just for the small number
 * of HMTL tags which don't require a matching end tag.
 * Also make HTML conform to the simple HTML requirements such as 
 * no double hyphens. Double hyphens are replaced by - and the character
 * entity for a hyphen.
 *
 * Cases where this fails and has to be corrected in the XML by hand: 
 *  Attributes' values missing their double quotes , e.g. size=-2
 *  Mangled HTML tags e.g. &lt;ttt> 
 *
 * <p><b>NOT USED</b>. There is often too much bad HTML in
 * doc blocks to try to handle every case correctly. Better just to
 * stuff the *lt; and &amp: characters with stuffHTMLTags(). Though
 * the resulting XML is not as elegant, it does the job with less
 * intervention by the user.
 */
public static String convertHTMLTagsToXHTML(String htmlText) {
    StringBuffer sb = new StringBuffer(htmlText);
    int i = 0;
    boolean inTag = false;
    String tag = null;
    // Needs to re-evaluate this length at each loop
    while (i < sb.length()) {
        char c = sb.charAt(i);
        if (inTag) {
            if (c == '>') {
                // OPTION Could fail at or fix some errorneous tags here
                // Make the best guess as to whether this tag is terminated
                if (Comments.isMinimizedTag(tag) && htmlText.indexOf("</" + tag + ">", i) == -1)
                    sb.insert(i, "/");
                inTag = false;
            } else {
                // OPTION could also make sure that attribute values are
                // surrounded by quotes.
                tag += c;
            }
        }
        if (c == '<') {
            inTag = true;
            tag = "";
        }
        // -- is not allowed in XML, but !-- is part of an comment,
        // and --> is also part of a comment
        if (c == '-' && i > 0 && sb.charAt(i - 1) == '-') {
            if (!(i > 1 && sb.charAt(i - 2) == '!')) {
                sb.setCharAt(i, '&');
                sb.insert(i + 1, "#045;");
                i += 5;
            }
        }
        i++;
    }
    if (inTag) {
        // Oops. Someone forgot to close their HTML tag, e.g. "<code."
        // Close it for them.
        sb.insert(i, ">");
    }
    return sb.toString();
}

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

/** Add two URI path segments.
 * Handles null and empty paths, path and query params (eg ?a=b or
 * ;JSESSIONID=xxx) and avoids duplicate '/'
 * @param p1 URI path segment //  ww  w. j a va 2s.  c  o  m
 * @param p2 URI path segment
 * @return Legally combined path segments.
 */
public static String addPaths(String p1, String p2) {
    if (p1 == null || p1.length() == 0) {
        if (p2 == null || p2.length() == 0)
            return p1;
        return p2;
    }
    if (p2 == null || p2.length() == 0)
        return p1;

    int split = p1.indexOf(';');
    if (split < 0)
        split = p1.indexOf('?');
    if (split == 0)
        return p2 + p1;
    if (split < 0)
        split = p1.length();

    StringBuffer buf = new StringBuffer(p1.length() + p2.length() + 2);
    buf.append(p1);

    if (buf.charAt(split - 1) == '/') {
        if (p2.startsWith("/")) {
            buf.deleteCharAt(split - 1);
            buf.insert(split - 1, p2);
        } else
            buf.insert(split, p2);
    } else {
        if (p2.startsWith("/"))
            buf.insert(split, p2);
        else {
            buf.insert(split, '/');
            buf.insert(split + 1, p2);
        }
    }

    return buf.toString();
}

From source file:org.wso2.andes.management.ui.views.ViewUtility.java

/**
 * Converts the input string to displayable format by converting some character case or inserting space
 * @param input/*from ww  w .  j  a  v  a 2 s  .  c om*/
 * @return formatted string
 */
public static String getDisplayText(String input) {
    StringBuffer result = new StringBuffer(input);
    if (Character.isLowerCase(result.charAt(0))) {
        result.setCharAt(0, Character.toUpperCase(result.charAt(0)));
    }
    for (int i = 1; i < input.length(); i++) {
        if (Character.isUpperCase(result.charAt(i)) && !Character.isWhitespace(result.charAt(i - 1))
                && Character.isLowerCase(result.charAt(i - 1))) {
            result.insert(i, " ");
            i++;
        } else if (Character.isLowerCase(result.charAt(i)) && Character.isWhitespace(result.charAt(i - 1))) {
            result.setCharAt(i, Character.toUpperCase(result.charAt(i)));
        }

    }

    return result.toString();
}

From source file:org.qedeq.base.utility.StringUtility.java

/**
 * Search for first line followed by whitespace and delete this string within the whole
 * text.//  ww  w  . j  a  v a  2  s . c o  m
 * <p>
 * For example the following text
 *<pre>
 *       Do you know the muffin man,
 *       The muffin man, the muffin man,
 *       Do you know the muffin man,
 *       Who lives on Drury Lane?
 *</pre>
 * will be converted into:
 *<pre>
 *Do you know the muffin man,
 *The muffin man, the muffin man,
 *Do you know the muffin man,
 *Who lives on Drury Lane?
 *</pre>
 *
 * @param   buffer  Work on this text.
 */
public static void deleteLineLeadingWhitespace(final StringBuffer buffer) {
    int current = 0;
    int lastLf = -1;

    // detect position of last line feed before content starts (lastLf)
    while (current < buffer.length()) {
        if (!Character.isWhitespace(buffer.charAt(current))) {
            break;
        }
        if ('\n' == buffer.charAt(current)) {
            lastLf = current;
        }
        current++;
    }
    // string from last whitespace line feed until first non whitespace
    final String empty = buffer.substring(lastLf + 1, current);

    // delete this string out of the text
    if (empty.length() > 0) {
        //            System.out.println(string2Hex(empty));
        buffer.delete(lastLf + 1, current); // delete first occurence
        replace(buffer, "\n" + empty, "\n"); // delete same whitespace on all following lines
    }
}

From source file:com.easyjf.util.StringUtils.java

/**
 * Trim leading whitespace from the given String.
 * //  w ww  . j  av  a2s . c  o m
 * @param str
 *            the String to check
 * @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:com.easyjf.util.StringUtils.java

/**
 * Trim trailing whitespace from the given String.
 * /*from   w w w  .  j  a  v  a  2 s  .  co  m*/
 * @param str
 *            the String to check
 * @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:com.easyjf.util.StringUtils.java

/**
 * Trim leading and trailing whitespace from the given String.
 * /*from  www. ja v  a2  s  .c  o  m*/
 * @param str
 *            the String to check
 * @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:Main.java

/**
/* FindAndReplace - finds and replaces in StringBuffer theSBuffer,
/*   starts at fromIndex, returns index of find.
 * /* w  w  w  . j a v a2s .  c  o  m*/
 * @param findString, replaceString, sBuffer, index
 * @return int
 * 
 */
public static int FindAndReplace(String find, String replace, StringBuffer theSBuffer, int fromIndex) {

    String interString;
    int theIndex, i, j;

    if (find == null)
        return -1;
    if (replace == null)
        return -1;
    if (theSBuffer == null)
        return -1;
    int theSBufferLength = theSBuffer.length();
    int findLength = find.length();
    if (theSBufferLength == 0)
        return -1;
    if (findLength == 0)
        return -1;
    if (theSBufferLength < findLength)
        return -1;
    if ((fromIndex < 0) || (fromIndex > theSBufferLength))
        return -1;

    interString = theSBuffer.toString();
    theIndex = interString.indexOf(find, fromIndex);
    if (theIndex == -1)
        return -1;

    //// on 9210 the following code ...
    for (i = theIndex; i < theSBufferLength - findLength; i++) {
        theSBuffer.setCharAt(i, theSBuffer.charAt(i + findLength));
    }
    for (j = theSBufferLength - 1; j >= (theSBufferLength - findLength); j--) {
        theSBuffer.setCharAt(j, (char) (0));
    }
    int newLength = theSBufferLength - findLength;
    theSBuffer.setLength(newLength);
    theSBuffer.insert(theIndex, replace);
    return theIndex;
}

From source file:org.apache.airavata.registry.tool.DBMigrator.java

public static boolean checkStringBufferEndsWith(StringBuffer buffer, String suffix) {
    if (suffix.length() > buffer.length()) {
        return false;
    }//from w  w w .j av a  2  s  .  c om
    // this loop is done on purpose to avoid memory allocation performance
    // problems on various JDKs
    // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
    // implementation is ok though does allocation/copying
    // StringBuffer.toString().endsWith() does massive memory
    // allocation/copying on JDK 1.5
    // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
    int endIndex = suffix.length() - 1;
    int bufferIndex = buffer.length() - 1;
    while (endIndex >= 0) {
        if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
            return false;
        }
        bufferIndex--;
        endIndex--;
    }
    return true;
}

From source file:org.wso2.carbon.identity.application.common.persistence.IdentityApplicationDBInitializer.java

/**
 * Checks that a string buffer ends up with a given string. It may sound
 * trivial with the existing JDK API but the various implementation among
 * JDKs can make those methods extremely resource intensive and perform
 * poorly due to massive memory allocation and copying. See
 *
 * @param buffer the buffer to perform the check on
 * @param suffix the suffix//from  w ww .ja v  a  2 s . com
 * @return <code>true</code> if the character sequence represented by the
 *         argument is a suffix of the character sequence represented by
 *         the StringBuffer object; <code>false</code> otherwise. Note that the
 *         result will be <code>true</code> if the argument is the
 *         empty string.
 */
private static boolean checkStringBufferEndsWith(StringBuffer buffer, String suffix) {

    if (suffix.length() > buffer.length()) {
        return false;
    }
    // this loop is done on purpose to avoid memory allocation performance
    // problems on various JDKs StringBuffer.lastIndexOf() was introduced
    // in jdk 1.4 and implementation is ok though does allocation/copying
    // StringBuffer.toString().endsWith() does massive memory
    // allocation/copying on JDK 1.5
    // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
    int endIndex = suffix.length() - 1;
    int bufferIndex = buffer.length() - 1;
    while (endIndex >= 0) {
        if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
            return false;
        }
        bufferIndex--;
        endIndex--;
    }
    return true;
}