Java String Tokenize tokenizeDirectiveCall(String data)

Here you can find the source of tokenizeDirectiveCall(String data)

Description

tokenize Directive Call

License

Open Source License

Declaration

public static List<String> tokenizeDirectiveCall(String data) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2011 Atlas of Living Australia
 * All Rights Reserved.//from w  w w  . j av a2 s . com
 * 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> tokenizeDirectiveCall(String data) {
        List<String> tokens = new ArrayList<String>();
        if (data == null) {
            return tokens;
        }

        boolean inQuotedString = false;
        int endLastToken = -1;
        for (int i = 0; i < data.length(); i++) {
            boolean isEndToken = false;

            char c = data.charAt(i);

            char prevChar = 0;
            if (i > 0) {
                prevChar = data.charAt(i - 1);
            }

            char nextChar = 0;
            if (i < data.length() - 1) {
                nextChar = data.charAt(i + 1);
            }

            // open and close parentheses are tokens on their own (provided we
            // are not inside a quoted string)
            if ((c == '(' || c == ')') && !inQuotedString) {
                isEndToken = true;
                // If the next character is an open or end parenthesis, we are
                // at the end of the curent token
                // (provided we are not inside a quoted string)
            } else if ((nextChar == '(' || nextChar == ')') && !inQuotedString) {
                isEndToken = true;
            } else if (c == '"') {
                // Ignore quote if it is in the middle of a string -
                // don't throw error for unmatched quotes.
                // this is the behaviour in the legacy intkey - may change this
                // later.

                if (i == 0) {
                    inQuotedString = true;
                } else if (i != data.length() - 1) {
                    if (inQuotedString && (nextChar == ' ' || nextChar == ',' || nextChar == '\n'
                            || nextChar == '\r' || nextChar == '(' || nextChar == ')')) {
                        inQuotedString = false;
                        isEndToken = true;
                    } else if (!inQuotedString && (prevChar == ' ' || prevChar == ',' || prevChar == '\n'
                            || prevChar == '\r' || prevChar == '(' || prevChar == ')')) {
                        inQuotedString = true;
                    }
                }
            } else if ((c == ' ' || c == '\n' || c == '\r') && !inQuotedString) {
                // if we're not inside a quoted string, then a space or newline
                // designates
                // the end of a token
                isEndToken = true;
            }

            if (i == (data.length() - 1)) {
                // end of data string always designates the end of a token
                isEndToken = true;
            }

            if (isEndToken) {
                String token = null;
                if (endLastToken == -1) {
                    token = data.substring(0, i + 1);
                } else {
                    token = data.substring(endLastToken + 1, i + 1);
                }

                // use trim to remove any remaining whitespace. Tokens that
                // consist solely of whitespace should be completely omitted.
                String trimmedToken = token.trim();
                if (trimmedToken.length() > 0) {
                    tokens.add(removeEnclosingQuotes(token.trim()));
                }
                endLastToken = i;
            }
        }

        return tokens;
    }

    public static String removeEnclosingQuotes(String str) {
        if (str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
            return (str.substring(1, str.length() - 1));
        }
        return str;
    }
}

Related

  1. tokenize(String value)
  2. tokenize(String value)
  3. tokenize(String[] stringArray, String delimiter)
  4. tokenizeAutocomplete(String text, int minSize)
  5. tokenizeClassName(final String className)
  6. tokenizeExpression(String s)
  7. tokenizeFilter(CharSequence filter)
  8. tokenizeLine(String input)
  9. tokenizeLine(String line)