Java URI Parse parseParameters(URI uri)

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

Description

Get properties from a URI

License

Open Source License

Parameter

Parameter Description
uri a parameter

Exception

Parameter Description
Exception an exception

Return

Map of properties

Declaration

public static Map<String, String> parseParameters(URI uri) throws Exception 

Method Source Code

//package com.java2s;
/**/*  w w  w.  j  a v a 2s  . c  o  m*/
 * Copyright (C) 2010-2011, FuseSource Corp.  All rights reserved.
 *
 *     http://fusesource.com
 *
 * The software in this package is published under the terms of the
 * CDDL license a copy of which has been included with this distribution
 * in the license.txt file.
 */

import java.net.URI;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /**
     * Get properties from a URI
     *
     * @param uri
     * @return <Code>Map</Code> of properties
     * @throws Exception
     */
    public static Map<String, String> parseParameters(URI uri) throws Exception {
        return uri.getQuery() == null ? Collections.EMPTY_MAP : parseQuery(stripPrefix(uri.getQuery(), "?"));
    }

    /**
     * Parse properties from a named resource -eg. a URI or a simple name e.g. foo?name="fred"&size=2
     *
     * @param uri
     * @return <Code>Map</Code> of properties
     * @throws Exception
     */
    public static Map<String, String> parseParameters(String uri) throws Exception {
        return uri == null ? Collections.EMPTY_MAP : parseQuery(stripUpto(uri, '?'));
    }

    /**
     * Get properties from a uri
     *
     * @param uri
     * @return <Code>Map</Code> of properties
     * @throws Exception
     */
    public static Map<String, String> parseQuery(String uri) throws Exception {
        if (uri != null) {
            Map<String, String> rc = new HashMap<String, String>();
            if (uri != null) {
                String[] parameters = uri.split("&");
                for (int i = 0; i < parameters.length; i++) {
                    int p = parameters[i].indexOf("=");
                    if (p >= 0) {
                        String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
                        String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
                        rc.put(name, value);
                    } else {
                        rc.put(parameters[i], null);
                    }
                }
            }
            return rc;
        }
        return Collections.EMPTY_MAP;
    }

    /**
     * Return a String past a prefix
     *
     * @param value
     * @param prefix
     * @return stripped
     */
    public static String stripPrefix(String value, String prefix) {
        if (value.startsWith(prefix)) {
            return value.substring(prefix.length());
        }
        return value;
    }

    /**
     * Return a String from to a character
     *
     * @param value
     * @param c
     * @return stripped
     */
    public static String stripUpto(String value, char c) {
        String result = null;
        int index = value.indexOf(c);
        if (index > 0) {
            result = value.substring(index + 1);
        }
        return result;
    }
}

Related

  1. parse(final String uri)
  2. parse(final URI uri, boolean decodeQueryParam)
  3. parseAWSUri(URI uri, String defaultAccessKey, String defaultSecretAccessKey)
  4. parseLeaf(final URI uri)
  5. parseParentURI(final URI uri)
  6. parsePathElements(URI uri)
  7. parsePorts(URI connectionURI)
  8. parseQuery(String uri)