Java String Tokenize tokenizeName(String name)

Here you can find the source of tokenizeName(String name)

Description

tokenize Name

License

Open Source License

Declaration

public static String[] tokenizeName(String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright ? 2008, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  www. j  a  v a 2s.  c  o m*/
 * IBM Corporation - initial API and implementation
 *
 *******************************************************************************/

public class Main {
    public static String[] tokenizeName(String name) {

        char[] nameChars = name.toCharArray();

        char[][] tokens = new char[][] { new char[nameChars.length],
                new char[nameChars.length], new char[nameChars.length] };
        boolean delimiter = false;
        int token = 0;
        int index = 0;
        for (int i = 0; i < nameChars.length; i++) {
            if (nameChars[i] == '"') {
                // Open/close delimiter
                delimiter = !delimiter;
                tokens[token][index++] = nameChars[i];
                continue;
            }
            if (nameChars[i] == '.' && delimiter == false) {
                // Name is qualified, increment token
                ++token;
                index = 0;
                // Safety check for invalid names, ie more than 3 part names
                if (token > 2)
                    break;
                continue;
            }
            // Save character
            tokens[token][index++] = nameChars[i];
        }

        String[] result = new String[3];
        // one part name means table name
        // two part name means schema qualified
        // three part name means database qualified
        result[2] = new String(tokens[token]).trim();// table
        result[1] = (token > 0 ? new String(tokens[token - 1]).trim()
                : null);
        result[0] = (token > 1 ? new String(tokens[token - 2]).trim()
                : null);

        return result;
    }
}

Related

  1. tokenizeDirectiveCall(String data)
  2. tokenizeExpression(String s)
  3. tokenizeFilter(CharSequence filter)
  4. tokenizeLine(String input)
  5. tokenizeLine(String line)
  6. tokenizeNumbersFromString(String stream)
  7. tokenizeOnWhitespace(String input)
  8. tokenizePath(String path)
  9. tokenizePathAsArray(String path)