Java XML Parse String parse(final String content)

Here you can find the source of parse(final String content)

Description

parses the argument text into an array of individual arguments using the space character as the delimiter.

An individual argument containing spaces must have a double quote (") at the start and end.

License

Open Source License

Declaration

public static String[] parse(final String content) 

Method Source Code


//package com.java2s;
// This code is made available under the terms of the Eclipse Public License,

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static final char ARG_DELIMITER = ' ';
    private static final char ARG_DBL_QUOTE = '"';

    /** <p>parses the argument text into an array of individual arguments using
      * the space character as the delimiter.</p>
      *// w w  w . jav  a 2 s . c o m
      * <p>An individual argument containing spaces must have a double quote (")
      * at the start and end. Two double quotes together is taken to mean an
      * embedded double quote in the argument text.</p>
      */
    public static String[] parse(final String content) {
        String[] result = new String[0];
        if (content != null && content.length() > 0) {
            List<String> alResult = new ArrayList<String>();
            boolean inQuotes = false;
            int start = 0;
            int end = content.length();
            StringBuffer buffer = new StringBuffer(end);

            while (start < end) {
                char ch = content.charAt(start);
                start++;

                switch (ch) {
                case ARG_DELIMITER:
                    if (inQuotes) {
                        buffer.append(ch);
                    } else {
                        if (buffer.length() > 0) {
                            alResult.add(buffer.toString());
                            buffer.setLength(0);
                        }
                    }
                    break;

                case ARG_DBL_QUOTE:
                    if (start < end) {
                        if (content.charAt(start) == ARG_DBL_QUOTE) {
                            // Two quotes together represents one quote
                            buffer.append(ch);
                            start++;
                        } else {
                            inQuotes = !inQuotes;
                        }
                    } else {
                        // A lone quote at the end, just drop it.
                        inQuotes = false;
                    }
                    break;

                default:
                    buffer.append(ch);
                    break;
                }
            }

            if (buffer.length() > 0) {
                alResult.add(buffer.toString());
            }

            result = new String[alResult.size()];
            alResult.toArray(result);
        }
        return result;
    }
}

Related

  1. parse(final String line)
  2. parse(final String xmlContent)
  3. parse(String input)
  4. parse(String input, char delim, char esc)