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:com.pureinfo.srm.patent.domain.impl.PatentMgrImpl.java

/**
 * @see com.pureinfo.srm.patent.domain.IPatentMgr#findAllAuthorizedOf(int)
 *///w w  w  .j av a2  s. com
public List findAllAuthorizedOf(int _nYear) throws PureException {
    IStatement query = null;
    IObjects result = null;
    StringBuffer sbuff = new StringBuffer();
    try {
        String sYear = String.valueOf(_nYear);
        String firstDate = sYear + "-01-01";
        String endDate = sYear + "-12-31";
        sbuff.append("SELECT * FROM {this} WHERE {this.status}=");
        sbuff.append(SRMConstants.PATENT_STATUS_HAS_RIGHT);
        sbuff.append(" AND {this.pkType}<>1 AND {this.warrantDate}>='").append(firstDate);
        sbuff.append("' AND {this.warrantDate}<='").append(endDate).append('\'');
        sbuff.append(" ORDER BY {this.department}");
        String strSQL = sbuff.toString();
        query = this.createQuery(strSQL, 0);
        result = query.executeQuery();
        return result.toList();
    } finally {
        sbuff.setLength(0);
        DolphinHelper.clear(result, query);
    }
}

From source file:com.pureinfo.srm.project.CodeGenerator.java

private String makeUpCode(String[] tokens, String sPattern, String sSerialNum) {
    StringBuffer sbuff = new StringBuffer();
    boolean isLastCharDollar = false;
    for (int i = 0; i < sPattern.length(); i++) {
        char ch = sPattern.charAt(i);
        if (isLastCharDollar) {
            if (ch == 'Y') {
                char cDnum = sPattern.charAt(i + 1);
                int nDigital = 4;
                if (cDnum >= '0' && cDnum <= '9') {
                    nDigital = cDnum - '0';
                    i++;/*w  w w.  j  ava 2 s.c  om*/
                }
                sbuff.append(getYear(nDigital));
            } else if (ch == 'S') {

                sbuff.append(sSerialNum);
            } else if (ch >= '0' && ch <= '9') {
                sbuff.append(tokens[ch - '0']);
            } else {
                sbuff.append(ch);
            }
            isLastCharDollar = false;
        } else {
            if (ch == '$') {
                isLastCharDollar = true;
            } else {
                sbuff.append(ch);
                isLastCharDollar = false;
            }
        }
    }

    String str = sbuff.toString();
    sbuff.setLength(0);
    return str;
}

From source file:org.eclipse.wb.tests.designer.core.model.ObjectInfoTest.java

/**
 * Test that listener automatically removed after {@link ObjectInfo#refresh()} when its
 * {@link ObjectInfo} is not linked to root.
 *///from w ww .j  av a  2  s.  c  o  m
public void test_broadcast_autoRemove_whenRemoveParent() throws Exception {
    TestObjectInfo parent = new TestObjectInfo("parent");
    TestObjectInfo child_1 = new TestObjectInfo("child");
    TestObjectInfo child_2 = new TestObjectInfo("child2");
    // build hierarchy
    parent.addChild(child_1);
    child_1.addChild(child_2);
    // add child and listener (bound to child)
    final StringBuffer buffer = new StringBuffer();
    ObjectEventListener listener = new ObjectEventListener() {
        @Override
        public void dispose() throws Exception {
            buffer.append("dispose");
        }
    };
    child_2.addBroadcastListener(listener);
    // listener works
    {
        child_2.getBroadcastObject().dispose();
        assertEquals("dispose", buffer.toString());
        buffer.setLength(0);
    }
    // remove "child_1", do refresh()
    parent.removeChild(child_1);
    parent.refresh();
    // "child_2" is linked to "child_1", which is not linked to root, so listener was removed
    {
        child_2.getBroadcastObject().dispose();
        assertEquals("", buffer.toString());
        buffer.setLength(0);
    }
}

From source file:com.adito.replacementproxy.replacers.FormReplacer.java

