Example usage for java.lang StringBuffer length

List of usage examples for java.lang StringBuffer length

Introduction

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

Prototype

@Override
    public synchronized int length() 

Source Link

Usage

From source file:com.vmware.bdd.utils.CommonUtil.java

public static String inputsConvert(Set<String> words) {
    String newWordStr = "";
    if (words != null && !words.isEmpty()) {
        StringBuffer wordsBuff = new StringBuffer();
        for (String word : words) {
            wordsBuff.append(word).append(",");
        }//from ww  w .  jav a2s  . co m
        wordsBuff.delete(wordsBuff.length() - 1, wordsBuff.length());
        newWordStr = wordsBuff.toString();
    }
    return newWordStr;
}

From source file:jdiff.API.java

/** 
 * <b>NOT USED</b>. //w  ww  .  j  ava2  s  .co  m
 *
 * 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:gda.jython.translator.GeneralTranslator.java

public static String[] splitGroup(String during) {
    String[] output = new String[0];
    StringBuffer buf = new StringBuffer();
    int index = 0;
    String[] quoteTypes = new String[] { "'''", "r\"", "r\'", "\'", "\"" }; //put most complex first
    while (index < during.length()) {
        if (during.regionMatches(index, " ", 0, 1) || during.regionMatches(index, ",", 0, 1)) {
            if (buf.length() > 0) {
                output = (String[]) ArrayUtils.add(output, buf.toString());
                buf = new StringBuffer();
            }//from w w  w.ja v  a  2s .c om
            index++;
            continue;
        }
        boolean quoteTypeFound = false;
        for (String quoteType : quoteTypes) {
            if (during.regionMatches(index, quoteType, 0, quoteType.length())) {
                if (buf.length() > 0) {
                    output = (String[]) ArrayUtils.add(output, buf.toString());
                    buf = new StringBuffer();
                }
                buf.append(quoteType);
                index += quoteType.length();
                //start of quote so go up to the matching quote - allowing for escaped versions
                String endQuote = quoteType;
                if (endQuote == "r\"")
                    endQuote = "\"";
                if (endQuote == "r\'")
                    endQuote = "\'";
                while (index < during.length()) {
                    if (during.regionMatches(index, endQuote, 0, endQuote.length())) {
                        buf.append(endQuote);
                        index += endQuote.length();
                        output = (String[]) ArrayUtils.add(output, buf.toString());
                        buf = new StringBuffer();
                        break;
                    }
                    char charAt = during.charAt(index);
                    buf.append(during.charAt(index));
                    index++;
                    if (charAt == '\\') {
                        if (index < during.length()) {
                            buf.append(during.charAt(index));
                            index++;
                        }
                    }
                }
                if (buf.length() > 0) {
                    output = (String[]) ArrayUtils.add(output, buf.toString());
                    buf = new StringBuffer();
                }
                quoteTypeFound = true;
                break;
            }
        }
        if (quoteTypeFound)
            continue;
        //add to StringBuffer
        char charAt = during.charAt(index);
        buf.append(charAt);
        index++;
        if (charAt == '\\') {
            if (index < during.length()) {
                buf.append(during.charAt(index));
                index++;
            }
        }
    }
    if (buf.length() > 0) {
        output = (String[]) ArrayUtils.add(output, buf.toString());
        buf = new StringBuffer();
    }
    //at end move over buf contents
    return output;
}

From source file:com.qk.applibrary.db.sqlite.SqlBuilder.java

/**
 * ??sql?/*from   w ww.j  a v a2 s.com*/
 * @return
 */
public static SqlInfo buildInsertSql(Object entity) {

    List<KeyValue> keyValueList = getSaveKeyValueListByEntity(entity);

    StringBuffer strSQL = new StringBuffer();
    SqlInfo sqlInfo = null;
    if (keyValueList != null && keyValueList.size() > 0) {

        sqlInfo = new SqlInfo();

        strSQL.append("INSERT INTO ");
        strSQL.append(TableInfo.get(entity.getClass()).getTableName());
        strSQL.append(" (");
        for (KeyValue kv : keyValueList) {
            strSQL.append(kv.getKey()).append(",");
            sqlInfo.addValue(kv.getValue());
        }
        strSQL.deleteCharAt(strSQL.length() - 1);
        strSQL.append(") VALUES ( ");

        int length = keyValueList.size();
        for (int i = 0; i < length; i++) {
            strSQL.append("?,");
        }
        strSQL.deleteCharAt(strSQL.length() - 1);
        strSQL.append(")");

        sqlInfo.setSql(strSQL.toString());
    }

    return sqlInfo;
}

From source file:com.adito.core.RequestParameterMap.java

