Java String Extract extractArrayData(String input, StringBuilder output)

Here you can find the source of extractArrayData(String input, StringBuilder output)

Description

extract Array Data

License

Apache License

Declaration

public static Map<String, String> extractArrayData(String input, StringBuilder output) 

Method Source Code

//package com.java2s;
/*/*w ww  .ja  v  a2 s.  c  o  m*/
 * Portions Copyright 2013-2015 Technology Concepts & Design, Inc
 * Portions Copyright 2015-2017 ZomboDB, LLC
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    public static Map<String, String> extractArrayData(String input, StringBuilder output) {
        Map<String, String> arrayData = new HashMap<>();
        boolean inArrayData = false;
        int arrStart = -1;
        int blockStart = -1;

        for (int i = 0, many = input.length(); i < many; i++) {
            char ch = input.charAt(i);
            char nextChar = i < many - 1 ? input.charAt(i + 1) : 0;

            switch (ch) {
            case '[':
                if (nextChar == '[' && !inArrayData) {
                    output.append(input.substring(blockStart, i));
                    blockStart = -1;
                    inArrayData = true;
                    i++;
                    arrStart = i + 1; // plus one to skip the double brackets at start of array: [[
                }
                break;
            case ']':
                if (nextChar == ']' && inArrayData) {
                    String arrayName = "$" + arrayData.size();
                    arrayData.put(arrayName, input.substring(arrStart, i));
                    if (blockStart != -1) {
                        output.append(input.substring(blockStart, i));
                        blockStart = -1;
                    }
                    output.append("[[").append(arrayName).append("]]");
                    inArrayData = false;
                    i++;
                }
                break;
            default:
                if (!inArrayData && blockStart == -1)
                    blockStart = i;
                break;
            }
        }

        if (blockStart != -1)
            output.append(input.substring(blockStart, input.length()));

        return arrayData;
    }
}

Related

  1. extract(final String grammarString, final char separator)
  2. extract(Properties p, String prefix)
  3. extract_tokens(String in_string, String delimiters)
  4. extractAggregationPrimKeysFromSql( final String sql)
  5. extractArrayData(String input, StringBuilder output)
  6. extractCharNgram(String content, int n)
  7. extractCommands(String fileContents)
  8. extractData(String message)