Java String Split by Length splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)

Here you can find the source of splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)

Description

split By Trimmed Delimiter Non Nested In Brackets Or Quotes Or Comments

License

Open Source License

Declaration

private static String[] splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s,
            final char delimiter, final char quoteChar, char bracketChar,
            final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  .j a v  a2s  . co  m*/
 * (C) Copyright IBM Corp. 2008
 *
 * LICENSE: Eclipse Public License v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.util.ArrayList;

public class Main {
    private static String[] splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s,
            final char delimiter, final char quoteChar, char bracketChar,
            final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd) {

        if (null == s || 0 == s.length() || 0 == maxElmts)
            return new String[0];

        char closingBracketChar;
        switch (bracketChar) {
        default:
            bracketChar = '(';
        case '(':
            closingBracketChar = ')';
            break;
        case '[':
            closingBracketChar = ']';
            break;
        case '{':
            closingBracketChar = '}';
            break;
        case '<':
            closingBracketChar = '>';
            break;
        }

        ArrayList<String> results = new ArrayList<String>();

        int slen = s.length();
        int bracketNestingDepth = 0;
        int start = 0, elmtCount = 0;

        boolean isInQuotedExpression = false;
        int consecutiveQuoteCount = 0;
        char currentQuoteChar = quoteChar;

        if (null == cmtStart || null == cmtEnd)
            cmtStart = cmtEnd = null;
        final int cslen = null == cmtStart ? -1 : cmtStart.length(), celen = null == cmtEnd ? -1 : cmtEnd.length();
        boolean isInComment = false;

        for (int i = 0; i < slen; i++) {
            char c = s.charAt(i);

            if (isInComment) {
                // Check for end of a comment
                if (i >= celen - 1) {
                    isInComment = false;
                    for (int k = 0; k < celen; k++)
                        if (cmtEnd.charAt(k) != s.charAt(i - celen + k + 1)) {
                            isInComment = true;
                            break;
                        }
                    if (isInComment)
                        continue;
                }
            }

            // Not in a comment - look for nesting inside quotes..
            if ('\0' == quoteChar && '\0' == currentQuoteChar && ('\'' == c || '"' == c))
                currentQuoteChar = c;

            //         if ( c == currentQuoteChar ) System.out.println("Hit quote at end of: " + (1<i?s.charAt(i-2):"") + (0<i?s.charAt(i-1):"") + c);

            if (currentQuoteChar == c) {
                // Only start counting consecutive quotes once we are IN the quoted expression
                if (isInQuotedExpression)
                    consecutiveQuoteCount++;
                else
                    isInQuotedExpression = true;
                continue;
            }

            // Test if we have been in a quoted expression
            if (isInQuotedExpression) {
                if (0 == consecutiveQuoteCount)
                    continue;
                // An uneven number of quotes marks the end of a nested quoted expression            
                isInQuotedExpression = 0 == consecutiveQuoteCount % 2;
                consecutiveQuoteCount = 0;
                if (isInQuotedExpression)
                    continue;
            }

            if ('\0' == quoteChar)
                currentQuoteChar = '\0';

            if (bracketChar == c)
                bracketNestingDepth++;
            else if (closingBracketChar == c && 0 < bracketNestingDepth)
                bracketNestingDepth--;

            if (0 < bracketNestingDepth)
                continue;

            // Check if this is the start of a comment
            if (null != cmtStart && i >= cslen - 1) {
                isInComment = true;
                for (int k = 0; k < cslen; k++)
                    if (cmtStart.charAt(k) != s.charAt(i - cslen + k + 1)) {
                        isInComment = false;
                        break;
                    }
                if (isInComment)
                    continue;
            }

            // Check if we hit a delimiter char
            //         if ( c == delimiter ) System.out.println("Reached delimiter, with bnd: " + bracketNestingDepth + ", entry: " + s.substring(start, i).trim());
            if (c == delimiter) {
                final String entry = s.substring(start, i).trim();
                if (includeIntermediaryEmptyElmts || 0 < entry.length()) {
                    results.add(entry);
                    if (0 < maxElmts && ++elmtCount == maxElmts) {
                        slen = 0;
                        break;
                    }
                }
                start = i + 1;
                continue;
            }
        }

        // Always add the last string - even if empty - (as the above code only deals with the strings having a delimiter at the end)
        if (0 < slen)
            results.add(s.substring(start).trim());

        return (String[]) results.toArray(new String[0]);
    }
}

Related

  1. split(String message, int maxPages, int pageSize)
  2. splitArray(byte[] array, int len)
  3. splitByLength(String string, int len)
  4. splitByteArray(byte[] inByteArray, int length)
  5. splitByteArray(final byte[] data, final int packetLength)
  6. splitByWholeSeparatorWorker(final String str, final String separator, final int max, final boolean preserveAllTokens)
  7. splitEqually(String _text, int _len)
  8. splitEscapedString(String str, char sep, char esc, int maxPart)
  9. splitFilenames(String text)