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:org.apache.poi.ss.format.CellNumberFormatter.java

private void writeScientific(double value, StringBuffer output, Set<StringMod> mods) {

    StringBuffer result = new StringBuffer();
    FieldPosition fractionPos = new FieldPosition(DecimalFormat.FRACTION_FIELD);
    decimalFmt.format(value, result, fractionPos);
    writeInteger(result, output, integerSpecials, mods, integerCommas);
    writeFractional(result, output);/*from   w w w.  j  a v  a2s  . c  om*/

    /*
    * Exponent sign handling is complex.
    *
    * In DecimalFormat, you never put the sign in the format, and the sign only
    * comes out of the format if it is negative.
    *
    * In Excel, you always say whether to always show the sign ("e+") or only
    * show negative signs ("e-").
    *
    * Also in Excel, where you put the sign in the format is NOT where it comes
    * out in the result.  In the format, the sign goes with the "e"; in the
    * output it goes with the exponent value.  That is, if you say "#e-|#" you
    * get "1e|-5", not "1e-|5". This makes sense I suppose, but it complicates
    * things.
    *
    * Finally, everything else in this formatting code assumes that the base of
    * the result is the original format, and that starting from that situation,
    * the indexes of the original special characters can be used to place the new
    * characters.  As just described, this is not true for the exponent's sign.
    * <p/>
    * So here is how we handle it:
    *
    * (1) When parsing the format, remove the sign from after the 'e' and put it
    * before the first digit of the exponent (where it will be shown).
    *
    * (2) Determine the result's sign.
    *
    * (3) If it's missing, put the sign into the output to keep the result
    * lined up with the output. (In the result, "after the 'e'" and "before the
    * first digit" are the same because the result has no extra chars to be in
    * the way.)
    *
    * (4) In the output, remove the sign if it should not be shown ("e-" was used
    * and the sign is negative) or set it to the correct value.
    */

    // (2) Determine the result's sign.
    int ePos = fractionPos.getEndIndex();
    int signPos = ePos + 1;
    char expSignRes = result.charAt(signPos);
    if (expSignRes != '-') {
        // not a sign, so it's a digit, and therefore a positive exponent
        expSignRes = '+';
        // (3) If it's missing, put the sign into the output to keep the result
        // lined up with the output.
        result.insert(signPos, '+');
    }

    // Now the result lines up like it is supposed to with the specials' indexes
    ListIterator<Special> it = exponentSpecials.listIterator(1);
    Special expSign = it.next();
    char expSignFmt = expSign.ch;

    // (4) In the output, remove the sign if it should not be shown or set it to
    // the correct value.
    if (expSignRes == '-' || expSignFmt == '+')
        mods.add(replaceMod(expSign, true, expSign, true, expSignRes));
    else
        mods.add(deleteMod(expSign, true, expSign, true));

    StringBuffer exponentNum = new StringBuffer(result.substring(signPos + 1));
    writeInteger(exponentNum, output, exponentDigitSpecials, mods, false);
}

From source file:com.xwtec.xwserver.util.json.JSONObject.java

/**
 * Make a prettyprinted JSON text of this JSONObject.
 * <p>//  w  ww. j  a v a 2  s  .co  m
 * Warning: This method assumes that the data structure is acyclical.
 *
 * @param indentFactor The number of spaces to add to each level of
 *        indentation.
 * @param indent The indentation of the top level.
 * @return a printable, displayable, transmittable representation of the
 *         object, beginning with <code>{</code>&nbsp;<small>(left brace)</small>
 *         and ending with <code>}</code>&nbsp;<small>(right brace)</small>.
 * @throws JSONException If the object contains an invalid number.
 */
public String toString(int indentFactor, int indent) {
    if (isNullObject()) {
        return JSONNull.getInstance().toString();
    }
    int i;
    int n = size();
    if (n == 0) {
        return "{}";
    }
    if (indentFactor == 0) {
        return this.toString();
    }
    Iterator keys = keys();
    StringBuffer sb = new StringBuffer("{");
    int newindent = indent + indentFactor;
    Object o;
    if (n == 1) {
        o = keys.next();
        sb.append(JSONUtils.quote(o.toString()));
        sb.append(": ");
        sb.append(JSONUtils.valueToString(this.properties.get(o), indentFactor, indent));
    } else {
        while (keys.hasNext()) {
            o = keys.next();
            if (sb.length() > 1) {
                sb.append(",\n");
            } else {
                sb.append('\n');
            }
            for (i = 0; i < newindent; i += 1) {
                sb.append(' ');
            }
            sb.append(JSONUtils.quote(o.toString()));
            sb.append(": ");
            sb.append(JSONUtils.valueToString(this.properties.get(o), indentFactor, newindent));
        }
        if (sb.length() > 1) {
            sb.append('\n');
            for (i = 0; i < indent; i += 1) {
                sb.append(' ');
            }
        }
        for (i = 0; i < indent; i += 1) {
            sb.insert(0, ' ');
        }
    }
    sb.append('}');
    return sb.toString();
}

