Example usage for java.lang StringBuffer insert

List of usage examples for java.lang StringBuffer insert

Introduction

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

Prototype

@Override
public StringBuffer insert(int offset, double d) 

Source Link

Usage

From source file:DoubleDocument.java

/**
 * Strip all non digit characters.  The first character must be '-' or '+'.
 * Only one '.' is allowed.//from w  ww  .  j a  v  a2s  .  co m
 */
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {

    if (str == null) {
        return;
    }

    // Get current value
    String curVal = getText(0, getLength());
    boolean hasDot = curVal.indexOf('.') != -1;

    // Strip non digit characters
    char[] buffer = str.toCharArray();
    char[] digit = new char[buffer.length];
    int j = 0;

    if (offs == 0 && buffer != null && buffer.length > 0 && buffer[0] == '-')
        digit[j++] = buffer[0];

    for (int i = 0; i < buffer.length; i++) {
        if (Character.isDigit(buffer[i]))
            digit[j++] = buffer[i];
        if (!hasDot && buffer[i] == '.') {
            digit[j++] = '.';
            hasDot = true;
        }
    }

    // Now, test that new value is within range.
    String added = new String(digit, 0, j);
    try {
        StringBuffer val = new StringBuffer(curVal);
        val.insert(offs, added);
        String valStr = val.toString();
        if (valStr.equals(".") || valStr.equals("-") || valStr.equals("-."))
            super.insertString(offs, added, a);
        else {
            Double.valueOf(valStr);
            super.insertString(offs, added, a);
        }
    } catch (NumberFormatException e) {
        // Ignore insertion, as it results in an out of range value
    }
}

From source file:org.ajax4jsf.renderkit.AjaxRendererUtils.java

/**
 * Replacement for buggy in MyFaces <code>RendererUtils</code>
 * //from   w w w.  j  a  v  a 2s.c  om
 * @param component
 * @return
 */
public static String getAbsoluteId(UIComponent component) {
    if (component == null)
        throw new NullPointerException(Messages.getMessage(Messages.COMPONENT_NULL_ERROR_2));

    StringBuffer idBuf = new StringBuffer();

    idBuf.append(component.getId());

    UIComponent parent = component;

    while ((parent = parent.getParent()) != null) {
        if (parent instanceof NamingContainer) {
            idBuf.insert(0, NamingContainer.SEPARATOR_CHAR);
            idBuf.insert(0, parent.getId());
        }
    }
    idBuf.insert(0, NamingContainer.SEPARATOR_CHAR);
    log.debug(Messages.getMessage(Messages.CALCULATE_COMPONENT_ID_INFO, component.getId(), idBuf.toString()));
    return idBuf.toString();
}

From source file:com.edgenius.wiki.render.RenderUtil.java

/**
 * Check if given string is end by a Blocked HTML tag (see RenderUtil.isBlockTag());
 * The "end" means the except spaces or tab mark (it won't be newline mark! please see code in NewLineFilter.replace()
 * , the last visible String must be some tag and tag must be blocked tag.
 * //from  w  w  w.  ja  va2  s. c om
 * @param buffer
 */
public static boolean isEndByBlockHtmlTag(StringBuffer buffer) {

    if (buffer != null && buffer.length() > 0) {
        String before = buffer.toString();

        int len = before.length();
        //0 start looking close tag '>'; 1 in last tag string; 2 need find paired tag to check
        int looking = 0;
        StringBuffer tag = null;
        HTMLNode node = null, pair = null;
        for (int idx = len - 1; idx >= 0; idx--) {
            char ch = before.charAt(idx);
            if (looking == 0) {
                if (ch == '>') {
                    looking = 1;
                    tag = new StringBuffer();
                    tag.append(ch);
                    continue;
                } else if (ch != ' ' && ch != '\t') {
                    break;
                }
            } else if (looking == 1) {
                tag.insert(0, ch);
                if (ch == '<') {
                    node = new HTMLNode(tag.toString(), false);
                    if (node.isCloseTag()) {
                        //have to continue to looking up its open tag to decide
                        looking = 2;
                    } else {
                        //it is not close tag, then I can decide if it is close tag now
                        return isBlockTag(node);
                    }
                }
            } else if (looking == 2) {
                if (ch == '>') {
                    looking = 3;
                    tag = new StringBuffer();
                    tag.append(ch);
                    continue;
                }
            } else if (looking == 3) {
                tag.insert(0, ch);
                if (ch == '<') {
                    pair = new HTMLNode(tag.toString(), false);
                    if (pair.isPaired(node)) {
                        return isBlockTag(pair);
                    } else {
                        //it is not paired open tag, have to continue;
                        looking = 2;
                        continue;
                    }
                }
            }

        }
    }
    return false;
}

From source file:web.UploadFile.java

