Java URL Query Parse parseQueryString(String query, boolean ignoreEmpty)

Here you can find the source of parseQueryString(String query, boolean ignoreEmpty)

Description

Parses a query string into a parameter map, decoding values as needed.

License

Apache License

Parameter

Parameter Description
query The query string: zero or more <code>name=value</code> pairs separated by ampersands, with or without a leading question mark.
ignoreEmpty If <code>true</code>, ignores any entries without a value (eg, "<code>name=</code>"; if <code>false</code> these are added to the map with an empty string for the value.

Exception

Parameter Description
RuntimeException on any failure.

Return

A map of the name-value pairs. Caller is permitted to modify this map.

Declaration

public static Map<String, String> parseQueryString(String query, boolean ignoreEmpty) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.HashMap;
import java.util.Map;

public class Main {
    /**//from  w  ww .j av a  2 s .  c  om
     *  Parses a query string into a parameter map, decoding values as needed.
     *  Does not support multiple parameter values; later values will overwrite
     *  earlier.
     *
     *  @param  query       The query string: zero or more <code>name=value</code>
     *                      pairs separated by ampersands, with or without a
     *                      leading question mark.
     *  @param  ignoreEmpty If <code>true</code>, ignores any entries without a
     *                      value (eg, "<code>name=</code>"; if <code>false</code>
     *                      these are added to the map with an empty string for
     *                      the value.
     *  @return A map of the name-value pairs. Caller is permitted to modify this
     *          map.
     *  @throws RuntimeException on any failure.
     */
    public static Map<String, String> parseQueryString(String query, boolean ignoreEmpty) {
        Map<String, String> result = new HashMap<String, String>();
        if ((query == null) || (query.length() == 0))
            return result;

        if (query.charAt(0) == '?')
            query = query.substring(1);
        // need to repeat test for empty string
        if (query.length() == 0)
            return result;

        for (String param : query.split("&")) {
            // why not use split again? because it doesn't handle a missing '='
            int delimIdx = param.indexOf('=');
            if (delimIdx < 0)
                throw new RuntimeException("unparsable parameter: " + param);

            String name = param.substring(0, delimIdx);
            String value = param.substring(delimIdx + 1);

            if ((value.length() > 0) || !ignoreEmpty)
                result.put(name, urlDecode(value));
        }

        return result;
    }

    /**
     *  A wrapper around <code>URLDecoder</code> that always decodes as
     *  UTF-8, and replaces its checked exception with a RuntimeException
     *  (that should never be thrown).
     *  <p>
     *  If passed null, will return an empty string.
     */
    public static String urlDecode(String src) {
        if (src == null)
            return "";

        try {
            return URLDecoder.decode(src, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("this JVM doesn't support UTF-8!", e);
        }
    }
}

Related

  1. parseQueryString(String path)
  2. parseQueryString(String path)
  3. parseQueryString(String q, String enc)
  4. parseQueryString(String query)
  5. parseQueryString(String query)
  6. parseQuerystring(String queryString)
  7. parseQueryString(String queryString)
  8. parseQueryString(String queryString)
  9. parseQueryString(String queryString, Map params)