From source file:org.zkoss.poi.ss.format.CellNumberFormatter.java

private void writeScientific(double value, StringBuffer output, Set<StringMod> mods) {

    StringBuffer result = new StringBuffer();
    FieldPosition fractionPos = new FieldPosition(DecimalFormat.FRACTION_FIELD);
    decimalFmt.format(value, result, fractionPos);
    writeInteger(result, output, integerSpecials, mods, integerCommas, false);
    writeFractional(result, output);// ww  w . ja v a2s  .  co  m

    /*
    * Exponent sign handling is complex.
    *
    * In DecimalFormat, you never put the sign in the format, and the sign only
    * comes out of the format if it is negative.
    *
    * In Excel, you always say whether to always show the sign ("e+") or only
    * show negative signs ("e-").
    *
    * Also in Excel, where you put the sign in the format is NOT where it comes
    * out in the result.  In the format, the sign goes with the "e"; in the
    * output it goes with the exponent value.  That is, if you say "#e-|#" you
    * get "1e|-5", not "1e-|5". This makes sense I suppose, but it complicates
    * things.
    *
    * Finally, everything else in this formatting code assumes that the base of
    * the result is the original format, and that starting from that situation,
    * the indexes of the original special characters can be used to place the new
    * characters.  As just described, this is not true for the exponent's sign.
    * <p/>
    * So here is how we handle it:
    *
    * (1) When parsing the format, remove the sign from after the 'e' and put it
    * before the first digit of the exponent (where it will be shown).
    *
    * (2) Determine the result's sign.
    *
    * (3) If it's missing, put the sign into the output to keep the result
    * lined up with the output. (In the result, "after the 'e'" and "before the
    * first digit" are the same because the result has no extra chars to be in
    * the way.)
    *
    * (4) In the output, remove the sign if it should not be shown ("e-" was used
    * and the sign is negative) or set it to the correct value.
    */

    // (2) Determine the result's sign.
    int ePos = fractionPos.getEndIndex();
    int signPos = ePos + 1;
    char expSignRes = result.charAt(signPos);
    if (expSignRes != '-') {
        // not a sign, so it's a digit, and therefore a positive exponent
        expSignRes = '+';
        // (3) If it's missing, put the sign into the output to keep the result
        // lined up with the output.
        result.insert(signPos, '+');
    }

    // Now the result lines up like it is supposed to with the specials' indexes
    ListIterator<Special> it = exponentSpecials.listIterator(1);
    Special expSign = it.next();
    char expSignFmt = expSign.ch;

    // (4) In the output, remove the sign if it should not be shown or set it to
    // the correct value.
    if (expSignRes == '-' || expSignFmt == '+')
        mods.add(replaceMod(expSign, true, expSign, true, expSignRes));
    else
        mods.add(deleteMod(expSign, true, expSign, true));

    StringBuffer exponentNum = new StringBuffer(result.substring(signPos + 1));
    writeInteger(exponentNum, output, exponentDigitSpecials, mods, false, false);
}

From source file:org.apache.clerezza.utils.Uri.java

/**
 * Set the path./*from  w  w w  .ja va 2  s . c  om*/
 *
 * @param path the path string
 * @throws URIException set incorrectly or fragment only
 * @see #encode
 */
