Java URL Decode decode( Map source, String encoding)

Here you can find the source of decode( Map source, String encoding)

Description

Decode all the keys and values based on the encoding.

License

Educational Community License

Declaration

public static Map<String, String[]> decode(
        Map<String, String[]> source, String encoding) 

Method Source Code

//package com.java2s;
//License from project: Educational Community License 

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**//  w w  w  .  j ava2s  . c om
     * Decode all the keys and values based on the encoding.
     */
    public static Map<String, String[]> decode(
            Map<String, String[]> source, String encoding) {
        try {
            Map<String, String[]> decoded = new HashMap<String, String[]>();
            for (Map.Entry<String, String[]> entry : source.entrySet()) {
                String key = URLDecoder.decode(entry.getKey(), encoding);
                String[] values = new String[entry.getValue().length];
                for (int i = 0; i < entry.getValue().length; i++) {
                    values[i] = (entry.getValue()[i] == null) ? null
                            : URLDecoder.decode(entry.getValue()[i],
                                    encoding);
                }
                decoded.put(key, values);
            }
            return decoded;
        } catch (UnsupportedEncodingException uee) {
            throw new IllegalArgumentException("Unsupported encoding: "
                    + encoding);
        }
    }

    /**
     * Just gets the first value for the key out of the map.
     * @param params
     * @param key
     * @return The value or <code>null</code> if it isn't found or is null.
     */
    public static String getValue(Map<String, String[]> params, String key) {
        String[] values = params.get(key);
        if (values != null) {
            if (values.length > 0) {
                if (values.length > 1) {
                    // Too many values, dropping one.
                }
                return values[0];
            }
        }
        return null;
    }
}

Related

  1. addParam(Map properties, String strParams, boolean bDecodeString)
  2. decode(final String content, final String encoding)
  3. decode(final String s, final String enc)
  4. decode(final String string)
  5. decode(Object value)