Example usage for java.lang StringBuffer lastIndexOf

List of usage examples for java.lang StringBuffer lastIndexOf

Introduction

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

Prototype

@Override
public int lastIndexOf(String str) 

Source Link

Usage

From source file:org.ecoinformatics.datamanager.database.DelimitedReader.java

private String[] processQuoteCharacterOneRowData(String oneRowData) throws Exception {
    String[] elements = null;/*w  w  w  .  j  a  v  a 2 s. c om*/
    Vector elementsVector = new Vector();
    if (oneRowData == null) {
        return elements;
    }
    quoteCharacter = transferQuoteCharacter(quoteCharacter);
    char quote = '#';
    boolean quoted = false;
    if (quoteCharacter != null) {
        quoted = true;
        quote = quoteCharacter.charAt(0);
    }
    char literal = '/';
    boolean literaled = false;
    if (literalCharacter != null) {
        literaled = true;
        literal = literalCharacter.charAt(0);
    }
    if (literaled && literalCharacter.length() != 1) {
        throw new Exception("Literal Character length should be 1 character in EML");
    }
    char currentChar = '2';
    StringBuffer fieldData = new StringBuffer();
    int length = oneRowData.length();
    int priviousDelimiterIndex = -2;
    int currentDelimiterIndex = -2;
    int delimiterLength = delimiter.length();
    boolean startQuote = false;
    boolean delimiterAtEnd = false;
    //this string buffer is only for deleting if hit a delimiter
    StringBuffer delimiterStorage = new StringBuffer(delimiter.length());
    for (int i = 0; i < length; i++) {
        currentChar = oneRowData.charAt(i);
        //System.out.println("current char is "+currentChar);
        fieldData.append(currentChar);
        if (i < delimiterLength) {
            delimiterStorage.append(currentChar);
        } else {
            //delimiterStorage.deleteCharAt(position);
            delimiterStorage = shiftBuffer(delimiterStorage, currentChar);
        }
        //System.out.println("current delimiter storage content is "+delimiterStorage.toString());
        //System.out.println("currnet value in the string buffer is "+fieldData.toString());
        // we should check if there is quoteCharacter in the string.
        if (quoted && currentChar == quote) {
            char previousChar = '1';
            boolean escapingQuote = false;
            // check if this quote is escaped
            if (literaled) {
                if ((i - 1) >= 0) {
                    previousChar = oneRowData.charAt(i - 1);
                    if (previousChar == literal) {
                        escapingQuote = true;
                        // delette the literal character
                        if (!includeLiteralCharacter) {
                            //if we don't want literal character in the data,
                            //we should delete literal character.
                            int fieldLength = fieldData.length();
                            if ((fieldLength - 1 - 1) >= 0) {
                                fieldData.deleteCharAt(fieldLength - 1 - 1);
                            }
                        }
                    }
                }
            }
            if (!escapingQuote) {
                if (!startQuote) {
                    //System.out.println("start quote");
                    startQuote = true;
                } else {
                    //System.out.println("end quote");
                    // at end of quote
                    //put string buffers value into vector and reset string buffer
                    startQuote = false;

                }
            }

        }

        //found a delimiter
        if (delimiterStorage.indexOf(delimiter) != -1 && !startQuote) {

            //check if there is literal character before the delimiter,
            //if does, this we should skip this delmiter
            int indexOfCharBeforeDelimiter = i - delimiterLength;
            boolean escapeDelimiter = false;
            if (literaled && indexOfCharBeforeDelimiter >= 0) {
                char charBeforeDelimiter = oneRowData.charAt(indexOfCharBeforeDelimiter);
                ////there is a literal character before delimiter we should skip this demlimiter
                if (charBeforeDelimiter == literal) {
                    if (!includeLiteralCharacter) {
                        //if we don't want literal character in the data,
                        //we should delete literal character.
                        int fieldLength = fieldData.length();
                        if ((fieldLength - delimiterLength - 1) >= 0) {
                            fieldData.deleteCharAt(fieldLength - delimiterLength - 1);
                        }
                    }
                    escapeDelimiter = true;
                    continue;
                }
            }

            // check if the delimiter is in the end of the string
            if (i == (length - 1) && !startQuote && !escapeDelimiter) {
                delimiterAtEnd = true;
            }

            ////here we should treat sequential delimiter as single delimiter
            if (collapseDelimiters) {
                priviousDelimiterIndex = currentDelimiterIndex;
                currentDelimiterIndex = i;
                //there is nothing between two delimiter, should skip it.
                if ((currentDelimiterIndex - priviousDelimiterIndex) == delimiterLength) {
                    //delete sequnced delimiter
                    fieldData = new StringBuffer();
                    continue;
                }
            }

            String value = "";
            int delimiterIndex = fieldData.lastIndexOf(delimiter);
            if (delimiterIndex == 0) {
                //this path means field data on has delimiter, no real data
                value = "";
            } else {
                value = fieldData.substring(0, delimiterIndex);

            }
            elementsVector.add(value);
            //reset string buffer fieldData
            fieldData = new StringBuffer();

        }
    }
    // if startQuote is true at the end, which means there is no close quote character in this row,
    // code should throw an exception
    if (startQuote) {
        throw new Exception("There is a un-closed quote in data file");
    }
    // add last field. If this string end of delimiter, we need add a ""
    // else, we need to add the value in string buffer.
    String lastFieldValue = null;
    if (delimiterAtEnd == true) {
        //this path means field data on has delimiter, no real data
        lastFieldValue = "";
    } else {
        lastFieldValue = fieldData.toString();

    }
    elementsVector.add(lastFieldValue);
    //transform vector to string array
    int size = elementsVector.size();
    elements = new String[size];
    for (int i = 0; i < size; i++) {
        elements[i] = (String) elementsVector.elementAt(i);
    }
    return elements;
}