public void setPath(String path) throws UriException {

    if (path == null || path.length() == 0) {
        _path = _opaque = (path == null) ? null : path.toCharArray();
        setURI();
        return;
    }
    // set the charset to do escape encoding
    String charset = getProtocolCharset();

    if (_is_net_path || _is_abs_path) {
        _path = encode(path, allowed_abs_path, charset);
    } else if (_is_rel_path) {
        StringBuffer buff = new StringBuffer(path.length());
        int at = path.indexOf('/');
        if (at == 0) { // never 0
            throw new UriException(UriException.PARSING, "incorrect relative path");
        }
        if (at > 0) {
            buff.append(encode(path.substring(0, at), allowed_rel_path, charset));
            buff.append(encode(path.substring(at), allowed_abs_path, charset));
        } else {
            buff.append(encode(path, allowed_rel_path, charset));
        }
        _path = buff.toString().toCharArray();
    } else if (_is_opaque_part) {
        StringBuffer buf = new StringBuffer();
        buf.insert(0, encode(path.substring(0, 1), uric_no_slash, charset));
        buf.insert(1, encode(path.substring(1), uric, charset));
        _opaque = buf.toString().toCharArray();
    } else {
        throw new UriException(UriException.PARSING, "incorrect path");
    }
    setURI();
}

From source file:bin.httpclient3.uri.URI.java

/**
 * Set the path./*from   ww w .  j  a  v  a 2 s .com*/
 *
 * @param path the path string
 * @throws URIException set incorrectly or fragment only
 * @see #encode
 */
public void setPath(String path) throws URIException {

    if (path == null || path.length() == 0) {
        _path = _opaque = (path == null) ? null : path.toCharArray();
        setURI();
        return;
    }
    // set the charset to do escape encoding
    String charset = getProtocolCharset();

    if (_is_net_path || _is_abs_path) {
        _path = encode(path, allowed_abs_path, charset);
    } else if (_is_rel_path) {
        StringBuffer buff = new StringBuffer(path.length());
        int at = path.indexOf('/');
        if (at == 0) { // never 0
            throw new URIException(URIException.PARSING, "incorrect relative path");
        }
        if (at > 0) {
            buff.append(encode(path.substring(0, at), allowed_rel_path, charset));
            buff.append(encode(path.substring(at), allowed_abs_path, charset));
        } else {
            buff.append(encode(path, allowed_rel_path, charset));
        }
        _path = buff.toString().toCharArray();
    } else if (_is_opaque_part) {
        StringBuffer buf = new StringBuffer();
        buf.insert(0, encode(path.substring(0, 1), uric_no_slash, charset));
        buf.insert(1, encode(path.substring(1), uric, charset));
        _opaque = buf.toString().toCharArray();
    } else {
        throw new URIException(URIException.PARSING, "incorrect path");
    }
    setURI();
}

From source file:org.etudes.mneme.impl.AttachmentServiceImpl.java

/**
   * Validate image with regular expression
   * @param image image for validation//www .  j a  v a 2  s .c o  m
   * @return true valid image, false invalid image
   */
public String adjustImagePath(String imageStr, String siteId) {
    Pattern pattern;
    Matcher matcher;

    String IMAGE_PATTERN = "(\"[\\S]+(\\.(?i)(jpg|png|gif|bmp))\")";
    String toolId = null;

    try {
        Site site = this.siteService.getSite(siteId);
        ToolConfiguration config = site.getToolForCommonId("sakai.mneme");
        if (config != null)
            toolId = config.getId();
    } catch (IdUnusedException e) {
        M_log.warn("get: missing site: " + siteId);
    }

    pattern = Pattern.compile(IMAGE_PATTERN);

    matcher = pattern.matcher(imageStr);
    boolean findRes = matcher.find();
    if (!findRes)
        return imageStr;
    if (findRes) {
        StringBuffer sb = new StringBuffer(imageStr);
        sb.insert(matcher.start() + 1, "/portal/tool/" + toolId + "/icons/");
        return sb.toString();
    }
    return imageStr;
}

From source file:org.exoplatform.wiki.rendering.render.confluence.ConfluenceSyntaxEscapeHandler.java