private String doRepl(String attrs) {
    StringBuffer attrName = new StringBuffer();
    char quote = '\0';
    StringBuffer attrVal = new StringBuffer();
    boolean doName = true;
    boolean doVal = false;
    CaseInsensitiveMap a = new CaseInsensitiveMap();
    for (int i = 0; i < attrs.length(); i++) {
        char ch = attrs.charAt(i);
        if (ch == '\'' && quote == '\0') {
            quote = '\'';
        } else if (ch == '"' && quote == '\0') {
            quote = '"';
            //            } else if (((doName && (ch == '\r' || ch == '\n')) || (doVal && ( i == attrs.length() - 1 ) ) || (doVal && ch == ' ' && quote == ' ' && attrVal.length() > 0)
            //                || (doVal && ch == '\'' && quote == '\'') || (doVal && ch == '\"' && quote == '\"'))) {
        } else if (doVal && (ch == quote || (quote == '\0' && ch == ' ') || i == (attrs.length() - 1))) {
            if (quote == '\0') {
                attrVal.append(ch);//w  w  w. j  a va2s.  com
            }
            quote = '\0';
            String an = attrName.toString();
            if (!an.equals("")) {
                a.put(attrName.toString(), attrVal.toString());
            }
            attrName.setLength(0);
            attrVal.setLength(0);
            doVal = false;
            doName = true;
        } else if (ch == '=' && doName) {
            doName = false;
            doVal = true;
        } else {
            if (doName) {
                if ((ch != ' ' && ch != '\r' && ch != '\n') || attrName.length() > 0) {
                    attrName.append(ch);
                }
            } else if (doVal) {
                attrVal.append(ch);
            }
        }
    }
    StringBuffer buf = new StringBuffer("<form");
    String sslexUrl = context.toExternalForm();
    if (a.containsKey("action")) {
        try {
            String contextPath = context.toExternalForm();
            if (contextPath.endsWith("/")) {
                contextPath = contextPath.substring(0, contextPath.length() - 1);
            }
            String originalAction = a.get("action").toString();
            try {
                sslexUrl = new URL(originalAction).toExternalForm();
            } catch (MalformedURLException murle) {
                /**
                 * LDP - Bug fix: the commented out code causes problems with relative
                 * URLs used in the action parameter of a form
                 * 
                 * Example:
                 * 
                 * Context URL:
                 * http://foobar/OA_HTML/AppsLocalLogin.jsp
                 * 
                 * Action:
                 * fndvald.jsp
                 * 
                 * Results In:
                 * 
                 * http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp
                 * 
                 * Fixed code results in:
                 * 
                 * http://foobar/OA_HTML/fndvald.jsp
                 * 
                 * Slash should not be a problem because URL is intelligent enough to work out that
                 * the slash means from the root of the URL. I have tested this case and it does indeed
                 * work the same way regardless of the value of originalAction, in fact originalAction
                 * can be a completely different URL for example http://localhost/fndvald.jsp
                 * 
                 * Here are the test cases:
                 * 
                 * new URL("http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp", "fndvald.jsp").toExternalForm();
                 * == http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp
                 * 
                 * new URL("http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp", "/fndvald.jsp").toExternalForm();
                 * == http://foobar/fndvald.jsp
                 * 
                 * new URL("http://foobar/OA_HTML/AppsLocalLogin.jsp/fndvald.jsp", "http://localhost/fndvald.jsp").toExternalForm();
                 * == http://localhost/fndvald.jsp
                 */
                //if(originalAction.startsWith("/")) {
                sslexUrl = new URL(context, originalAction).toExternalForm();
                //}
                //else {
                //   sslexUrl = new URL(DAVUtilities.concatenatePaths(contextPath, originalAction)).toExternalForm();
                //}
            }
        } catch (MalformedURLException e) {
            log.error("Failed to process FORM action", e);
        }
    }

    a.put("action", "/replacementProxyEngine/" + ticket + "/" + Util.urlEncode(Utils.htmlunescape(sslexUrl)));

    for (Iterator i = a.entrySet().iterator(); i.hasNext();) {
        buf.append(" ");
        Map.Entry entry = (Map.Entry) i.next();
        buf.append(entry.getKey().toString());
        buf.append("=\"");
        buf.append(entry.getValue());
        buf.append("\"");
    }
    buf.append(">");

    return buf.toString();
}

