Java URI Parse parseUriQueryParams(URI uri)

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

Description

parse Uri Query Params

License

Open Source License

Declaration

public static Map<String, String> parseUriQueryParams(URI uri) 

Method Source Code

//package com.java2s;
/*/*  ww w  . ja v a  2 s  .c  o m*/
 * Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 *
 * 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.net.URI;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static final String URI_QUERY_PARAM_LINK_CHAR = "&";
    public static final String URI_QUERY_PARAM_KV_CHAR = "=";

    public static Map<String, String> parseUriQueryParams(URI uri) {
        Map<String, String> params = new HashMap<String, String>();
        String query = uri.getQuery();
        if (query == null || query.isEmpty()) {
            return params;
        }

        String[] keyValuePairs = query.split(URI_QUERY_PARAM_LINK_CHAR);
        for (String kvPair : keyValuePairs) {
            String[] kvSplit = kvPair.split(URI_QUERY_PARAM_KV_CHAR);
            if (kvSplit.length < 2) {
                continue;
            }
            String key = kvSplit[0];
            String value = kvSplit[1];
            if (kvSplit.length > 2) {
                StringBuilder sb = new StringBuilder();
                sb.append(value);
                for (int i = 2; i < kvSplit.length; i++) {
                    sb.append(URI_QUERY_PARAM_KV_CHAR + kvSplit[i]);
                }
                value = sb.toString();
            }
            params.put(key, value);
        }
        return params;
    }
}

Related

  1. parseURI(String connectionString, URI defaultURI)
  2. parseUri(String s)
  3. parseURI(String target)
  4. parseURI(String uriStr)
  5. parseUriParameters(URI uri)
  6. parseURIs(String uri)