Java URL Parameter Builder parseParameters(final String value)

Here you can find the source of parseParameters(final String value)

Description

Parse the given application/x-www-form-urlencoded String and return the parameters as a (name/value) Map.

License

Apache License

Declaration

static Map<String, String> parseParameters(final String value) 

Method Source Code

//package com.java2s;
/* Utils.java/*from  ww  w  .ja  v a  2s. c  o m*/
 *
 * Created: 2012-10-01 (Year-Month-Day)
 * Character encoding: UTF-8
 *
 ****************************************** LICENSE *******************************************
 *
 * Copyright (c) 2012 - 2013 XIAM Solutions B.V. (http://www.xiam.nl)
 *
 * 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.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * The canonical character set name for {@code UTF-8}.
     * {@code UTF-8} is the only character set used by JSON.
     */
    static final String UTF8 = "UTF-8";
    private static final Pattern KEY_VALUES = Pattern.compile("([^&]+)=([^&]+)");

    /**
     * Parse the given {@code application/x-www-form-urlencoded} String and return the
     * parameters as a (name/value) Map. If the String is empty or {@code null} an empty
     * Map is returned.
     */
    static Map<String, String> parseParameters(final String value) {
        if (value == null || value.isEmpty()) {
            return Collections.emptyMap();
        }
        final Map<String, String> map = new HashMap<String, String>();
        final Matcher m = KEY_VALUES.matcher(value);
        while (m.find()) {
            map.put(decodeRfc5849(m.group(1)), decodeRfc5849(m.group(2)));
        }
        return map;
    }

    /**
     * Percent decodeRfc5849 the value as specified by the RFC5849 (3.6).
     */
    static String decodeRfc5849(final String value) {
        try {
            return URLDecoder.decode(value, UTF8);
        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError(ex); // UTF-8 is always supported
        }
    }
}

Related

  1. mapToStr(Map map)
  2. mapToString(Map map)
  3. parse(final Map> parameters, final Scanner scanner, final String encoding)
  4. parse_parameters(String input)
  5. parseGetParameters(HttpExchange exchange)
  6. parseResponseParams(String body)
  7. sendGetRequest(String path, Map params, String enc)
  8. sendPostRequest(String path, Map params, String enc)
  9. serializeParameters(final Map parameters)