Java URL Parameter Builder parse(final Map> parameters, final Scanner scanner, final String encoding)

Here you can find the source of parse(final Map> parameters, final Scanner scanner, final String encoding)

Description

Adds all parameters within the Scanner to the list of parameters, as encoded by encoding.

License

Open Source License

Parameter

Parameter Description
parameters List to add parameters to.
scanner Input that contains the parameters to parse.
encoding Encoding to use when decoding the parameters.

Declaration

public static void parse(final Map<String, Set<String>> parameters, final Scanner scanner,
        final String encoding) 

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;

import java.util.*;

public class Main {
    private static final String PARAMETER_SEPARATOR = "&";
    private static final String NAME_VALUE_SEPARATOR = "=";

    /**//from w  ww .  j av  a 2  s  .c  om
     * Returns Map<String, Set<String>>  as built from the
     * URI's query portion. For example, a URI of
     * http://example.org/path/to/file?a=1&b=2&c=3&c=4 would return a Map three
     * key is a name name of parameter Set<String> is a values, a={1},
     * one for b={2} and two for c={3,4}.
     * <p/>
     * <p/>
     * This is typically useful while parsing an HTTP PUT.
     *
     * @param uri
     *         uri to parse
     * @param encoding
     *         encoding to use while parsing the query
     */
    public static Map<String, Set<String>> parse(final URI uri, final String encoding) {
        Map<String, Set<String>> result = Collections.emptyMap();
        final String query = uri.getRawQuery();
        if (query != null && query.length() > 0) {
            result = new HashMap<>();
            parse(result, new Scanner(query), encoding);
        }
        return result;
    }

    /**
     * Adds all parameters within the Scanner to the list of
     * <code>parameters</code>, as encoded by <code>encoding</code>. For
     * example, a scanner containing the string <code>a=1&b=2&c=3</code> would
     * add the Map<String, Set<String>>  a=1, b=2, and c=3 to the
     * list of parameters.
     *
     * @param parameters
     *         List to add parameters to.
     * @param scanner
     *         Input that contains the parameters to parse.
     * @param encoding
     *         Encoding to use when decoding the parameters.
     */
    public static void parse(final Map<String, Set<String>> parameters, final Scanner scanner,
            final String encoding) {
        scanner.useDelimiter(PARAMETER_SEPARATOR);
        while (scanner.hasNext()) {
            final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
            if (nameValue.length == 0 || nameValue.length > 2)
                throw new IllegalArgumentException("bad parameter");

            final String name = decode(nameValue[0], encoding);
            String value = null;
            if (nameValue.length == 2)
                value = decode(nameValue[1], encoding);

            Set<String> values = parameters.get(name);
            if (values == null) {
                values = new LinkedHashSet<>();
                parameters.put(name, values);
            }
            if (value != null) {
                values.add(value);
            }
        }
    }

    private static String decode(final String content, final String encoding) {
        try {
            return URLDecoder.decode(content, encoding != null ? encoding : "UTF-8");
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    }
}

Related

  1. mapToFormEncodedString(Map data)
  2. mapToFormString(Map map)
  3. mapToStr( Map map)
  4. mapToStr(Map map)
  5. mapToString(Map map)
  6. parse_parameters(String input)
  7. parseGetParameters(HttpExchange exchange)
  8. parseParameters(final String value)
  9. parseResponseParams(String body)