public void escape(StringBuffer accumulatedBuffer, ConfluenceSyntaxListenerChain listenerChain,
        boolean escapeLastChar, Pattern escapeFirstIfMatching) {
    BlockStateChainingListener blockStateListener = listenerChain.getBlockStateChainingListener();

    // Escape tilde symbol (i.e. the escape character).
    // Note: This needs to be the first replacement since other replacements below also use the tilde symbol
    replaceAll(accumulatedBuffer, ESCAPE_CHAR, ESCAPE_CHAR + ESCAPE_CHAR);

    // When in a paragraph we need to escape symbols that are at beginning of lines and that could be confused
    // with list items, headers or tables.
    if (blockStateListener.isInLine() && isOnNewLine()) {

        // Look for list pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(LIST_PATTERN, accumulatedBuffer);

        // Look for header pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(HEADER_PATTERN, accumulatedBuffer);

        // Look for table character patterns at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(TABLE_PATTERN, accumulatedBuffer);

        // Look for quote pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(QUOTE_PATTERN, accumulatedBuffer);
    }/*from ww w  . java  2s.  co m*/

    // Escape table characters
    if (blockStateListener.isInTable()) {
        replaceAll(accumulatedBuffer, "|", ESCAPE_CHAR + "|");
        replaceAll(accumulatedBuffer, "||", ESCAPE_CHAR + "|" + ESCAPE_CHAR + "|");
    }

    if (escapeFirstIfMatching != null) {
        escapeFirstMatchedCharacter(escapeFirstIfMatching, accumulatedBuffer);
    }

    // When in a header we need to escape "=" symbols since otherwise they would
    // be confused for end of section characters.
    if (blockStateListener.isInHeader()) {
        replaceAll(accumulatedBuffer, "=", ESCAPE_CHAR + "=");
    }

    // Escape verbatim "{{{"
    replaceAll(accumulatedBuffer, "{{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape "{{"
    replaceAll(accumulatedBuffer, "{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape groups
    replaceAll(accumulatedBuffer, "(((", ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(");
    replaceAll(accumulatedBuffer, ")))", ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")");

    // Escape reserved keywords
    Matcher matcher = DOUBLE_CHARS_PATTERN.matcher(accumulatedBuffer.toString());
    for (int i = 0; matcher.find(); i = i + 2) {
        accumulatedBuffer.replace(matcher.start() + i, matcher.end() + i,
                ESCAPE_CHAR + matcher.group().charAt(0) + ESCAPE_CHAR + matcher.group().charAt(1));
    }

    // Escape ":" in "image:something", "attach:something" and "mailto:something"
    // Note: even though there are some restriction in the URI specification as to what character is valid after
    // the ":" character following the scheme we only check for characters greater than the space symbol for
    // simplicity.
    escapeURI(accumulatedBuffer, "image:");
    escapeURI(accumulatedBuffer, "attach:");
    escapeURI(accumulatedBuffer, "mailto:");

    // Escape last character if we're told to do so. This is to handle cases such as:
    // - onWord("hello:") followed by onFormat(ITALIC) which would lead to "hello://" if the ":" wasn't escaped
    // - onWord("{") followed by onMacro() which would lead to "{{{" if the "{" wasn't escaped
    if (escapeLastChar) {
        accumulatedBuffer.insert(accumulatedBuffer.length() - 1, '~');
    }

    // Escape begin link
    replaceAll(accumulatedBuffer, "[", ESCAPE_CHAR + "[");

    // Escape link label
    int linkLevel = getLinkLevel(listenerChain);

    if (linkLevel > 0) {
        // This need to be done after anything else because link label add another level of escaping (escaped as
        // link label and then escaped as wiki content).
        String escape = StringUtils.repeat(ESCAPE_CHAR, linkLevel);
        replaceAll(accumulatedBuffer, ESCAPE_CHAR, escape + ESCAPE_CHAR);
        replaceAll(accumulatedBuffer, "]", escape + "]");
        replaceAll(accumulatedBuffer, "^", escape + "^");
        replaceAll(accumulatedBuffer, "|", escape + "|");
    }
}

From source file:org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeHandler.java

public void escape(StringBuffer accumulatedBuffer, XWikiSyntaxListenerChain listenerChain,
        boolean escapeLastChar, Pattern escapeFirstIfMatching) {
    BlockStateChainingListener blockStateListener = listenerChain.getBlockStateChainingListener();

    // Escape tilde symbol (i.e. the escape character).
    // Note: This needs to be the first replacement since other replacements below also use the tilde symbol
    replaceAll(accumulatedBuffer, ESCAPE_CHAR, ESCAPE_CHAR + ESCAPE_CHAR);

    // Escape anything that looks like starting of custom parameters
    replaceAll(accumulatedBuffer, "(%", ESCAPE_CHAR + "(%");

    // When in a paragraph we need to escape symbols that are at beginning of lines and that could be confused
    // with list items, headers or tables.
    if (blockStateListener.isInLine() && isOnNewLine()) {

        // Look for list pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(LIST_PATTERN, accumulatedBuffer);

        // Look for header pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(HEADER_PATTERN, accumulatedBuffer);

        // Look for table character patterns at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(TABLE_PATTERN, accumulatedBuffer);

        // Look for quote pattern at beginning of line and escape the first character only (it's enough)
        escapeFirstMatchedCharacter(QUOTE_PATTERN, accumulatedBuffer);
    }/*  w  ww .  j  a  v a  2s .co  m*/

    // Escape table characters
    if (blockStateListener.isInTable()) {
        replaceAll(accumulatedBuffer, "|", ESCAPE_CHAR + "|");
        replaceAll(accumulatedBuffer, "!!", ESCAPE_CHAR + "!!");
    }

    if (escapeFirstIfMatching != null) {
        escapeFirstMatchedCharacter(escapeFirstIfMatching, accumulatedBuffer);
    }

    // When in a header we need to escape "=" symbols since otherwise they would be confused for end of section
    // characters.
    if (blockStateListener.isInHeader()) {
        replaceAll(accumulatedBuffer, "=", ESCAPE_CHAR + "=");
    }

    // Escape verbatim "{{{"
    replaceAll(accumulatedBuffer, "{{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape "{{"
    replaceAll(accumulatedBuffer, "{{", ESCAPE_CHAR + "{" + ESCAPE_CHAR + "{");

    // Escape groups
    replaceAll(accumulatedBuffer, "(((", ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(" + ESCAPE_CHAR + "(");
    replaceAll(accumulatedBuffer, ")))", ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")" + ESCAPE_CHAR + ")");

    // Escape reserved keywords
    Matcher matcher = DOUBLE_CHARS_PATTERN.matcher(accumulatedBuffer.toString());
    for (int i = 0; matcher.find(); i = i + 2) {
        accumulatedBuffer.replace(matcher.start() + i, matcher.end() + i,
                ESCAPE_CHAR + matcher.group().charAt(0) + ESCAPE_CHAR + matcher.group().charAt(1));
    }

    // Escape ":" in "image:something", "attach:something" and "mailto:something"
    // Note: even though there are some restriction in the URI specification as to what character is valid after
    // the ":" character following the scheme we only check for characters greater than the space symbol for
    // simplicity.
    escapeURI(accumulatedBuffer, "image:");
    escapeURI(accumulatedBuffer, "attach:");
    escapeURI(accumulatedBuffer, "mailto:");

    // Escape last character if we're told to do so. This is to handle cases such as:
    // - onWord("hello:") followed by onFormat(ITALIC) which would lead to "hello://" if the ":" wasn't escaped
    // - onWord("{") followed by onMacro() which would lead to "{{{" if the "{" wasn't escaped
    if (escapeLastChar) {
        accumulatedBuffer.insert(accumulatedBuffer.length() - 1, '~');
    }

    // Escape begin link
    replaceAll(accumulatedBuffer, "[[", ESCAPE_CHAR + "[" + ESCAPE_CHAR + "[");

    // Escape link label
    int linkLevel = getLinkLevel(listenerChain);

    if (linkLevel > 0) {
        // This need to be done after anything else because link label add another level of escaping (escaped as
        // link label and then escaped as wiki content).
        String escape = StringUtils.repeat(ESCAPE_CHAR, linkLevel);
        replaceAll(accumulatedBuffer, ESCAPE_CHAR, escape + ESCAPE_CHAR);
        replaceAll(accumulatedBuffer, "]]", escape + "]" + escape + "]");
        replaceAll(accumulatedBuffer, ">>", escape + ">" + escape + ">");
        replaceAll(accumulatedBuffer, "||", escape + "|" + escape + "|");
    }
}

From source file:org.infoglue.cms.controllers.kernel.impl.simple.ContentController.java

public String getContentIdPath(Integer contentId) throws Exception {
    StringBuffer sb = new StringBuffer();

    ContentVO contentVO = getContentVOWithId(contentId);
    sb.insert(0, contentVO.getId());
    while (contentVO != null && contentVO.getParentContentId() != null) {
        sb.insert(0, contentVO.getParentContentId() + ",");
        if (contentVO.getParentContentId() != null)
            contentVO = getContentVOWithId(contentVO.getParentContentId());
        else/*from w ww  .j  a va 2s  .  co  m*/
            contentVO = null;
    }

    return sb.toString();
}

From source file:org.infoglue.cms.controllers.kernel.impl.simple.ContentController.java

private void insertContentNameInPath(StringBuffer sb, ContentVO contentVO) {
    if (contentVO.getName() == null || contentVO.getName().equals("")) {
        sb.insert(0, "]");
        sb.insert(0, contentVO.getId());
        sb.insert(0, "[");
    } else {/*ww w.ja  va 2  s  .c o m*/
        sb.insert(0, contentVO.getName());
    }
}