Java URL Query Parse parseQueryParameters( String queryString, String encoding)

Here you can find the source of parseQueryParameters( String queryString, String encoding)

Description

Parses the parameters from the given url query string.

License

Apache License

Declaration

public static Map<String, String> parseQueryParameters(
        String queryString, String encoding) 

Method Source Code

//package com.java2s;
/* 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
 * /*from  w w w.  j  a v a  2s.  co m*/
 *      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.LinkedHashMap;

import java.util.Map;

import java.util.Scanner;

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

    /**
     * Parses the parameters from the given url query string. When no encoding is
     * passed, default UTF-8 is used. Based on the implementation in Commons
     * httpclient UrlEncodedUtils.
     */
    public static Map<String, String> parseQueryParameters(
            String queryString, String encoding) {
        Map<String, String> parameters = new LinkedHashMap<String, String>();
        if (queryString != null) {
            // Remove '?' if present at beginning of query-string
            if (queryString.startsWith(QUERY_STRING_SEPARATOR)) {
                queryString = queryString.substring(1);
            }

            Scanner scanner = new Scanner(queryString);
            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);
                }
                parameters.put(name, value);
            }
        }
        return parameters;
    }

    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. parseQuery(String query)
  2. parseQuery(String query)
  3. parseQuery(String query)
  4. parseQuery(String queryString)
  5. parseQueryParameters( String query)
  6. parseQueryString(final String newQueryString, final String encoding)
  7. parseQueryString(String input)
  8. parseQueryString(String path)
  9. parseQueryString(String path)