Java XML Parse String parse(String line)

Here you can find the source of parse(String line)

Description

parse

License

Apache License

Declaration

public static String[] parse(String line) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String[] parse(String line) throws Exception {
        line = line.trim();/*  ww  w  .  j ava  2  s . com*/
        if (line.length() == 0)
            return new String[0];
        List<String> args = new ArrayList<>();
        int i = 0;
        while (i < line.length() && line.charAt(i) != ' ')
            i++;
        args.add("-" + line.substring(0, i));
        while (i < line.length() && line.charAt(i) == ' ')
            i++;
        while (i < line.length()) {
            StringBuffer sb = new StringBuffer();
            if (line.charAt(i) == '"') {
                i++;
                while (i < line.length()) {
                    if (line.charAt(i) == '\\') {
                        if (i + 1 >= line.length())
                            throw new IllegalArgumentException("Nothing after '\\'");
                        sb.append(line.charAt(i + 1));
                        i += 2;
                    } else if (line.charAt(i) == '"')
                        break;
                    else {
                        sb.append(line.charAt(i));
                        i++;
                    }
                }
                if (i == line.length()) {
                    throw new IllegalArgumentException("'\"' does not match");
                } else {
                    i++;
                }
            } else {
                while (i < line.length()) {
                    if (line.charAt(i) == '\\') {
                        if (i + 1 >= line.length())
                            throw new IllegalArgumentException("Nothing after '\\'");
                        sb.append(line.charAt(i + 1));
                        i += 2;
                    } else if (line.charAt(i) == ' ' || line.charAt(i) == '"')
                        break;
                    else {
                        sb.append(line.charAt(i));
                        i++;
                    }
                }
            }
            args.add(sb.toString());
            while (i < line.length() && line.charAt(i) == ' ')
                i++;
        }
        return args.toArray(new String[args.size()]);
    }
}

Related

  1. parse(final String content)
  2. parse(final String line)
  3. parse(final String xmlContent)
  4. parse(String input)
  5. parse(String input, char delim, char esc)
  6. parse(String line)
  7. parse(String s)
  8. parse(String text)
  9. parse(String xml)