Java URL Parameter Builder getParams(String str)

Here you can find the source of getParams(String str)

Description

get Params

License

Apache License

Declaration

public static Map<String, String> getParams(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright 2007-2013 See AUTHORS file./* w  w w  . j  a  v a2 s  . c o  m*/
 * 
* 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.io.UnsupportedEncodingException;
import java.net.URLDecoder;

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

public class Main {

    public static Map<String, String> getParams(String str) {
        return getParams(str, "&", "utf-8", true);
    }

    public static Map<String, String> getParams(String str, String splitStr) {
        return getParams(str, splitStr, "utf-8", true);
    }

    public static Map<String, String> getParams(String str, String splitStr, String encoding) {
        return getParams(str, splitStr, encoding, true);
    }

    public static Map<String, String> getParams(String str, String splitStr, String encoding, boolean trimQuotate) {
        if (str == null || "".equals(str)) {
            return Collections.emptyMap();
        }

        Map<String, String> paras = new HashMap<String, String>();
        String[] ps = str.split("[" + splitStr + "]");
        for (int i = 0; i < ps.length; i++) {
            int index = ps[i].indexOf("=");
            if (index != -1 && index != 0 && index != ps[i].length()) {
                String name = ps[i].substring(0, index);
                String value = ps[i].substring(index + 1, ps[i].length());
                try {
                    value = URLDecoder.decode(value, encoding);
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }

                if (trimQuotate) {
                    if (value.startsWith("\"") || value.startsWith("'")) {
                        value = value.substring(1, value.length());
                    }
                    if (value.endsWith("\"") || value.endsWith("'")) {
                        value = value.substring(0, value.length() - 1);
                    }
                }

                paras.put(name.trim(), value);
            }
        }

        return paras;
    }

    public static String[] split(String str, char ch) {
        char[] chs = str.toCharArray();
        String[] strs = new String[10];
        int strIndex = 0;
        int index1 = 0;
        int index2 = 0;
        while (index2 < chs.length) {
            if (chs[index2] == ch) {
                strs[strIndex] = new String(chs, index1, index2 - index1);
                index1 = index2 + 1;
                strIndex++;

                if (strIndex >= strs.length) {
                    String[] nstrs = new String[strs.length + 10];
                    System.arraycopy(strs, 0, nstrs, 0, strs.length);
                    strs = nstrs;
                }
            }

            index2++;
        }
        if (index1 <= index2) {
            strs[strIndex] = new String(chs, index1, index2 - index1);
            strIndex++;

            if (strIndex >= strs.length) {
                String[] nstrs = new String[strs.length + 10];
                System.arraycopy(strs, 0, nstrs, 0, strs.length);
                strs = nstrs;
            }
        }
        String fstrs[] = new String[strIndex];
        System.arraycopy(strs, 0, fstrs, 0, strIndex);
        return fstrs;
    }
}

Related

  1. extractParameters(String parameterString)
  2. format(final Map> parameters, final String encoding)
  3. format(Map parameters, final String encoding)
  4. getParameter(String source, int index)
  5. getParameterMap(byte[] bytea)
  6. getUrlParameters(final String url)
  7. getUrlParameters(String url)
  8. getUrlParameters(String url)
  9. getUrlParameters(String url)