Java URL Query Parse parseQuery(String query)

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

Description

Extract and decode query parameters from an encoded query string

License

Apache License

Parameter

Parameter Description
query The raw query string of a URI or URL.

Return

The map containing the result.

Declaration

public static Map<String, String> parseQuery(String query) 

Method Source Code

//package com.java2s;
/**/* w  w  w.j av  a 2 s.  co  m*/
 * Copyright 2012-, Cloudsmith Inc.
 *
 * 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;

public class Main {
    private static final String QUERY_ENCODING = "UTF-8";

    /**
     * Extract and decode query parameters from an encoded query string
     * 
     * @param query The raw query string of a URI or URL.
     * @return The map containing the result.
     */
    public static Map<String, String> parseQuery(String query) {
        if (query == null || query.isEmpty())
            return Collections.emptyMap();

        Map<String, String> result = new HashMap<String, String>();
        int top = query.length();
        int start = 0;
        for (int idx = 0; idx < top; ++idx) {
            char c = query.charAt(idx);
            if (c == '&') {
                parsePair(result, query.substring(start, idx));
                start = idx + 1;
            }
        }
        if (start < top)
            parsePair(result, query.substring(start));
        return result;
    }

    private static void parsePair(Map<String, String> result, String pair) {
        int sep = pair.indexOf('=');
        if (sep < 0)
            result.put(decode(pair), null);
        else
            result.put(decode(pair.substring(0, sep)), decode(pair.substring(sep + 1)));
    }

    /**
     * Decode a value using the URLDecoder and UTF-8
     * 
     * @param value The value to decode
     * @return The decoded value.
     */
    public static String decode(String value) {
        try {
            return URLDecoder.decode(value, QUERY_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. parseParameters(String query)
  2. parseQuery(String query)
  3. parseQuery(String query)
  4. parseQuery(String query)
  5. parseQuery(String query)
  6. parseQuery(String queryString)
  7. parseQueryParameters( String query)
  8. parseQueryParameters( String queryString, String encoding)
  9. parseQueryString(final String newQueryString, final String encoding)