Java String Extract extract(final String grammarString, final char separator)

Here you can find the source of extract(final String grammarString, final char separator)

Description

Breaks a grammar string into its parts and returns those.

License

Open Source License

Parameter

Parameter Description
grammarString - the string to parse
separator - part separator

Return

- list of grammar parts

Declaration

public static List<String> extract(final String grammarString, final char separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Software Engineering Institute, TU Dortmund.
 * 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://from w  ww .  j a v  a2s. c om
 *    {SecSE group} - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Breaks a grammar string into its parts and returns those.
     * This method works different than the inbuilt split() in so far
     * that bracket levels inside the string are accounted for.
     * Example: "name=someName,contents=<some(name=other,value=someValue)>"
     * is split into "name=someName","contents=<some(name=other,value=someValue)>"
     * when using separator ",". 
     * @param grammarString - the string to parse
     * @param separator - part separator
     * @return - list of grammar parts
     */
    public static List<String> extract(final String grammarString, final char separator) {
        String grammar = grammarString;
        List<Integer> separatorIndices = findSeparatorIndices(grammar, separator);
        ArrayList<String> extractedStrings = new ArrayList<>();
        int indexBegin = 0;
        for (int sepIndex : separatorIndices) {
            extractedStrings.add(grammar.substring(indexBegin, sepIndex));
            indexBegin = sepIndex + 1;
        }
        extractedStrings.add(grammar.substring(indexBegin));
        return extractedStrings;
    }

    /**
     * Finds and stores the indices of the separators.
     * @param grammar - the string to parse
     * @param separator - separates grammar string parts
     * @return - list of indices where separators are
     */
    private static List<Integer> findSeparatorIndices(final String grammar, final char separator) {
        int bracketLevelCount = 0;
        ArrayList<Integer> separatorIndices = new ArrayList<>();
        for (int index = 0; index < grammar.length(); index++) {
            char ch = grammar.charAt(index);
            switch (ch) {
            case '(':
            case '{':
            case '[':
            case '<':
                bracketLevelCount++;
                break;
            case ')':
            case '}':
            case ']':
            case '>':
                bracketLevelCount--;
                break;
            default:
                if (ch == separator && bracketLevelCount == 0) {
                    separatorIndices.add(Integer.valueOf(index));
                }
            }
        }
        return separatorIndices;
    }
}

Related

  1. extract(Properties p, String prefix)
  2. extract_tokens(String in_string, String delimiters)
  3. extractAggregationPrimKeysFromSql( final String sql)
  4. extractArrayData(String input, StringBuilder output)