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
UnsupportedEncodingException an exception

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;
/*/*  w  w  w .  j  av  a 2  s . c o  m*/
 * OrbisGIS is a GIS application dedicated to scientific spatial simulation.
 * This cross-platform GIS is developed at French IRSTV institute and is able to
 * manipulate and create vector and raster spatial information.
 *
 * OrbisGIS is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * This file is part of OrbisGIS.
 *
 * OrbisGIS is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * OrbisGIS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, please consult: <http://www.orbisgis.org/>
 * or contact directly:
 * info_at_ orbisgis.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 UnsupportedEncodingException
     */
    public static Map<String, String> getQueryKeyValuePairs(URI uri) throws UnsupportedEncodingException {
        Map<String, String> queryParameters = new HashMap<String, String>();
        StringTokenizer stringTokenizer = new StringTokenizer(uri.getRawQuery(), "&");
        while (stringTokenizer.hasMoreTokens()) {
            String keyValue = stringTokenizer.nextToken().trim();
            if (!keyValue.isEmpty()) {
                int equalPos = keyValue.indexOf("=");
                // If there is no value
                if (equalPos == -1 || equalPos == keyValue.length() - 1) {
                    queryParameters.put(URLDecoder.decode(keyValue, ENCODING).toLowerCase(), null);
                } else {
                    String key = URLDecoder.decode(keyValue.substring(0, equalPos), ENCODING);
                    String value = URLDecoder.decode(keyValue.substring(equalPos + 1), ENCODING);
                    queryParameters.put(key.toLowerCase(), value);
                }
            }
        }
        return queryParameters;
    }
}

Related

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