Example usage for java.lang StringBuffer delete

List of usage examples for java.lang StringBuffer delete

Introduction

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

Prototype

@Override
public synchronized StringBuffer delete(int start, int end) 

Source Link

Usage

From source file:com.gemstone.gemfire.management.internal.cli.parser.preprocessor.PreprocessorUtils.java

/**
 * //  www.jav a2s  .  co m
 * This function will trim the given input string. It will not only remove the
 * spaces and tabs at the end but also compress multiple spaces and tabs to a
 * single space
 * 
 * @param input
 *          The input string on which the trim operation needs to be performed
 * @param retainLineSeparator
 *          whether to retain the line separator.
 * 
 * @return String
 */
public static TrimmedInput trim(final String input, final boolean retainLineSeparator) {
    if (input != null) {
        String inputCopy = input;
        StringBuffer output = new StringBuffer();
        // First remove the trailing white spaces, we do not need those
        inputCopy = StringUtils.stripEnd(inputCopy, null);
        // As this parser is for optionParsing, we also need to remove
        // the trailing optionSpecifiers provided it has previous
        // options. Remove the trailing LONG_OPTION_SPECIFIERs
        // in a loop. It is necessary to check for previous options for
        // the case of non-mandatory arguments.
        // "^(.*)(\\s-+)$" - something that ends with a space followed by a series of hyphens.
        while (Pattern.matches("^(.*)(\\s-+)$", inputCopy)) {
            inputCopy = StringUtils.removeEnd(inputCopy, SyntaxConstants.SHORT_OPTION_SPECIFIER);

            // Again we need to trim the trailing white spaces
            // As we are in a loop
            inputCopy = StringUtils.stripEnd(inputCopy, null);
        }
        // Here we made use of the String class function trim to remove the
        // space and tabs if any at the
        // beginning and the end of the string
        int noOfSpacesRemoved = 0;
        {
            int length = inputCopy.length();
            inputCopy = inputCopy.trim();
            noOfSpacesRemoved += length - inputCopy.length();
        }
        // Now we need to compress the multiple spaces and tabs to single space
        // and tabs but we also need to ignore the white spaces inside the
        // quotes and parentheses

        StringBuffer buffer = new StringBuffer();

        boolean startWhiteSpace = false;
        for (int i = 0; i < inputCopy.length(); i++) {
            char ch = inputCopy.charAt(i);
            buffer.append(ch);
            if (PreprocessorUtils.isWhitespace(ch)) {
                if (PreprocessorUtils.isSyntaxValid(buffer.toString())) {
                    if (startWhiteSpace) {
                        noOfSpacesRemoved++;
                    } else {
                        startWhiteSpace = true;
                        if (ch == '\n') {
                            if (retainLineSeparator) {
                                output.append("\n");
                            }
                        } else {
                            output.append(" ");
                        }
                    }
                    buffer.delete(0, buffer.length());
                } else {
                    output.append(ch);
                }
            } else {
                startWhiteSpace = false;
                output.append(ch);
            }
        }
        return new TrimmedInput(output.toString(), noOfSpacesRemoved);
    } else {
        return null;
    }
}

From source file:es.itg.sensorweb.model.swecommon.DataRecord.java

@Override
@JsonIgnore// w ww  .  j  a  v  a  2s . c  om
public String getTextEncodedValue(String blockSeparator, String tokenSeparator) {
    StringBuffer sb = new StringBuffer();

    for (AbstractDataComponent field : fields) {
        sb.append(field.getTextEncodedValue(blockSeparator, tokenSeparator));
        sb.append(tokenSeparator);
    }
    sb.delete(sb.length() - tokenSeparator.length(), sb.length());
    return sb.toString();
}

From source file:hydrograph.ui.expression.editor.util.ExpressionEditorUtil.java

/**
 * Returns package name from formatted text. 
 * //from   ww  w.jav  a 2s .c o  m
 * @param formattedPackageName
 *            Format should be : package-name.* - jarname
 * @return
 *          package-name
 */
public String getPackageNameFromFormattedText(String formattedPackageName) {
    StringBuffer buffer = new StringBuffer();
    try {
        buffer.append(formattedPackageName);
        buffer.delete(buffer.lastIndexOf(Constants.ASTERISK), formattedPackageName.length() - 1);
        buffer.delete(buffer.lastIndexOf(Constants.DOT), buffer.length());
        return buffer.toString();
    } catch (Exception exception) {
        LOGGER.warn("Invalid format of package name", exception);
    }
    return Constants.EMPTY_STRING;
}

From source file:Comments.java

