Example usage for java.lang StringBuffer setLength

List of usage examples for java.lang StringBuffer setLength

Introduction

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

Prototype

@Override
public synchronized void setLength(int newLength) 

Source Link

Usage

From source file:org.pentaho.marketplace.util.web.HttpUtil.java

private static String parseName(final String s, final StringBuffer sb) {
    sb.setLength(0);/*from www  .  j  av  a 2s  .  c o  m*/
    char c;
    for (int i = 0; i < s.length(); i++) {
        c = s.charAt(i);
        switch (c) {
        case 43: { // '+'
            sb.append(' ');
            break;
        }
        case 37: { // '%'
            try {
                sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16));
                i += 2;
                break;
            } catch (NumberFormatException numberformatexception) {
                throw new IllegalArgumentException();
            } catch (StringIndexOutOfBoundsException oob) {
                String rest = s.substring(i);
                sb.append(rest);
                if (rest.length() == 2) {
                    i++;
                }
            }
            break;
        }
        default: {
            sb.append(c);
            break;
        }
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Trims leading and trailing spaces from a string buffer
 * @param buffer the buffer to be edited 
 *///from  w  w w .  j  ava  2  s.  com
public static void trim(StringBuffer buffer) {
    int i = 0;
    while (i < buffer.length() && isSpace(buffer.charAt(i)))
        i++;
    buffer.delete(0, i);
    int j = buffer.length() - 1;
    while (j >= 0 && isSpace(buffer.charAt(j)))
        j--;
    buffer.setLength(j + 1);
}

From source file:TextUtilities.java

/**
 * Trims the specified string on the right only.
 *///w w w . j av  a 2  s .  c  o  m

public static String trimRight(String s) {
    StringBuffer buf = new StringBuffer(s);
    int length = buf.length();
    while (Character.isWhitespace(buf.charAt(length - 1)))
        buf.setLength(--length);

    return buf.toString();
}

From source file:org.eclipse.wst.ws.internal.parser.discovery.NetUtils.java

/**
 * Get the java.net.URLConnection given a string representing the URL. This class ensures
 * that proxy settings in WSAD are respected.
 * @param urlString String representing the URL.
 * @return java.net.URLCDonnection URLConnection to the URL.
 *//*from  ww w . j  av  a2  s  . c  o m*/
public static final URLConnection getURLConnection(String urlString) {
    try {
        URL url = createURL(urlString);
        URLConnection uc = url.openConnection();
        String proxyUserName = System.getProperty("http.proxyUserName");
        String proxyPassword = System.getProperty("http.proxyPassword");
        if (proxyUserName != null && proxyPassword != null) {
            StringBuffer userNamePassword = new StringBuffer(proxyUserName);
            userNamePassword.append(':').append(proxyPassword);
            Base64 encoder = new Base64();
            String encoding = new String(encoder.encode(userNamePassword.toString().getBytes()));
            userNamePassword.setLength(0);
            userNamePassword.append("Basic ").append(encoding);
            uc.setRequestProperty("Proxy-authorization", userNamePassword.toString());
        }
        return uc;
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return null;
}

From source file:org.jboss.tools.ws.ui.utils.NetUtils.java

/**
 * Get the java.net.URLConnection given a string representing the URL. This class ensures
 * that proxy settings in WSAD are respected.
 * @param urlString String representing the URL.
 * @return java.net.URLCDonnection URLConnection to the URL.
 *//*  w w w .ja v  a 2s.com*/
public static final URLConnection getURLConnection(String urlString) {
    try {
        URL url = createURL(urlString);
        URLConnection uc = url.openConnection();
        String proxyUserName = System.getProperty("http.proxyUserName"); //$NON-NLS-1$
        String proxyPassword = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
        if (proxyUserName != null && proxyPassword != null) {
            StringBuffer userNamePassword = new StringBuffer(proxyUserName);
            userNamePassword.append(':').append(proxyPassword);
            Base64 encoder = new Base64();
            String encoding = new String(encoder.encode(userNamePassword.toString().getBytes()));
            userNamePassword.setLength(0);
            userNamePassword.append("Basic ").append(encoding); //$NON-NLS-1$
            uc.setRequestProperty("Proxy-authorization", userNamePassword.toString()); //$NON-NLS-1$
        }
        return uc;
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return null;
}

From source file:TextUtilities.java

/**
 * <P>Returns a substring of the given StringBuffer's string which consists of
 * the characters from the beginning of it until the first occurrence of the
 * given delimiter string or if the delimiter doesn't occur, until the end
 * of the string. The StringBuffer is modified so it no longer contains those
 * characters or the delimiter.//from   w w w .jav a  2 s  .com
 * <P>Examples:
 * <UL>
 *   <LI>nextToken(new StringBuffer("abcdefgh"), "de") returns "abc" and
 *       the StringBuffer is modified to represent the string "fgh".
 *   <LI>nextToken(new StringBuffer("abcdefgh"), "a") returns an empty string
 *       and the StringBuffer is modified to represent the string "bcdefgh".
 *   <LI>nextToken(new StringBuffer("abcdefgh"), "k") returns "abcdefgh" and
 *       the StringBuffer is modified to represent an empty string.
 * </UL>
 */

public static String nextToken(StringBuffer buf, String delimiter) {
    String bufStr = buf.toString();
    int delimIndex = bufStr.indexOf(delimiter);

    if (delimIndex == -1) {
        buf.setLength(0);
        return bufStr;
    }

    String str = bufStr.substring(0, delimIndex);
    buf.reverse();
    buf.setLength(buf.length() - delimIndex - delimiter.length());
    buf.reverse();

    return str;
}

From source file:com.hangum.tadpole.rdb.core.dialog.export.sqltoapplication.application.SQLToAxisjConvert.java

/**
 * sql to string//  w  w w. jav a2  s. com
 * 
 * @param userDB
 * @param name
 * @param sql
 * @return
 */
public static String sqlToString(UserDBDAO userDB, String sql, Map options,
        List<AxisjHeaderDAO> listAxisjHeader) {
    String retHtml = "";
    try {
        String STR_TEMPLATE = IOUtils.toString(SQLToAxisjConvert.class.getResource("axis.js.template"));

        QueryExecuteResultDTO queryResult = QueryUtils.executeQuery(userDB, sql, 0, 4);
        Map<Integer, String> columnLabel = queryResult.getColumnLabelName();

        String strHead = "";
        StringBuffer sbGroup = new StringBuffer();
        for (AxisjHeaderDAO dao : listAxisjHeader) {
            sbGroup.append(String.format(GROUP_TEMPLATE, dao.getKey(), dao.getLabel(), dao.getWidth(),
                    AxisjConsts.alignValue[dao.getAlign()], AxisjConsts.sortValue[dao.getSort()],
                    dao.isColHeadTool() ? "true" : "false", dao.getFormatter(), dao.getTooltip(),
                    dao.getDisabled(), dao.getChecked()));
        }
        strHead = StringUtils.removeEnd(sbGroup.toString(), ",");
        // 

        String strBody = "";
        StringBuffer sbData = new StringBuffer();
        TadpoleResultSet tdbResult = queryResult.getDataList();
        for (Map<Integer, Object> resultRow : tdbResult.getData()) {
            sbData.setLength(0);
            for (int i = 0; i < columnLabel.size(); i++) {
                String strColumnLabel = columnLabel.get(i);
                String strColumnValue = "" + resultRow.get(i);
                strColumnValue = StringUtils.replaceEach(strColumnValue,
                        new String[] { ">", "<", "\"", "\r", "\n" },
                        new String[] { "&gt;", "&lt;", "\\\"", "", "\\n" });
                sbData.append(String.format(GROUP_DATA_TEMPLATE, strColumnLabel, "\"" + strColumnValue + "\""));
            }

            strBody += PREFIX_TAB + "{" + StringUtils.removeEnd(sbData.toString(), ",") + "},";
        }
        strBody = StringUtils.removeEnd(strBody, ",");

        retHtml = StringUtils.replaceEach(STR_TEMPLATE, new String[] { "_TDB_TEMPLATE_TITLE_" },
                new String[] { (String) options.get("name") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_THEME_" },
                new String[] { (String) options.get("theme") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_FIXEDCOL_" },
                new String[] { (String) options.get("fixedColSeq") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_FITTOWIDTH_" },
                new String[] { (String) options.get("fitToWidth") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_HEADALIGN_" },
                new String[] { (String) options.get("colHeadAlign") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_MERGECELL_" },
                new String[] { (String) options.get("mergeCells") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_HEIGHT_" },
                new String[] { (String) options.get("height") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_SORT_" },
                new String[] { (String) options.get("sort") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_HEADTOOL_" },
                new String[] { (String) options.get("colHeadTool") });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_VIEWMODE_" },
                new String[] { (String) options.get("viewMode") });

        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_HEAD_" },
                new String[] { strHead });
        retHtml = StringUtils.replaceEach(retHtml, new String[] { "_TDB_TEMPLATE_BODY_" },
                new String[] { strBody });
    } catch (Exception e) {
        logger.error("SQL template exception", e);
    }

    return retHtml;
}

From source file:au.csiro.casda.sodalint.SodaLinter.java

/**
 * Returns a list of startup announcements with which the taplint application introduces itself.
 *
 * @return announcement lines//from www .j  a  v a  2 s. c  o  m
 */
private static List<String> getAnnouncements() {

    /* Version report. */
    String version = getSodaLintVersion();
    String versionLine = new StringBuilder().append("This is sodalint ").append(version)
            .append(" validating against ").append(SODA_VERSION).toString();

    /* Count by report type of known FixedCode instances. */
    Map<ReportType, int[]> codeMap = new LinkedHashMap<ReportType, int[]>();
    for (ReportType type : Arrays.asList(ReportType.values())) {
        codeMap.put(type, new int[1]);
    }
    for (SodaCode code : Arrays.asList(SodaCode.values())) {
        codeMap.get(code.getType())[0]++;
    }
    StringBuffer cbuf = new StringBuffer().append("Static report types: ");
    for (Map.Entry<ReportType, int[]> entry : codeMap.entrySet()) {
        cbuf.append(entry.getKey()).append("(").append(entry.getValue()[0]).append(")").append(", ");
    }
    cbuf.setLength(cbuf.length() - 2);
    String codesLine = cbuf.toString();

    /* Return lines. */
    List<String> announcements = new ArrayList<>();
    announcements.add(versionLine);
    announcements.add(codesLine);
    return announcements;
}

From source file:org.jbpm.bpel.xml.ActivityReader.java

private static String generateName(Activity activity, CompositeActivity parent) {
    String activityClass = ClassUtils.getShortClassName(activity.getClass());
    StringBuffer nameBuffer = new StringBuffer(activityClass);

    // separator//from w ww  . jav  a  2  s .  co m
    nameBuffer.append('#');

    // index
    final int baseLength = nameBuffer.length();
    String name;

    for (int i = 1; parent.hasNode(name = nameBuffer.append(i).toString()); i++)
        nameBuffer.setLength(baseLength); // remove appended number

    return name;
}

From source file:Utilities.java

/**
 * Converts the specified array into a string by appending all its elements
 * separated by a semicolon.//from   w w w. ja v a  2  s . c o m
 */

public static String arrayToString(Object[] arr) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < arr.length; i++) {
        buf.append(arr[i]);
        buf.append("; ");
    }
    if (arr.length > 0)
        buf.setLength(buf.length() - 2); // get rid of the extra "; "

    return buf.toString();
}