private static void appendToUriString(StringBuffer buf, String name, String val, String urlCharacterEncoding)
        throws UnsupportedEncodingException {
    if (buf.length() > 0) {
        buf.append("&");
    }//from  ww  w. ja v a2s.  co  m
    buf.append(URLEncoder.encode(name, urlCharacterEncoding));
    buf.append("=");
    buf.append(URLEncoder.encode(val, urlCharacterEncoding));
}

From source file:org.frontcache.core.FCUtils.java

/**
 * http://localhost:8080/coin_instance_details.htm? -> /coin_instance_details.htm?
 * //  w  ww . jav  a  2  s.  com
 * @param urlStr
 * @return
 */
public static String buildRequestURI(String urlStr) {

    String outStr = urlStr;
    int idx = urlStr.indexOf("//");
    if (-1 < idx) {
        idx = urlStr.indexOf("/", idx + "//".length());
        if (-1 < idx)
            outStr = urlStr.substring(idx);
    }

    idx = outStr.indexOf("?"); // has params
    if (-1 < idx) {
        String uri = outStr.substring(0, idx + 1);
        String queryParams = outStr.substring(idx + 1);

        // encode / decode params
        try {
            Map<String, List<String>> parameterMap = splitQueryParameters(queryParams);

            StringBuffer queryParamsSB = new StringBuffer("");

            for (String key : parameterMap.keySet()) {
                for (String value : parameterMap.get(key)) {
                    if (null != value) {
                        if (queryParamsSB.length() > 1)
                            queryParamsSB.append("&");

                        queryParamsSB.append(key).append("=").append(URLEncoder.encode(value, "UTF-8"));
                    }
                }
            }

            queryParams = queryParamsSB.toString();
        } catch (Exception e) {
            logger.error("error buildRequestURI for " + urlStr, e);
        }

        outStr = uri + queryParams;
    }

    return outStr;
}

From source file:com.android.email.LegacyConversions.java

/**
 * Helper function to append text to a StringBuffer, creating it if necessary.
 * Optimization:  The majority of the time we are *not* appending - we should have a path
 * that deals with single strings.//from   w  w w  .ja v  a  2 s . c  om
 */
private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
    if (newText == null) {
        return sb;
    } else if (sb == null) {
        sb = new StringBuffer(newText);
    } else {
        if (sb.length() > 0) {
            sb.append('\n');
        }
        sb.append(newText);
    }
    return sb;
}

From source file:com.sfs.whichdoctor.formatter.OutputFormatter.java

/**
 * Output the person's formatted legal name.
 *
 * @param person the person//from ww w  .j a  v  a 2  s.c  om
 *
 * @return the string
 */
public static String toLegalName(final PersonBean person) {
    final StringBuffer name = new StringBuffer();

    if (person != null) {
        if (StringUtils.isNotBlank(person.getFirstName())) {
            name.append(person.getFirstName().trim());
        }
        if (name.length() > 0) {
            name.append(" ");
        }
        if (StringUtils.isNotBlank(person.getMiddleName())) {
            name.append(person.getMiddleName().trim());
        }
        if (name.length() > 0) {
            name.append(" ");
        }
        if (StringUtils.isNotBlank(person.getLastName())) {
            name.append(person.getLastName().trim());
        }
    }
    return name.toString();
}

From source file:bboss.org.apache.velocity.util.StringUtils.java

/**
 * Perform a series of substitutions. The substitions
 * are performed by replacing $variable in the target
 * string with the value of provided by the key "variable"
 * in the provided hashtable./*w w  w.  j  a  v a  2 s  . c  om*/
 *
 * @param argStr target string
 * @param vars name/value pairs used for substitution
 * @return String target string with replacements.
 */
public static StringBuffer stringSubstitution(String argStr, Map vars) {
    StringBuffer argBuf = new StringBuffer();

    for (int cIdx = 0; cIdx < argStr.length();) {
        char ch = argStr.charAt(cIdx);

        switch (ch) {
        case '$':
            StringBuffer nameBuf = new StringBuffer();
            for (++cIdx; cIdx < argStr.length(); ++cIdx) {
                ch = argStr.charAt(cIdx);
                if (ch == '_' || Character.isLetterOrDigit(ch))
                    nameBuf.append(ch);
                else
                    break;
            }

            if (nameBuf.length() > 0) {
                String value = (String) vars.get(nameBuf.toString());

                if (value != null) {
                    argBuf.append(value);
                }
            }
            break;

        default:
            argBuf.append(ch);
            ++cIdx;
            break;
        }
    }

    return argBuf;
}

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

/**
 * Trim leading whitespace from the given String.
 * //from  w  ww  .  jav  a 2 s  .co  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();
}