Java URL Build queryStringToMap(String queryString)

Here you can find the source of queryStringToMap(String queryString)

Description

Parses and form-urldecodes a querystring-like string into a map

License

Apache License

Parameter

Parameter Description
queryString querystring-like String

Return

a map with the form-urldecoded parameters

Declaration


public static Map<String, String> queryStringToMap(String queryString) 

Method Source Code


//package com.java2s;
/*//from  w ww.  java2s .  c om
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.HashMap;

import java.util.Map;

public class Main {
    private static final String EMPTY_STRING = "";
    private static final String UTF_8 = "UTF-8";
    private static final String PAIR_SEPARATOR = "=";
    private static final String PARAM_SEPARATOR = "&";
    private static final String ERROR_MSG = String.format("Cannot find specified encoding: %s", UTF_8);

    /**
     * Parses and form-urldecodes a querystring-like string into a map
     * 
     * @param queryString querystring-like String
     * @return a map with the form-urldecoded parameters
     */
    // TODO Move to MapUtils
    public static Map<String, String> queryStringToMap(String queryString) {
        Map<String, String> result = new HashMap<String, String>();
        if (queryString != null && queryString.length() > 0) {
            for (String param : queryString.split(PARAM_SEPARATOR)) {
                String pair[] = param.split(PAIR_SEPARATOR);
                String key = formURLDecode(pair[0]);
                String value = pair.length > 1 ? formURLDecode(pair[1]) : EMPTY_STRING;
                result.put(key, value);
            }
        }
        return result;
    }

    /**
     * Decodes a application/x-www-form-urlencoded string
     * 
     * @param string form-urlencoded string
     * @return plain string
     */
    public static String formURLDecode(String string) {
        try {
            return URLDecoder.decode(string, UTF_8);
        } catch (UnsupportedEncodingException uee) {
            throw new IllegalStateException(ERROR_MSG, uee);
        }
    }
}

Related

  1. queryString(final Map> params)
  2. queryString(final Map values)
  3. queryStringFromMap(Map map)
  4. queryStringify(HashMap qString)
  5. queryStringToMap(String query, Boolean decode)
  6. queryStringToMap(String queryString, String charset)
  7. queryStringToMap(String queryString, String charSet)
  8. queryToMap(final String query)
  9. queryToParams(String query)