From source file:org.eclipse.wb.tests.designer.core.model.ObjectInfoTest.java

/**
 * Test for using interfaces as broadcast classes.
 *//*ww  w  . ja v  a 2  s  .  co m*/
public void test_broadcast_interface() throws Exception {
    TestObjectInfo parent = new TestObjectInfo("parent");
    // add listener
    final StringBuffer buffer = new StringBuffer();
    BroadcastTestInterface listener = new BroadcastTestInterface() {
        public void invoke() {
            buffer.append("invoke");
        }
    };
    parent.addBroadcastListener(listener);
    // send broadcast
    parent.getBroadcast(BroadcastTestInterface.class).invoke();
    assertEquals("invoke", buffer.toString());
    // remove listener and check again
    {
        buffer.setLength(0);
        parent.removeBroadcastListener(listener);
        // send broadcast
        parent.getBroadcast(BroadcastTestInterface.class).invoke();
        assertEquals("", buffer.toString());
    }
}

From source file:com.vmware.bdd.rest.RestResource.java

private void redirectRequest(long taskId, HttpServletRequest request, HttpServletResponse response) {
    StringBuffer url = request.getRequestURL();
    String pathInfo = request.getPathInfo();
    if (!CommonUtil.validataPathInfo(pathInfo)) {
        throw BddException.INVALID_PARAMETER("requested path info", pathInfo);
    }//w  w w.java  2 s. c  o  m
    int subLength = url.length() - pathInfo.length();
    url.setLength(subLength);
    url.append("/task/").append(Long.toString(taskId));
    response.setHeader(Constants.RESPONSE_HEADER_LOCATION, url.toString());
}

From source file:com.tremolosecurity.provisioning.core.providers.BasicDB.java

private PreparedStatement updateField(User user, Connection con, StringBuffer b, String attrName, String userID,
        int userIDnum) throws SQLException {
    b.setLength(0);
    b.append("UPDATE ").append(this.userTable).append(" SET ");
    this.getFieldName(attrName, b).append("=? WHERE ");
    this.getFieldName(this.userPrimaryKey, b).append("=?");
    PreparedStatement ps = con.prepareStatement(b.toString());
    ps.setString(1, user.getAttribs().get(attrName).getValues().get(0));
    if (userIDnum != -1) {
        ps.setInt(2, userIDnum);//from   w ww  .j  ava2s  .  c  o  m
    } else {

        Attribute.DataType tat = user.getAttribs().get(attrName).getDataType();
        switch (tat) {
        case string:
            ps.setString(2, userID);
            break;
        case intNum:
            ps.setInt(2, Integer.parseInt(userID));
            break;
        case longNum:
            ps.setLong(2, Long.parseLong(userID));
            break;

        case date:
            ps.setDate(2, new Date(ISODateTimeFormat.date().parseDateTime(userID).getMillis()));
            break;
        case timeStamp:
            ps.setTimestamp(2, new Timestamp(ISODateTimeFormat.dateTime().parseDateTime(userID).getMillis()));
            break;
        }

        ps.setString(2, userID);
    }
    ps.executeUpdate();
    return ps;
}

From source file:net.sf.farrago.namespace.jdbc.MedJdbcDataServer.java