private File checkExist(String filename) {
    File f = new File(filePath + "/" + filename);

    if (f.exists()) {
        StringBuffer sb = new StringBuffer(filename);
        sb.insert(sb.lastIndexOf("."), "-" + new Date().getTime());
        f = new File(filePath + "/" + sb.toString());
    }/*from  ww  w  . j  a v a2  s .  c  o m*/
    return f;
}

From source file:com.thinkbiganalytics.util.PartitionKey.java

public String getFormulaWithAlias() {
    surroundFormulaColumnWithTick();/*from   ww w . jav a2  s. c o  m*/
    if (alias != null) {
        int idx = formula.indexOf("(");
        if (idx > -1) {
            StringBuffer sb = new StringBuffer(formula);
            sb.insert(idx + 1, alias + ".");
            return sb.toString();
        }
        return getKeyWithAlias();
    }
    return getFormula();
}

From source file:com.panet.imeta.core.util.UUID4Util.java

/**
 * Turn a byte array into a version four UUID string.
 * Adapted from org.apache.commons.id.uuid.UUID.java
 * @param raw/*from  w  w  w .j a v  a  2s .  c o  m*/
 * @return
 */
private String getUUIDString(byte[] raw) {
    StringBuffer buf = new StringBuffer(new String(encodeHex(raw)));
    while (buf.length() != 32) {
        buf.insert(0, "0");
    }
    buf.ensureCapacity(32);
    buf.insert(8, '-');
    buf.insert(13, '-');
    buf.insert(18, '-');
    buf.insert(23, '-');
    return buf.toString();
}

From source file:org.eclipse.datatools.connectivity.sample.ftp.internal.FTPFileObject.java

public String getName() {
    StringBuffer sb = new StringBuffer();
    FTPFileObject parent = this;
    while (parent != null) {
        sb.insert(0, "/" + parent.getFTPFile().getName());
        if (parent.getParent() instanceof FTPFileObject) {
            parent = (FTPFileObject) parent.getParent();
        } else {/*from w w w.j a  v  a 2  s . c  o m*/
            parent = null;
        }
    }
    while (sb.charAt(0) == '/') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

From source file:org.bbreak.excella.reports.exporter.OoPdfOutputStreamExporter.java

@Override
public void output(Workbook book, BookData bookdata, ConvertConfiguration configuration)
        throws ExportException {
    if (log.isInfoEnabled()) {
        log.info("??" + outputStream.getClass().getCanonicalName() + "????");
    }//from w ww .  java 2  s.  co  m

    // ?
    int point = getFilePath().indexOf(EXTENTION);
    StringBuffer sb = new StringBuffer(getFilePath());
    sb.insert(point, TMP_FILE_PREFIX);
    String tmpFilePath = sb.toString();
    setFilePath(tmpFilePath);

    // ?PDF???
    super.output(book, bookdata, configuration);

    File pdfFile = new File(getFilePath());

    // ?Stream????
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new FileInputStream(pdfFile));
        out = new BufferedOutputStream(outputStream);
        int b;

        while ((b = in.read()) != -1) {
            out.write(b);
        }
    } catch (IOException e) {
        throw new ExportException(e);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            throw new ExportException(e);
        } finally {
            // PDF?
            pdfFile.delete();
        }
    }
}

From source file:com.ts.control.UploadFile.java

private File checkExist(String fileName) {
    File f = new File(saveFile + "/" + fileName);

    if (f.exists()) {
        StringBuffer sb = new StringBuffer(fileName);
        sb.insert(sb.lastIndexOf("."), "-" + new Date().getTime());
        f = new File(saveFile + "/" + sb.toString());
    }//  w  w  w  .java  2  s  . com
    return f;
}

From source file:net.sf.groovyMonkey.ScriptMetadata.java

public static String stripIllegalChars(final String string) {
    if (StringUtils.isBlank(string))
        return "";
    if (StringUtils.equals(string.trim(), ">"))
        return "_";
    final boolean prefix = string.trim().startsWith(">");
    final boolean postfix = string.trim().endsWith(">");
    final String processed = StringUtils.removeEnd(StringUtils.removeStart(string.trim(), ">"), ">");
    final StringBuffer buffer = new StringBuffer();
    if (processed.contains(">")) {
        for (final String str : StringUtils.stripAll(StringUtils.split(processed, ">"))) {
            final StringBuffer stripped = new StringBuffer(
                    compile("[^\\p{Alnum}_-]").matcher(str).replaceAll(""));
            buffer.append("__").append("" + stripped);
        }/* w  w w . j ava  2s  .c om*/
    } else
        buffer.append(compile("[^\\p{Alnum}_-]").matcher(processed).replaceAll(""));
    if (prefix)
        buffer.insert(0, "_");
    if (postfix)
        buffer.append("_");
    return "" + buffer;
}