public static String removeComment(String input) {

    StringBuffer sb = new StringBuffer(input);
    char NQ = ' ', quote = NQ;
    int len = sb.length();
    for (int j = 0, lineno = 1; j < len; j++) {
        if (sb.charAt(j) == '\n')
            ++lineno;//from   w  ww .  j  a va2  s  .c o m

        if (quote != NQ) {
            if (sb.charAt(j) == quote) {
                quote = NQ;
            } else if (sb.charAt(j) == '\\') {
                j++;
                //fix for  "123\\\r\n123" 
                if (sb.charAt(j) == '\r')
                    j++;
                // if(sb.charAt(j) == '\n') j++;
            } else if (sb.charAt(j) == '\n') {
                throw new IllegalStateException("Unterminated string at line " + lineno);
            }
        } else if (sb.charAt(j) == '/' && j + 1 < len && (sb.charAt(j + 1) == '*' || sb.charAt(j + 1) == '/')) {
            int l = j;
            boolean eol = sb.charAt(++j) == '/';
            while (++j < len) {
                if (sb.charAt(j) == '\n')
                    ++lineno;

                if (eol) {
                    if (sb.charAt(j) == '\n') {
                        sb.delete(l, sb.charAt(j - 1) == '\r' ? j - 1 : j);
                        len = sb.length();
                        j = l;
                        break;
                    }
                } else if (sb.charAt(j) == '*' && j + 1 < len && sb.charAt(j + 1) == '/') {
                    sb.delete(l, j + 2);
                    len = sb.length();
                    j = l;
                    break;
                }
            }
        } else if (sb.charAt(j) == '\'' || sb.charAt(j) == '"') {
            quote = sb.charAt(j);
        } else if (sb.charAt(j) == '/') { // regex
            boolean regex = false;
            for (int k = j;;) {
                if (--k < 0) {
                    regex = true;
                    break;
                }

                char ck = sb.charAt(k);
                if (!Character.isWhitespace(ck)) {
                    regex = ck == '(' || ck == ',' || ck == '=' || ck == ':' || ck == '?' || ck == '{'
                            || ck == '[' || ck == ';' || ck == '!' || ck == '&' || ck == '|' || ck == '^'
                            || (ck == 'n' && k > 4 && "return".equals(sb.substring(k - 5, k + 1)))
                            || (ck == 'e' && k > 2 && "case".equals(sb.substring(k - 3, k + 1)));
                    break;
                }
            }
            if (regex) {
                while (++j < len && sb.charAt(j) != '/') {
                    if (sb.charAt(j) == '\\')
                        j++;
                    else if (sb.charAt(j) == '\n') {
                        throw new IllegalStateException("Unterminated regex at line " + lineno);
                    }
                }
            }
        }
    }
    return sb.toString();
}

From source file:com.panet.imeta.core.row.ValueDataUtil.java

/**
 * Replace value occurances in a String with another value.
 * @param string The original String./*  w w w.j  a v  a2  s.c o m*/
 * @param repl The text to replace
 * @param with The new text bit
 * @return The resulting string with the text pieces replaced.
 */
public static final String replace(String string, String repl, String with) {
    StringBuffer str = new StringBuffer(string);
    for (int i = str.length() - 1; i >= 0; i--) {
        if (str.substring(i).startsWith(repl)) {
            str.delete(i, i + repl.length());
            str.insert(i, with);
        }
    }
    return str.toString();
}

From source file:ro.cs.cm.common.TextTable.java

public TextTable(String theHeader, String elementSeparator, byte align) {
    if (elementSeparator != null) {
        ELEMENT_SEPARATOR = elementSeparator;
    }//from  w w w .  j  av  a  2 s. c o  m
    String[] st = theHeader.split(ELEMENT_SEPARATOR);
    headerAlign = align;
    header = new String[st.length];
    this.collNo = header.length;
    // populam headerul
    int i = 0;
    StringBuffer sb = new StringBuffer();
    for (int j = 0; j < st.length; j++) {
        sb.delete(0, sb.length());
        sb.append(" ");
        sb.append((String) st[j]);
        sb.append(" ");
        header[i++] = sb.toString();
    }
}

From source file:de.nrw.hbz.regal.sync.MyConfiguration.java

/**
 * //from   www.j  av  a  2s.  c o m
 * generates the configuration in terms of arguments and options
 * 
 * @param args
 *            Command line arguments
 * @param options
 *            Command line options
 * @throws ParseException
 *             When the configuration cannot be parsed
 */
MyConfiguration(String[] args, Options options) {
    try {
        CommandLineParser parser = new BasicParser();
        CommandLine commandLine = parser.parse(options, args);
        for (Option option : commandLine.getOptions()) {
            String key = option.getLongOpt();
            // System.out.println(key);
            if (key.compareTo("set") == 0) {
                String[] vals = option.getValues();
                if (vals == null || vals.length == 0) {
                    this.addProperty(key, "N/A");
                } else {
                    StringBuffer val = new StringBuffer();
                    for (int i = 0; i < vals.length; i++) {
                        val.append(vals[i]);
                        val.append(",");
                    }
                    val.delete(val.length(), val.length());
                    this.addProperty(key, val.toString());
                }
            } else {
                String val = option.getValue();
                if (val == null) {
                    this.addProperty(key, "N/A");
                } else {

                    this.addProperty(key, val);
                }
            }
        }
    } catch (ParseException e) {
        throw new ConfigurationParseException(e);
    }
}