From source file:com.xpn.xwiki.render.filter.XWikiLinkFilter.java

@Override
public void handleMatch(StringBuffer buffer, org.radeox.regex.MatchResult result, FilterContext context) {
    RenderEngine engine = context.getRenderContext().getRenderEngine();

    if (engine instanceof WikiRenderEngine) {
        WikiRenderEngine wikiEngine = (WikiRenderEngine) engine;
        Writer writer = new StringBufferWriter(buffer);

        XWikiContext xcontext = (XWikiContext) context.getRenderContext().get("xcontext");
        String str = result.group(1);
        if (str != null) {
            // TODO: This line creates bug XWIKI-188. The encoder seems to be broken. Fix this!
            // The only unescaping done should be %xx => char,
            // since &#nnn; must be preserved (the active encoding cannot handle the character)
            // and + should be preserved (for "Doc.C++ examples").
            // Anyway, this unescaper only treats &#nnn;
            // trim the name and unescape it
            // str = Encoder.unescape(str.trim());
            str = str.trim();/*  ww  w.  j  a v a2  s  .c  o m*/
            String text = null, href = null, target = null;
            boolean specificText = false;

            // Is there an alias like [alias|link] ?
            int pipeIndex = str.indexOf('|');
            int pipeLength = 1;
            if (pipeIndex == -1) {
                pipeIndex = str.indexOf('>');
            }
            if (pipeIndex == -1) {
                pipeIndex = str.indexOf("&gt;");
                pipeLength = 4;
            }
            if (-1 != pipeIndex) {
                text = str.substring(0, pipeIndex).trim();
                str = str.substring(pipeIndex + pipeLength);
                specificText = true;
            }

            // Is there a target like [alias|link|target] ?
            pipeIndex = str.indexOf('|');
            pipeLength = 1;
            if (pipeIndex == -1) {
                pipeIndex = str.indexOf('>');
            }
            if (pipeIndex == -1) {
                pipeIndex = str.indexOf("&gt;");
                pipeLength = 4;
            }
            if (-1 != pipeIndex) {
                target = str.substring(pipeIndex + pipeLength).trim();
                str = str.substring(0, pipeIndex);
            }
            // Done splitting

            // Fill in missing components
            href = str.trim();
            if (text == null) {
                text = href;
            }
            // Done, now print the link

            // Determine target type: external, interwiki, internal
            int protocolIndex = href.indexOf("://");
            if (((protocolIndex >= 0) && (protocolIndex < 10)) || (href.indexOf("mailto:") == 0)) {
                // External link
                buffer.append("<span class=\"wikiexternallink\"><a href=\"");
                buffer.append(Utils.createPlaceholder(Util.escapeURL(href), xcontext));
                buffer.append("\"");
                if (target != null) {
                    buffer.append(" " + constructRelAttribute(target, xcontext));
                } else {
                    XWiki xwiki = xcontext.getWiki();
                    String defaulttarget = xwiki.Param("xwiki.render.externallinks.defaulttarget", "");
                    if (!defaulttarget.equals("")) {
                        buffer.append(" " + constructRelAttribute(defaulttarget, xcontext));
                    }
                }
                buffer.append(">");
                buffer.append(Utils.createPlaceholder(cleanText(text), xcontext));
                buffer.append("</a></span>");
                return;
            }

            int hashIndex = href.lastIndexOf('#');
            String hash = "";

            if (-1 != hashIndex && hashIndex != href.length() - 1) {
                hash = href.substring(hashIndex + 1);
                href = href.substring(0, hashIndex);
            }
            if (href.trim().equals("")) {
                // Internal (anchor) link
                buffer.append("<span class=\"wikilink\"><a href=\"#");
                buffer.append(hash);
                buffer.append("\">");
                if (!specificText || text.length() == 0) {
                    text = Encoder.unescape(hash);
                }
                buffer.append(cleanText(text));
                buffer.append("</a></span>");
                return;
            }

            /*
             * // We need to keep this in XWiki int colonIndex = name.indexOf(':'); // typed link ? if (-1 !=
             * colonIndex) { // for now throw away the type information name = name.substring(colonIndex + 1); }
             */

            int atIndex = href.lastIndexOf('@');
            // InterWiki link
            if (-1 != atIndex) {
                String extSpace = href.substring(atIndex + 1);
                // Kown extarnal space?
                InterWiki interWiki = InterWiki.getInstance();
                if (interWiki.contains(extSpace)) {
                    href = href.substring(0, atIndex);
                    try {
                        if (-1 != hashIndex) {
                            interWiki.expand(writer, extSpace, href, text, hash);
                        } else {
                            interWiki.expand(writer, extSpace, href, text);
                        }
                    } catch (IOException e) {
                        log.debug("InterWiki " + extSpace + " not found.");
                    }
                } else {
                    buffer.append("&#91;<span class=\"error\">");
                    buffer.append(result.group(1));
                    buffer.append("?</span>&#93;");
                }
            } else {
                // internal link
                if (wikiEngine.exists(href)) {
                    if (specificText == false) {
                        text = getWikiView(href);
                        wikiEngine.appendLink(buffer, href, text, hash);
                    } else {
                        wikiEngine.appendLink(buffer, href, text, hash);
                    }
                } else if (wikiEngine.showCreate()) {
                    if (specificText == false) {
                        text = getWikiView(href);
                    }
                    wikiEngine.appendCreateLink(buffer, href, text);
                    // links with "create" are not cacheable because
                    // a missing wiki could be created
                    context.getRenderContext().setCacheable(false);
                } else {
                    // cannot display/create wiki, so just display the text
                    buffer.append(text);
                }
                if (target != null) {
                    int where = buffer.lastIndexOf(" href=\"");
                    if (where >= 0) {
                        buffer.insert(where, " " + constructRelAttribute(target, xcontext));
                    }
                }
            }
        } else {
            buffer.append(Encoder.escape(result.group(0)));
        }
    }
}

From source file:com.jhh.hdb.sqlparser.MySemanticAnalyzer.java

private ASTNode findCTEFromName(QB qb, String cteName) {

    /*//from w w  w  .  j  a v a2 s. c o  m
     * When saving a view definition all table references in the AST are qualified; including CTE references.
     * Where as CTE definitions have no DB qualifier; so we strip out the DB qualifier before searching in
     * <code>aliasToCTEs</code> map.
     */
    String currDB = SessionState.get().getCurrentDatabase();
    if (currDB != null && cteName.startsWith(currDB) && cteName.length() > currDB.length()
            && cteName.charAt(currDB.length()) == '.') {
        cteName = cteName.substring(currDB.length() + 1);
    }

    StringBuffer qId = new StringBuffer();
    if (qb.getId() != null) {
        qId.append(qb.getId());
    }

    while (qId.length() > 0) {
        String nm = qId + ":" + cteName;
        if (aliasToCTEs.containsKey(nm)) {
            return aliasToCTEs.get(nm);
        }
        int lastIndex = qId.lastIndexOf(":");
        lastIndex = lastIndex < 0 ? 0 : lastIndex;
        qId.setLength(lastIndex);
    }
    return aliasToCTEs.get(cteName);
}