Java URI to Parameter getQueryKeyValuePairs(URI uri)

Here you can find the source of getQueryKeyValuePairs(URI uri)

Description

Read the Query part of an URI.

License

Open Source License

Parameter

Parameter Description
uri URI to split

Exception

Parameter Description

Return

Key/Value pairs of query, the key is lowercase and value may be null

Declaration

public static Map<String, String> getQueryKeyValuePairs(URI uri) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/**/*from   ww  w. j a v a2 s .  c  o  m*/
 * H2GIS is a library that brings spatial support to the H2 Database Engine
 * <http://www.h2database.com>. H2GIS is developed by CNRS
 * <http://www.cnrs.fr/>.
 *
 * This code is part of the H2GIS project. H2GIS is free software; 
 * you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation;
 * version 3.0 of the License.
 *
 * H2GIS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details <http://www.gnu.org/licenses/>.
 *
 *
 * For more information, please consult: <http://www.h2gis.org/>
 * or contact directly: info_at_h2gis.org
 */

import java.io.UnsupportedEncodingException;
import java.net.URI;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

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

    /**
     * Read the Query part of an URI.
     * @param uri URI to split
     * @return Key/Value pairs of query, the key is lowercase and value may be null
     * @throws java.io.UnsupportedEncodingException
     */
    public static Map<String, String> getQueryKeyValuePairs(URI uri) throws UnsupportedEncodingException {
        Map<String, String> queryParameters = new HashMap<String, String>();
        String query = uri.getRawQuery();
        if (query == null) {
            // Maybe invalid URI
            try {
                uri = URI.create(uri.getRawSchemeSpecificPart());
                query = uri.getRawQuery();
                if (query == null) {
                    return queryParameters;
                }
            } catch (IllegalArgumentException ex) {
                return queryParameters;
            }
        }
        StringTokenizer stringTokenizer = new StringTokenizer(query, "&");
        while (stringTokenizer.hasMoreTokens()) {
            String keyValue = stringTokenizer.nextToken().trim();
            if (!keyValue.isEmpty()) {
                int equalPos = keyValue.indexOf("=");
                // If there is no value
                String key = URLDecoder.decode(keyValue.substring(0, equalPos != -1 ? equalPos : keyValue.length()),
                        ENCODING);
                if (equalPos == -1 || equalPos == keyValue.length() - 1) {
                    // Key without value
                    queryParameters.put(key.toLowerCase(), "");
                } else {
                    String value = URLDecoder.decode(keyValue.substring(equalPos + 1), ENCODING);
                    queryParameters.put(key.toLowerCase(), value);
                }
            }
        }
        return queryParameters;
    }
}

Related

  1. getKey(String uri)
  2. getKey(URI uri)
  3. getParameters(URI uri)
  4. getParameters(URI uri)
  5. getQueryKeyValuePairs(URI uri)
  6. getQueryParameter(final String param, URI data)
  7. getQueryParameters(URI uri)
  8. getQueryParameters(URI uri)
  9. getQueryParameterValues(String uri, String name)