From source file:com.abstratt.mdd.core.util.MDDUtil.java

public static String computeSignatureName(List<Parameter> signature) {
    StringBuffer name = new StringBuffer("{(");
    final List<Parameter> inputParameters = FeatureUtils.filterParameters(signature,
            ParameterDirectionKind.IN_LITERAL);
    for (Parameter parameter : inputParameters) {
        if (parameter.getName() != null) {
            name.append(parameter.getName());
            name.append(" ");
        }/* ww w . ja v a 2  s.  com*/
        name.append(": ");
        final Type parameterType = parameter.getType();
        final String parameterTypeName = parameterType == null ? "any" : parameterType.getQualifiedName();
        name.append(parameterTypeName);
        addMultiplicity(name, parameter);
        name.append(", ");
    }
    if (!inputParameters.isEmpty())
        name.delete(name.length() - ", ".length(), name.length());
    name.append(")");
    List<Parameter> returnParameter = FeatureUtils.filterParameters(signature,
            ParameterDirectionKind.RETURN_LITERAL);
    if (!returnParameter.isEmpty()) {
        name.append(" : ");
        final Type returnType = returnParameter.get(0).getType();
        final String returnTypeName = returnType == null ? "any" : returnType.getQualifiedName();
        name.append(returnTypeName);
    }
    name.append('}');
    return name.toString();
}

From source file:de.suse.swamp.core.filter.DatabaseFilter.java

public String buildSQLString(ArrayList columns, ArrayList tables, ArrayList conditions) {
    StringBuffer sqlString = new StringBuffer("SELECT DISTINCT ");
    for (Iterator it = columns.iterator(); it.hasNext();) {
        sqlString.append(StringEscapeUtils.escapeSql((String) it.next())).append(", ");
    }/*  w w w.j a  va  2s .  c  o m*/
    sqlString.delete(sqlString.length() - 2, sqlString.length());

    sqlString.append(" FROM ");
    for (Iterator it = tables.iterator(); it.hasNext();) {
        sqlString.append(StringEscapeUtils.escapeSql((String) it.next())).append(", ");
    }
    sqlString.delete(sqlString.length() - 2, sqlString.length());

    if (conditions != null && conditions.size() >= 1) {
        sqlString.append(" WHERE ");
        for (Iterator it = conditions.iterator(); it.hasNext();) {
            sqlString.append((String) it.next()).append(" AND ");
        }
        sqlString.delete(sqlString.length() - 5, sqlString.length());
    }

    // does this filter set an ordering?
    if (descending != null && orderColumn != null) {
        sqlString.append(" ORDER BY " + orderColumn);
        if (descending.equals(Boolean.TRUE))
            sqlString.append(" DESC");
        else
            sqlString.append(" ASC");
        sqlString.append(", dbWorkflows.wfid ASC");

    }
    if (limit > 0) {
        sqlString.append(" LIMIT " + limit);
    }

    sqlString.append(";");
    return sqlString.toString();
}

From source file:com.callidusrobotics.ui.PaginatedMenuBox.java

private void printMessages(final Console console) {
    final int row = consoleTextBox.getInternalRow() + 3;
    final int col = consoleTextBox.getInternalCol() + 1;

    if (lineBuffer.isEmpty()) {
        return;// www. j a va  2  s .  c  o m
    }

    // TODO: This has a nice effect when the menuBuffer size is divisible by the pageSize, but can look wonky otherwise
    final int startIndex = currentIndex >= maxLines
            ? Math.max(currentIndex - maxLines, lineBuffer.size() - maxLines)
            : 0;
    final int nEntries = Math.min(maxLines, lineBuffer.size());

    // Copy the messageBuffer into the console's buffer
    final StringBuffer stringBuffer = new StringBuffer(lineLength);
    for (int r = startIndex; r < startIndex + nEntries; r++) {
        final BufferLine line = lineBuffer.get(r);
        stringBuffer.delete(0, stringBuffer.length());
        stringBuffer.append(line.line, 0, Math.min(lineLength, line.line.length()));
        for (int c = line.line.length(); c < lineLength; c++) {
            stringBuffer.append(' ');
        }

        if (r == currentIndex) {
            console.print(row + (r - startIndex) % maxLines, col, stringBuffer.toString(), selectForeground,
                    selectBackground);
        } else {
            console.print(row + (r - startIndex) % maxLines, col, stringBuffer.toString(), line.foreground,
                    line.background);
        }
    }
}