Java URI Parse parseQuery(String uri)

Here you can find the source of parseQuery(String uri)

Description

parse Query

License

Apache License

Declaration

public static Map<String, List<String>> parseQuery(String uri) 

Method Source Code


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

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static final String DEFAULT_CHARSET = "utf-8";

    public static Map<String, List<String>> parseQuery(String uri) {
        int pathEndPos = uri.indexOf('?');
        String s = pathEndPos < 0 ? uri : uri.substring(pathEndPos + 1);

        Map<String, List<String>> params = new LinkedHashMap<String, List<String>>();
        String name = null;/*from  ww w .  j a  v a  2 s  .c  om*/
        int pos = 0; // Beginning of the unprocessed region
        int i; // End of the unprocessed region
        char c = 0; // Current character
        for (i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (c == '=' && name == null) {
                if (pos != i) {
                    name = decode(s.substring(pos, i));
                }
                pos = i + 1;
            } else if (c == '&') {
                if (name == null && pos != i) {
                    // We haven't seen an `=' so far but moved forward.
                    // Must be a param of the form '&a&' so add it with
                    // an empty value.
                    addParam(params, decode(s.substring(pos, i)), "");
                } else if (name != null) {
                    addParam(params, name, decode(s.substring(pos, i)));
                    name = null;
                }
                pos = i + 1;
            }
        }

        if (pos != i) { // Are there characters we haven't dealt with?
            if (name == null) { // Yes and we haven't seen any `='.
                addParam(params, decode(s.substring(pos, i)), "");
            } else { // Yes and this must be the last value.
                addParam(params, name, decode(s.substring(pos, i)));
            }
        } else if (name != null) { // Have we seen a name without value?
            addParam(params, name, "");
        }

        return params;
    }

    public static String decode(String src) {
        try {
            return URLDecoder.decode(src, DEFAULT_CHARSET);
        } catch (UnsupportedEncodingException e) {
            return src;
        }
    }

    private static void addParam(Map<String, List<String>> params, String name, String value) {
        List<String> values = params.get(name);
        if (values == null) {
            values = new ArrayList<>(1); // Often there's only 1 value.
            params.put(name, values);
        }
        values.add(value);
    }
}

Related

  1. parseLeaf(final URI uri)
  2. parseParameters(URI uri)
  3. parseParentURI(final URI uri)
  4. parsePathElements(URI uri)
  5. parsePorts(URI connectionURI)
  6. parseQuery(URI aURI, boolean shouldDecode)
  7. parseQuery(URI uri)
  8. parseQuery(URI uri)
  9. parseQueryString(final URI uri, final M queryParams)