private void parseMapping(DatabaseMetaData databaseMetaData, String mapping, boolean isTableMapping,
        boolean isTablePrefixMapping) throws SQLException {
    if (!isTableMapping) {
        // Force valid parameters.
        isTablePrefixMapping = false;//w  w  w.  ja  va2  s  .  co  m
    }

    String srcSchema = null;
    String srcTable = null;
    String targetSchema = null;
    String targetTable = null;

    StringBuffer buffer = new StringBuffer(64);
    buffer.setLength(0);

    boolean insideQuotes = false;
    boolean atSource = true;

    int len = mapping.length();
    int i = 0;

    while (i < len) {
        char c = mapping.charAt(i);
        switch (c) {
        case '"':
            if (!isQuoteChar(mapping, i)) {
                // escape character, add one quote
                buffer.append(c);
                i++;
            } else {
                if (insideQuotes) {
                    // this is the endQuote
                    insideQuotes = false;
                } else {
                    // this is the startQuote
                    insideQuotes = true;
                }
            }
            i++;
            break;
        case '.':
            if (!isTableMapping) {
                // non special characters
                buffer.append(c);
            } else {
                // in table mapping, "." is a special character
                if (insideQuotes) {
                    buffer.append(c);
                } else {
                    if (atSource) {
                        srcSchema = buffer.toString();
                        srcSchema = srcSchema.trim();
                    } else {
                        targetSchema = buffer.toString();
                        targetSchema = targetSchema.trim();
                    }
                    buffer.setLength(0);
                }
            }
            i++;
            break;
        case ':':
            if (insideQuotes) {
                buffer.append(c);
            } else {
                srcTable = buffer.toString();
                srcTable = srcTable.trim();
                atSource = false;
                buffer.setLength(0);
            }
            i++;
            break;
        case ';':
            if (insideQuotes) {
                buffer.append(c);
            } else {
                targetTable = buffer.toString();
                targetTable = targetTable.trim();
                atSource = true;
                buffer.setLength(0);
                if (isTableMapping) {
                    if (isTablePrefixMapping) {
                        createTablePrefixMaps(srcSchema, srcTable, targetSchema, targetTable);
                    } else {
                        createTableMaps(srcSchema, srcTable, targetSchema, targetTable);
                    }
                } else {
                    createSchemaMaps(databaseMetaData, srcTable, targetTable);
                }
            }
            i++;
            break;
        default:

            // non special characters
            buffer.append(c);
            i++;
            break;
        }
        if (i == len) {
            targetTable = buffer.toString();
            targetTable = targetTable.trim();
            buffer.setLength(0);
            if (isTableMapping) {
                if (isTablePrefixMapping) {
                    createTablePrefixMaps(srcSchema, srcTable, targetSchema, targetTable);
                } else {
                    createTableMaps(srcSchema, srcTable, targetSchema, targetTable);
                }
            } else {
                createSchemaMaps(databaseMetaData, srcTable, targetTable);
            }
        }
    }
}

From source file:com.google.dart.tools.ui.internal.text.dart.DartAutoIndentStrategy_NEW.java

/**
 * Computes the difference of two indentations and returns the difference in length of current and
 * correct. If the return value is positive, <code>addition</code> is initialized with a substring
 * of that length of <code>correct</code>.
 * /*ww w . j  a v  a2s.c om*/
 * @param correct the correct indentation
 * @param current the current indentation (might contain non-whitespace)
 * @param difference a string buffer - if the return value is positive, it will be cleared and set
 *          to the substring of <code>current</code> of that length
 * @param tabLength the length of a tab
 * @return the difference in length of <code>correct</code> and <code>current</code>
 */
private int subtractIndent(CharSequence correct, CharSequence current, StringBuffer difference, int tabLength) {
    int c1 = computeVisualLength(correct, tabLength);
    int c2 = computeVisualLength(current, tabLength);
    int diff = c1 - c2;
    if (diff <= 0) {
        return diff;
    }

    difference.setLength(0);
    int len = 0, i = 0;
    while (len < diff) {
        char c = correct.charAt(i++);
        difference.append(c);
        len += computeVisualLength(c, tabLength);
    }

    return diff;
}

From source file:net.ddns.mlsoftlaberge.contactslist.ui.ContactAdminFragment.java

private String onlyNumbers(String from) {
    StringBuffer tonum = new StringBuffer();
    tonum.setLength(14);
    int i;//w  w  w.  java 2s  .c  o  m
    int j = 0;
    for (i = 0; i < from.length(); ++i) {
        char c = from.charAt(i);
        if (c >= '0' && c <= '9') {
            tonum.setCharAt(j, c);
            j++;
        }
    }
    tonum.setLength(j);
    return tonum.toString();
}